Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Hiroshiba committed Nov 6, 2024
1 parent 3b6bd19 commit af2cc53
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 23 deletions.
40 changes: 20 additions & 20 deletions src/domain/defaultEngine/latetDefaultEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,47 +4,47 @@

import { z } from "zod";

/** パッケージ(vvppやvvppp1ファイル)ごとのスキーマ */
const defaultEnginePackageSchema = z.object({
url: z.string(),
name: z.string(),
size: z.number(),
hash: z.string().optional(),
});

/** デバイスごとのスキーマ */
const defaultEngineDeviceSchema = z.object({
/** パッケージ情報のスキーマ */
const enginePackageSchema = z.object({
version: z.string(),
packages: z.array(defaultEnginePackageSchema),
packages: z
.object({
url: z.string(),
name: z.string(),
size: z.number(),
hash: z.string().optional(),
})
.array(),
});
export type EnginePackage = z.infer<typeof enginePackageSchema>;

/** デフォルトエンジンの更新情報のスキーマ */
const defaultEngineInfosSchema = z.object({
const latestDefaultEngineInfoSchema = z.object({
formatVersion: z.number(),
windows: z.object({
x64: z.object({
CPU: defaultEngineDeviceSchema,
"GPU/CPU": defaultEngineDeviceSchema,
CPU: enginePackageSchema,
"GPU/CPU": enginePackageSchema,
}),
}),
macos: z.object({
x64: z.object({
CPU: defaultEngineDeviceSchema,
CPU: enginePackageSchema,
}),
arm64: z.object({
CPU: defaultEngineDeviceSchema,
CPU: enginePackageSchema,
}),
}),
linux: z.object({
x64: z.object({
CPU: defaultEngineDeviceSchema,
"GPU/CPU": defaultEngineDeviceSchema,
CPU: enginePackageSchema,
"GPU/CPU": enginePackageSchema,
}),
}),
});

/** デフォルトエンジンの更新情報を取得する */
export const fetchDefaultEngineInfos = async (url: string) => {
export const fetchLatestDefaultEngineInfo = async (url: string) => {
const response = await fetch(url);
return defaultEngineInfosSchema.parse(await response.json());
return latestDefaultEngineInfoSchema.parse(await response.json());
};
6 changes: 3 additions & 3 deletions tests/unit/domain/defaultEngine/defaultEngine.node.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@

import path from "path";
import fs from "fs";
import { fetchDefaultEngineInfos } from "@/domain/defaultEngine/latetDefaultEngine";
import { fetchLatestDefaultEngineInfo } from "@/domain/defaultEngine/latetDefaultEngine";

const currentDir = "tests/unit/domain/defaultEngine";

test("fetchDefaultEngineInfos", async () => {
test("fetchLatestDefaultEngineInfo", async () => {
// テスト用のjsonファイルでfetchをモックする
// 元ファイルは https://raw.githubusercontent.com/VOICEVOX/voicevox_blog/master/src/generateLatestDefaultEngineInfos.ts
const p = path.resolve(currentDir, "latestDefaultEngineInfos.json");
const json = fs.readFileSync(p, "utf-8");
const spy = vi.spyOn(global, "fetch").mockResolvedValue(new Response(json));

// 読み込めることを確認
const infos = await fetchDefaultEngineInfos("https://example.com/");
const infos = await fetchLatestDefaultEngineInfo("https://example.com/");
expect(infos.formatVersion).toBe(1);

spy.mockRestore();
Expand Down

0 comments on commit af2cc53

Please sign in to comment.