-
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
6 changed files
with
95 additions
and
0 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
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)); | ||
} |
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,9 @@ | ||
# vvpps | ||
|
||
テスト用のVVPPファイルを作るためのディレクトリ。 | ||
各ディレクトリをzip化して拡張子を.vvppに変えればテスト用ファイルになる。 | ||
|
||
- perfect.vvpp:完璧なVVPP | ||
- invalid_manifest.vvpp:不正なエンジンマニフェストがあるVVPP | ||
- no_manifest.vvpp:エンジンマニフェストがないVVPP | ||
- no_engine_id.vvpp:エンジンマニフェストはあるが、エンジンidがないVVPP |
1 change: 1 addition & 0 deletions
1
tests/unit/backend/electron/vvpps/invalid_manifest.vvpp/engine_manifest.json
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 @@ | ||
invalid manifest |
6 changes: 6 additions & 0 deletions
6
tests/unit/backend/electron/vvpps/no_engine_id.vvpp/engine_manifest.json
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,6 @@ | ||
{ | ||
"name": "Dummy Engine", | ||
"command": "dummy.exe", | ||
"port": 404, | ||
"supported_features": {} | ||
} |
Empty file.
7 changes: 7 additions & 0 deletions
7
tests/unit/backend/electron/vvpps/perfect.vvpp/engine_manifest.json
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,7 @@ | ||
{ | ||
"name": "Dummy Engine", | ||
"uuid": "00000000-0000-0000-0000-000000000001", | ||
"command": "dummy.exe", | ||
"port": 404, | ||
"supported_features": {} | ||
} |