| 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 { |
| 10 | artifactNamespace, |
| 11 | candidateId, |
| 12 | desktopPlatforms, |
| 13 | npmPackages, |
| 14 | sealCandidate, |
| 15 | sealedNotesPath, |
| 16 | verifyCandidate, |
| 17 | } from "./release-candidate.mjs"; |
| 18 | |
| 19 | function fixture(t) { |
| 20 | const root = mkdtempSync(path.join(tmpdir(), "reasonix-candidate-")); |
| 21 | t.after(() => rmSync(root, { recursive: true, force: true })); |
| 22 | for (const platform of desktopPlatforms) { |
| 23 | const dir = path.join(root, "desktop", platform, "files"); |
| 24 | mkdirSync(dir, { recursive: true }); |
| 25 | const artifact = `${platform}.zip`; |
| 26 | writeFileSync(path.join(dir, artifact), platform); |
| 27 | const sha256 = createHash("sha256").update(platform).digest("hex"); |
| 28 | writeFileSync(path.join(root, "desktop", platform, "identity.json"), JSON.stringify({ |
| 29 | schema: 1, |
| 30 | platform, |
| 31 | sourceSHA: "a".repeat(40), |
| 32 | controlSHA: "b".repeat(40), |
| 33 | tag: "desktop-v1.2.3", |
| 34 | version: "v1.2.3", |
| 35 | channel: "stable", |
| 36 | signingFingerprint: "v1:fingerprint", |
| 37 | prefix: "desktop-123-1-preflight", |
| 38 | files: [{ name: artifact, size: Buffer.byteLength(platform), sha256 }], |
| 39 | })); |
| 40 | } |
| 41 | const cli = path.join(root, "cli"); |
| 42 | mkdirSync(cli); |
| 43 | 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"]) { |
| 44 | writeFileSync(path.join(cli, name), name); |
| 45 | } |
| 46 | const npm = path.join(root, "npm"); |
| 47 | mkdirSync(npm); |
| 48 | for (const name of npmPackages) writeFileSync(path.join(npm, `${name}-1.2.3.tgz`), name); |
| 49 | const evidence = path.join(root, "evidence"); |
| 50 | mkdirSync(evidence); |
| 51 | for (const kind of ["windows-amd64", "windows-arm64", "macos-universal-intel"]) { |
| 52 | const platform = kind === "macos-universal-intel" ? "darwin-universal" : kind; |
| 53 | writeFileSync(path.join(evidence, `${kind}.json`), JSON.stringify({ |
| 54 | schema: 1, |
| 55 | kind, |
| 56 | status: "passed", |
| 57 | version: "v1.2.3", |
| 58 | sha256: createHash("sha256").update(platform).digest("hex"), |
| 59 | })); |
| 60 | } |
| 61 | writeFileSync(path.join(evidence, "release-notes.md"), sealedNotes); |
| 62 | return root; |
| 63 | } |
| 64 | |
| 65 | const sealedNotes = "notes by @alice\n"; |
| 66 | |
| 67 | const metadata = { |
| 68 | version: "1.2.3", sourceSHA: "a".repeat(40), buildControlSHA: "b".repeat(40), |
| 69 | acceptanceControlSHA: "c".repeat(40), notesSourceSHA: "a".repeat(40), |
| 70 | catalogSha256: "d".repeat(64), renderedNotesSha256: createHash("sha256").update("notes by @alice\n").digest("hex"), |
| 71 | repository: "esengine/DeepSeek-Reasonix", workflow: ".github/workflows/release-candidate.yml", |
| 72 | runId: "123", runAttempt: "1", desktopPrefix: "desktop-123-1-preflight", |
| 73 | payloadArtifactId: "456", payloadArtifactName: `release-candidate-payload-${candidateId("1.2.3", "a".repeat(40), "d".repeat(64))}`, |
| 74 | evidenceArtifactId: "457", evidenceArtifactName: `release-candidate-evidence-${candidateId("1.2.3", "a".repeat(40), "d".repeat(64))}`, |
| 75 | desktopFingerprint: "v1:fingerprint", createdAt: "2026-01-01T00:00:00.000Z", |
| 76 | expiresAt: "2026-02-01T00:00:00.000Z", |
| 77 | acceptance: ["windows-amd64", "windows-arm64", "macos-universal-intel"].map(kind => ({ kind, status: "passed", evidencePath: `evidence/${kind}.json` })), |
| 78 | }; |
| 79 | |
| 80 | test("seals and verifies a complete immutable candidate", t => { |
| 81 | const root = fixture(t); |
| 82 | const record = sealCandidate(root, metadata); |
| 83 | assert.equal(record.candidateId, candidateId("1.2.3", "a".repeat(40), "d".repeat(64))); |
| 84 | assert.doesNotThrow(() => verifyCandidate(root, record, new Date("2026-01-02T00:00:00Z"))); |
| 85 | }); |
| 86 | |
| 87 | test("the rendered notes are sealed into the payload and bound to their hash", t => { |
| 88 | const root = fixture(t); |
| 89 | const record = sealCandidate(root, metadata); |
| 90 | assert.equal(record.notes.renderedPath, sealedNotesPath); |
| 91 | assert.equal(readFileSync(path.join(root, record.notes.renderedPath), "utf8"), sealedNotes); |
| 92 | assert.throws(() => sealCandidate(root, { ...metadata, renderedNotesSha256: "e".repeat(64) }), /do not match renderedSha256/); |
| 93 | writeFileSync(path.join(root, sealedNotesPath), "notes by @mallory\n"); |
| 94 | assert.throws(() => verifyCandidate(root, record, new Date("2026-01-02T00:00:00Z")), /digest mismatch/); |
| 95 | rmSync(path.join(root, sealedNotesPath)); |
| 96 | assert.throws(() => sealCandidate(root, metadata), /missing sealed release notes/); |
| 97 | }); |
| 98 | |
| 99 | test("a candidate sealed before notes were sealed still verifies", t => { |
| 100 | const root = fixture(t); |
| 101 | const record = sealCandidate(root, metadata); |
| 102 | rmSync(path.join(root, sealedNotesPath)); |
| 103 | const legacy = structuredClone(record); |
| 104 | delete legacy.notes.renderedPath; |
| 105 | legacy.files = legacy.files.filter(file => file.path !== sealedNotesPath); |
| 106 | assert.doesNotThrow(() => verifyCandidate(root, legacy, new Date("2026-01-02T00:00:00Z"))); |
| 107 | const forged = { ...legacy, notes: { ...legacy.notes, renderedPath: "evidence/other.md" } }; |
| 108 | assert.throws(() => verifyCandidate(root, forged, new Date("2026-01-02T00:00:00Z")), /sealed release notes path/); |
| 109 | }); |
| 110 | |
| 111 | test("rehearsal bytes remain isolated from the publication identity", t => { |
| 112 | const root = fixture(t); |
| 113 | const id = candidateId(metadata.version, metadata.sourceSHA, metadata.catalogSha256); |
| 114 | const record = sealCandidate(root, { |
| 115 | ...metadata, purpose: "rehearsal", |
| 116 | payloadArtifactName: `${artifactNamespace("rehearsal")}-payload-${id}`, |
| 117 | evidenceArtifactName: `${artifactNamespace("rehearsal")}-evidence-${id}`, |
| 118 | }); |
| 119 | assert.equal(record.purpose, "rehearsal"); |
| 120 | assert.doesNotThrow(() => verifyCandidate(root, record, new Date("2026-01-02T00:00:00Z"), "rehearsal")); |
| 121 | assert.throws(() => verifyCandidate(root, record, new Date("2026-01-02T00:00:00Z")), /purpose mismatch/); |
| 122 | assert.throws(() => sealCandidate(root, { ...metadata, purpose: "rehearsal" }), /payloadArtifactName/); |
| 123 | assert.throws(() => sealCandidate(root, { ...metadata, purpose: "unknown" }), /purpose/); |
| 124 | }); |
| 125 | |
| 126 | for (const mutation of ["payload", "expired", "revoked", "wrong-source", "missing-acceptance"]) { |
| 127 | test(`rejects ${mutation} candidate reuse`, t => { |
| 128 | const root = fixture(t); |
| 129 | const record = sealCandidate(root, metadata); |
| 130 | if (mutation === "payload") writeFileSync(path.join(root, "cli", "SHA256SUMS"), "changed"); |
| 131 | if (mutation === "expired") record.validity.expiresAt = "2025-01-01T00:00:00.000Z"; |
| 132 | if (mutation === "revoked") record.validity.revoked = true; |
| 133 | if (mutation === "wrong-source") record.source.repository = "fork/Reasonix"; |
| 134 | if (mutation === "missing-acceptance") record.acceptance.pop(); |
| 135 | assert.throws(() => verifyCandidate(root, record, new Date("2026-01-02T00:00:00Z"))); |
| 136 | }); |
| 137 | } |
| 138 | |
| 139 | test("rejects a candidate missing a platform before sealing", t => { |
| 140 | const root = fixture(t); |
| 141 | rmSync(path.join(root, "desktop", desktopPlatforms[0]), { recursive: true }); |
| 142 | assert.throws(() => sealCandidate(root, metadata), /missing Desktop bundle/); |
| 143 | }); |
| 144 | |
| 145 | test("rejects acceptance evidence that names bytes outside the sealed platform bundle", t => { |
| 146 | const root = fixture(t); |
| 147 | const receipt = path.join(root, "evidence", "windows-amd64.json"); |
| 148 | const value = JSON.parse(readFileSync(receipt, "utf8")); |
| 149 | value.sha256 = "f".repeat(64); |
| 150 | writeFileSync(receipt, JSON.stringify(value)); |
| 151 | assert.throws(() => sealCandidate(root, metadata), /does not name a sealed file/); |
| 152 | }); |
| 153 | |
| 154 | test("candidate id command accepts the catalog digest as its third argument", () => { |
| 155 | const output = execFileSync(process.execPath, [ |
| 156 | fileURLToPath(new URL("./release-candidate.mjs", import.meta.url)), |
| 157 | "id", "1.2.3", "a".repeat(40), "d".repeat(64), |
| 158 | ], { encoding: "utf8" }).trim(); |
| 159 | assert.equal(output, candidateId("1.2.3", "a".repeat(40), "d".repeat(64))); |
| 160 | }); |
| 161 |