170 lines · 8.0 KBRaw
| 1 | import assert from "node:assert/strict"; |
| 2 | import { test } from "node:test"; |
| 3 | import { checkDecisions, decisionIdFor, DECISION_ID, MISSING_COST, narrative, netDecisions, parseDecisions, reviewCommits } from "../decision.ts"; |
| 4 | |
| 5 | const A = "DEC-01JB7Q3M8Z4K2N6V9XWRT5HYCE"; |
| 6 | const B = "DEC-01JB6ZK2P0R8D4T1M7QWN3VXSA"; |
| 7 | const SHA = "8f8b47dcf240385c09a95af895ecda6e97734b1e"; |
| 8 | |
| 9 | const message = `Move session storage to Durable Objects |
| 10 | |
| 11 | Session writes were racing under concurrent logins. |
| 12 | |
| 13 | \`\`\`decision |
| 14 | decision_id: ${A} |
| 15 | task: 52 |
| 16 | decision: "Store sessions in Durable Objects rather than D1." |
| 17 | reason: |
| 18 | - Need serialization for concurrent session mutation |
| 19 | - Avoid stale replica reads during authentication |
| 20 | alternatives_considered: |
| 21 | - D1 |
| 22 | - KV |
| 23 | rejected: |
| 24 | D1: Read replication complicates authoritative session state. |
| 25 | KV: 'Eventual consistency isn''t suitable.' |
| 26 | evidence: |
| 27 | - src/auth/session.ts |
| 28 | supersedes: |
| 29 | - ${B} |
| 30 | \`\`\` |
| 31 | `; |
| 32 | |
| 33 | test("parses a full decision record", () => { |
| 34 | const { decisions, problems } = parseDecisions(message, SHA); |
| 35 | assert.deepEqual(problems, []); |
| 36 | assert.deepEqual(decisions, [ |
| 37 | { |
| 38 | decisionId: A, |
| 39 | decision: "Store sessions in Durable Objects rather than D1.", |
| 40 | toReverse: "", |
| 41 | reason: ["Need serialization for concurrent session mutation", "Avoid stale replica reads during authentication"], |
| 42 | alternativesConsidered: ["D1", "KV"], |
| 43 | rejected: { D1: "Read replication complicates authoritative session state.", KV: "Eventual consistency isn't suitable." }, |
| 44 | assumptions: [], |
| 45 | affectedComponents: [], |
| 46 | evidence: ["src/auth/session.ts"], |
| 47 | supersedes: [B], |
| 48 | }, |
| 49 | ]); |
| 50 | }); |
| 51 | |
| 52 | test("a plain commit message has no decisions", () => { |
| 53 | assert.deepEqual(parseDecisions("Fix typo in README\n", SHA), { decisions: [], problems: [] }); |
| 54 | }); |
| 55 | |
| 56 | test("reports missing, unknown and malformed fields", () => { |
| 57 | const { decisions, problems } = parseDecisions("x\n\n```decision\ndecision_id: DEC-nope\ntask: abc\nstatus: active\nreason: one\n```\n", SHA); |
| 58 | assert.deepEqual(decisions, []); |
| 59 | assert.deepEqual( |
| 60 | problems.map((problem) => problem.field), |
| 61 | ["status", "decision_id", "decision", "reason"], |
| 62 | ); |
| 63 | }); |
| 64 | |
| 65 | test("rejects tabs, unclosed blocks and mixed nesting", () => { |
| 66 | assert.match(parseDecisions("```decision\ndecision:\tx\n```\n", SHA).problems[0].message, /tabs/); |
| 67 | assert.match(parseDecisions("```decision\ndecision: x\n", SHA).problems[0].message, /not closed/); |
| 68 | assert.match(parseDecisions("```decision\nreason:\n - a\n b: c\n```\n", SHA).problems[0].message, /mixes/); |
| 69 | assert.match(parseDecisions("```decision\nreason:\ndecision: x\n```\n", SHA).problems[0].message, /reason has no value/); |
| 70 | }); |
| 71 | |
| 72 | test("checks duplicates and supersedes across a push", () => { |
| 73 | const record = parseDecisions(message, SHA).decisions[0]; |
| 74 | assert.deepEqual(checkDecisions([record], { known: new Set([B]) }), []); |
| 75 | |
| 76 | const problems = checkDecisions([record, record], { known: new Set() }); |
| 77 | assert.deepEqual( |
| 78 | problems.map((problem) => problem.field), |
| 79 | ["supersedes", "decision_id", "supersedes"], |
| 80 | ); |
| 81 | }); |
| 82 | |
| 83 | test("reviews pushed commits into records to index and problems that reject the push", () => { |
| 84 | const sha = (n: number) => String(n).repeat(40); |
| 85 | const bare = "Pick a queue\n\n```decision\ndecision: Queue pushes.\nreason:\n - x\n```\n"; |
| 86 | const broken = "Try again\n\n```decision\ndecision: no reason\n```\n"; |
| 87 | const { records, problems } = reviewCommits( |
| 88 | [ |
| 89 | { hash: sha(1), message: "Fix typo" }, |
| 90 | { hash: sha(2), message }, |
| 91 | { hash: sha(3), message: bare }, |
| 92 | { hash: sha(4), message: broken }, |
| 93 | { hash: sha(5), message }, |
| 94 | { hash: sha(6), message: "Fix typo\n\nDecision: none\n" }, |
| 95 | { hash: sha(7), message: "Merge main", parents: [sha(5), sha(6)] }, |
| 96 | ], |
| 97 | { known: new Set([B]) }, |
| 98 | ); |
| 99 | assert.deepEqual( |
| 100 | records.map(({ commit, record }) => [commit, record.decisionId]), |
| 101 | [ |
| 102 | [sha(2), A], |
| 103 | [sha(3), decisionIdFor(sha(3), 0)], |
| 104 | ], |
| 105 | ); |
| 106 | assert.deepEqual(problems, [ |
| 107 | { commit: sha(4), message: "decision block 1: reason is required." }, |
| 108 | { commit: sha(5), message: `${A} already exists.` }, |
| 109 | ]); |
| 110 | }); |
| 111 | |
| 112 | test("a strict review refuses a record that does not say what it costs to reverse", () => { |
| 113 | const without = "Pick a queue\n\n```decision\ndecision: Queue pushes.\nreason:\n - x\n```\n"; |
| 114 | const withCost = without.replace("```\n", "to_reverse: Teams build on the queue.\n```\n"); |
| 115 | const review = (text: string) => reviewCommits([{ hash: "3".repeat(40), message: text }], { known: new Set(), strict: true }); |
| 116 | assert.deepEqual(review(without).problems, [{ commit: "3".repeat(40), message: `"Queue pushes.": ${MISSING_COST}` }]); |
| 117 | assert.deepEqual(review(withCost).problems, []); |
| 118 | assert.equal(review(withCost).records[0].record.toReverse, "Teams build on the queue."); |
| 119 | assert.deepEqual(review("Fix typo").problems, []); |
| 120 | }); |
| 121 | |
| 122 | test("net decisions leave out a superseded decision and a repeated sentence", () => { |
| 123 | const entry = (decisionId: string, decision: string, supersedes: string[] = []) => ({ record: { ...parseDecisions(message, SHA).decisions[0], decisionId, decision, supersedes } }); |
| 124 | const net = netDecisions([entry("a", "Use a separate Worker."), entry("b", "Limit jobs."), entry("c", "Limit jobs."), entry("d", "Use the main Worker.", ["a"])]); |
| 125 | assert.deepEqual(net.map(({ record }) => record.decisionId), ["b", "d"]); |
| 126 | }); |
| 127 | |
| 128 | test("derives a stable, valid id from the commit and the block's position", () => { |
| 129 | const first = decisionIdFor(SHA, 0); |
| 130 | assert.match(first, DECISION_ID); |
| 131 | assert.equal(first, decisionIdFor(SHA, 0)); |
| 132 | assert.notEqual(first, decisionIdFor(SHA, 1)); |
| 133 | assert.notEqual(first, decisionIdFor("1".repeat(40), 0)); |
| 134 | const two = parseDecisions("x\n\n```decision\ndecision: a\nreason:\n - r\n```\n\n```decision\ndecision: b\nreason:\n - r\n```\n", SHA).decisions; |
| 135 | assert.deepEqual(two.map((record) => record.decisionId), [decisionIdFor(SHA, 0), decisionIdFor(SHA, 1)]); |
| 136 | }); |
| 137 | |
| 138 | test("supersedes names a decision by id, or by its commit and block number", () => { |
| 139 | const record = parseDecisions(`x\n\n\`\`\`decision\ndecision: a\nreason:\n - r\nsupersedes:\n - ${SHA}#2\n - ${A}\n\`\`\`\n`, "1".repeat(40)).decisions[0]; |
| 140 | assert.deepEqual(record.supersedes, [decisionIdFor(SHA, 1), A]); |
| 141 | const { problems } = parseDecisions(`x\n\n\`\`\`decision\ndecision: a\nreason:\n - r\nsupersedes:\n - 8ead82f#1\n\`\`\`\n`, SHA); |
| 142 | assert.deepEqual(problems.map((problem) => problem.field), ["supersedes"]); |
| 143 | }); |
| 144 | |
| 145 | test("an old record that names a task is still valid, and the task is not kept", () => { |
| 146 | const old = "x\n\n```decision\ntask: 9\ndecision: a\nreason:\n - r\n```\n"; |
| 147 | const review = reviewCommits([{ hash: SHA, message: old }], { known: new Set() }); |
| 148 | assert.deepEqual(review.problems, []); |
| 149 | assert.equal("task" in review.records[0].record, false); |
| 150 | }); |
| 151 | |
| 152 | test("narrative keeps the body and drops the subject and decision blocks", () => { |
| 153 | assert.equal(narrative(message), "Session writes were racing under concurrent logins."); |
| 154 | assert.equal(narrative("Subject only"), ""); |
| 155 | assert.equal(narrative("Fix typo\n\nOnly words.\n\nDecision: none\n"), "Only words."); |
| 156 | }); |
| 157 | |
| 158 | test("a commit with more decision blocks than allowed is refused, and no block past the cap gets an id", () => { |
| 159 | const block = "```decision\ndecision: Keep it.\nreason:\n - It works.\n```"; |
| 160 | const message = `Subject\n\n${Array.from({ length: 33 }, () => block).join("\n\n")}`; |
| 161 | const { decisions, problems } = parseDecisions(message, "a".repeat(40)); |
| 162 | assert.equal(decisions.length, 32); |
| 163 | assert.ok(decisions.every((record) => !record.decisionId.includes("undefined"))); |
| 164 | assert.ok(problems.some((problem) => /at most 32/.test(problem.message))); |
| 165 | }); |
| 166 | |
| 167 | test("narrative drops trailing git trailers and collapses runs of blank lines", () => { |
| 168 | const message = "Subject\n\nFirst paragraph.\n\n\n\nSecond paragraph: with a colon.\n\n```decision\ndecision: Keep it.\nreason:\n - It works.\n```\n\nCo-Authored-By: Claude <noreply@anthropic.com>\nClaude-Session: https://claude.ai/code/session_x\n"; |
| 169 | assert.equal(narrative(message), "First paragraph.\n\nSecond paragraph: with a colon."); |
| 170 | }); |