| 1 | import assert from "node:assert/strict"; |
| 2 | import { test } from "node:test"; |
| 3 | |
| 4 | import { messageParts, parseCommitMessage } from "./commit-message.ts"; |
| 5 | |
| 6 | test("co-authors leave the body and keep only their names", () => { |
| 7 | const parsed = parseCommitMessage( |
| 8 | "Document every option\n\nAdds the missing rows.\n\nCo-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>", |
| 9 | ); |
| 10 | assert.equal(parsed.subject, "Document every option"); |
| 11 | assert.equal(parsed.body, "Adds the missing rows."); |
| 12 | assert.deepEqual(parsed.coAuthors, ["Claude Sonnet 5.5"]); |
| 13 | assert.deepEqual(parsed.trailers, []); |
| 14 | }); |
| 15 | |
| 16 | test("a message that is only trailers has no body", () => { |
| 17 | const parsed = parseCommitMessage("Fix it\n\nCo-Authored-By: Ada <ada@example.com>\nG1t-Pull: syntaqx/hello#72"); |
| 18 | assert.equal(parsed.body, ""); |
| 19 | assert.deepEqual(parsed.coAuthors, ["Ada"]); |
| 20 | assert.deepEqual(parsed.trailers, [{ key: "G1t-Pull", value: "syntaqx/hello#72" }]); |
| 21 | }); |
| 22 | |
| 23 | test("a trailer shows its name, never an address", () => { |
| 24 | const parsed = parseCommitMessage("Fix it\n\nSigned-off-by: Ada <ada@private.example>\nReviewed-by: <sam@x.io>"); |
| 25 | assert.deepEqual(parsed.trailers, [{ key: "Signed-off-by", value: "Ada" }]); |
| 26 | }); |
| 27 | |
| 28 | test("prose that happens to contain a colon stays prose", () => { |
| 29 | const parsed = parseCommitMessage("Fix it\n\nNote: this also renames a function.\nAnd tidies the tests."); |
| 30 | assert.equal(parsed.body, "Note: this also renames a function.\nAnd tidies the tests."); |
| 31 | assert.deepEqual(parsed.coAuthors, []); |
| 32 | }); |
| 33 | |
| 34 | test("references split from the text around them", () => { |
| 35 | assert.deepEqual(messageParts("Fix the login form. Closes #12"), [ |
| 36 | { kind: "text", text: "Fix the login form. Closes " }, |
| 37 | { kind: "ref", text: "#12", repo: null, number: 12 }, |
| 38 | ]); |
| 39 | assert.deepEqual(messageParts("#3 and acme/web#4."), [ |
| 40 | { kind: "ref", text: "#3", repo: null, number: 3 }, |
| 41 | { kind: "text", text: " and " }, |
| 42 | { kind: "ref", text: "acme/web#4", repo: "acme/web", number: 4 }, |
| 43 | { kind: "text", text: "." }, |
| 44 | ]); |
| 45 | }); |
| 46 | |
| 47 | test("a full hash is a commit; a short one, a link's fragment or a word's # are text", () => { |
| 48 | const sha = "0123456789abcdef0123456789abcdef01234567"; |
| 49 | assert.deepEqual(messageParts(`closed this in ${sha}`), [ |
| 50 | { kind: "text", text: "closed this in " }, |
| 51 | { kind: "commit", text: sha, sha }, |
| 52 | ]); |
| 53 | for (const text of ["see 0123abc", "https://example.com/page#12", "C#12 support", "issue#12", "##12"]) { |
| 54 | assert.deepEqual(messageParts(text), [{ kind: "text", text }], text); |
| 55 | } |
| 56 | assert.deepEqual(messageParts(""), []); |
| 57 | }); |
| 58 | |
| 59 | test("a session's id is a session, as a note says what it closed the issue in", () => { |
| 60 | const sha = "0123456789abcdef0123456789abcdef01234567"; |
| 61 | const id = "ses_01m4z1z7nse8w9cjnecymt7rzn"; |
| 62 | assert.deepEqual(messageParts(`closed this in ${sha} via session ${id}`), [ |
| 63 | { kind: "text", text: "closed this in " }, |
| 64 | { kind: "commit", text: sha, sha }, |
| 65 | { kind: "text", text: " via session " }, |
| 66 | { kind: "session", text: id, id }, |
| 67 | ]); |
| 68 | for (const text of ["ses_short", "uses_01m4z1z7nse8w9cjnecymt7rzn"]) { |
| 69 | assert.deepEqual(messageParts(text), [{ kind: "text", text }], text); |
| 70 | } |
| 71 | }); |