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 root = path.resolve(import.meta.dirname, "../../.."); const temporary = fs.mkdtempSync(path.join(os.tmpdir(), "compositor-webgpu-unit-")); function transpile(sourceName, outputName, replacements = []) { const sourcePath = path.join(root, "web/protocol", sourceName); const transpiled = ts.transpileModule(fs.readFileSync(sourcePath, "utf8"), { compilerOptions: { module: ts.ModuleKind.ES2022, target: ts.ScriptTarget.ES2022 }, fileName: sourcePath, reportDiagnostics: true, }); assert.deepEqual(transpiled.diagnostics, []); const output = replacements.reduce((value, [from, to]) => value.replaceAll(from, to), transpiled.outputText); fs.writeFileSync(path.join(temporary, outputName), output); } transpile("capability-gates.ts", "capability-gates.mjs"); transpile("compositor.ts", "compositor.mjs", [ ['from "./capability-gates"', 'from "./capability-gates.mjs"'], ]); const compositor = await import(pathToFileURL(path.join(temporary, "compositor.mjs"))); const golden = JSON.parse(fs.readFileSync(path.join(root, "tests/golden/M11-07/compositor-node-golden.json"), "utf8")); const sourceColor = [0.125, 0.25, 0.5, 0.75]; function graph(nodeTypes, extraNodes = [], resources = []) { const nodes = nodeTypes.map((type, index) => ({ id: `node:${index}`, type, name: `${type}:${index}`, properties: type === "CONSTANT_COLOR" ? { color: sourceColor } : type === "EXPOSURE" ? { exposure: 1 } : {}, })); return { schemaVersion: 1, id: `graph:${nodeTypes.join("-")}`, name: "M11-07 unit", outputNodeId: nodes.at(-1).id, nodes: [...nodes, ...extraNodes], links: nodes.slice(1).map((node, index) => ({ fromNodeId: nodes[index].id, fromSocket: "Image", toNodeId: node.id, toSocket: "Image" })), resources, }; } function repeatedPixel(pixel, count) { const result = new Float32Array(count * 4); for (let index = 0; index < count; index++) result.set(pixel, index * 4); return result; } test("M11-07 freezes only nodes with an independent CPU/WebGPU golden", () => { assert.deepEqual(compositor.COMPOSITOR_WEBGPU_NODE_ALLOWLIST, golden.allowlist); for (const candidate of golden.cases) { const value = graph(candidate.nodeTypes); const plan = compositor.compileCompositorWebGPUPlan(value); assert.deepEqual(plan.instructions.map((instruction) => instruction.type), candidate.nodeTypes); const cpu = compositor.executeCompositorGraph(value, new Map(), { width: golden.width, height: golden.height }); const expected = repeatedPixel(candidate.pixel, golden.width * golden.height); assert.deepEqual(cpu.composite.data, expected, candidate.scene); assert.equal(crypto.createHash("sha256").update(new Uint8Array(cpu.composite.data.buffer)).digest("hex"), candidate.float32Sha256); } }); test("M11-07 rejects every CPU-only or undeclared node before WebGPU compilation", () => { const properties = { ALPHA_OVER: {}, BLUR: { radius: 1 }, IMAGE: { resourceId: "resource:test" }, MIX: { factor: 0.5 }, RENDER_LAYER: { resourceId: "resource:test" }, TRANSFORM: {}, UNSUPPORTED: {}, VIEWER: {} }; for (const type of golden.blockedNodeTypes) { const node = { id: `blocked:${type}`, type, name: type, properties: properties[type], ...(type === "UNSUPPORTED" ? { blenderType: "CompositorNodeGlare" } : {}) }; const resources = type === "IMAGE" || type === "RENDER_LAYER" ? [{ id: "resource:test", kind: type, sourceId: "image:test" }] : []; assert.throws(() => compositor.compileCompositorWebGPUPlan(graph(["CONSTANT_COLOR", "COMPOSITE"], [node], resources)), { code: "COMPOSITOR_NODE_UNSUPPORTED" }); } }); test("M11-07 rejects disconnected allowlisted nodes and non-Image links", () => { assert.throws( () => compositor.compileCompositorWebGPUPlan(graph(["CONSTANT_COLOR", "COMPOSITE"], [{ id: "extra", type: "INVERT", name: "extra", properties: {} }])), { code: "COMPOSITOR_GRAPH_INVALID" }, ); const malformed = graph(["CONSTANT_COLOR", "EXPOSURE", "COMPOSITE"]); malformed.links[0].toSocket = "Value"; assert.throws(() => compositor.compileCompositorWebGPUPlan(malformed), { code: "COMPOSITOR_GRAPH_INVALID" }); }); test.after(() => fs.rmSync(temporary, { recursive: true, force: true }));