54 lines
2.7 KiB
JavaScript
54 lines
2.7 KiB
JavaScript
// Optional smoke test for locally exported original game scripts; no game assets in git.
|
|
import assert from "node:assert/strict";
|
|
import { readFileSync } from "node:fs";
|
|
import { fileURLToPath } from "node:url";
|
|
import { test } from "node:test";
|
|
import { build } from "esbuild";
|
|
import { parseHTML } from "linkedom";
|
|
|
|
test("original Ren'Py scripts initialize and retain the player's save loader", {
|
|
skip: !process.env.PGAMES_RENPY_RUNTIME,
|
|
}, async () => {
|
|
const { window, document } = parseHTML("<!doctype html><html><body></body></html>");
|
|
const storage = new Map();
|
|
Object.assign(globalThis, {
|
|
window, document,
|
|
localStorage: { getItem: (key) => storage.get(key) ?? null, setItem: (key, value) => storage.set(key, value) },
|
|
ResizeObserver: class { observe() {} disconnect() {} },
|
|
requestAnimationFrame: (fn) => setTimeout(() => fn(performance.now()), 4),
|
|
cancelAnimationFrame: clearTimeout,
|
|
});
|
|
const result = await build({
|
|
entryPoints: [fileURLToPath(new URL("../src/player/engine.ts", import.meta.url))],
|
|
bundle: true, write: false, format: "esm", platform: "node", logLevel: "silent",
|
|
});
|
|
const { Engine } = await import(`data:text/javascript;base64,${Buffer.from(result.outputFiles[0].text).toString("base64")}`);
|
|
const { game, modules } = JSON.parse(readFileSync(process.env.PGAMES_RENPY_RUNTIME, "utf8"));
|
|
const tr = { lang: "en", renpyName: null, str: (s) => s, line: (_id, s) => s };
|
|
const root = document.createElement("div");
|
|
document.body.append(root);
|
|
const faults = [];
|
|
const warn = console.warn;
|
|
console.warn = (...args) => { faults.push(args.map(String).join(" ")); };
|
|
let engine;
|
|
try {
|
|
engine = new Engine(root, game, { files: {} }, modules,
|
|
{ list: async () => [], get: async () => null, put: async () => {}, remove: async () => {} },
|
|
"original-smoke", () => {}, tr, async () => tr);
|
|
const load = engine.builtins.renpy.load;
|
|
const room = engine.py.instantiate(engine.builtins.MusicRoom, []);
|
|
const stop = engine.builtins.MusicRoom.find("Stop").value(room);
|
|
assert.equal(stop.get_selected(), true, "MusicRoom must capture the initialized audio system");
|
|
for (let i = 0; i < game.init.length; i++) {
|
|
try { engine.py.exec([game.init[i]]); }
|
|
catch (error) { throw new Error(`init[${i}] ${JSON.stringify(game.init[i]).slice(0, 350)}: ${error.message}`); }
|
|
}
|
|
engine.newStore();
|
|
assert.equal(engine.builtins.renpy.load, load, "init must retain the native save loader");
|
|
assert.deepEqual(faults.filter((s) => /ошибка init|ошибка выражения|неизвестное имя|неизвестная функция/.test(s)), []);
|
|
} finally {
|
|
console.warn = warn;
|
|
engine?.destroy();
|
|
}
|
|
});
|