-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathcollect-corrections.test.ts
More file actions
393 lines (335 loc) · 10.9 KB
/
collect-corrections.test.ts
File metadata and controls
393 lines (335 loc) · 10.9 KB
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
import { describe, expect, it, vi } from "vitest";
const mod = await import("../collect-corrections.js");
const {
truncateTitle,
sanitizeText,
escapeForTable,
resolveContext,
findOrCreateTrackingIssue,
appendCorrection,
maybeAssignCCA,
} = mod;
// ---------------------------------------------------------------------------
// Pure functions
// ---------------------------------------------------------------------------
describe("truncateTitle", () => {
it("returns short titles unchanged", () => {
expect(truncateTitle("Short title")).toBe("Short title");
});
it("returns titles at exactly the max length unchanged", () => {
const title = "a".repeat(50);
expect(truncateTitle(title)).toBe(title);
});
it("truncates long titles with ellipsis", () => {
const title = "a".repeat(60);
const result = truncateTitle(title);
expect(result.length).toBeLessThanOrEqual(50);
expect(result).toMatch(/\.\.\.$/);
});
it("trims trailing whitespace before ellipsis", () => {
const title = "a".repeat(44) + " " + "b".repeat(10);
const result = truncateTitle(title);
expect(result).not.toMatch(/\s\.\.\.$/);
expect(result).toMatch(/\.\.\.$/);
});
});
describe("sanitizeText", () => {
it("collapses newlines into spaces", () => {
expect(sanitizeText("line1\nline2\r\nline3\rline4")).toBe(
"line1 line2 line3 line4",
);
});
it("replaces <br> tags with spaces", () => {
expect(sanitizeText("hello<br>world<br />there")).toBe(
"hello world there",
);
});
it("collapses multiple spaces", () => {
expect(sanitizeText("too many spaces")).toBe("too many spaces");
});
it("trims leading and trailing whitespace", () => {
expect(sanitizeText(" padded ")).toBe("padded");
});
it("handles empty string", () => {
expect(sanitizeText("")).toBe("");
});
});
describe("escapeForTable", () => {
it("escapes pipe characters", () => {
expect(escapeForTable("a | b")).toBe("a \\| b");
});
it("escapes backslashes", () => {
expect(escapeForTable("path\\to\\file")).toBe("path\\\\to\\\\file");
});
it("escapes both pipes and backslashes", () => {
expect(escapeForTable("a\\|b")).toBe("a\\\\\\|b");
});
it("returns clean text unchanged", () => {
expect(escapeForTable("no special chars")).toBe("no special chars");
});
});
describe("resolveContext", () => {
it("resolves from slash command payload", () => {
const payload = {
command: { resource: { number: 42 } },
data: { Feedback: "Wrong label" },
};
const result = resolveContext(payload, "testuser");
expect(result).toEqual({
issueNumber: 42,
feedback: "Wrong label",
sender: "testuser",
});
});
it("resolves from manual dispatch payload", () => {
const payload = {
issue_number: "7",
feedback: "Should be enhancement",
};
const result = resolveContext(payload, "admin");
expect(result).toEqual({
issueNumber: 7,
feedback: "Should be enhancement",
sender: "admin",
});
});
it("prefers slash command fields over dispatch fields", () => {
const payload = {
command: { resource: { number: 10 } },
data: { Feedback: "From slash" },
issue_number: "99",
feedback: "From dispatch",
};
const result = resolveContext(payload, "user");
expect(result.issueNumber).toBe(10);
expect(result.feedback).toBe("From slash");
});
it("throws on missing issue number", () => {
expect(() => resolveContext({ feedback: "oops" }, "u")).toThrow(
"Missing issue_number",
);
});
it("throws on missing feedback", () => {
expect(() =>
resolveContext({ issue_number: "1" }, "u"),
).toThrow("Missing feedback");
});
it("throws on non-numeric issue number", () => {
expect(() =>
resolveContext({ issue_number: "abc", feedback: "test" }, "u"),
).toThrow("Invalid issue_number: abc");
});
it("throws on negative issue number", () => {
expect(() =>
resolveContext({ issue_number: "-1", feedback: "test" }, "u"),
).toThrow("Invalid issue_number: -1");
});
it("throws on decimal issue number", () => {
expect(() =>
resolveContext({ issue_number: "1.5", feedback: "test" }, "u"),
).toThrow("Invalid issue_number: 1.5");
});
});
// ---------------------------------------------------------------------------
// Octokit-dependent functions
// ---------------------------------------------------------------------------
function mockGitHub(overrides: Record<string, any> = {}) {
return {
rest: {
issues: {
listForRepo: vi.fn().mockResolvedValue({ data: [] }),
create: vi.fn().mockResolvedValue({
data: { number: 100, body: "" },
}),
get: vi.fn().mockResolvedValue({
data: { title: "Test issue title", number: 1 },
}),
update: vi.fn().mockResolvedValue({}),
addAssignees: vi.fn().mockResolvedValue({}),
...overrides,
},
},
} as any;
}
const OWNER = "test-owner";
const REPO = "test-repo";
describe("findOrCreateTrackingIssue", () => {
it("returns existing unassigned tracking issue", async () => {
const existing = { number: 5, assignees: [], body: "..." };
const github = mockGitHub({
listForRepo: vi.fn().mockResolvedValue({ data: [existing] }),
});
const result = await findOrCreateTrackingIssue(github, OWNER, REPO);
expect(result).toBe(existing);
expect(github.rest.issues.create).not.toHaveBeenCalled();
});
it("skips issues with assignees and creates a new one", async () => {
const assigned = {
number: 5,
assignees: [{ login: "copilot" }],
body: "...",
};
const github = mockGitHub({
listForRepo: vi.fn().mockResolvedValue({ data: [assigned] }),
});
const result = await findOrCreateTrackingIssue(github, OWNER, REPO);
expect(result.number).toBe(100); // from create mock
expect(github.rest.issues.create).toHaveBeenCalledWith(
expect.objectContaining({
owner: OWNER,
repo: REPO,
title: "Triage Agent Corrections",
}),
);
});
it("creates a new issue when none exist", async () => {
const github = mockGitHub();
const result = await findOrCreateTrackingIssue(github, OWNER, REPO);
expect(result.number).toBe(100);
expect(github.rest.issues.create).toHaveBeenCalled();
});
});
describe("appendCorrection", () => {
const trackingBody = [
"# Triage Agent Corrections",
"",
"| Issue | Feedback | Submitted by | Date |",
"|-------|----------|--------------|------|",
"",
].join("\n");
it("appends a row and returns correction count of 1", async () => {
const github = mockGitHub();
const trackingIssue = { number: 10, body: trackingBody } as any;
const correction = {
issueNumber: 3,
feedback: "Wrong label",
sender: "alice",
};
const count = await appendCorrection(
github,
OWNER,
REPO,
trackingIssue,
correction,
);
expect(count).toBe(1);
expect(github.rest.issues.update).toHaveBeenCalledWith(
expect.objectContaining({
issue_number: 10,
body: expect.stringContaining("[#3]"),
}),
);
});
it("counts existing rows correctly", async () => {
const bodyWithRows =
trackingBody.trimEnd() +
"\n| <a href=\"url\">[#1] Title</a> | feedback | @bob | 2026-01-01 |\n";
const github = mockGitHub();
const trackingIssue = { number: 10, body: bodyWithRows } as any;
const correction = {
issueNumber: 2,
feedback: "Also wrong",
sender: "carol",
};
const count = await appendCorrection(
github,
OWNER,
REPO,
trackingIssue,
correction,
);
expect(count).toBe(2);
});
it("handles empty tracking issue body", async () => {
const github = mockGitHub();
const trackingIssue = { number: 10, body: "" } as any;
const correction = {
issueNumber: 1,
feedback: "test",
sender: "user",
};
const count = await appendCorrection(
github,
OWNER,
REPO,
trackingIssue,
correction,
);
// No table header found → 0 existing rows + 1
expect(count).toBe(1);
});
it("sanitizes and escapes feedback in the row", async () => {
const github = mockGitHub();
const trackingIssue = { number: 10, body: trackingBody } as any;
const correction = {
issueNumber: 1,
feedback: "has | pipe\nand newline",
sender: "user",
};
await appendCorrection(github, OWNER, REPO, trackingIssue, correction);
const updatedBody =
github.rest.issues.update.mock.calls[0][0].body as string;
expect(updatedBody).toContain("has \\| pipe and newline");
// Verify the feedback cell doesn't contain raw newlines
const rows = updatedBody.split("\n").filter((l) => l.startsWith("| <a"));
expect(rows).toHaveLength(1);
expect(rows[0]).not.toContain("\n");
});
});
describe("module entrypoint - workflow_dispatch", () => {
it("processes feedback from workflow_dispatch inputs", async () => {
const github = mockGitHub({
listForRepo: vi.fn().mockResolvedValue({
data: [{ number: 50, assignees: [], body: trackingBodyForEntrypoint }],
}),
});
const context = {
repo: { owner: OWNER, repo: REPO },
payload: {
// workflow_dispatch has no client_payload; inputs carry the data
inputs: { issue_number: "7", feedback: "Should be enhancement" },
sender: { login: "dispatcher" },
},
};
await mod.default({ github, context });
// Verify the correction was appended referencing the right issue
expect(github.rest.issues.update).toHaveBeenCalledWith(
expect.objectContaining({
issue_number: 50,
body: expect.stringContaining("[#7]"),
}),
);
});
});
const trackingBodyForEntrypoint = [
"# Triage Agent Corrections",
"",
"| Issue | Feedback | Submitted by | Date |",
"|-------|----------|--------------|------|",
"",
].join("\n");
describe("maybeAssignCCA", () => {
it("assigns CCA when threshold is reached", async () => {
const github = mockGitHub();
const trackingIssue = { number: 10 } as any;
await maybeAssignCCA(github, OWNER, REPO, trackingIssue, 10);
expect(github.rest.issues.addAssignees).toHaveBeenCalledWith({
owner: OWNER,
repo: REPO,
issue_number: 10,
assignees: ["copilot"],
});
});
it("assigns CCA when threshold is exceeded", async () => {
const github = mockGitHub();
const trackingIssue = { number: 10 } as any;
await maybeAssignCCA(github, OWNER, REPO, trackingIssue, 15);
expect(github.rest.issues.addAssignees).toHaveBeenCalled();
});
it("does not assign CCA below threshold", async () => {
const github = mockGitHub();
const trackingIssue = { number: 10 } as any;
await maybeAssignCCA(github, OWNER, REPO, trackingIssue, 9);
expect(github.rest.issues.addAssignees).not.toHaveBeenCalled();
});
});