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-03F/sandbox-dispose-report.json"); const manifestPath = path.join(root, "tests/golden/M13-03F/manifest.json"); const temporary = fs.mkdtempSync(path.join(os.tmpdir(), "m13-03f-dispose-")); const hashFile = (file) => crypto.createHash("sha256").update(fs.readFileSync(file)).digest("hex"); try { const source = fs.readFileSync(path.join(root, "web/app/src/testing/script-sandbox-dispose.ts"), "utf8"); const output = ts.transpileModule(source, { compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2022 }, fileName: "script-sandbox-dispose.ts", }).outputText; fs.writeFileSync(path.join(temporary, "script-sandbox-dispose.cjs"), output); const protocol = createRequire(import.meta.url)(path.join(temporary, "script-sandbox-dispose.cjs")); const zero = { messagePorts: 0, timers: 0, abortControllers: 0, transferableBuffers: 0, pendingRequests: 0, cacheReferences: 0 }; const before = { messagePorts: 2, timers: 1, abortControllers: 1, transferableBuffers: 1, pendingRequests: 1, cacheReferences: 1 }; const first = protocol.createScriptSandboxDisposeReceipt(1); const second = protocol.createScriptSandboxDisposeReceipt(2); assert.deepEqual(first.resources, zero); assert.deepEqual(second.resources, zero); assert.equal(first.idempotent, false); assert.equal(second.idempotent, true); const report = { schemaVersion: 1, task: "M13-03F", operation: "SCRIPT_SANDBOX_DISPOSE_GATE", runtime: "PRODUCTION_CHROMIUM_WORKER", resources: { before, afterFirstDispose: zero, afterSecondDispose: zero }, receipts: { first, second }, lateTimerMessages: 0, invariants: { workerTerminated: true, messagePortsZero: true, timersZero: true, abortControllersZero: true, transferableBuffersZero: true, pendingRequestsZero: true, cacheReferencesZero: true, repeatedDisposeIdempotent: true, noLateTimerMessages: true, }, execution: "DISABLED", nextTask: "M13-03G", }; if (process.env.UPDATE_M13_03F_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 manifest = JSON.parse(fs.readFileSync(manifestPath, "utf8")); assert.deepEqual( { schemaVersion: manifest.schemaVersion, task: manifest.task, parentTask: manifest.parentTask, nextTask: manifest.nextTask }, { schemaVersion: 1, task: "M13-03F", parentTask: "M13-03E", nextTask: "M13-03G" }, ); for (const artifact of Object.values(manifest.artifacts)) { assert.equal(hashFile(path.join(root, artifact.path)), artifact.sha256, `artifact hash mismatch ${artifact.path}`); } process.stdout.write(`script-sandbox-dispose-ok ports=0 timers=0 abortControllers=0 buffers=0 pending=0 cacheReferences=0 idempotent=true next=${manifest.nextTask}\n`); } finally { fs.rmSync(temporary, { recursive: true, force: true }); }