diff --git a/src/domain/defaultEngine/latetDefaultEngine.ts b/src/domain/defaultEngine/latetDefaultEngine.ts index 6c6637e959..62c95534c8 100644 --- a/src/domain/defaultEngine/latetDefaultEngine.ts +++ b/src/domain/defaultEngine/latetDefaultEngine.ts @@ -4,47 +4,46 @@ 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 engineVariantSchema = 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(), }); -/** デフォルトエンジンの更新情報のスキーマ */ -const defaultEngineInfosSchema = z.object({ +/** デフォルトエンジンの最新情報のスキーマ */ +const latestDefaultEngineInfoSchema = z.object({ formatVersion: z.number(), windows: z.object({ x64: z.object({ - CPU: defaultEngineDeviceSchema, - "GPU/CPU": defaultEngineDeviceSchema, + CPU: engineVariantSchema, + "GPU/CPU": engineVariantSchema, }), }), macos: z.object({ x64: z.object({ - CPU: defaultEngineDeviceSchema, + CPU: engineVariantSchema, }), arm64: z.object({ - CPU: defaultEngineDeviceSchema, + CPU: engineVariantSchema, }), }), linux: z.object({ x64: z.object({ - CPU: defaultEngineDeviceSchema, - "GPU/CPU": defaultEngineDeviceSchema, + CPU: engineVariantSchema, + "GPU/CPU": engineVariantSchema, }), }), }); -/** デフォルトエンジンの更新情報を取得する */ -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()); }; diff --git a/tests/unit/domain/defaultEngine/defaultEngine.node.spec.ts b/tests/unit/domain/defaultEngine/defaultEngine.node.spec.ts deleted file mode 100644 index 0e49596d76..0000000000 --- a/tests/unit/domain/defaultEngine/defaultEngine.node.spec.ts +++ /dev/null @@ -1,21 +0,0 @@ -// テスト用のファイルを読み込むのでNode環境で実行する - -import path from "path"; -import fs from "fs"; -import { fetchDefaultEngineInfos } from "@/domain/defaultEngine/latetDefaultEngine"; - -const currentDir = "tests/unit/domain/defaultEngine"; - -test("fetchDefaultEngineInfos", 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/"); - expect(infos.formatVersion).toBe(1); - - spy.mockRestore(); -}); diff --git a/tests/unit/domain/defaultEngine/latetDefaultEngine.spec.ts b/tests/unit/domain/defaultEngine/latetDefaultEngine.spec.ts new file mode 100644 index 0000000000..d3b09e4d1b --- /dev/null +++ b/tests/unit/domain/defaultEngine/latetDefaultEngine.spec.ts @@ -0,0 +1,16 @@ +import latestDefaultEngineInfos from "./latestDefaultEngineInfos.json"; +import { fetchLatestDefaultEngineInfo } from "@/domain/defaultEngine/latetDefaultEngine"; + +test("fetchLatestDefaultEngineInfo", async () => { + // テスト用のjsonファイルでfetchをモックする + // 元ファイルは https://raw.githubusercontent.com/VOICEVOX/voicevox_blog/master/src/generateLatestDefaultEngineInfos.ts + const spy = vi + .spyOn(global, "fetch") + .mockResolvedValue(new Response(JSON.stringify(latestDefaultEngineInfos))); + + // 読み込めることを確認 + const infos = await fetchLatestDefaultEngineInfo("https://example.com/"); + expect(infos.formatVersion).toBe(1); + + spy.mockRestore(); +});