// NIHO ホストスクリプトの試験の実行入口(host-script-sdk-v0.4)。NIHO が生成します。直接編集しないでください。 // // 使い方: node .niho/sdk/run-test.mjs scripts/ [--verbose] // scripts//script.json, index.js, fixtures/*.json を読み、全 fixture を試験器で実行して expect と比べます。 // 失敗があれば終了コード 1。外部通信はしません。 import { readdir, readFile } from "node:fs/promises"; import { basename, join, resolve } from "node:path"; import { checkManifest, matchExpect, runFixture, SDK_VERSION } from "./emulator.mjs"; const STABLE_KEY_PATTERN = /^[a-z0-9]+(?:-[a-z0-9]+)*$/; async function readJson(path) { try { return { value: JSON.parse(await readFile(path, "utf8")) }; } catch (error) { return { error: path + ": " + (error instanceof Error ? error.message : String(error)) }; } } async function main() { const args = process.argv.slice(2); const verbose = args.includes("--verbose"); const target = args.find((arg) => !arg.startsWith("--")); if (!target) { console.error("使い方: node .niho/sdk/run-test.mjs scripts/ [--verbose]"); return 2; } const directory = resolve(target); const key = basename(directory); console.log("NIHO host script SDK " + SDK_VERSION + " / " + key); if (!STABLE_KEY_PATTERN.test(key) || key.length > 64) { console.error("フォルダ名(stableKey)は小文字・数字・ハイフンの64文字以下にしてください: " + key); return 1; } const manifestRead = await readJson(join(directory, "script.json")); if (manifestRead.error) { console.error(manifestRead.error); return 1; } const manifestIssues = checkManifest(manifestRead.value); if (manifestIssues.length > 0) { console.error("script.json に問題があります:"); for (const item of manifestIssues) console.error(" - " + item.path + ": " + item.message); return 1; } let names; try { names = (await readdir(join(directory, "fixtures"))).filter((name) => name.endsWith(".json")).sort(); } catch { names = []; } if (names.length === 0) { console.error("fixtures/*.json がありません。fixture を 1 つ以上書いてください"); return 1; } let failed = 0; for (const name of names) { const fixtureRead = await readJson(join(directory, "fixtures", name)); if (fixtureRead.error) { failed += 1; console.log("FAIL " + name); console.log(" " + fixtureRead.error); continue; } const fixture = fixtureRead.value; const result = await runFixture({ file: join(directory, "index.js"), manifest: manifestRead.value, fixture }); const verdict = matchExpect(result, fixture.expect); const label = (typeof fixture.name === "string" && fixture.name ? fixture.name : name) + " (" + name + ")"; console.log((verdict.pass ? "PASS " : "FAIL ") + label); if (!verdict.pass) { failed += 1; for (const failure of verdict.failures) console.log(" - " + failure); } if (!verdict.pass || verbose) { if (result.error) console.log(" error: " + JSON.stringify(result.error)); for (const warning of result.warnings) console.log(" ! " + warning); for (const line of result.logs) console.log(" log> " + line); if (verbose) console.log(" output: " + JSON.stringify(result.output)); } } console.log((failed === 0 ? "OK" : "NG") + ": " + (names.length - failed) + "/" + names.length + " 件が期待どおり"); return failed === 0 ? 0 : 1; } main().then((code) => { process.exitCode = code; }, (error) => { console.error(error); process.exitCode = 1; });