Skip to content

Commit

Permalink
リアファクタリング
Browse files Browse the repository at this point in the history
  • Loading branch information
Hiroshiba committed Nov 6, 2024
1 parent af2cc53 commit 13c9a7d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 19 deletions.
19 changes: 9 additions & 10 deletions src/domain/defaultEngine/latetDefaultEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import { z } from "zod";

/** パッケージ情報のスキーマ */
const enginePackageSchema = z.object({
const EngineVariantSchema = z.object({
version: z.string(),
packages: z
.object({
Expand All @@ -16,34 +16,33 @@ const enginePackageSchema = z.object({
})
.array(),
});
export type EnginePackage = z.infer<typeof enginePackageSchema>;

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

/** デフォルトエンジンの更新情報を取得する */
/** デフォルトエンジンの最新情報を取得する */
export const fetchLatestDefaultEngineInfo = async (url: string) => {
const response = await fetch(url);
return latestDefaultEngineInfoSchema.parse(await response.json());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
// テスト用のファイルを読み込むのでNode環境で実行する

import path from "path";
import fs from "fs";
import latestDefaultEngineInfos from "./latestDefaultEngineInfos.json";
import { fetchLatestDefaultEngineInfo } from "@/domain/defaultEngine/latetDefaultEngine";

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

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 spy = vi
.spyOn(global, "fetch")
.mockResolvedValue(new Response(JSON.stringify(latestDefaultEngineInfos)));

// 読み込めることを確認
const infos = await fetchLatestDefaultEngineInfo("https://example.com/");
Expand Down

0 comments on commit 13c9a7d

Please sign in to comment.