From 4689cf898e8e5f70aa1f1ed10715d03076096a50 Mon Sep 17 00:00:00 2001 From: Hiroshiba Date: Fri, 8 Nov 2024 07:31:01 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20=E3=82=A8=E3=83=B3=E3=82=B8?= =?UTF-8?q?=E3=83=B3=E6=9C=80=E6=96=B0=E6=83=85=E5=A0=B1=E5=91=A8=E3=82=8A?= =?UTF-8?q?=E3=81=AE=E3=83=AA=E3=83=95=E3=82=A1=E3=82=AF=E3=82=BF=E3=83=AA?= =?UTF-8?q?=E3=83=B3=E3=82=B0=20(#2346)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../defaultEngine/latetDefaultEngine.ts | 43 +++++++++---------- .../defaultEngine/defaultEngine.node.spec.ts | 21 --------- .../defaultEngine/latetDefaultEngine.spec.ts | 16 +++++++ 3 files changed, 37 insertions(+), 43 deletions(-) delete mode 100644 tests/unit/domain/defaultEngine/defaultEngine.node.spec.ts create mode 100644 tests/unit/domain/defaultEngine/latetDefaultEngine.spec.ts 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(); +});