import assert from "node:assert/strict"; 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(), "glb-recovery-unit-")); const sourcePath = path.join(root, "web/protocol/glb-recovery.ts"); 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 modulePath = path.join(temporary, "glb-recovery.mjs"); fs.writeFileSync(modulePath, transpiled.outputText); const protocol = await import(pathToFileURL(modulePath)); const hash = "a".repeat(64); const outputHash = "b".repeat(64); test("M12-06G keeps cancellation and quota failure fail-closed", () => { const running = protocol.beginGLBRecoveryOperation({ operationId: "import-1", operation: "IMPORT", workerGeneration: 1, baseRevision: 7, inputBytes: 128, inputSha256: hash, }); assert.equal(running.status, "RUNNING"); assert.equal(running.candidateRevision, 8); const cancelled = protocol.cancelGLBRecoveryOperation(running); assert.deepEqual(protocol.parseGLBRecoveryReceipt(cancelled), cancelled); assert.equal(cancelled.errorCode, "GLB_OPERATION_CANCELLED"); assert.equal(cancelled.temporaryBytes, 0); assert.equal(cancelled.committed, false); assert.throws(() => protocol.commitGLBRecoveryOperation(cancelled, { bytes: 1, sha256: outputHash }), /GLB_RECOVERY_INVALID/); assert.equal(protocol.blockGLBRecoveryForQuota(running).errorCode, "GLB_OPFS_QUOTA"); }); test("M12-06G binds committed output and worker-generation recovery", () => { const running = protocol.beginGLBRecoveryOperation({ operationId: "export-1", operation: "EXPORT", workerGeneration: 1, baseRevision: 11, inputBytes: 256, inputSha256: hash, }); const committed = protocol.commitGLBRecoveryOperation(running, { bytes: 512, sha256: outputHash }); assert.equal(committed.status, "COMMITTED"); assert.equal(committed.committed, true); assert.equal(committed.liveRequests, 0); const recovered = protocol.recoverGLBRecoveryOperation(committed, 2); assert.equal(recovered.status, "RECOVERED"); assert.equal(recovered.workerGeneration, 2); assert.equal(recovered.errorCode, "GLB_WORKER_RESTARTED"); assert.throws(() => protocol.recoverGLBRecoveryOperation(committed, 1), /GLB_RECOVERY_INVALID/); }); test.after(() => fs.rmSync(temporary, { recursive: true, force: true }));