-
Notifications
You must be signed in to change notification settings - Fork 312
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
184 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,117 @@ | ||
import { mkdtemp, rm } from "fs/promises"; | ||
import { tmpdir } from "os"; | ||
import { join } from "path"; | ||
import fs from "fs"; | ||
import os from "os"; | ||
import path from "path"; | ||
import { beforeEach, expect, test } from "vitest"; | ||
import { createVvppFile } from "./helper"; | ||
import { EngineId, MinimumEngineManifestType } from "@/type/preload"; | ||
import VvppManager from "@/backend/electron/manager/vvppManager"; | ||
import { uuid4 } from "@/helpers/random"; | ||
|
||
const dummyMinimumManifest: MinimumEngineManifestType = { | ||
name: "Test Engine", | ||
uuid: EngineId("295c656b-b800-449f-aee6-b03e493816d7"), | ||
command: "", | ||
port: 5021, | ||
supported_features: {}, | ||
}; | ||
|
||
interface VvppManagerTestContext { | ||
interface Context { | ||
vvppEngineDir: string; | ||
manager: VvppManager; | ||
} | ||
|
||
beforeEach<VvppManagerTestContext>(async (context) => { | ||
const vvppEngineDir = await mkdtemp(join(tmpdir(), "vvppTest")); | ||
context.manager = new VvppManager({ vvppEngineDir }); | ||
let tmpDir: string; | ||
beforeAll(() => { | ||
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), uuid4())); | ||
}); | ||
afterAll(() => { | ||
fs.rmdirSync(tmpDir, { recursive: true }); | ||
}); | ||
|
||
return async () => { | ||
await rm(vvppEngineDir, { recursive: true }); | ||
}; | ||
beforeEach<Context>(async (context) => { | ||
context.vvppEngineDir = path.join(tmpDir, uuid4()); | ||
context.manager = new VvppManager({ | ||
vvppEngineDir: context.vvppEngineDir, | ||
tmpDir, | ||
}); | ||
}); | ||
|
||
test<VvppManagerTestContext>("追加エンジンのディレクトリ名は想定通りか", ({ | ||
manager, | ||
}) => { | ||
test<Context>("追加エンジンのディレクトリ名は想定通りか", ({ manager }) => { | ||
const dummyMinimumManifest: MinimumEngineManifestType = { | ||
name: "Test Engine", | ||
uuid: EngineId("295c656b-b800-449f-aee6-b03e493816d7"), | ||
command: "", | ||
port: 5021, | ||
supported_features: {}, | ||
}; | ||
|
||
const dirName = manager.toValidDirName(dummyMinimumManifest); | ||
// NOTE: パターンを変更する場合アンインストーラーのコードを変更する必要がある | ||
const pattern = /^.+\+.{8}-.{4}-.{4}-.{4}-.{12}$/; | ||
|
||
expect(dirName).toMatch(pattern); | ||
}); | ||
|
||
test<Context>("エンジンをインストールできる", async ({ | ||
vvppEngineDir, | ||
manager, | ||
}) => { | ||
const targetName = "perfect.vvpp"; | ||
const vvppFilePath = await createVvppFile(targetName, tmpDir); | ||
|
||
await manager.install(vvppFilePath); | ||
expect(getEngineDirInfos(vvppEngineDir).length).toBe(1); | ||
}); | ||
|
||
test<Context>("エンジンを2回インストールすると処理が予約され、後で上書きされる", async ({ | ||
vvppEngineDir, | ||
manager, | ||
}) => { | ||
const targetName = "perfect.vvpp"; | ||
const vvppFilePath = await createVvppFile(targetName, tmpDir); | ||
|
||
await manager.install(vvppFilePath); | ||
const infos1 = getEngineDirInfos(vvppEngineDir); | ||
expect(infos1.length).toBe(1); | ||
|
||
await manager.install(vvppFilePath); | ||
const infos2 = getEngineDirInfos(vvppEngineDir); | ||
expect(infos2.length).toBe(1); | ||
expect(infos1[0].createdTime).toBe(infos2[0].createdTime); // 同じファイル | ||
|
||
await manager.handleMarkedEngineDirs(); | ||
const infos3 = getEngineDirInfos(vvppEngineDir); | ||
expect(infos3.length).toBe(1); | ||
expect(infos1[0].createdTime).not.toBe(infos3[0].createdTime); // 別のファイル | ||
}); | ||
|
||
test<Context>("エンジンをアンインストール予約すると、後で削除される", async ({ | ||
vvppEngineDir, | ||
manager, | ||
}) => { | ||
const targetName = "perfect.vvpp"; | ||
const targetUuid = EngineId("00000000-0000-0000-0000-000000000001"); | ||
const vvppFilePath = await createVvppFile(targetName, tmpDir); | ||
|
||
await manager.install(vvppFilePath); | ||
const infos1 = getEngineDirInfos(vvppEngineDir); | ||
expect(infos1.length).toBe(1); | ||
|
||
manager.markWillDelete(targetUuid); | ||
const infos2 = getEngineDirInfos(vvppEngineDir); | ||
expect(infos2.length).toBe(1); | ||
|
||
await manager.handleMarkedEngineDirs(); | ||
const infos3 = getEngineDirInfos(vvppEngineDir); | ||
expect(infos3.length).toBe(0); | ||
}); | ||
|
||
/** | ||
* インストールされているエンジンディレクトリの情報を取得する | ||
*/ | ||
export function getEngineDirInfos(vvppEngineDir: string) { | ||
const files = fs.readdirSync(vvppEngineDir, { | ||
recursive: true, | ||
withFileTypes: true, | ||
}); | ||
const notTmpFiles = files.filter((file) => !file.parentPath.includes(".tmp")); | ||
const manifestFiles = notTmpFiles.filter( | ||
(file) => path.basename(file.name) === "engine_manifest.json", | ||
); | ||
const infos = manifestFiles.map((file) => ({ | ||
createdTime: fs.statSync(path.join(file.parentPath, file.name)).ctimeMs, | ||
})); | ||
return infos; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { exec } from "child_process"; | ||
import { promisify } from "util"; | ||
import path from "@/helpers/path"; | ||
import { uuid4 } from "@/helpers/random"; | ||
|
||
/** テスト用のVVPPファイルを作成する */ | ||
export async function createVvppFile(targetName: string, tmpDir: string) { | ||
const sourceDir = path.join(__dirname, "vvpps", targetName); | ||
const outputFilePath = path.join(tmpDir, uuid4() + targetName); | ||
await createZipFile(sourceDir, outputFilePath); | ||
return outputFilePath; | ||
} | ||
|
||
/** 7zを使って指定したフォルダからzipファイルを作成する */ | ||
async function createZipFile(sourceDir: string, outputFilePath: string) { | ||
const sevenZipBin = import.meta.env.VITE_7Z_BIN_NAME; | ||
const command = `"${sevenZipBin}" a -tzip "${outputFilePath}" "${path.join(sourceDir, "*")}"`; | ||
await promisify(exec)(command); | ||
} |
Oops, something went wrong.