|
| 1 | +import assert from "node:assert/strict"; |
| 2 | +import { mkdtemp, writeFile } from "node:fs/promises"; |
| 3 | +import os from "node:os"; |
| 4 | +import path from "node:path"; |
| 5 | +import test from "node:test"; |
| 6 | + |
| 7 | +import { runCli } from "./cli.js"; |
| 8 | +import { validateRehearsalEvidence } from "./rehearsal.js"; |
| 9 | + |
| 10 | +const version = "2026.08.8"; |
| 11 | +const sourceCommit = "b".repeat(40); |
| 12 | + |
| 13 | +function rehearsal(overrides: Record<string, unknown> = {}) { |
| 14 | + return { |
| 15 | + version, |
| 16 | + sourceCommit, |
| 17 | + runId: "wrun_01K5NRQ8ABCDEF", |
| 18 | + runUrl: "https://ai-workflow.blazity.com/runs/wrun_01K5NRQ8ABCDEF", |
| 19 | + repository: "Blazity/aiw-checks-fixture", |
| 20 | + checksDurationSec: 1180, |
| 21 | + outcome: "success", |
| 22 | + recordedAt: "2026-08-19T10:00:00Z", |
| 23 | + recordedBy: "someone@blazity.com", |
| 24 | + ...overrides, |
| 25 | + }; |
| 26 | +} |
| 27 | + |
| 28 | +async function recordPath(contents: string): Promise<string> { |
| 29 | + const directory = await mkdtemp(path.join(os.tmpdir(), "artur-rehearsal-")); |
| 30 | + const file = path.join(directory, `${version}.json`); |
| 31 | + await writeFile(file, contents); |
| 32 | + return file; |
| 33 | +} |
| 34 | + |
| 35 | +test("accepts a rehearsal of the released commit whose checks outlived one invocation", async () => { |
| 36 | + const rehearsalPath = await recordPath(JSON.stringify(rehearsal())); |
| 37 | + const record = await validateRehearsalEvidence({ version, sourceCommit, rehearsalPath }); |
| 38 | + assert.equal(record.runId, "wrun_01K5NRQ8ABCDEF"); |
| 39 | + assert.equal(record.checksDurationSec, 1180); |
| 40 | +}); |
| 41 | + |
| 42 | +test("accepts checks that last exactly the one-invocation limit", async () => { |
| 43 | + const rehearsalPath = await recordPath(JSON.stringify(rehearsal({ checksDurationSec: 300 }))); |
| 44 | + await assert.doesNotReject(validateRehearsalEvidence({ version, sourceCommit, rehearsalPath })); |
| 45 | +}); |
| 46 | + |
| 47 | +test("blocks a release with no recorded rehearsal", async () => { |
| 48 | + const directory = await mkdtemp(path.join(os.tmpdir(), "artur-rehearsal-missing-")); |
| 49 | + await assert.rejects( |
| 50 | + validateRehearsalEvidence({ |
| 51 | + version, |
| 52 | + sourceCommit, |
| 53 | + rehearsalPath: path.join(directory, `${version}.json`), |
| 54 | + }), |
| 55 | + /No rehearsal is recorded for 2026\.08\.8/, |
| 56 | + ); |
| 57 | +}); |
| 58 | + |
| 59 | +test("blocks a rehearsal record that is not valid JSON", async () => { |
| 60 | + const rehearsalPath = await recordPath("{ not json"); |
| 61 | + await assert.rejects( |
| 62 | + validateRehearsalEvidence({ version, sourceCommit, rehearsalPath }), |
| 63 | + /is not valid JSON/, |
| 64 | + ); |
| 65 | +}); |
| 66 | + |
| 67 | +test("blocks a rehearsal record with a missing field", async () => { |
| 68 | + const { checksDurationSec: _omitted, ...withoutDuration } = rehearsal(); |
| 69 | + const rehearsalPath = await recordPath(JSON.stringify(withoutDuration)); |
| 70 | + await assert.rejects( |
| 71 | + validateRehearsalEvidence({ version, sourceCommit, rehearsalPath }), |
| 72 | + /does not match the required shape.*checksDurationSec/s, |
| 73 | + ); |
| 74 | +}); |
| 75 | + |
| 76 | +test("blocks a rehearsal record with a malformed commit, timestamp or URL", async () => { |
| 77 | + const rehearsalPath = await recordPath( |
| 78 | + JSON.stringify( |
| 79 | + rehearsal({ sourceCommit: "B".repeat(40), recordedAt: "19-08-2026", runUrl: "runs/1" }), |
| 80 | + ), |
| 81 | + ); |
| 82 | + await assert.rejects( |
| 83 | + validateRehearsalEvidence({ version, sourceCommit, rehearsalPath }), |
| 84 | + /sourceCommit.*runUrl.*recordedAt/s, |
| 85 | + ); |
| 86 | +}); |
| 87 | + |
| 88 | +test("blocks a rehearsal of a different source commit", async () => { |
| 89 | + const rehearsalPath = await recordPath( |
| 90 | + JSON.stringify(rehearsal({ sourceCommit: "c".repeat(40) })), |
| 91 | + ); |
| 92 | + await assert.rejects( |
| 93 | + validateRehearsalEvidence({ version, sourceCommit, rehearsalPath }), |
| 94 | + /ran at source commit cccc.*ships bbbb/s, |
| 95 | + ); |
| 96 | +}); |
| 97 | + |
| 98 | +test("blocks a rehearsal that did not end in success", async () => { |
| 99 | + const rehearsalPath = await recordPath(JSON.stringify(rehearsal({ outcome: "failed" }))); |
| 100 | + await assert.rejects( |
| 101 | + validateRehearsalEvidence({ version, sourceCommit, rehearsalPath }), |
| 102 | + /ended with outcome "failed"/, |
| 103 | + ); |
| 104 | +}); |
| 105 | + |
| 106 | +test("blocks a rehearsal whose checks never crossed one invocation", async () => { |
| 107 | + const rehearsalPath = await recordPath(JSON.stringify(rehearsal({ checksDurationSec: 299 }))); |
| 108 | + await assert.rejects( |
| 109 | + validateRehearsalEvidence({ version, sourceCommit, rehearsalPath }), |
| 110 | + /299 seconds, below the 300 second floor/, |
| 111 | + ); |
| 112 | +}); |
| 113 | + |
| 114 | +test("blocks a rehearsal whose checks were stubs", async () => { |
| 115 | + const rehearsalPath = await recordPath(JSON.stringify(rehearsal({ checksDurationSec: 12 }))); |
| 116 | + await assert.rejects( |
| 117 | + validateRehearsalEvidence({ version, sourceCommit, rehearsalPath }), |
| 118 | + /12 seconds, below the 300 second floor/, |
| 119 | + ); |
| 120 | +}); |
| 121 | + |
| 122 | +test("validate-rehearsal CLI reads the recorded rehearsal for the released version", async () => { |
| 123 | + const rehearsalPath = await recordPath(JSON.stringify(rehearsal())); |
| 124 | + await assert.doesNotReject( |
| 125 | + runCli([ |
| 126 | + "validate-rehearsal", |
| 127 | + "--version", |
| 128 | + version, |
| 129 | + "--source-commit", |
| 130 | + sourceCommit, |
| 131 | + "--rehearsal", |
| 132 | + rehearsalPath, |
| 133 | + ]), |
| 134 | + ); |
| 135 | + await assert.rejects( |
| 136 | + runCli(["validate-rehearsal", "--version", version, "--rehearsal", rehearsalPath]), |
| 137 | + /Missing required argument: --source-commit/, |
| 138 | + ); |
| 139 | +}); |
0 commit comments