79 lines
3.3 KiB
JavaScript
79 lines
3.3 KiB
JavaScript
import assert from "node:assert/strict";
|
|
import { readFileSync, mkdtempSync, symlinkSync, rmSync } from "node:fs";
|
|
import { execFileSync } from "node:child_process";
|
|
import { tmpdir } from "node:os";
|
|
import { join, resolve } from "node:path";
|
|
import { pathToFileURL } from "node:url";
|
|
import { createHash } from "node:crypto";
|
|
import { chromium } from "playwright";
|
|
const root = new URL("../", import.meta.url);
|
|
const baseline = execFileSync(
|
|
"git",
|
|
["show", "5552e079c17d23d90bc1a675d8e0a893ccacc32c:packages/engine/src/achievements.ts"],
|
|
{ cwd: root, encoding: "utf8" }
|
|
);
|
|
const current = readFileSync(new URL("packages/engine/src/achievements.ts", root), "utf8");
|
|
const withoutManifest = (source) =>
|
|
source.replace(/export function achievementDefinitions\(\): AchievementManifest \{[\s\S]*?\n\}/, "");
|
|
assert.equal(withoutManifest(current), withoutManifest(baseline), "entire evaluator and catalog remain byte-identical");
|
|
const originalIds = [...baseline.matchAll(/id: "([\w-]+)"/g)].map((m) => m[1]);
|
|
const manifest = JSON.parse(readFileSync(new URL("assets/achievements/manifest.json", root)));
|
|
assert.deepEqual(
|
|
manifest.assets.map((a) => a.id),
|
|
originalIds
|
|
);
|
|
assert.equal(new Set(manifest.assets.map((a) => a.pngSha256)).size, 19);
|
|
const hash = (data) => createHash("sha256").update(data).digest("hex");
|
|
for (const source of manifest.sources) {
|
|
assert.equal(hash(readFileSync(new URL(source.path, root))), source.sha256);
|
|
}
|
|
const browser = await chromium.launch({ headless: true });
|
|
const page = await browser.newPage();
|
|
for (const asset of manifest.assets) {
|
|
assert.equal(asset.key, `achievement-${asset.id}`);
|
|
for (const ext of ["svg", "png"]) {
|
|
const data = readFileSync(new URL(`assets/achievements/${asset[ext]}`, root));
|
|
assert.equal(hash(data), asset[`${ext}Sha256`]);
|
|
const dimensions = await page.evaluate(
|
|
async ({ base64, ext }) => {
|
|
const image = new Image();
|
|
image.src = `data:image/${ext === "svg" ? "svg+xml" : "png"};base64,${base64}`;
|
|
await image.decode();
|
|
return [image.naturalWidth, image.naturalHeight];
|
|
},
|
|
{ base64: data.toString("base64"), ext }
|
|
);
|
|
assert.deepEqual(dimensions, [256, 256]);
|
|
}
|
|
}
|
|
await browser.close();
|
|
let temp;
|
|
try {
|
|
let moduleUrl = new URL("packages/engine/dist/wrapper.js", root);
|
|
if (process.argv[2]) {
|
|
temp = mkdtempSync(join(tmpdir(), "outpost-art-pack-"));
|
|
execFileSync("tar", ["-xzf", resolve(process.argv[2]), "-C", temp]);
|
|
symlinkSync(new URL("packages/engine/node_modules", root).pathname, join(temp, "package/node_modules"));
|
|
moduleUrl = pathToFileURL(join(temp, "package/dist/wrapper.js"));
|
|
assert.equal(JSON.parse(readFileSync(join(temp, "package/package.json"))).version, "0.10.33");
|
|
}
|
|
const wrapper = await import(moduleUrl);
|
|
const defs = wrapper.achievementDefinitions();
|
|
assert.equal(defs.revision, 3);
|
|
assert.deepEqual(
|
|
defs.achievements.map((a) => a.id),
|
|
originalIds
|
|
);
|
|
for (const a of defs.achievements) {
|
|
assert.deepEqual(a.icon, { type: "asset", key: `achievement-${a.id}` });
|
|
assert.ok(manifest.assets.some((asset) => asset.key === a.icon.key));
|
|
}
|
|
assert.deepEqual(wrapper.achievements({ ended: false }), []);
|
|
console.log(
|
|
"PASS: 19 original IDs, unchanged evaluator/catalog, 38 image decodes/hashes, source hashes, unique assets, packed manifest hooks"
|
|
);
|
|
} finally {
|
|
if (temp) {
|
|
rmSync(temp, { recursive: true, force: true });
|
|
}
|
|
}
|