Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions tests/unit/backend/electron/vvppFile.node.spec.ts
Original file line number Diff line number Diff line change
@@ -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));
}
9 changes: 9 additions & 0 deletions tests/unit/backend/electron/vvpps/README.md
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
invalid manifest
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "Dummy Engine",
"command": "dummy.exe",
"port": 404,
"supported_features": {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "Dummy Engine",
"uuid": "00000000-0000-0000-0000-000000000001",
"command": "dummy.exe",
"port": 404,
"supported_features": {}
}
Loading