From 9e8b7910670c3f2cf7b8b43d6feadf49526127b7 Mon Sep 17 00:00:00 2001 From: mes123456 Date: Wed, 2 Dec 2026 13:32:06 +0800 Subject: [PATCH] test(web): iterate every engine command through the revision contract Freeze the required/optional/unchecked classification, walk all 174 mutation actions at a matching and a stale revision, cover the required and optional matrices, the folded shader error code, the assert helper and unknown command handling, and assert every declared revision field is actually declared by the command's protocol type. Co-Authored-By: Claude Fable 5.2 --- web/tests/unit/web-engine-revisions.test.mjs | 309 +++++++++++++++++++ 1 file changed, 309 insertions(+) create mode 100644 web/tests/unit/web-engine-revisions.test.mjs diff --git a/web/tests/unit/web-engine-revisions.test.mjs b/web/tests/unit/web-engine-revisions.test.mjs new file mode 100644 index 000000000..c0c8f8eb2 --- /dev/null +++ b/web/tests/unit/web-engine-revisions.test.mjs @@ -0,0 +1,309 @@ +import assert from "node:assert/strict"; +import fs from "node:fs"; +import path from "node:path"; +import test from "node:test"; +import ts from "typescript"; + +const root = path.resolve(import.meta.dirname, "../../.."); + +function transpile(file, replacements = {}) { + let source = fs.readFileSync(path.join(root, file), "utf8"); + for (const [from, to] of Object.entries(replacements)) source = source.replaceAll(`from "${from}"`, `from "${to}"`); + const result = ts.transpileModule(source, { + compilerOptions: { module: ts.ModuleKind.ES2022, target: ts.ScriptTarget.ES2022 }, + fileName: file, + reportDiagnostics: true, + }); + assert.deepEqual(result.diagnostics, []); + return `data:text/javascript;base64,${Buffer.from(result.outputText).toString("base64")}`; +} + +const capabilities = await import(transpile("web/protocol/web-engine-capabilities.ts")); +const revisions = await import(transpile("web/protocol/web-engine-revisions.ts")); +const CAPS = capabilities.WEB_ENGINE_COMMAND_CAPABILITIES; +const CONTRACTS = revisions.WEB_ENGINE_REVISION_CONTRACTS; +const evaluate = revisions.evaluateWebEngineRevision; +const assertRevision = revisions.assertWebEngineRevision; +const RevisionError = revisions.WebEngineRevisionError; + +const REQUIRED = [ + "decimateMesh", + "editEditorContext", + "editShaderNode", + "moveGreasePencilFrame", + "moveGreasePencilLayer", + "moveNLAStrip", + "previewDecimateMesh", + "setSculptMeshAttributes", + "setSequencerStrips", + "setSequencerTransitions", +]; +const OPTIONAL = [ + "addModifier", + "editConstraint", + "moveConstraint", + "moveModifier", + "removeModifier", + "renameConstraint", + "renameModifier", + "setConstraint", + "setCurveTopology", + "setGreasePencilDrawing", + "setGreasePencilInterpolate", + "setGreasePencilMaterial", + "setGreasePencilSelection", + "setGreasePencilStrokes", + "setModifierEnabled", + "setModifierParameters", + "setModifierVisibility", + "setNLAProperty", + "setNLAStack", + "setPoseConstraint", + "setSceneViewLayerAOVs", + "setSceneViewLayerLightgroups", +]; +const PREFLIGHT = [ + "addModifier", + "decimateMesh", + "editConstraint", + "editEditorContext", + "moveConstraint", + "moveModifier", + "moveNLAStrip", + "previewDecimateMesh", + "removeModifier", + "renameConstraint", + "renameModifier", + "setConstraint", + "setCurveTopology", + "setGreasePencilDrawing", + "setGreasePencilInterpolate", + "setGreasePencilMaterial", + "setGreasePencilSelection", + "setGreasePencilStrokes", + "setModifierEnabled", + "setModifierParameters", + "setModifierVisibility", + "setNLAProperty", + "setPoseConstraint", + "setSculptMeshAttributes", + "setSequencerStrips", +]; +const UNCHECKED = [ + "setNLAStack", + "setSceneViewLayerAOVs", + "setSceneViewLayerLightgroups", + "setSequencerTransitions", +]; + +const keysBy = (select) => Object.keys(CONTRACTS).filter((type) => select(CONTRACTS[type])).sort(); + +const withField = (command, field, value) => { + if (field === "baseRevision") return { ...command, baseRevision: value }; + const [owner, property] = field.split("."); + return { ...command, [owner]: { ...(command[owner] ?? {}), [property]: value } }; +}; + +function commandProperties() { + const read = (file) => + ts.createSourceFile(file, fs.readFileSync(path.join(root, file), "utf8"), ts.ScriptTarget.ES2022, true); + const engine = read("web/protocol/web-engine.ts"); + const nla = read("web/protocol/nla.ts"); + const readProperties = (members) => + members.filter((member) => ts.isPropertySignature(member)).map((member) => member.name.getText()); + const readDeclaredType = (members) => { + const typeMember = members.find( + (member) => ts.isPropertySignature(member) && member.name.getText() === "type", + ); + const typeNode = typeMember?.type; + return typeNode && ts.isLiteralTypeNode(typeNode) && ts.isStringLiteral(typeNode.literal) + ? typeNode.literal.text + : null; + }; + const interfaces = new Map(); + const collectInterfaces = (node) => { + if (ts.isInterfaceDeclaration(node)) { + interfaces.set(node.name.text, { + type: readDeclaredType(node.members), + fields: readProperties(node.members), + }); + } + ts.forEachChild(node, collectInterfaces); + }; + collectInterfaces(nla); + + const properties = new Map(); + const visit = (node) => { + if ( + ts.isTypeAliasDeclaration(node) && + node.name.text === "WebEngineEditCommand" && + ts.isUnionTypeNode(node.type) + ) { + for (const member of node.type.types) { + if (ts.isTypeLiteralNode(member)) { + const type = readDeclaredType(member.members); + if (type) properties.set(type, new Set(readProperties(member.members))); + } else if (ts.isTypeReferenceNode(member)) { + const shape = interfaces.get(member.typeName.getText()); + if (shape?.type) properties.set(shape.type, new Set(shape.fields)); + } + } + } + ts.forEachChild(node, visit); + }; + visit(engine); + return properties; +} + +test("the contract is exhaustive over the runtime capability registry", () => { + const contractKeys = Object.keys(CONTRACTS).sort(); + const capabilityKeys = Object.keys(CAPS).sort(); + assert.equal(contractKeys.length, 174); + assert.deepEqual(contractKeys, capabilityKeys); + for (const type of capabilityKeys) { + assert.equal(Object.prototype.hasOwnProperty.call(CONTRACTS, type), true, type); + } + assert.equal(Object.prototype.hasOwnProperty.call(CONTRACTS, "toString"), false); + assert.equal(Object.prototype.hasOwnProperty.call(CONTRACTS, "__proto__"), false); +}); + +test("the contract shape matches its classification", () => { + for (const [type, contract] of Object.entries(CONTRACTS)) { + if (contract.policy === "NONE") { + assert.equal(contract.enforcement, "NONE", type); + assert.equal(contract.field, null, type); + assert.equal(contract.errorCode, null, type); + continue; + } + assert.notEqual(contract.field, null, type); + assert.notEqual(contract.errorCode, null, type); + if (contract.enforcement === "UNCHECKED") { + assert.equal(typeof contract.note, "string", type); + assert.ok(contract.note.length > 0, type); + } + } +}); + +test("the classification is frozen", () => { + assert.deepEqual(keysBy((contract) => contract.policy === "REQUIRED"), REQUIRED); + assert.deepEqual(keysBy((contract) => contract.policy === "OPTIONAL"), OPTIONAL); + assert.equal(keysBy((contract) => contract.policy === "NONE").length, 142); + assert.deepEqual(keysBy((contract) => contract.enforcement === "WORKER_PREFLIGHT"), PREFLIGHT); + assert.deepEqual(keysBy((contract) => contract.enforcement === "UNCHECKED"), UNCHECKED); + assert.deepEqual(keysBy((contract) => contract.enforcement === "PROTOCOL_VALIDATOR"), [ + "moveGreasePencilFrame", + "moveGreasePencilLayer", + ]); + assert.deepEqual(keysBy((contract) => contract.enforcement === "WORKER_FOLDED"), ["editShaderNode"]); +}); + +test("the command routes stay at the frozen inventory", () => { + const routes = { MAIN_AUTHORITATIVE: 0, MAIN_GUARDED: 0, PREVIEW: 0, LOCAL_HISTORY: 0 }; + for (const capability of Object.values(CAPS)) routes[capability.route] += 1; + assert.deepEqual(routes, { MAIN_AUTHORITATIVE: 169, MAIN_GUARDED: 2, PREVIEW: 1, LOCAL_HISTORY: 2 }); +}); + +test("required commands reject missing, malformed, stale and unopened revisions", () => { + for (const type of REQUIRED) { + const contract = CONTRACTS[type]; + const field = contract.field; + assert.equal(evaluate(withField({ type }, field, 7), 7).status, "OK", type); + const stale = evaluate(withField({ type }, field, 6), 7); + assert.equal(stale.status, "REVISION_CONFLICT", type); + assert.equal(stale.code, contract.errorCode, type); + const ahead = evaluate(withField({ type }, field, 8), 7); + assert.equal(ahead.status, "REVISION_CONFLICT", type); + assert.equal(evaluate({ type }, 7).status, "REVISION_REQUIRED", type); + for (const invalid of [1.5, Number.NaN, -1]) { + assert.equal(evaluate(withField({ type }, field, invalid), 7).status, "REVISION_INVALID", type); + } + assert.equal(evaluate(withField({ type }, field, 7), null).status, "REVISION_CONFLICT", type); + } +}); + +test("optional commands tolerate an absent revision but validate a present one", () => { + for (const type of OPTIONAL) { + const contract = CONTRACTS[type]; + const field = contract.field; + assert.equal(evaluate({ type }, 7).status, "OK", type); + assert.equal(evaluate(withField({ type }, field, 7), 7).status, "OK", type); + assert.equal(evaluate(withField({ type }, field, 6), 7).status, "REVISION_CONFLICT", type); + assert.equal(evaluate(withField({ type }, field, "7"), 7).status, "REVISION_INVALID", type); + } +}); + +test("every mutation action iterates through its contract", () => { + for (const type of Object.keys(CONTRACTS)) { + const contract = CONTRACTS[type]; + if (contract.policy === "NONE") { + assert.equal(evaluate({ type }, 7).status, "OK", type); + assert.equal(evaluate({ type }, null).status, "OK", type); + continue; + } + const command = withField({ type }, contract.field, 7); + assert.equal(evaluate(command, 7).status, "OK", type); + assert.equal(evaluate(command, 8).status, "REVISION_CONFLICT", type); + } +}); + +test("shader staleness keeps its folded INVALID_ARGUMENT code", () => { + const shader = evaluate({ type: "editShaderNode", baseRevision: 1 }, 2); + assert.equal(shader.status, "REVISION_CONFLICT"); + assert.equal(shader.code, "INVALID_ARGUMENT"); + const nla = evaluate({ type: "moveNLAStrip", baseRevision: 1 }, 2); + assert.equal(nla.status, "REVISION_CONFLICT"); + assert.equal(nla.code, "REVISION_CONFLICT"); +}); + +test("unchecked commands are disclosed and still evaluate their declared field", () => { + for (const type of UNCHECKED) { + const contract = CONTRACTS[type]; + const verdict = evaluate(withField({ type }, contract.field, 6), 7); + assert.equal(verdict.status, "REVISION_CONFLICT", type); + } +}); + +test("the assert helper reports the contract error code", () => { + assert.throws( + () => assertRevision({ type: "moveNLAStrip", baseRevision: 1 }, 2), + (error) => error instanceof RevisionError && error.code === "REVISION_CONFLICT", + ); + assert.throws( + () => assertRevision({ type: "editShaderNode", baseRevision: 1 }, 2), + (error) => error.code === "INVALID_ARGUMENT", + ); + let captured = null; + assert.throws( + () => + assertRevision({ type: "setSceneViewLayerAOVs", baseRevision: 1 }, 2, (code, message) => { + captured = { code, message }; + throw new Error("stop"); + }), + /stop/, + ); + assert.equal(captured.code, "REVISION_CONFLICT"); + assert.doesNotThrow(() => assertRevision({ type: "setFrame" }, null)); +}); + +test("every revision field is declared by the command protocol type", () => { + const properties = commandProperties(); + for (const [type, contract] of Object.entries(CONTRACTS)) { + if (contract.policy === "NONE") continue; + const fields = properties.get(type); + assert.ok(fields, `${type} is missing from WebEngineEditCommand`); + const owner = contract.field.split(".")[0]; + assert.ok(fields.has(owner), `${type} does not declare ${owner}`); + } +}); + +test("unknown commands never receive a revision verdict", () => { + assert.deepEqual(evaluate({ type: "doesNotExist" }, 7), { + status: "UNKNOWN_COMMAND", + type: "doesNotExist", + }); + assert.deepEqual(evaluate({ type: "toString" }, 7), { status: "UNKNOWN_COMMAND", type: "toString" }); + assert.throws( + () => assertRevision({ type: "doesNotExist" }, 7), + (error) => error.code === "COMMAND_UNSUPPORTED", + ); +});