CodeIssuesDiscussionsWikiPull RequestsProjectsCommitsActionsReleasesContributorsPulse● GatesSecuritySettingsDeploymentsPipelineInsightsAgents✨ Explain✨ Ask AI✨ Workspace✨ Spec✨ Tests▓ Debt Map✨ NL Search🏛 Archaeology
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 | /**
* Tests for src/lib/ai-commit-message.ts.
*
* We cover the pure / deterministic surface here (parsing, heuristics,
* truncation, prompt shape). The Anthropic-calling path is exercised
* indirectly via the heuristic fallback when ANTHROPIC_API_KEY is unset
* (the default in CI).
*/
import { describe, it, expect } from "bun:test";
import {
generateCommitMessage,
truncateDiff,
DIFF_BYTE_CAP,
__test,
} from "../lib/ai-commit-message";
const { heuristicMessage, parseModelOutput, buildPrompt, CONVENTIONAL_TYPES } =
__test;
const SAMPLE_DIFF = `diff --git a/src/foo.ts b/src/foo.ts
index abc..def 100644
--- a/src/foo.ts
+++ b/src/foo.ts
@@ -1,3 +1,3 @@
-old line
+new line
unchanged
`;
const NEW_FILE_DIFF = `diff --git a/src/feature.ts b/src/feature.ts
new file mode 100644
index 0000000..abc
--- /dev/null
+++ b/src/feature.ts
@@ -0,0 +1,3 @@
+export function hello() {
+ return "world";
+}
`;
const DOCS_DIFF = `diff --git a/README.md b/README.md
index 1..2 100644
--- a/README.md
+++ b/README.md
@@ -1,1 +1,1 @@
-Old
+New
`;
const TEST_DIFF = `diff --git a/src/__tests__/foo.test.ts b/src/__tests__/foo.test.ts
new file mode 100644
--- /dev/null
+++ b/src/__tests__/foo.test.ts
@@ -0,0 +1,1 @@
+test("hi", () => {});
`;
// ──────────────────────────── truncation ────────────────────────────
describe("truncateDiff", () => {
it("returns the diff unchanged when under cap", () => {
expect(truncateDiff("hello")).toBe("hello");
});
it("truncates to cap with a marker when over", () => {
const big = "x".repeat(DIFF_BYTE_CAP + 1000);
const out = truncateDiff(big);
expect(out.length).toBeLessThanOrEqual(DIFF_BYTE_CAP);
expect(out.endsWith("... (more)")).toBe(true);
});
it("respects a custom cap", () => {
const big = "x".repeat(2000);
const out = truncateDiff(big, 200);
expect(out.length).toBeLessThanOrEqual(200);
expect(out.endsWith("... (more)")).toBe(true);
});
it("does not append marker when input exactly fits", () => {
const exact = "x".repeat(50);
expect(truncateDiff(exact, 50)).toBe(exact);
});
});
// ──────────────────────────── heuristic ────────────────────────────
describe("heuristicMessage", () => {
it("returns a chore message for empty input", () => {
const m = heuristicMessage("");
expect(m.subject).toBe("chore: update");
expect(m.body).toBe("");
});
it("classifies docs-only diffs as docs:", () => {
const m = heuristicMessage(DOCS_DIFF);
expect(m.subject.startsWith("docs")).toBe(true);
});
it("classifies test-only diffs as test:", () => {
const m = heuristicMessage(TEST_DIFF);
expect(m.subject.startsWith("test")).toBe(true);
});
it("classifies new-file diffs as feat:", () => {
const m = heuristicMessage(NEW_FILE_DIFF);
expect(m.subject.startsWith("feat")).toBe(true);
});
it("extracts a scope when all paths share a top-level dir", () => {
const m = heuristicMessage(NEW_FILE_DIFF);
expect(m.subject).toMatch(/^feat\(src\):/);
});
it("includes touched files in the body", () => {
const m = heuristicMessage(SAMPLE_DIFF);
expect(m.body).toContain("src/foo.ts");
});
it("caps subject at 72 chars", () => {
const longPaths = Array.from({ length: 50 }, (_, i) => `src/${i}/file${i}.ts`).join("\n");
const big = longPaths
.split("\n")
.map((p) => `diff --git a/${p} b/${p}\n+++ b/${p}\n`)
.join("");
const m = heuristicMessage(big);
expect(m.subject.length).toBeLessThanOrEqual(72);
});
it("supports plain style (no type prefix)", () => {
const m = heuristicMessage(SAMPLE_DIFF, "plain");
expect(m.subject).not.toMatch(/^(feat|fix|chore|docs|test|refactor):/);
expect(m.subject[0]).toBe(m.subject[0].toUpperCase());
});
});
// ──────────────────────────── parseModelOutput ────────────────────────────
describe("parseModelOutput", () => {
it("parses well-formed JSON with subject + body", () => {
const m = parseModelOutput(
'{"subject": "feat(auth): add login", "body": "Because users need it."}'
);
expect(m.subject).toBe("feat(auth): add login");
expect(m.body).toBe("Because users need it.");
});
it("strips a ```json code fence", () => {
const text = '```json\n{"subject":"fix: bug","body":""}\n```';
const m = parseModelOutput(text);
expect(m.subject).toBe("fix: bug");
});
it("falls back to first-line/body parsing for plain text", () => {
const m = parseModelOutput("feat(api): expose new endpoint\n\nDetails here.");
expect(m.subject).toBe("feat(api): expose new endpoint");
expect(m.body).toContain("Details here.");
});
it("strips quotes around the subject", () => {
const m = parseModelOutput('"fix: oops"');
expect(m.subject).toBe("fix: oops");
});
it("normalises non-conventional subjects when conventional style is requested", () => {
const m = parseModelOutput("Add new feature", "conventional");
expect(m.subject.startsWith("chore:")).toBe(true);
});
it("preserves conventional subjects unchanged", () => {
const m = parseModelOutput("feat(x): do thing", "conventional");
expect(m.subject).toBe("feat(x): do thing");
});
it("caps overly long subjects at 72 chars", () => {
const long = "feat: " + "x".repeat(200);
const m = parseModelOutput(long);
expect(m.subject.length).toBeLessThanOrEqual(72);
expect(m.subject.endsWith("...")).toBe(true);
});
it("returns a safe default for empty / garbage input", () => {
const m = parseModelOutput("");
expect(m.subject).toBe("chore: update");
});
});
// ──────────────────────────── buildPrompt ────────────────────────────
describe("buildPrompt", () => {
it("mentions every conventional-commit type", () => {
const p = buildPrompt("(diff)", "conventional");
for (const t of CONVENTIONAL_TYPES) {
expect(p).toContain(t);
}
});
it("requires JSON output", () => {
const p = buildPrompt("(diff)", "conventional");
expect(p).toContain("JSON");
expect(p).toContain('"subject"');
expect(p).toContain('"body"');
});
it("omits the type-list when plain style is requested", () => {
const p = buildPrompt("(diff)", "plain");
expect(p).toContain("plain-English");
// The plain prompt should NOT list conventional types as required types.
expect(p).not.toContain("MUST start with one of: feat");
});
it("includes the diff verbatim", () => {
const p = buildPrompt("DIFF_MARKER", "conventional");
expect(p).toContain("DIFF_MARKER");
});
});
// ──────────────────────────── generateCommitMessage ────────────────────────────
describe("generateCommitMessage — fallback path", () => {
it("returns a heuristic message when ANTHROPIC_API_KEY is missing", async () => {
const before = process.env.ANTHROPIC_API_KEY;
delete process.env.ANTHROPIC_API_KEY;
try {
const m = await generateCommitMessage(SAMPLE_DIFF);
expect(m.subject.length).toBeGreaterThan(0);
// Heuristic always emits Conventional style by default.
expect(m.subject).toMatch(/^[a-z]+(\([^)]+\))?:/);
} finally {
if (before) process.env.ANTHROPIC_API_KEY = before;
}
});
it("returns an empty-commit placeholder for an empty diff", async () => {
const m = await generateCommitMessage("");
expect(m.subject).toContain("empty");
expect(m.body).toBe("");
});
it("respects the plain style option in fallback mode", async () => {
const before = process.env.ANTHROPIC_API_KEY;
delete process.env.ANTHROPIC_API_KEY;
try {
const m = await generateCommitMessage(SAMPLE_DIFF, { style: "plain" });
expect(m.subject).not.toMatch(/^[a-z]+:/);
} finally {
if (before) process.env.ANTHROPIC_API_KEY = before;
}
});
it("never throws on malformed input", async () => {
let threw = false;
try {
await generateCommitMessage("not a real diff at all 🤷");
} catch {
threw = true;
}
expect(threw).toBe(false);
});
it("handles a diff far over the truncation cap without crashing", async () => {
const before = process.env.ANTHROPIC_API_KEY;
delete process.env.ANTHROPIC_API_KEY;
try {
const big = "diff --git a/x.ts b/x.ts\n+++ b/x.ts\n" + "+x\n".repeat(200_000);
const m = await generateCommitMessage(big);
expect(m.subject.length).toBeGreaterThan(0);
expect(m.subject.length).toBeLessThanOrEqual(72);
} finally {
if (before) process.env.ANTHROPIC_API_KEY = before;
}
});
});
// ──────────────────────────── conventional format ────────────────────────────
describe("Conventional Commits format", () => {
// Scope allows any non-paren chars — Conventional Commits' BNF
// is permissive and our heuristic might surface a filename like
// "README.md" as the scope.
const CONVENTIONAL_RE = /^[a-z]+(\([^)]+\))?(!?): .+/;
it("heuristic output for a code change matches conventional regex", () => {
const m = heuristicMessage(SAMPLE_DIFF);
expect(m.subject).toMatch(CONVENTIONAL_RE);
});
it("heuristic output for a new-file diff matches conventional regex", () => {
const m = heuristicMessage(NEW_FILE_DIFF);
expect(m.subject).toMatch(CONVENTIONAL_RE);
});
it("heuristic output for docs matches conventional regex", () => {
const m = heuristicMessage(DOCS_DIFF);
expect(m.subject).toMatch(CONVENTIONAL_RE);
});
it("parseModelOutput normalisation produces a conventional subject", () => {
const m = parseModelOutput("Just a sentence with no type.", "conventional");
expect(m.subject).toMatch(CONVENTIONAL_RE);
});
});
|