import assert from "node:assert/strict"; import crypto from "node:crypto"; import fs from "node:fs/promises"; import os from "node:os"; import path from "node:path"; import { fileURLToPath } from "node:url"; import { submitIdempotentServerJobResult } from "./server-job-idempotency.mjs"; const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "../.."); const reportPath = path.join(root, "tests/golden/M13-04J/server-job-idempotency-report.json"); const manifestPath = path.join(root, "tests/golden/M13-04J/manifest.json"); const hashFile = async (file) => crypto.createHash("sha256").update(await fs.readFile(file)).digest("hex"); const h = (value) => crypto.createHash("sha256").update(value).digest("hex"); const rootDirectory = await fs.mkdtemp(path.join(os.tmpdir(), "m13-04j-check-")); const options = { receiptDirectory: path.join(rootDirectory, "receipts"), outputDirectory: path.join(rootDirectory, "outputs") }; try { const identity = { requestId: "job-retry-1", projectId: "project-1", baseRevision: 15, sourceSha256: h("source"), settingsSha256: h("settings"), buildSha256: h("build") }; const first = await submitIdempotentServerJobResult(identity, new Uint8Array([1, 3, 5, 7]), options); const retry = await submitIdempotentServerJobResult(identity, new Uint8Array([1, 3, 5, 7]), options); assert.equal(first.reused, false); assert.equal(retry.reused, true); await assert.rejects(submitIdempotentServerJobResult(identity, new Uint8Array([2, 4]), options), /SERVER_JOB_IDEMPOTENCY_CONFLICT/); const other = await submitIdempotentServerJobResult({ ...identity, requestId: "job-retry-2" }, new Uint8Array([2, 4]), options); assert.equal(other.reused, false); const concurrent = await Promise.all([ submitIdempotentServerJobResult({ ...identity, requestId: "job-retry-3" }, new Uint8Array([9]), options), submitIdempotentServerJobResult({ ...identity, requestId: "job-retry-3" }, new Uint8Array([9]), options), ]); assert.deepEqual(concurrent.map((value) => value.reused).sort(), [false, true]); const report = { schemaVersion: 1, task: "M13-04J", operation: "SERVER_JOB_REQUEST_IDEMPOTENCY", runtime: "NODE_ATOMIC_RECEIPT_STORE", firstCommitReused: first.reused, exactRetryReused: retry.reused, conflictBlocked: true, differentRequestIsolated: other.reused === false, concurrentReuse: concurrent.map((value) => value.reused).sort(), execution: "DISABLED", nextTask: "M13-05A" }; if (process.env.UPDATE_M13_04J_REPORT === "1") { await fs.mkdir(path.dirname(reportPath), { recursive: true }); await fs.writeFile(reportPath, `${JSON.stringify(report, null, 2)}\n`); } assert.deepEqual(JSON.parse(await fs.readFile(reportPath, "utf8")), report); const manifest = JSON.parse(await fs.readFile(manifestPath, "utf8")); assert.deepEqual({ schemaVersion: manifest.schemaVersion, task: manifest.task, parentTask: manifest.parentTask, nextTask: manifest.nextTask }, { schemaVersion: 1, task: "M13-04J", parentTask: "M13-04I", nextTask: "M13-05A" }); for (const artifact of Object.values(manifest.artifacts)) assert.equal(await hashFile(path.join(root, artifact.path)), artifact.sha256, `artifact hash mismatch ${artifact.path}`); process.stdout.write(`server-job-idempotency-ok first=COMMITTED retry=REUSED conflict=BLOCKED isolated=1 concurrent=REUSED execution=DISABLED next=${manifest.nextTask}\n`); } finally { await fs.rm(rootDirectory, { recursive: true, force: true }); }