| 1 | import assert from "node:assert/strict"; |
| 2 | import { execFileSync } from "node:child_process"; |
| 3 | import { createHash } from "node:crypto"; |
| 4 | import { mkdtempSync, mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs"; |
| 5 | import { tmpdir } from "node:os"; |
| 6 | import path from "node:path"; |
| 7 | import { fileURLToPath } from "node:url"; |
| 8 | import test from "node:test"; |
| 9 | import { artifactNamespace, candidateId, desktopPlatforms, npmPackages, sealCandidate, verifyCandidate } from "./release-candidate.mjs"; |
| 10 | |
| 11 | function fixture(t) { |
| 12 | const root = mkdtempSync(path.join(tmpdir(), "reasonix-candidate-")); |
| 13 | t.after(() => rmSync(root, { recursive: true, force: true })); |
| 14 | for (const platform of desktopPlatforms) { |
| 15 | const dir = path.join(root, "desktop", platform, "files"); |
| 16 | mkdirSync(dir, { recursive: true }); |
| 17 | const artifact = `${platform}.zip`; |
| 18 | writeFileSync(path.join(dir, artifact), platform); |
| 19 | const sha256 = createHash("sha256").update(platform).digest("hex"); |
| 20 | writeFileSync(path.join(root, "desktop", platform, "identity.json"), JSON.stringify({ |
| 21 | schema: 1, |
| 22 | platform, |
| 23 | sourceSHA: "a".repeat(40), |
| 24 | controlSHA: "b".repeat(40), |
| 25 | tag: "desktop-v1.2.3", |
| 26 | version: "v1.2.3", |
| 27 | channel: "stable", |
| 28 | signingFingerprint: "v1:fingerprint", |
| 29 | prefix: "desktop-123-1-preflight", |
| 30 | files: [{ name: artifact, size: Buffer.byteLength(platform), sha256 }], |
| 31 | })); |
| 32 | } |
| 33 | const cli = path.join(root, "cli"); |
| 34 | mkdirSync(cli); |
| 35 | for (const name of ["reasonix-darwin-amd64.tar.gz", "reasonix-darwin-arm64.tar.gz", "reasonix-linux-amd64.tar.gz", "reasonix-linux-arm64.tar.gz", "reasonix-windows-amd64.zip", "reasonix-windows-arm64.zip", "SHA256SUMS", "reasonix.rb"]) { |
| 36 | writeFileSync(path.join(cli, name), name); |
| 37 | } |
| 38 | const npm = path.join(root, "npm"); |
| 39 | mkdirSync(npm); |
| 40 | for (const name of npmPackages) writeFileSync(path.join(npm, `${name}-1.2.3.tgz`), name); |
| 41 | const evidence = path.join(root, "evidence"); |
| 42 | mkdirSync(evidence); |
| 43 | for (const kind of ["windows-amd64", "windows-arm64", "macos-universal-intel"]) { |
| 44 | const platform = kind === "macos-universal-intel" ? "darwin-universal" : kind; |
| 45 | writeFileSync(path.join(evidence, `${kind}.json`), JSON.stringify({ |
| 46 | schema: 1, |
| 47 | kind, |
| 48 | status: "passed", |
| 49 | version: "v1.2.3", |
| 50 | sha256: createHash("sha256").update(platform).digest("hex"), |
| 51 | })); |
| 52 | } |
| 53 | return root; |
| 54 | } |
| 55 | |
| 56 | const metadata = { |
| 57 | version: "1.2.3", sourceSHA: "a".repeat(40), buildControlSHA: "b".repeat(40), |
| 58 | acceptanceControlSHA: "c".repeat(40), notesSourceSHA: "a".repeat(40), |
| 59 | catalogSha256: "d".repeat(64), renderedNotesSha256: "e".repeat(64), |
| 60 | repository: "esengine/DeepSeek-Reasonix", workflow: ".github/workflows/release-candidate.yml", |
| 61 | runId: "123", runAttempt: "1", desktopPrefix: "desktop-123-1-preflight", |
| 62 | payloadArtifactId: "456", payloadArtifactName: `release-candidate-payload-${candidateId("1.2.3", "a".repeat(40), "d".repeat(64))}`, |
| 63 | evidenceArtifactId: "457", evidenceArtifactName: `release-candidate-evidence-${candidateId("1.2.3", "a".repeat(40), "d".repeat(64))}`, |
| 64 | desktopFingerprint: "v1:fingerprint", createdAt: "2026-01-01T00:00:00.000Z", |
| 65 | expiresAt: "2026-02-01T00:00:00.000Z", |
| 66 | acceptance: ["windows-amd64", "windows-arm64", "macos-universal-intel"].map(kind => ({ kind, status: "passed", evidencePath: `evidence/${kind}.json` })), |
| 67 | }; |
| 68 | |
| 69 | test("seals and verifies a complete immutable candidate", t => { |
| 70 | const root = fixture(t); |
| 71 | const record = sealCandidate(root, metadata); |
| 72 | assert.equal(record.candidateId, candidateId("1.2.3", "a".repeat(40), "d".repeat(64))); |
| 73 | assert.doesNotThrow(() => verifyCandidate(root, record, new Date("2026-01-02T00:00:00Z"))); |
| 74 | }); |
| 75 | |
| 76 | test("rehearsal bytes remain isolated from the publication identity", t => { |
| 77 | const root = fixture(t); |
| 78 | const id = candidateId(metadata.version, metadata.sourceSHA, metadata.catalogSha256); |
| 79 | const record = sealCandidate(root, { |
| 80 | ...metadata, purpose: "rehearsal", |
| 81 | payloadArtifactName: `${artifactNamespace("rehearsal")}-payload-${id}`, |
| 82 | evidenceArtifactName: `${artifactNamespace("rehearsal")}-evidence-${id}`, |
| 83 | }); |
| 84 | assert.equal(record.purpose, "rehearsal"); |
| 85 | assert.doesNotThrow(() => verifyCandidate(root, record, new Date("2026-01-02T00:00:00Z"), "rehearsal")); |
| 86 | assert.throws(() => verifyCandidate(root, record, new Date("2026-01-02T00:00:00Z")), /purpose mismatch/); |
| 87 | assert.throws(() => sealCandidate(root, { ...metadata, purpose: "rehearsal" }), /payloadArtifactName/); |
| 88 | assert.throws(() => sealCandidate(root, { ...metadata, purpose: "unknown" }), /purpose/); |
| 89 | }); |
| 90 | |
| 91 | for (const mutation of ["payload", "expired", "revoked", "wrong-source", "missing-acceptance"]) { |
| 92 | test(`rejects ${mutation} candidate reuse`, t => { |
| 93 | const root = fixture(t); |
| 94 | const record = sealCandidate(root, metadata); |
| 95 | if (mutation === "payload") writeFileSync(path.join(root, "cli", "SHA256SUMS"), "changed"); |
| 96 | if (mutation === "expired") record.validity.expiresAt = "2025-01-01T00:00:00.000Z"; |
| 97 | if (mutation === "revoked") record.validity.revoked = true; |
| 98 | if (mutation === "wrong-source") record.source.repository = "fork/Reasonix"; |
| 99 | if (mutation === "missing-acceptance") record.acceptance.pop(); |
| 100 | assert.throws(() => verifyCandidate(root, record, new Date("2026-01-02T00:00:00Z"))); |
| 101 | }); |
| 102 | } |
| 103 | |
| 104 | test("rejects a candidate missing a platform before sealing", t => { |
| 105 | const root = fixture(t); |
| 106 | rmSync(path.join(root, "desktop", desktopPlatforms[0]), { recursive: true }); |
| 107 | assert.throws(() => sealCandidate(root, metadata), /missing Desktop bundle/); |
| 108 | }); |
| 109 | |
| 110 | test("rejects acceptance evidence that names bytes outside the sealed platform bundle", t => { |
| 111 | const root = fixture(t); |
| 112 | const receipt = path.join(root, "evidence", "windows-amd64.json"); |
| 113 | const value = JSON.parse(readFileSync(receipt, "utf8")); |
| 114 | value.sha256 = "f".repeat(64); |
| 115 | writeFileSync(receipt, JSON.stringify(value)); |
| 116 | assert.throws(() => sealCandidate(root, metadata), /does not name a sealed file/); |
| 117 | }); |
| 118 | |
| 119 | test("candidate id command accepts the catalog digest as its third argument", () => { |
| 120 | const output = execFileSync(process.execPath, [ |
| 121 | fileURLToPath(new URL("./release-candidate.mjs", import.meta.url)), |
| 122 | "id", "1.2.3", "a".repeat(40), "d".repeat(64), |
| 123 | ], { encoding: "utf8" }).trim(); |
| 124 | assert.equal(output, candidateId("1.2.3", "a".repeat(40), "d".repeat(64))); |
| 125 | }); |
| 126 |