Files
workinf_Blender_Wasm/tools/web/check-script-manifest-canonical.mjs
mes123456 380cbed4ff
Some checks are pending
M6 deployable RC / quick (push) Waiting to run
M6 deployable RC / chromium (push) Blocked by required conditions
M6 deployable RC / release (push) Blocked by required conditions
Checkpoint web parity through Chromium input tasks
2026-08-19 10:39:03 -04:00

68 lines
5.1 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 { createRequire } from "node:module";
import { fileURLToPath } from "node:url";
import ts from "../../web/node_modules/typescript/lib/typescript.js";
const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "../..");
const reportPath = path.join(root, "tests/golden/M13-02B/manifest-canonical-report.json");
const manifestPath = path.join(root, "tests/golden/M13-02B/manifest.json");
const temporary = fs.mkdtempSync(path.join(os.tmpdir(), "m13-02b-canonical-"));
const digest = "a".repeat(64);
const signature = "b".repeat(128);
const hashFile = (file) => crypto.createHash("sha256").update(fs.readFileSync(file)).digest("hex");
const script = (id, overrides = {}) => ({ id, name: id, entryPath: `scripts/${id}.py`, sourceByteLength: 128, sourceSha256: digest, publisher: "local", signature, keyId: "key:local", permissions: ["READ_MAIN"], dependencies: [], module: false, cpuMs: 1000, memoryBytes: 1024 * 1024, wallMs: 5000, network: false, autorun: false, driverExpressions: false, addonInstall: false, ...overrides });
const first = {
schemaVersion: 1,
scripts: [
script("zeta", { permissions: ["WRITE_ASSET", "READ_MAIN"], dependencies: [{ id: "alpha", sourceSha256: digest, sourcePath: "deps/alpha.py" }, { id: "beta", sourceSha256: digest, sourcePath: "deps/beta.py" }] }),
script("alpha", { permissions: ["SUBMIT_SERVER_JOB", "READ_ASSET"] }),
script("beta"),
],
};
const second = {
schemaVersion: 1,
scripts: [
{ ...first.scripts[1], permissions: [...first.scripts[1].permissions].reverse(), ignored: "removed" },
{ ...first.scripts[0], permissions: [...first.scripts[0].permissions].reverse(), dependencies: [...first.scripts[0].dependencies].reverse() },
first.scripts[2],
],
};
for (const name of ["asset-path", "capability-gates", "scripting-platform"]) {
const source = fs.readFileSync(path.join(root, `web/protocol/${name}.ts`), "utf8");
const output = ts.transpileModule(source, { compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2022 }, fileName: `${name}.ts` }).outputText
.replace('require("./asset-path")', 'require("./asset-path.cjs")')
.replace('require("./capability-gates")', 'require("./capability-gates.cjs")');
fs.writeFileSync(path.join(temporary, `${name}.cjs`), output);
}
const require = createRequire(import.meta.url);
const protocol = require(path.join(temporary, "scripting-platform.cjs"));
try {
const firstSerialized = protocol.serializeScriptingManifest(first);
const secondSerialized = protocol.serializeScriptingManifest(second);
const canonical = protocol.canonicalizeScriptingManifest(second);
assert.equal(firstSerialized, secondSerialized);
assert.deepEqual({ firstId: canonical.scripts[0].id, firstPermission: canonical.scripts[0].permissions[0], dependencyOrder: canonical.scripts[2].dependencies.map((dependency) => dependency.id), unknownDropped: !("ignored" in canonical.scripts[0]) }, { firstId: "alpha", firstPermission: "READ_ASSET", dependencyOrder: ["alpha", "beta"], unknownDropped: true });
assert.notEqual(firstSerialized, protocol.serializeScriptingManifest({ schemaVersion: 1, scripts: [script("alpha", { sourceByteLength: 129 })] }));
assert.throws(() => protocol.serializeScriptingManifest({ ...first, schemaVersion: 2 }), /PROTOCOL_MISMATCH/);
const report = {
schemaVersion: 1,
task: "M13-02B",
operation: "SCRIPT_MANIFEST_CANONICAL_SERIALIZATION",
equalOrderVariants: true,
canonical: { scriptOrder: canonical.scripts.map((script) => script.id), alphaPermissions: canonical.scripts[0].permissions, zetaDependencies: canonical.scripts[2].dependencies.map((dependency) => dependency.id), unknownFieldsDropped: true },
signatureInput: { schemaVersion: 1, byteLength: Buffer.byteLength(firstSerialized), sha256: crypto.createHash("sha256").update(firstSerialized).digest("hex"), sourceByteLengthMutationChangesInput: true, schemaMutationRejected: true },
nextTask: "M13-02C",
};
if (process.env.UPDATE_M13_02B_REPORT === "1") { fs.mkdirSync(path.dirname(reportPath), { recursive: true }); fs.writeFileSync(reportPath, JSON.stringify(report, null, 2) + "\n"); }
assert.deepEqual(JSON.parse(fs.readFileSync(reportPath, "utf8")), report);
const manifestValue = JSON.parse(fs.readFileSync(manifestPath, "utf8"));
assert.deepEqual({ schemaVersion: manifestValue.schemaVersion, task: manifestValue.task, parentTask: manifestValue.parentTask, nextTask: manifestValue.nextTask }, { schemaVersion: 1, task: "M13-02B", parentTask: "M13-02A", nextTask: "M13-02C" });
for (const artifact of Object.values(manifestValue.artifacts)) assert.equal(hashFile(path.join(root, artifact.path)), artifact.sha256, `artifact hash mismatch ${artifact.path}`);
process.stdout.write(`script-manifest-canonical-ok equal=true bytes=${Buffer.byteLength(firstSerialized)} unknownDropped=true schemaMutation=blocked next=${manifestValue.nextTask}\n`);
}
finally { fs.rmSync(temporary, { recursive: true, force: true }); }