90 lines
4.4 KiB
JavaScript
90 lines
4.4 KiB
JavaScript
import assert from "node:assert/strict";
|
|
import crypto from "node:crypto";
|
|
import fs from "node:fs";
|
|
import os from "node:os";
|
|
import path from "node:path";
|
|
import { pathToFileURL } from "node:url";
|
|
import test from "node:test";
|
|
import ts from "typescript";
|
|
|
|
const repoRoot = path.resolve(import.meta.dirname, "../../..");
|
|
const temporary = fs.mkdtempSync(path.join(os.tmpdir(), "asset-catalog-negatives-unit-"));
|
|
const sources = ["asset-path.ts", "capability-gates.ts", "asset-library-io.ts", "asset-catalog-v2.ts", "asset-catalog-migration.ts"];
|
|
for (const sourceName of sources) {
|
|
const sourcePath = path.join(repoRoot, "web/protocol", sourceName);
|
|
const result = ts.transpileModule(fs.readFileSync(sourcePath, "utf8"), {
|
|
compilerOptions: { module: ts.ModuleKind.ES2022, target: ts.ScriptTarget.ES2022 },
|
|
fileName: sourcePath,
|
|
reportDiagnostics: true,
|
|
});
|
|
assert.deepEqual(result.diagnostics, []);
|
|
fs.writeFileSync(path.join(temporary, sourceName.replace(/\.ts$/, ".mjs")), result.outputText.replaceAll(/from "\.\/([a-z0-9-]+)"/g, 'from "./$1.mjs"'));
|
|
}
|
|
const v2Protocol = await import(pathToFileURL(path.join(temporary, "asset-catalog-v2.mjs")));
|
|
const migration = await import(pathToFileURL(path.join(temporary, "asset-catalog-migration.mjs")));
|
|
const v1 = JSON.parse(fs.readFileSync(path.join(repoRoot, "tests/golden/M12-01D/catalog-v1.json"), "utf8"));
|
|
const v2 = JSON.parse(fs.readFileSync(path.join(repoRoot, "tests/golden/M12-01D/catalog-v2.json"), "utf8"));
|
|
const golden = JSON.parse(fs.readFileSync(path.join(repoRoot, "tests/golden/M12-01F/negative-cases.json"), "utf8"));
|
|
const manifest = JSON.parse(fs.readFileSync(path.join(repoRoot, "tests/golden/M12-01F/manifest.json"), "utf8"));
|
|
|
|
test("M12-01F binds every negative input contract", () => {
|
|
assert.equal(manifest.negativeCaseCount, golden.cases.length);
|
|
assert.equal(manifest.nextTask, "M12-01G");
|
|
for (const artifact of Object.values(manifest.artifacts)) {
|
|
const actual = crypto.createHash("sha256").update(fs.readFileSync(path.join(repoRoot, artifact.path))).digest("hex");
|
|
assert.equal(actual, artifact.sha256, `${artifact.path} hash drifted`);
|
|
}
|
|
});
|
|
|
|
async function codeFrom(run) {
|
|
try {
|
|
await run();
|
|
return "NO_ERROR";
|
|
}
|
|
catch (error) {
|
|
return error?.code ?? "UNKNOWN_ERROR";
|
|
}
|
|
}
|
|
|
|
test("M12-01F rejects duplicate identities, cycles, budgets, and unknown fields", async () => {
|
|
const duplicateCatalog = structuredClone(v2);
|
|
duplicateCatalog.catalogs.push(structuredClone(duplicateCatalog.catalogs[0]));
|
|
const duplicateAsset = structuredClone(v2);
|
|
duplicateAsset.assets.push(structuredClone(duplicateAsset.assets[0]));
|
|
const legacyCycle = structuredClone(v1);
|
|
legacyCycle.catalogs[0].parentId = legacyCycle.catalogs[1].id;
|
|
const parentMismatch = structuredClone(v2);
|
|
parentMismatch.catalogs[0].parentPath = "Characters";
|
|
const simpleName = structuredClone(v2);
|
|
simpleName.catalogs[0].simpleName = "\u00e9".repeat(32);
|
|
const tag = structuredClone(v2);
|
|
tag.assets[0].tags = ["\u00e9".repeat(32)];
|
|
tag.assets[0].activeTag = 0;
|
|
const unknownTop = { ...structuredClone(v2), future: true };
|
|
const unknownAsset = structuredClone(v2);
|
|
unknownAsset.assets[0].future = true;
|
|
const unknownLegacyAsset = structuredClone(v1);
|
|
unknownLegacyAsset.assets[0].future = true;
|
|
|
|
const cases = [
|
|
["DUPLICATE_CATALOG_ID", () => v2Protocol.parseAssetCatalogManifestV2(duplicateCatalog)],
|
|
["DUPLICATE_ASSET_ID", () => v2Protocol.parseAssetCatalogManifestV2(duplicateAsset)],
|
|
["LEGACY_PARENT_CYCLE", () => migration.migrateAssetCatalogV1ToV2(legacyCycle)],
|
|
["PARENT_PATH_MISMATCH", () => v2Protocol.parseAssetCatalogManifestV2(parentMismatch)],
|
|
["OVERLONG_SIMPLE_NAME_UTF8", () => v2Protocol.parseAssetCatalogManifestV2(simpleName)],
|
|
["OVERLONG_TAG_UTF8", () => v2Protocol.parseAssetCatalogManifestV2(tag)],
|
|
["V2_UNKNOWN_TOP_LEVEL", () => v2Protocol.parseAssetCatalogManifestV2(unknownTop)],
|
|
["V2_UNKNOWN_ASSET_FIELD", () => v2Protocol.parseAssetCatalogManifestV2(unknownAsset)],
|
|
["V1_UNKNOWN_ASSET_FIELD", () => migration.migrateAssetCatalogV1ToV2(unknownLegacyAsset)],
|
|
];
|
|
const actual = [];
|
|
for (const [id, run] of cases) {
|
|
actual.push({ id, code: await codeFrom(run) });
|
|
assert.deepEqual(await v2Protocol.parseAssetCatalogManifestV2(v2), v2, `${id} poisoned the valid parser path`);
|
|
}
|
|
assert.deepEqual(actual, golden.cases);
|
|
assert.equal(golden.nextTask, "M12-01G");
|
|
});
|
|
|
|
test.after(() => fs.rmSync(temporary, { recursive: true, force: true }));
|