77 lines
2.6 KiB
JavaScript
77 lines
2.6 KiB
JavaScript
import test from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import { access, mkdtemp, cp, readFile, writeFile, rm } from 'node:fs/promises';
|
|
import { tmpdir } from 'node:os';
|
|
import path from 'node:path';
|
|
import { canonicalAssets, validateInputs, validateOutputs, outputDir } from './achievement-asset-inputs.mjs';
|
|
|
|
test('all 15 playable registry names have canonical emblems, four PNGs and eleven Lucide icons', async () => {
|
|
const canonical = await canonicalAssets();
|
|
assert.equal(canonical.length, 15);
|
|
assert.equal(canonical.filter((a) => a.source.type === 'png').length, 4);
|
|
assert.equal(canonical.filter((a) => a.source.type === 'lucide').length, 11);
|
|
assert.equal(new Set(canonical.map((a) => a.key)).size, 15);
|
|
for (const a of canonical) {
|
|
assert.match(a.key, /^achievement-faction-[a-z]+$/);
|
|
assert.ok(!['Barbarians', 'Pirates', 'Choose Civilization'].includes(a.civilization));
|
|
assert.notEqual(a.source.icon, 'Landmark');
|
|
}
|
|
await validateInputs();
|
|
});
|
|
|
|
test('unknown factions, NPCs, omitted assets, wrong icon and changed source hashes fail closed', async () => {
|
|
const original = await validateInputs();
|
|
for (const mutate of [
|
|
(m) => {
|
|
m.assets[0].civilization = 'Atlantis';
|
|
},
|
|
(m) => {
|
|
m.assets[0].civilization = 'Pirates';
|
|
},
|
|
(m) => {
|
|
m.assets.pop();
|
|
},
|
|
(m) => {
|
|
m.assets.find((a) => a.source.type === 'lucide').source.icon = 'Landmark';
|
|
},
|
|
(m) => {
|
|
m.assets[0].source.sha256 = '0'.repeat(64);
|
|
},
|
|
(m) => {
|
|
m.render.lucideVersion = 'latest';
|
|
},
|
|
]) {
|
|
const changed = structuredClone(original);
|
|
mutate(changed);
|
|
await assert.rejects(validateInputs(changed));
|
|
}
|
|
});
|
|
|
|
let generated = true;
|
|
try {
|
|
await access(path.join(outputDir, 'receipt.json'));
|
|
} catch {
|
|
generated = false;
|
|
}
|
|
test(
|
|
'generated PNG dimensions and hashes match receipt (run build first)',
|
|
{ skip: !generated },
|
|
async () => {
|
|
const receipt = await validateOutputs();
|
|
assert.equal(receipt.renderer.reproducibility, 'two independent page renders byte-identical');
|
|
},
|
|
);
|
|
test('generated byte tampering is rejected', { skip: !generated }, async () => {
|
|
const directory = await mkdtemp(path.join(tmpdir(), 'achievement-assets-test-'));
|
|
try {
|
|
await cp(outputDir, directory, { recursive: true });
|
|
const receipt = await validateOutputs(directory);
|
|
const file = path.join(directory, receipt.assets[0].file);
|
|
const bytes = await readFile(file);
|
|
bytes[bytes.length - 1] ^= 1;
|
|
await writeFile(file, bytes);
|
|
await assert.rejects(validateOutputs(directory));
|
|
} finally {
|
|
await rm(directory, { recursive: true, force: true });
|
|
}
|
|
});
|