diff --git a/tests/unit/backend/electron/vvppFile.node.spec.ts b/tests/unit/backend/electron/vvppFile.node.spec.ts new file mode 100644 index 0000000000..bc4d071c1a --- /dev/null +++ b/tests/unit/backend/electron/vvppFile.node.spec.ts @@ -0,0 +1,72 @@ +import os from "os"; +import path from "path"; +import { exec } from "child_process"; +import fs from "fs"; +import { promisify } from "util"; +import { test, afterAll, beforeAll } from "vitest"; +import { extractVvpp } from "@/backend/electron/vvppFile"; +import { uuid4 } from "@/helpers/random"; + +let tmpDir: string; +beforeAll(() => { + tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), uuid4())); +}); +afterAll(() => { + fs.rmdirSync(tmpDir, { recursive: true }); +}); + +test("正しいVVPPファイルからエンジンを切り出せる", async () => { + const targetName = "perfect.vvpp"; + const sourceDir = path.join(__dirname, "vvpps", targetName); + const outputFilePath = path.join(tmpDir, uuid4() + targetName); + await createZipFile(sourceDir, outputFilePath); + await extractVvpp(outputFilePath, tmpDir); +}); + +test.fails("分割されたVVPPファイルからエンジンを切り出せる", async () => { + // TODO: electronのappに依存しているのでテストが通らない。依存がなくなり次第.failsを失くす + const targetName = "perfect.vvpp"; + const sourceDir = path.join(__dirname, "vvpps", targetName); + const outputFilePath = path.join(tmpDir, uuid4() + targetName); + await createZipFile(sourceDir, outputFilePath); + + const outputFilePath1 = outputFilePath + ".1.vvppp"; + const outputFilePath2 = outputFilePath + ".2.vvppp"; + splitFile(outputFilePath, outputFilePath1, outputFilePath2); + await extractVvpp(outputFilePath1, tmpDir); +}); + +test.each([ + ["invalid_manifest.vvpp", /SyntaxError|is not valid JSON/], + ["no_engine_id.vvpp", undefined], // TODO: エンジンIDが見つからない専用のエラーを用意する + ["no_manifest.vvpp", /ENOENT|engine_manifest.json/], +])( + "不正なVVPPファイルからエンジンを切り出せない: %s", + async (targetName, expectedError) => { + const sourceDir = path.join(__dirname, "vvpps", targetName); + const outputFilePath = path.join(tmpDir, uuid4() + targetName); + await createZipFile(sourceDir, outputFilePath); + await expect(extractVvpp(outputFilePath, tmpDir)).rejects.toThrow( + expectedError, + ); + }, +); + +/** 7zを使って指定したフォルダからzipファイルを作成する */ +async function createZipFile(sourceDir: string, outputFilePath: string) { + const zipBin = import.meta.env.VITE_7Z_BIN_NAME; + const command = `"${zipBin}" a -tzip "${outputFilePath}" "${sourceDir}\\*"`; + await promisify(exec)(command); +} + +/** ファイルを2つに分割する */ +function splitFile( + inputFilePath: string, + outputFilePath1: string, + outputFilePath2: string, +) { + const data = fs.readFileSync(inputFilePath); + const midPoint = Math.floor(data.length / 2); + fs.writeFileSync(outputFilePath1, data.subarray(0, midPoint)); + fs.writeFileSync(outputFilePath2, data.subarray(midPoint)); +} diff --git a/tests/unit/backend/electron/vvpps/README.md b/tests/unit/backend/electron/vvpps/README.md new file mode 100644 index 0000000000..72730c93d0 --- /dev/null +++ b/tests/unit/backend/electron/vvpps/README.md @@ -0,0 +1,9 @@ +# vvpps + +テスト用のVVPPファイルを作るためのディレクトリ。 +各ディレクトリをzip化して拡張子を.vvppに変えればテスト用ファイルになる。 + +- perfect.vvpp:完璧なVVPP +- invalid_manifest.vvpp:不正なエンジンマニフェストがあるVVPP +- no_manifest.vvpp:エンジンマニフェストがないVVPP +- no_engine_id.vvpp:エンジンマニフェストはあるが、エンジンidがないVVPP diff --git a/tests/unit/backend/electron/vvpps/invalid_manifest.vvpp/engine_manifest.json b/tests/unit/backend/electron/vvpps/invalid_manifest.vvpp/engine_manifest.json new file mode 100644 index 0000000000..ea368b7a02 --- /dev/null +++ b/tests/unit/backend/electron/vvpps/invalid_manifest.vvpp/engine_manifest.json @@ -0,0 +1 @@ +invalid manifest \ No newline at end of file diff --git a/tests/unit/backend/electron/vvpps/no_engine_id.vvpp/engine_manifest.json b/tests/unit/backend/electron/vvpps/no_engine_id.vvpp/engine_manifest.json new file mode 100644 index 0000000000..e2575552e2 --- /dev/null +++ b/tests/unit/backend/electron/vvpps/no_engine_id.vvpp/engine_manifest.json @@ -0,0 +1,6 @@ +{ + "name": "Dummy Engine", + "command": "dummy.exe", + "port": 404, + "supported_features": {} +} diff --git a/tests/unit/backend/electron/vvpps/no_manifest.vvpp/invalid_file_name.json b/tests/unit/backend/electron/vvpps/no_manifest.vvpp/invalid_file_name.json new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/unit/backend/electron/vvpps/perfect.vvpp/engine_manifest.json b/tests/unit/backend/electron/vvpps/perfect.vvpp/engine_manifest.json new file mode 100644 index 0000000000..7f32baf842 --- /dev/null +++ b/tests/unit/backend/electron/vvpps/perfect.vvpp/engine_manifest.json @@ -0,0 +1,7 @@ +{ + "name": "Dummy Engine", + "uuid": "00000000-0000-0000-0000-000000000001", + "command": "dummy.exe", + "port": 404, + "supported_features": {} +}