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

comments.test.ts

199 lines5.7 KB

1import { describe, expect, it } from 'vitest'
2import type { GitHubClient } from '../../scripts/maintainer/github'
3import {
4 COMMENT_MARKER,
5 buildReviewComment,
6 upsertReviewComment,
7} from './comments'
8​
9const AUTOMATED =
10 'This comment is automated by a Grok agent. It is not a maintainer review.'
11​
12const REPO = 'TanStack/ai'
13​
14const MACHINE = 'tanstack-ai-bot'
15​
16type StoredComment = {
17 id: number
18 issueNumber: number
19 body: string
20 user: { login: string }
22​
23function createFakeGitHub(initial: Array<StoredComment> = []) {
24 const comments: Array<StoredComment> = initial.map((comment) => ({
25 ...comment,
26 }))
27 let nextId =
28 comments.reduce((max, comment) => Math.max(max, comment.id), 0) + 1
29​
30 const client = {
31 graphql() {
32 throw new Error('not used')
33 },
34 async rest(method, path, body) {
35 const listMatch =
36 /^\/repos\/[^/]+\/[^/]+\/issues\/(\d+)\/comments(?:\?per_page=100)?$/.exec(
37 path,
38 )
39 const patchMatch =
40 /^\/repos\/[^/]+\/[^/]+\/issues\/comments\/(\d+)$/.exec(path)
41​
42 if (method === 'GET' && listMatch?.[1] !== undefined) {
43 const issueNumber = Number(listMatch[1])
44 return comments.filter((comment) => comment.issueNumber === issueNumber)
45 }
46​
47 if (method === 'POST' && listMatch?.[1] !== undefined) {
48 const issueNumber = Number(listMatch[1])
49 const comment = {
50 id: nextId,
51 issueNumber,
52 body: readBody(body),
53 user: { login: MACHINE },
54 }
55 nextId += 1
56 comments.push(comment)
57 return comment
58 }
59​
60 if (method === 'PATCH' && patchMatch?.[1] !== undefined) {
61 const id = Number(patchMatch[1])
62 const existing = comments.find((comment) => comment.id === id)
63 if (!existing) {
64 throw new Error(`comment ${id} not found`)
65 }
66 existing.body = readBody(body)
67 return existing
68 }
69​
70 throw new Error(`unexpected ${method} ${path}`)
71 },
72 } satisfies GitHubClient
73​
74 return { client, comments }
76​
77function readBody(body: unknown) {
78 if (
79 typeof body === 'object' &&
80 body !== null &&
81 'body' in body &&
82 typeof body.body === 'string'
83 ) {
84 return body.body
85 }
86 throw new Error('expected { body: string }')
88​
89function sampleInput() {
90 return {
91 verdict: 'ready' as const,
92 headSha: 'abc123def456',
93 findings: ['src/chat.ts:40 null crash on empty messages'],
94 pushNote: 'Did not push: fork has maintainer edits off.',
95 label: 'ai-ready' as const,
96 securityNote:
97 'clean. Added label `secure`. Approved 2 waiting workflow runs.',
98 }
100​
101describe('buildReviewComment', () => {
102 it('starts with the automated banner and includes the required fields', () => {
103 const body = buildReviewComment(sampleInput())
104 expect(body.startsWith(AUTOMATED)).toBe(true)
105 expect(body).toContain(COMMENT_MARKER)
106 expect(body).toContain('ready')
107 expect(body).toContain('abc123def456')
108 expect(body).toContain('src/chat.ts:40 null crash on empty messages')
109 expect(body).toContain('Did not push: fork has maintainer edits off.')
110 expect(body).toContain('ai-ready')
111 expect(body).toContain('**Security**')
112 expect(body).toContain('Added label `secure`')
113 })
114})
115​
116describe('upsertReviewComment', () => {
117 it('posts once on a new PR', async () => {
118 const { client, comments } = createFakeGitHub()
119 const body = buildReviewComment(sampleInput())
120​
121 await upsertReviewComment(client, REPO, 42, body, MACHINE)
122​
123 expect(comments).toEqual([
124 { id: 1, issueNumber: 42, body, user: { login: MACHINE } },
125 ])
126 expect(comments[0]?.body).toContain(COMMENT_MARKER)
127 expect(comments[0]?.body).toContain(AUTOMATED)
128 })
129​
130 it('patches the same comment id on a second call', async () => {
131 const { client, comments } = createFakeGitHub()
132 const first = buildReviewComment(sampleInput())
133 const second = buildReviewComment({
134 ...sampleInput(),
135 verdict: 'polish',
136 pushNote: 'Pushed commit 9f3c1 on the PR branch.',
137 label: 'ai-needs-work',
138 })
139​
140 await upsertReviewComment(client, REPO, 42, first, MACHINE)
141 await upsertReviewComment(client, REPO, 42, second, MACHINE)
142​
143 expect(comments).toHaveLength(1)
144 expect(comments[0]?.id).toBe(1)
145 expect(comments[0]?.body).toBe(second)
146 expect(comments[0]?.body).toContain(COMMENT_MARKER)
147 expect(comments[0]?.body).toContain(AUTOMATED)
148 expect(comments[0]?.body).toContain('Pushed commit 9f3c1 on the PR branch.')
149 })
150​
151 it('does not patch a comment that lacks the marker', async () => {
152 const { client, comments } = createFakeGitHub([
153 {
154 id: 7,
155 issueNumber: 42,
156 body: 'human review notes',
157 user: { login: 'alice' },
158 },
159 ])
160 const body = buildReviewComment(sampleInput())
161​
162 await upsertReviewComment(client, REPO, 42, body, MACHINE)
163​
164 expect(comments).toEqual([
165 {
166 id: 7,
167 issueNumber: 42,
168 body: 'human review notes',
169 user: { login: 'alice' },
170 },
171 { id: 8, issueNumber: 42, body, user: { login: MACHINE } },
172 ])
173 })
174​
175 it('does not patch an attacker comment that copies the marker', async () => {
176 const attackerBody = `pwn\n${COMMENT_MARKER}`
177 const { client, comments } = createFakeGitHub([
178 {
179 id: 3,
180 issueNumber: 42,
181 body: attackerBody,
182 user: { login: 'attacker' },
183 },
184 ])
185 const body = buildReviewComment(sampleInput())
186​
187 await upsertReviewComment(client, REPO, 42, body, MACHINE)
188​
189 expect(comments).toEqual([
190 {
191 id: 3,
192 issueNumber: 42,
193 body: attackerBody,
194 user: { login: 'attacker' },
195 },
196 { id: 4, issueNumber: 42, body, user: { login: MACHINE } },
197 ])
198 })
199})