Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

event.test.ts

243 lines7.6 KB

1import { describe, expect, it } from 'vitest'
2import { readFile } from 'node:fs/promises'
3import type { GitHubClient } from '../../scripts/maintainer/github.ts'
4import { parseReviewEvent, resolveReviewEvent } from './event'
5​
6function workflowFixture(
7 options: {
8 duplicate?: boolean
9 stale?: boolean
10 wrongWorkflow?: boolean
11 failedSignal?: boolean
12 wrongEvent?: boolean
13 noCandidate?: boolean
14 wrongRepo?: boolean
15 wrongBranch?: boolean
16 } = {},
17) {
18 const sha = 'a'.repeat(40)
19 const pull = {
20 number: 42,
21 state: 'open',
22 title: 'Change source',
23 body: null,
24 html_url: 'https://github.com/TanStack/ai/pull/42',
25 draft: false,
26 maintainer_can_modify: false,
27 user: { login: 'alice' },
28 labels: [],
29 base: {
30 sha: 'b'.repeat(40),
31 ref: 'main',
32 repo: { full_name: 'TanStack/ai' },
33 },
34 head: { sha, ref: 'feature', repo: { full_name: 'alice/ai' } },
35 }
36 const pages: Array<string> = []
37 const client = {
38 graphql() {
39 throw new Error('unused')
40 },
41 async rest(method, path) {
42 if (method !== 'GET') throw new Error('unexpected mutation')
43 if (path.endsWith('/actions/runs/123'))
44 return {
45 id: 123,
46 workflow_id: options.wrongWorkflow ? 8 : 7,
47 event: options.wrongEvent ? 'push' : 'pull_request',
48 status: 'completed',
49 conclusion: options.failedSignal ? 'failure' : 'success',
50 repository: {
51 full_name: options.wrongRepo ? 'other/ai' : 'TanStack/ai',
52 },
53 head_repository: { full_name: 'alice/ai', owner: { login: 'alice' } },
54 head_branch: options.wrongBranch ? 'other-branch' : 'feature',
55 head_sha: sha,
56 }
57 if (path.endsWith('/actions/workflows/ai-review-signal.yml'))
58 return { id: 7, path: '.github/workflows/ai-review-signal.yml' }
59 if (path.includes('/pulls?')) {
60 pages.push(path)
61 if (options.noCandidate) return []
62 return options.duplicate ? [pull, { ...pull, number: 43 }] : [pull]
63 }
64 if (path.endsWith('/pulls/42'))
65 return options.stale
66 ? { ...pull, head: { ...pull.head, sha: 'c'.repeat(40) } }
67 : pull
68 throw new Error(`unexpected ${path}`)
69 },
70 } satisfies GitHubClient
71 return { client, pages }
73​
74describe('resolveReviewEvent', () => {
75 // The base repo's /commits/{sha}/pulls returns [] for a fork head on the live
76 // API. The fixture throws on that route, so this pins the head-filter lookup.
77 it('resolves a fork with no event PR list through the head filter', async () => {
78 const { client, pages } = workflowFixture()
79 const result = await resolveReviewEvent({
80 client,
81 repo: 'TanStack/ai',
82 eventName: 'workflow_run',
83 event: { workflow_run: { id: 123, pull_requests: [] } },
84 })
85 expect(result.parsed).toEqual({
86 prNumber: 42,
87 mode: 'auto',
88 commentAuthor: null,
89 eventName: 'workflow_run',
90 })
91 expect(result.pr.headRepo).toBe('alice/ai')
92 expect(pages).toEqual([
93 '/repos/TanStack/ai/pulls?state=open&base=main&head=alice%3Afeature&per_page=100',
94 ])
95 })
96​
97 const untrusted = 'workflow_run does not match the trusted signal workflow'
98 const notUnique = 'workflow_run has no unique current pull request'
99 it.each([
100 { duplicate: true, message: notUnique },
101 { stale: true, message: 'workflow_run pull request changed' },
102 { wrongWorkflow: true, message: untrusted },
103 { failedSignal: true, message: untrusted },
104 { wrongEvent: true, message: untrusted },
105 { noCandidate: true, message: notUnique },
106 { wrongRepo: true, message: untrusted },
107 { wrongBranch: true, message: notUnique },
108 ])('rejects ambiguous, stale, or unrelated runs', async (options) => {
109 const { message, ...fixture } = options
110 const { client } = workflowFixture(fixture)
111 await expect(
112 resolveReviewEvent({
113 client,
114 repo: 'TanStack/ai',
115 eventName: 'workflow_run',
116 event: { workflow_run: { id: 123, pull_requests: [{ number: 99 }] } },
117 }),
118 ).rejects.toThrow(message)
119 })
120​
121 it('keeps status labels out of the signal and concurrency behind the review guard', async () => {
122 const signal = await readFile(
123 new URL('../../.github/workflows/ai-review-signal.yml', import.meta.url),
124 'utf8',
125 )
126 const review = await readFile(
127 new URL('../../.github/workflows/ai-review.yml', import.meta.url),
128 'utf8',
129 )
130 expect(signal).toContain('types: [opened, synchronize, ready_for_review]')
131 expect(signal).not.toContain('labeled')
132 expect(review).not.toMatch(/^concurrency:/m)
133 expect(review).toContain(' concurrency:')
134 expect(review).not.toMatch(/^\s*pull_request_target:/m)
135 expect(review).not.toMatch(/^\s*id-token:/m)
136 expect(review).not.toMatch(/uses:\s*actions\/cache/)
137 expect(review).toContain("github.repository_owner == 'TanStack'")
138 expect(review).toContain("github.ref == 'refs/heads/main'")
139 expect(review).toContain(' ref: main')
140 expect(review).toContain(' persist-credentials: false')
141 })
142})
143​
144describe('parseReviewEvent', () => {
145 it('requires authenticated workflow_run resolution', () => {
146 expect(() =>
147 parseReviewEvent({ eventName: 'workflow_run', event: {} }),
148 ).toThrow('requires authenticated resolution')
149 })
150 it('parses workflow_dispatch string pr_number as manual', () => {
151 expect(
152 parseReviewEvent({
153 eventName: 'workflow_dispatch',
154 event: { inputs: { pr_number: '88' } },
155 }),
156 ).toEqual({
157 prNumber: 88,
158 mode: 'manual',
159 commentAuthor: null,
160 eventName: 'workflow_dispatch',
161 })
162 })
163​
164 it('parses workflow_dispatch numeric pr_number as manual', () => {
165 expect(
166 parseReviewEvent({
167 eventName: 'workflow_dispatch',
168 event: { inputs: { pr_number: 7 } },
169 }),
170 ).toEqual({
171 prNumber: 7,
172 mode: 'manual',
173 commentAuthor: null,
174 eventName: 'workflow_dispatch',
175 })
176 })
177​
178 it('parses issue_comment on a PR as manual', () => {
179 expect(
180 parseReviewEvent({
181 eventName: 'issue_comment',
182 event: {
183 issue: {
184 number: 12,
185 pull_request: {
186 url: 'https://api.github.com/repos/TanStack/ai/pulls/12',
187 },
188 },
189 comment: { user: { login: 'alem' } },
190 },
191 }),
192 ).toEqual({
193 prNumber: 12,
194 mode: 'manual',
195 commentAuthor: 'alem',
196 eventName: 'issue_comment',
197 })
198 })
199​
200 it('returns a null commentAuthor when the comment user is missing', () => {
201 expect(
202 parseReviewEvent({
203 eventName: 'issue_comment',
204 event: {
205 issue: { number: 12, pull_request: {} },
206 comment: {},
207 },
208 }),
209 ).toEqual({
210 prNumber: 12,
211 mode: 'manual',
212 commentAuthor: null,
213 eventName: 'issue_comment',
214 })
215 })
216​
217 it.each(['push', 'pull_request'])('throws on the %s event', (eventName) => {
218 expect(() => parseReviewEvent({ eventName, event: {} })).toThrow(
219 `Unknown GitHub event: ${eventName}`,
220 )
221 })
222​
223 it('throws when workflow_dispatch has no valid pr_number', () => {
224 expect(() =>
225 parseReviewEvent({
226 eventName: 'workflow_dispatch',
227 event: { inputs: { pr_number: 'nope' } },
228 }),
229 ).toThrow('workflow_dispatch is missing a valid inputs.pr_number')
230 })
231​
232 it('throws when issue_comment is not on a pull request', () => {
233 expect(() =>
234 parseReviewEvent({
235 eventName: 'issue_comment',
236 event: {
237 issue: { number: 12 },
238 comment: { user: { login: 'alem' } },
239 },
240 }),
241 ).toThrow('issue_comment is not on a pull request')
242 })
243})