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

comments.test.ts

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