Open History to see the last commit
1import { describe, expect, it } from 'vitest'
2import { classifyDiscussion, classifyIssue, classifyPR } from './classify'
3import { ACK_MARKER, REPRO_MARKER } from './comments'
4import {
5 NOW,
6 comment,
7 commit,
8 config,
9 daysAgo,
10 hoursAgo,
11 makeDiscussion,
12 makeIssue,
13 makePR,
14 review,
15} from './fixtures'
16
17describe('classifyPR — waiting-on state machine', () => {
18 it('brand-new PR with no maintainer response waits on maintainer', () => {
19 const t = classifyPR(makePR({ createdAt: hoursAgo(3) }), config, NOW)
20 expect(t.waitingOn).toBe('maintainer')
21 expect(t.waitingReason).toBe('no-response')
22 expect(t.unansweredSince).toBe(hoursAgo(3))
23 expect(t.slaBreached).toBe(false) // 3h < 24h first-response SLA
24 })
25
26 it('breaches first-response SLA after 24h of silence', () => {
27 const t = classifyPR(makePR({ createdAt: hoursAgo(30) }), config, NOW)
28 expect(t.slaBreached).toBe(true)
29 expect(t.unansweredHours).toBeCloseTo(30, 0)
30 })
31
32 it('maintainer replied last → waiting on author, SLA clock cleared', () => {
33 const t = classifyPR(
34 makePR({
35 createdAt: daysAgo(3),
36 timeline: [
37 comment('contributor', daysAgo(2), 'please review'),
38 comment('alem', daysAgo(1), 'looking at it'),
39 ],
40 }),
41 config,
42 NOW,
43 )
44 expect(t.waitingOn).toBe('author')
45 expect(t.waitingReason).toBe('maintainer-replied')
46 expect(t.unansweredSince).toBeNull()
47 expect(t.slaBreached).toBe(false)
48 expect(t.firstResponseHours).toBeCloseTo(48, 0)
49 })
50
51 it('author replied after maintainer → back to waiting on maintainer (follow-up SLA)', () => {
52 const t = classifyPR(
53 makePR({
54 createdAt: daysAgo(6),
55 timeline: [
56 comment('alem', daysAgo(5), 'needs changes'),
57 comment('contributor', daysAgo(3), 'done, please re-check'),
58 ],
59 }),
60 config,
61 NOW,
62 )
63 expect(t.waitingOn).toBe('maintainer')
64 expect(t.waitingReason).toBe('author-replied')
65 expect(t.freshContributorReply).toBe(true)
66 expect(t.unansweredSince).toBe(daysAgo(3))
67 expect(t.slaBreached).toBe(true) // 72h > 48h follow-up SLA
68 })
69
70 it('the PR author being a maintainer does not count as a response', () => {
71 const t = classifyPR(
72 makePR({
73 author: 'tom',
74 createdAt: hoursAgo(30),
75 timeline: [comment('tom', hoursAgo(29), 'self-note')],
76 }),
77 config,
78 NOW,
79 )
80 expect(t.rosterAuthor).toBe(true)
81 expect(t.lastMaintainerResponseAt).toBeNull()
82 // roster-authored PRs are exempt from the SLA clock
83 expect(t.slaBreached).toBe(false)
84 })
85
86 it('bot comments never count as maintainer responses', () => {
87 const t = classifyPR(
88 makePR({
89 createdAt: hoursAgo(30),
90 timeline: [comment('github-actions[bot]', hoursAgo(29), 'ack', true)],
91 }),
92 config,
93 NOW,
94 )
95 expect(t.lastMaintainerResponseAt).toBeNull()
96 expect(t.slaBreached).toBe(true)
97 })
98
99 it('approved + green + conflict-free → ready to merge, waiting on maintainer', () => {
100 const t = classifyPR(
101 makePR({ reviewDecision: 'APPROVED', ciState: 'success' }),
102 config,
103 NOW,
104 )
105 expect(t.readyToMerge).toBe(true)
106 expect(t.waitingOn).toBe('maintainer')
107 expect(t.waitingReason).toBe('ready-to-merge')
108 })
109
110 it('pending CI is not ready to merge', () => {
111 const t = classifyPR(
112 makePR({ reviewDecision: 'APPROVED', ciState: 'pending' }),
113 config,
114 NOW,
115 )
116 expect(t.readyToMerge).toBe(false)
117 })
118
119 it('unknown mergeability is not ready to merge', () => {
120 const t = classifyPR(
121 makePR({
122 reviewDecision: 'APPROVED',
123 ciState: 'success',
124 mergeable: 'UNKNOWN',
125 }),
126 config,
127 NOW,
128 )
129 expect(t.readyToMerge).toBe(false)
130 })
131
132 it('no CI checks at all (unknown state) can still be ready to merge', () => {
133 const t = classifyPR(
134 makePR({ reviewDecision: 'APPROVED', ciState: 'unknown' }),
135 config,
136 NOW,
137 )
138 expect(t.readyToMerge).toBe(true)
139 })
140
141 it('a contributor pasting the ack marker does not suppress the ack', () => {
142 const t = classifyPR(
143 makePR({
144 timeline: [comment('contributor', hoursAgo(2), `sneaky ${ACK_MARKER}`)],
145 }),
146 config,
147 NOW,
148 )
149 expect(t.hasAckComment).toBe(false)
150 })
151
152 it('conflicts beat CI and last-mover: waiting on author', () => {
153 const t = classifyPR(
154 makePR({
155 mergeable: 'CONFLICTING',
156 timeline: [comment('contributor', hoursAgo(2), 'ping?')],
157 }),
158 config,
159 NOW,
160 )
161 expect(t.waitingOn).toBe('author')
162 expect(t.waitingReason).toBe('merge-conflicts')
163 // …but the unanswered-message clock still runs independently
164 expect(t.unansweredSince).not.toBeNull()
165 })
166
167 it('failing CI → waiting on author', () => {
168 const t = classifyPR(makePR({ ciState: 'failure' }), config, NOW)
169 expect(t.waitingOn).toBe('author')
170 expect(t.waitingReason).toBe('ci-failing')
171 })
172
173 it('outstanding changes-requested waits on author; author push flips it back', () => {
174 const requested = classifyPR(
175 makePR({
176 reviewDecision: 'CHANGES_REQUESTED',
177 timeline: [review('alem', daysAgo(2), 'CHANGES_REQUESTED')],
178 }),
179 config,
180 NOW,
181 )
182 expect(requested.waitingOn).toBe('author')
183 expect(requested.waitingReason).toBe('changes-requested')
184
185 const pushed = classifyPR(
186 makePR({
187 reviewDecision: 'CHANGES_REQUESTED',
188 timeline: [
189 review('alem', daysAgo(2), 'CHANGES_REQUESTED'),
190 commit('contributor', daysAgo(1)),
191 ],
192 }),
193 config,
194 NOW,
195 )
196 expect(pushed.waitingOn).toBe('maintainer')
197 })
198
199 it('drafts wait on author and never breach SLA', () => {
200 const t = classifyPR(
201 makePR({ isDraft: true, createdAt: daysAgo(10) }),
202 config,
203 NOW,
204 )
205 expect(t.waitingOn).toBe('author')
206 expect(t.waitingReason).toBe('draft')
207 expect(t.slaBreached).toBe(false)
208 })
209
210 it('goes stale after author silence beyond the threshold', () => {
211 const t = classifyPR(
212 makePR({
213 createdAt: daysAgo(30),
214 mergeable: 'CONFLICTING',
215 timeline: [
216 comment('alem', daysAgo(20), 'please rebase'),
217 comment('contributor', daysAgo(16), 'will do'),
218 ],
219 }),
220 config,
221 NOW,
222 )
223 expect(t.waitingOn).toBe('author')
224 expect(t.staleAuthor).toBe(true)
225 })
226})
227
228describe('classifyPR — hygiene and flags', () => {
229 it('flags missing changeset and missing E2E only for published-source PRs', () => {
230 const src = classifyPR(
231 makePR({ files: ['packages/ai/src/core/chat.ts'] }),
232 config,
233 NOW,
234 )
235 expect(src.missingChangeset).toBe(true)
236 expect(src.missingE2E).toBe(true)
237
238 const covered = classifyPR(
239 makePR({
240 files: [
241 'packages/ai/src/core/chat.ts',
242 '.changeset/two-dogs-run.md',
243 'testing/e2e/tests/chat.spec.ts',
244 ],
245 }),
246 config,
247 NOW,
248 )
249 expect(covered.missingChangeset).toBe(false)
250 expect(covered.missingE2E).toBe(false)
251
252 const docsOnly = classifyPR(
253 makePR({ files: ['docs/guide.md'] }),
254 config,
255 NOW,
256 )
257 expect(docsOnly.missingChangeset).toBe(false)
258 expect(docsOnly.missingE2E).toBe(false)
259 })
260
261 it('suspects spam only when all three signals align', () => {
262 const spam = classifyPR(
263 makePR({
264 authorAccountCreatedAt: daysAgo(5),
265 additions: 2,
266 deletions: 1,
267 linkedIssues: [],
268 authorAssociation: 'NONE',
269 }),
270 config,
271 NOW,
272 )
273 expect(spam.suspectedSpam).toBe(true)
274 expect(spam.spamReasons).toHaveLength(3)
275
276 const youngButSubstantial = classifyPR(
277 makePR({
278 authorAccountCreatedAt: daysAgo(5),
279 additions: 400,
280 deletions: 100,
281 linkedIssues: [],
282 authorAssociation: 'NONE',
283 }),
284 config,
285 NOW,
286 )
287 expect(youngButSubstantial.suspectedSpam).toBe(false)
288
289 const collaborator = classifyPR(
290 makePR({
291 authorAccountCreatedAt: daysAgo(5),
292 additions: 2,
293 deletions: 1,
294 linkedIssues: [],
295 authorAssociation: 'COLLABORATOR',
296 }),
297 config,
298 NOW,
299 )
300 expect(collaborator.suspectedSpam).toBe(false)
301 })
302
303 it('detects a prior ack comment via the marker', () => {
304 const t = classifyPR(
305 makePR({
306 timeline: [
307 comment(
308 'github-actions[bot]',
309 daysAgo(1),
310 `Thanks!\n${ACK_MARKER}`,
311 true,
312 ),
313 ],
314 }),
315 config,
316 NOW,
317 )
318 expect(t.hasAckComment).toBe(true)
319 })
320
321 it('flags first-time contributors', () => {
322 const t = classifyPR(
323 makePR({ authorAssociation: 'FIRST_TIME_CONTRIBUTOR' }),
324 config,
325 NOW,
326 )
327 expect(t.firstTimeContributor).toBe(true)
328 })
329})
330
331describe('classifyIssue', () => {
332 it('needs repro when the body has no link or code fence', () => {
333 const bare = classifyIssue(
334 makeIssue({ body: 'it just breaks, please fix' }),
335 config,
336 NOW,
337 new Set(),
338 )
339 expect(bare.needsRepro).toBe(true)
340
341 const withLink = classifyIssue(makeIssue(), config, NOW, new Set())
342 expect(withLink.needsRepro).toBe(false)
343
344 const withCode = classifyIssue(
345 makeIssue({ body: 'crash:\n```ts\nchat()\n```' }),
346 config,
347 NOW,
348 new Set(),
349 )
350 expect(withCode.needsRepro).toBe(false)
351 })
352
353 it('links issues to open PRs that close them', () => {
354 const t = classifyIssue(
355 makeIssue({ number: 50 }),
356 config,
357 NOW,
358 new Set([50]),
359 )
360 expect(t.hasOpenPR).toBe(true)
361 })
362
363 it('detects a prior repro-request comment via the marker', () => {
364 const t = classifyIssue(
365 makeIssue({
366 body: 'no repro here',
367 timeline: [
368 comment(
369 'github-actions[bot]',
370 daysAgo(1),
371 `Repro?\n${REPRO_MARKER}`,
372 true,
373 ),
374 ],
375 }),
376 config,
377 NOW,
378 new Set(),
379 )
380 expect(t.hasReproComment).toBe(true)
381 })
382
383 it('runs the same response SLA as PRs', () => {
384 const t = classifyIssue(
385 makeIssue({ createdAt: hoursAgo(30) }),
386 config,
387 NOW,
388 new Set(),
389 )
390 expect(t.waitingOn).toBe('maintainer')
391 expect(t.slaBreached).toBe(true)
392 })
393
394 it('a never-answered issue is not a fresh contributor reply', () => {
395 const t = classifyIssue(
396 makeIssue({
397 createdAt: hoursAgo(30),
398 timeline: [comment('contributor', hoursAgo(2), 'any update?')],
399 }),
400 config,
401 NOW,
402 new Set(),
403 )
404 expect(t.freshContributorReply).toBe(false)
405 })
406
407 it('a contributor reply after a maintainer response is fresh', () => {
408 const t = classifyIssue(
409 makeIssue({
410 createdAt: daysAgo(3),
411 timeline: [
412 comment('alem', daysAgo(2), 'can you share a repro?'),
413 comment('contributor', daysAgo(1), 'here you go'),
414 ],
415 }),
416 config,
417 NOW,
418 new Set(),
419 )
420 expect(t.freshContributorReply).toBe(true)
421 expect(t.waitingOn).toBe('maintainer')
422 })
423})
424
425describe('classifyDiscussion', () => {
426 it('needs attention when unanswered with no maintainer comment', () => {
427 const t = classifyDiscussion(makeDiscussion(), config, NOW)
428 expect(t.needsAttention).toBe(true)
429 expect(t.waitingHours).toBeCloseTo(72, 0)
430 })
431
432 it('a maintainer comment clears the attention flag even if unanswered', () => {
433 const t = classifyDiscussion(
434 makeDiscussion({
435 comments: [{ actor: 'jack', isBot: false, at: daysAgo(1) }],
436 }),
437 config,
438 NOW,
439 )
440 expect(t.needsAttention).toBe(false)
441 })
442})