Skip to content

Where people and agents ship software together. The open-source git platform for the whole job: issues, agents, checks and deploys to the edge.

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