|
| 1 | +// @octokit/rest is ESM-only; FACTORY-form mock required before the import. |
| 2 | +jest.mock('@octokit/rest', () => ({ Octokit: jest.fn().mockImplementation(() => ({})) })); |
| 3 | + |
| 4 | +import { fetchUserActivity, __setOctokitForTest } from '@/lib/github'; |
| 5 | +import { formatIntegrityAbortReason, DEFAULT_THRESHOLDS } from '@/lib/report-runner/types'; |
| 6 | + |
| 7 | +/** |
| 8 | + * GLOOK-50 follow-up. The first fix made the merged-PR search raise on any |
| 9 | + * untrustworthy result. On 2026-09-09 that aborted the whole report at 21%: |
| 10 | + * |
| 11 | + * ABORT (GLOOK-13): 21 of 100 engineers couldn't be fetched (21%). |
| 12 | + * Most failures are GitHub search timeouts (21 of 21) |
| 13 | + * |
| 14 | + * ~19 of those 21 were false. Verified against GitHub: sl-chromatic-bot, |
| 15 | + * sl-data-team-jenkins, magdalenastaller, keser, flaksie, gkim-smartling and |
| 16 | + * viakivchuk-smartling all have GENUINELY zero merged PRs, and an empty issue |
| 17 | + * search routinely reports incomplete_results=true while being correct. |
| 18 | + * |
| 19 | + * Commit search is different and stays strict: there an empty timed-out page |
| 20 | + * really did hide 43 commits. The asymmetry is the point. |
| 21 | + */ |
| 22 | + |
| 23 | +jest.useFakeTimers({ doNotFake: ['nextTick'], now: new Date('2026-09-09T12:00:00Z') }); |
| 24 | +afterEach(() => { jest.clearAllTimers(); __setOctokitForTest(null); }); |
| 25 | + |
| 26 | +/** |
| 27 | + * Drain the 2.5s inter-page and 5s retry sleeps. |
| 28 | + * |
| 29 | + * Deliberately does NOT stop at the first moment `getTimerCount()` hits zero: |
| 30 | + * between two awaited sleeps there is a tick with no timer pending, and |
| 31 | + * breaking there leaves the promise waiting forever on a clock that has |
| 32 | + * stopped advancing. |
| 33 | + */ |
| 34 | +async function drain<T>(p: Promise<T>): Promise<T> { |
| 35 | + let settled = false; |
| 36 | + const tracked = p.then( |
| 37 | + (v) => { settled = true; return v; }, |
| 38 | + (e) => { settled = true; throw e; }, |
| 39 | + ); |
| 40 | + tracked.catch(() => {}); // don't trip unhandled-rejection while we pump |
| 41 | + for (let i = 0; i < 200 && !settled; i++) { |
| 42 | + jest.advanceTimersByTime(10_000); |
| 43 | + for (let j = 0; j < 10; j++) await Promise.resolve(); |
| 44 | + } |
| 45 | + return tracked; |
| 46 | +} |
| 47 | + |
| 48 | +const SINCE = new Date('2026-08-26T00:00:00Z'); |
| 49 | +const HEALTHY_EMPTY_COMMITS = { total_count: 0, items: [], incomplete_results: false }; |
| 50 | + |
| 51 | +/** Octokit stub: healthy commit search, scripted issue search. */ |
| 52 | +function stub(issueData: any) { |
| 53 | + const calls = { commits: 0, issues: 0 }; |
| 54 | + __setOctokitForTest({ |
| 55 | + search: { |
| 56 | + commits: async () => { calls.commits++; return { data: HEALTHY_EMPTY_COMMITS }; }, |
| 57 | + issuesAndPullRequests: async () => { calls.issues++; return { data: issueData }; }, |
| 58 | + }, |
| 59 | + }); |
| 60 | + return calls; |
| 61 | +} |
| 62 | + |
| 63 | +describe('an empty merged-PR search that timed out', () => { |
| 64 | + const TIMED_OUT_EMPTY = { total_count: 0, items: [], incomplete_results: true }; |
| 65 | + |
| 66 | + it('does NOT skip the member — it keeps the zero and records the doubt', async () => { |
| 67 | + stub(TIMED_OUT_EMPTY); |
| 68 | + const activity = await drain(fetchUserActivity('Smartling', 'sl-chromatic-bot', SINCE)); |
| 69 | + expect(activity.prs).toEqual([]); |
| 70 | + expect(activity.prsUnverified).toMatch(/timed out on an empty result/); |
| 71 | + }); |
| 72 | + |
| 73 | + it('retries before giving up on it', async () => { |
| 74 | + const calls = stub(TIMED_OUT_EMPTY); |
| 75 | + await drain(fetchUserActivity('Smartling', 'keser', SINCE)); |
| 76 | + // Exactly 3 attempts on the merged-PR page: 1 + SEARCH_TIMEOUT_RETRIES. |
| 77 | + expect(calls.issues).toBe(3); |
| 78 | + }); |
| 79 | + |
| 80 | + it('takes the good answer when a retry recovers', async () => { |
| 81 | + let n = 0; |
| 82 | + __setOctokitForTest({ |
| 83 | + search: { |
| 84 | + commits: async () => ({ data: HEALTHY_EMPTY_COMMITS }), |
| 85 | + issuesAndPullRequests: async () => { |
| 86 | + n++; |
| 87 | + if (n === 1) return { data: TIMED_OUT_EMPTY }; |
| 88 | + return { |
| 89 | + data: { |
| 90 | + total_count: 1, incomplete_results: false, |
| 91 | + items: [{ |
| 92 | + number: 7, title: 'fix', repository_url: 'https://api.github.com/repos/Smartling/pinch', |
| 93 | + pull_request: { merged_at: '2026-09-01T00:00:00Z' }, |
| 94 | + }], |
| 95 | + }, |
| 96 | + }; |
| 97 | + }, |
| 98 | + }, |
| 99 | + }); |
| 100 | + const activity = await drain(fetchUserActivity('Smartling', 'devx', SINCE)); |
| 101 | + expect(activity.prs).toHaveLength(1); |
| 102 | + expect(activity.prsUnverified).toBeUndefined(); |
| 103 | + }); |
| 104 | +}); |
| 105 | + |
| 106 | +describe('a self-contradicting merged-PR page still raises', () => { |
| 107 | + it('raises when GitHub counts PRs but delivers none', async () => { |
| 108 | + // The protection worth keeping: this is the shape that would land a |
| 109 | + // developer with 42 merged PRs in the report as 0. Observed in the same |
| 110 | + // run for ksoloviov-smartling (total_count=3) and kbroadrick (2). |
| 111 | + stub({ total_count: 3, items: [], incomplete_results: true }); |
| 112 | + await expect(drain(fetchUserActivity('Smartling', 'ksoloviov-smartling', SINCE))) |
| 113 | + .rejects.toThrow(/no trustworthy result for @ksoloviov-smartling: counted 3 PRs but delivered none/); |
| 114 | + }); |
| 115 | +}); |
| 116 | + |
| 117 | +describe('a healthy empty merged-PR search', () => { |
| 118 | + it('is trusted with no retry and no doubt recorded', async () => { |
| 119 | + const calls = stub({ total_count: 0, items: [], incomplete_results: false }); |
| 120 | + const activity = await drain(fetchUserActivity('Smartling', 'flaksie', SINCE)); |
| 121 | + expect(activity.prs).toEqual([]); |
| 122 | + expect(activity.prsUnverified).toBeUndefined(); |
| 123 | + // One call only: fetchUserActivity does commits + merged PRs; |
| 124 | + // countReviewedPRs is invoked separately by report-runner. |
| 125 | + expect(calls.issues).toBe(1); |
| 126 | + }); |
| 127 | +}); |
| 128 | + |
| 129 | +/** |
| 130 | + * The abort banner classifies a skip as a search brownout by pattern-matching |
| 131 | + * the skip reason. Nothing coupled the thrown messages to that matcher, so a |
| 132 | + * reworded raise silently sent the on-call to rotate the PAT during a GitHub |
| 133 | + * outage — which is the exact lead #70 added the attribution to prevent. |
| 134 | + */ |
| 135 | +describe('thrown search messages stay matched to the brownout attribution', () => { |
| 136 | + const attributable = (reason: string) => { |
| 137 | + const snap = { |
| 138 | + expectedCount: 100, |
| 139 | + thresholds: DEFAULT_THRESHOLDS, |
| 140 | + skipped: Array.from({ length: 6 }, (_, i) => ({ |
| 141 | + login: `u${i}`, reason, classification: 'unknown' as const, |
| 142 | + })), |
| 143 | + }; |
| 144 | + return formatIntegrityAbortReason(snap); |
| 145 | + }; |
| 146 | + |
| 147 | + it('attributes the contradictory merged-PR raise to a search brownout', () => { |
| 148 | + const msg = attributable( |
| 149 | + 'GitHub merged-PR search gave no trustworthy result for @u: counted 3 PRs but delivered none (page 1)', |
| 150 | + ); |
| 151 | + expect(msg).toMatch(/GitHub search timeouts/); |
| 152 | + expect(msg).not.toMatch(/auth\/permission/); |
| 153 | + }); |
| 154 | + |
| 155 | + it('attributes the mid-pagination merged-PR raise too', () => { |
| 156 | + const msg = attributable( |
| 157 | + 'GitHub merged-PR search gave no trustworthy result for @u: under-delivered 100 of 250 (page 2)', |
| 158 | + ); |
| 159 | + expect(msg).toMatch(/GitHub search timeouts/); |
| 160 | + }); |
| 161 | + |
| 162 | + it('attributes the commit-search raises', () => { |
| 163 | + expect(attributable('GitHub commit search gave no trustworthy result for @u (…) after 3 attempts')) |
| 164 | + .toMatch(/GitHub search timeouts/); |
| 165 | + expect(attributable('GitHub commit search under-delivered for @u (…): got 0 of 21')) |
| 166 | + .toMatch(/GitHub search timeouts/); |
| 167 | + }); |
| 168 | + |
| 169 | + it('still blames auth for a genuine permission failure', () => { |
| 170 | + expect(attributable('Validation Failed: the listed users cannot be searched')) |
| 171 | + .toMatch(/auth\/permission/); |
| 172 | + }); |
| 173 | +}); |
0 commit comments