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 303 304 305 306 307 308 309 310 311 312 313 | /**
* Block J22 — Code review suggestion blocks. Pure extract + apply tests.
*/
import { describe, it, expect } from "bun:test";
import {
detectLineEnding,
splitLines,
extractSuggestions,
applySuggestionToContent,
applyNthSuggestion,
__internal,
} from "../lib/code-suggestions";
describe("code-suggestions — detectLineEnding", () => {
it("returns CRLF for CRLF content", () => {
expect(detectLineEnding("a\r\nb\r\n")).toBe("\r\n");
});
it("returns LF for LF content", () => {
expect(detectLineEnding("a\nb\n")).toBe("\n");
});
it("returns LF for empty", () => {
expect(detectLineEnding("")).toBe("\n");
});
it("returns CRLF if ANY CRLF present", () => {
expect(detectLineEnding("a\nb\r\nc")).toBe("\r\n");
});
});
describe("code-suggestions — extractSuggestions", () => {
it("returns [] for empty or non-string input", () => {
expect(extractSuggestions("")).toEqual([]);
expect(extractSuggestions(null as unknown as string)).toEqual([]);
expect(extractSuggestions(undefined as unknown as string)).toEqual([]);
});
it("extracts a single single-line suggestion", () => {
const body = "LGTM but\n\n```suggestion\nconst x = 1;\n```\n\nthoughts?";
const out = extractSuggestions(body);
expect(out).toHaveLength(1);
expect(out[0].content).toBe("const x = 1;");
expect(out[0].index).toBe(0);
});
it("extracts multi-line suggestions preserving internal newlines", () => {
const body = "```suggestion\nline1\nline2\nline3\n```\n";
const out = extractSuggestions(body);
expect(out).toHaveLength(1);
expect(out[0].content).toBe("line1\nline2\nline3");
});
it("captures empty suggestions (intent: delete the line)", () => {
const body = "```suggestion\n```\n";
const out = extractSuggestions(body);
expect(out).toHaveLength(1);
expect(out[0].content).toBe("");
});
it("skips fences whose info-string isn't `suggestion`", () => {
const body = "```js\nconst x = 1;\n```\n";
expect(extractSuggestions(body)).toEqual([]);
});
it("extracts multiple suggestions in order", () => {
const body =
"First:\n```suggestion\nfoo\n```\n\nSecond:\n```suggestion\nbar\nbaz\n```\n";
const out = extractSuggestions(body);
expect(out).toHaveLength(2);
expect(out[0].content).toBe("foo");
expect(out[1].content).toBe("bar\nbaz");
expect(out[0].index).toBe(0);
expect(out[1].index).toBe(1);
});
it("skips unterminated fences", () => {
const body = "```suggestion\nconst x = 1;";
expect(extractSuggestions(body)).toEqual([]);
});
it("tolerates 4+ backtick fences (for suggestions that contain ```)", () => {
const body =
"````suggestion\nconst s = `template ${with} backticks`;\n````\n";
const out = extractSuggestions(body);
expect(out).toHaveLength(1);
expect(out[0].content).toBe(
"const s = `template ${with} backticks`;"
);
});
it("ignores trailing info after `suggestion`", () => {
const body = "```suggestion block\nfoo\n```\n";
expect(extractSuggestions(body)).toHaveLength(1);
});
it("is case-sensitive on the language token (GitHub parity)", () => {
// GitHub's renderer is case-sensitive. This keeps our parser
// predictable — bump to case-insensitive when GitHub relaxes.
const body = "```Suggestion\nfoo\n```\n";
expect(extractSuggestions(body)).toEqual([]);
});
});
describe("code-suggestions — applySuggestionToContent", () => {
const content = "line one\nline two\nline three\n";
it("replaces a single line (1-indexed)", () => {
const res = applySuggestionToContent({
content,
startLine: 2,
endLine: 2,
suggestion: "TWO!",
});
expect(res.ok).toBe(true);
expect(res.content).toBe("line one\nTWO!\nline three\n");
});
it("replaces with multiple lines", () => {
const res = applySuggestionToContent({
content,
startLine: 2,
endLine: 2,
suggestion: "a\nb\nc",
});
expect(res.ok).toBe(true);
expect(res.content).toBe("line one\na\nb\nc\nline three\n");
});
it("replaces a multi-line range", () => {
const res = applySuggestionToContent({
content,
startLine: 1,
endLine: 2,
suggestion: "merged",
});
expect(res.ok).toBe(true);
expect(res.content).toBe("merged\nline three\n");
});
it("preserves CRLF line endings", () => {
const crlf = "a\r\nb\r\nc\r\n";
const res = applySuggestionToContent({
content: crlf,
startLine: 2,
endLine: 2,
suggestion: "B",
});
expect(res.ok).toBe(true);
expect(res.content).toBe("a\r\nB\r\nc\r\n");
});
it("preserves no-trailing-newline files", () => {
const res = applySuggestionToContent({
content: "a\nb\nc",
startLine: 3,
endLine: 3,
suggestion: "C",
});
expect(res.ok).toBe(true);
expect(res.content).toBe("a\nb\nC");
});
it("strips trailing newline from the suggestion", () => {
const res = applySuggestionToContent({
content: "a\nb\n",
startLine: 1,
endLine: 1,
suggestion: "A\n",
});
expect(res.ok).toBe(true);
expect(res.content).toBe("A\nb\n");
});
it("rejects bad ranges", () => {
const r1 = applySuggestionToContent({
content,
startLine: 0,
endLine: 1,
suggestion: "x",
});
expect(r1).toEqual({ ok: false, reason: "bad_range" });
const r2 = applySuggestionToContent({
content,
startLine: 2,
endLine: 1,
suggestion: "x",
});
expect(r2).toEqual({ ok: false, reason: "bad_range" });
});
it("rejects out-of-bounds line numbers", () => {
const r = applySuggestionToContent({
content,
startLine: 5,
endLine: 5,
suggestion: "x",
});
expect(r).toEqual({ ok: false, reason: "line_out_of_bounds" });
});
it("returns no_change when the suggestion matches existing content", () => {
const r = applySuggestionToContent({
content,
startLine: 1,
endLine: 1,
suggestion: "line one",
});
expect(r).toEqual({ ok: false, reason: "no_change" });
});
it("empty suggestion deletes the line", () => {
const r = applySuggestionToContent({
content,
startLine: 2,
endLine: 2,
suggestion: "",
});
expect(r.ok).toBe(true);
expect(r.content).toBe("line one\n\nline three\n");
});
it("rejects non-string content", () => {
const r = applySuggestionToContent({
content: undefined as unknown as string,
startLine: 1,
endLine: 1,
suggestion: "x",
});
expect(r.ok).toBe(false);
});
});
describe("code-suggestions — applyNthSuggestion", () => {
const body =
"First:\n```suggestion\nalpha\n```\n\nSecond:\n```suggestion\nbeta\n```\n";
it("applies the 0th block by default", () => {
const r = applyNthSuggestion(body, 0, {
content: "x\n",
startLine: 1,
endLine: 1,
});
expect(r.ok).toBe(true);
if (r.ok) expect(r.content).toBe("alpha\n");
});
it("applies the Nth block", () => {
const r = applyNthSuggestion(body, 1, {
content: "x\n",
startLine: 1,
endLine: 1,
});
expect(r.ok).toBe(true);
if (r.ok) expect(r.content).toBe("beta\n");
});
it("returns not_found for an out-of-range index", () => {
const r = applyNthSuggestion(body, 99, {
content: "x\n",
startLine: 1,
endLine: 1,
});
expect(r).toEqual({ ok: false, reason: "not_found" });
});
it("returns not_found for a negative index", () => {
const r = applyNthSuggestion(body, -1, {
content: "x\n",
startLine: 1,
endLine: 1,
});
expect(r).toEqual({ ok: false, reason: "not_found" });
});
});
describe("code-suggestions — __internal parity", () => {
it("re-exports helpers", () => {
expect(__internal.extractSuggestions).toBe(extractSuggestions);
expect(__internal.applySuggestionToContent).toBe(applySuggestionToContent);
expect(__internal.detectLineEnding).toBe(detectLineEnding);
expect(__internal.splitLines).toBe(splitLines);
expect(__internal.applyNthSuggestion).toBe(applyNthSuggestion);
});
});
describe("code-suggestions — splitLines", () => {
it("handles LF + CRLF correctly", () => {
expect(splitLines("a\nb\nc")).toEqual(["a", "b", "c"]);
expect(splitLines("a\r\nb\r\nc")).toEqual(["a", "b", "c"]);
});
});
describe("code-suggestions — routes", () => {
// Import the app lazily so the pure tests above don't boot Hono.
it("POST apply-suggestion 401s for unauthenticated users", async () => {
const { default: app } = await import("../app");
const res = await app.request(
"/alice/repo/pulls/1/comments/00000000-0000-0000-0000-000000000000/apply-suggestion",
{ method: "POST" }
);
// requireAuth redirects web requests to /login.
expect([302, 401]).toContain(res.status);
});
it("invalid PR number returns 4xx", async () => {
const { default: app } = await import("../app");
const res = await app.request(
"/alice/repo/pulls/notanum/comments/00000000-0000-0000-0000-000000000000/apply-suggestion",
{ method: "POST" }
);
// Unauth first; if somehow auth'd, would be 400. Both are acceptable.
expect([302, 400, 401, 404]).toContain(res.status);
});
});
|