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(), "io-format-recovery-unit-")); const sourcePath = path.join(root, "web/protocol/io-format-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, "io-format-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-07J keeps cancellation and OOM receipts unpublished", () => { const running = protocol.beginIOFormatRecoveryOperation({ operationId: "ply-cancel-1", format: "PLY", operation: "IMPORT", workerGeneration: 1, baseRevision: 3, inputBytes: 32, inputSha256: hash }); const cancelled = protocol.cancelIOFormatRecoveryOperation(running); assert.deepEqual(cancelled, { ...running, status: "CANCELLED", errorCode: "IO_FORMAT_OPERATION_CANCELLED", temporaryBytes: 0, liveRequests: 0, publishedResults: 0, committed: false }); const oomRunning = protocol.beginIOFormatRecoveryOperation({ operationId: "obj-oom-1", format: "OBJ", operation: "IMPORT", workerGeneration: 1, baseRevision: 3, inputBytes: 512 * 1024 + 1, inputSha256: hash }); const blocked = protocol.blockIOFormatRecoveryOperation(oomRunning); assert.equal(blocked.errorCode, "IO_FORMAT_OOM"); assert.equal(blocked.publishedResults, 0); assert.throws(() => protocol.commitIOFormatRecoveryOperation(cancelled, { bytes: 1, sha256: outputHash }), /IO_FORMAT_RECOVERY_INVALID/); }); test("M12-07J binds output identity across restart and rejects stale generation", () => { const running = protocol.beginIOFormatRecoveryOperation({ operationId: "stl-restart-1", format: "STL", operation: "EXPORT", workerGeneration: 1, baseRevision: 7, inputBytes: 64, inputSha256: hash }); const committed = protocol.commitIOFormatRecoveryOperation(running, { bytes: 128, sha256: outputHash }); const recovered = protocol.recoverIOFormatRecoveryOperation(committed, 2); assert.deepEqual(recovered, { ...committed, status: "RECOVERED", workerGeneration: 2, errorCode: "IO_FORMAT_WORKER_RESTARTED" }); assert.equal(protocol.parseIOFormatRecoveryReceipt(recovered).outputSha256, outputHash); assert.throws(() => protocol.recoverIOFormatRecoveryOperation(committed, 1), /IO_FORMAT_RECOVERY_INVALID/); }); test.after(() => fs.rmSync(temporary, { recursive: true, force: true }));