Skip to content

Commit

Permalink
デフォルトエンジン更新情報jsonの仕様を書き、スキーマを定義 (#2257)
Browse files Browse the repository at this point in the history
* デフォルトエンジン更新情報jsonの仕様を書き、スキーマを定義

* mockRestoreする

* 不要なTextが入ってた

* 説明追加
  • Loading branch information
Hiroshiba authored Sep 4, 2024
1 parent d3904c4 commit 11c5aa8
Show file tree
Hide file tree
Showing 5 changed files with 218 additions and 0 deletions.
60 changes: 60 additions & 0 deletions docs/細かい設計方針.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,63 @@ export type HogeFugaType = z.infer<typeof hogeFugaSchema>;
| 追加時の処理 | zipファイルを所定のフォルダに展開 | エンジンのパスを`config.json`に保存 |
| 読み込み時の処理 | 所定のフォルダ内にあるものを読む | `config.json`に保存されたパスを読む |
| 削除時の処理 | 所定のフォルダ内のディレクトリを削除 | `config.json`からパスを削除 |

## デフォルトエンジンの更新情報

デフォルトエンジンの更新情報をjson形式で管理しています。
更新情報には最新のパッケージ(VVPP・VVPPPファイル)のURLやバージョンなどを記載しています。
パッケージの情報はOS・アーキテクチャ・デバイスごとに分けています。

ファイルフォーマットは以下の通りです。

```JSONC
{
//[number] ファイル構造バージョン(仕様変更毎にインクリメントされる)
"formatVersion": 1,

// Windowsの情報
"windows": {
"x64": {
"CPU": {
//[string] バージョン
"version": "x.x.x",

// vvppやvvpppの情報
"packages": [
{
//[string] ダウンロードURL
"url": "https://example.com/example.vvpp",

//[string] ファイル名
"name": "example.vvpp",

//[number] バイト数
"size": 123456,

//[string(Optional)] ハッシュ値
"hash": "xxxxxxx",
},
//...
]
},
"GPU/CPU": { /* 同上 */ }
}
},

"macos": {
"x64": {
"CPU": { /* 同上 */ }
},
"arm64": {
"CPU": { /* 同上 */ }
}
},

"linux": {
"x64": {
"CPU": { /* 同上 */ },
"GPU/CPU": { /* 同上 */ }
}
}
}
```
50 changes: 50 additions & 0 deletions src/domain/defaultEngine.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* デフォルトエンジン関連のモジュール
*/

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({
version: z.string(),
packages: z.array(defaultEnginePackageSchema),
});

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

/** デフォルトエンジンの更新情報を取得する */
export const fetchDefaultEngineInfos = async (url: string) => {
const response = await fetch(url);
return defaultEngineInfosSchema.parse(await response.json());
};
4 changes: 4 additions & 0 deletions tests/unit/backend/common/configManager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ class TestConfigManager extends BaseConfigManager {
}
}

afterEach(() => {
vi.resetAllMocks();
});

it("新規作成できる", async () => {
vi.spyOn(TestConfigManager.prototype, "exists").mockImplementation(
async () => false,
Expand Down
22 changes: 22 additions & 0 deletions tests/unit/domain/defaultEngine/defaultEngine.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// テスト用のファイルを読み込むのでNode環境で実行する
// @vitest-environment node

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

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();
});
82 changes: 82 additions & 0 deletions tests/unit/domain/defaultEngine/latestDefaultEngineInfos.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
{
"formatVersion": 1,
"windows": {
"x64": {
"CPU": {
"version": "0.20.0",
"packages": [
{
"url": "https://github.com/VOICEVOX/voicevox_engine/releases/download/0.20.0/voicevox_engine-windows-cpu-0.20.0.vvpp",
"name": "voicevox_engine-windows-cpu-0.20.0.vvpp",
"size": 1374659234
}
]
},
"GPU/CPU": {
"version": "0.20.0",
"packages": [
{
"url": "https://github.com/VOICEVOX/voicevox_engine/releases/download/0.20.0/voicevox_engine-windows-directml-0.20.0.vvpp",
"name": "voicevox_engine-windows-directml-0.20.0.vvpp",
"size": 1382829369
}
]
}
}
},
"macos": {
"x64": {
"CPU": {
"version": "0.20.0",
"packages": [
{
"url": "https://github.com/VOICEVOX/voicevox_engine/releases/download/0.20.0/voicevox_engine-macos-x64-0.20.0.001.vvppp",
"name": "voicevox_engine-macos-x64-0.20.0.001.vvppp",
"size": 1382766014
}
]
}
},
"arm64": {
"CPU": {
"version": "0.20.0",
"packages": [
{
"url": "https://github.com/VOICEVOX/voicevox_engine/releases/download/0.20.0/voicevox_engine-macos-arm64-0.20.0.001.vvppp",
"name": "voicevox_engine-macos-arm64-0.20.0.001.vvppp",
"size": 1375008115
}
]
}
}
},
"linux": {
"x64": {
"CPU": {
"version": "0.20.0",
"packages": [
{
"url": "https://github.com/VOICEVOX/voicevox_engine/releases/download/0.20.0/voicevox_engine-linux-cpu-0.20.0.vvpp",
"name": "voicevox_engine-linux-cpu-0.20.0.vvpp",
"size": 1399437028
}
]
},
"GPU/CPU": {
"version": "0.20.0",
"packages": [
{
"url": "https://github.com/VOICEVOX/voicevox_engine/releases/download/0.20.0/voicevox_engine-linux-nvidia-0.20.0.001.vvppp",
"name": "voicevox_engine-linux-nvidia-0.20.0.001.vvppp",
"size": 1992294400
},
{
"url": "https://github.com/VOICEVOX/voicevox_engine/releases/download/0.20.0/voicevox_engine-linux-nvidia-0.20.0.002.vvppp",
"name": "voicevox_engine-linux-nvidia-0.20.0.002.vvppp",
"size": 645130316
}
]
}
}
}
}

0 comments on commit 11c5aa8

Please sign in to comment.