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

pr.test.ts

154 lines4.4 KB

1import { describe, expect, it } from 'vitest'
2import type { GitHubClient } from '../../scripts/maintainer/github.ts'
3import { fetchPullRequest, fetchPullRequestDiff } from './pr.ts'
4​
5const REPO = 'TanStack/ai'
6const NUMBER = 42
7const SHA = 'abc123def456'
8​
9function samplePull() {
10 return {
11 number: NUMBER,
12 title: 'Fix chat crash',
13 body: 'Handle empty messages.',
14 html_url: 'https://github.com/TanStack/ai/pull/42',
15 draft: false,
16 user: { login: 'alice' },
17 head: {
18 sha: SHA,
19 ref: 'fix-chat',
20 repo: {
21 full_name: 'alice/ai',
22 owner: { login: 'alice' },
23 },
24 user: { login: 'alice' },
25 },
26 maintainer_can_modify: false,
27 labels: [{ name: 'bug' }],
28 }
30​
31function sampleFiles() {
32 return [
33 {
34 filename: 'src/chat.ts',
35 patch: '@@ -1,2 +1,3 @@\n line',
36 },
37 {
38 filename: 'src/index.ts',
39 patch: '@@ -4,1 +4,2 @@\n other',
40 },
41 ]
43​
44function createFakeGitHub(
45 options: {
46 pull?: unknown
47 files?: unknown
48 } = {},
49) {
50 const pull = options.pull === undefined ? samplePull() : options.pull
51 const files = options.files === undefined ? sampleFiles() : options.files
52 const pullPath = `/repos/${REPO}/pulls/${NUMBER}`
53 const filesPath = `/repos/${REPO}/pulls/${NUMBER}/files`
54​
55 const client = {
56 async graphql() {
57 throw new Error('graphql is unused')
58 },
59 async rest(method, path) {
60 if (method === 'GET' && path === pullPath) return pull
61 if (method === 'GET' && path.startsWith(`${filesPath}?`)) {
62 if (!Array.isArray(files)) return files
63 const params = new URL(`https://api.github.com${path}`).searchParams
64 const page = Number(params.get('page') ?? '1')
65 const perPage = Number(params.get('per_page') ?? '100')
66 const start = (page - 1) * perPage
67 return files.slice(start, start + perPage)
68 }
69 throw new Error(`unexpected ${method} ${path}`)
70 },
71 } satisfies GitHubClient
72​
73 return client
75​
76function loadPull(options?: { pull?: unknown; files?: unknown }) {
77 return fetchPullRequest(createFakeGitHub(options), REPO, NUMBER)
79​
80describe('fetchPullRequest', () => {
81 it('returns author, sha, maintainerCanModify false, and two file paths', async () => {
82 expect(await loadPull()).toEqual({
83 number: 42,
84 title: 'Fix chat crash',
85 body: 'Handle empty messages.',
86 htmlUrl: 'https://github.com/TanStack/ai/pull/42',
87 isDraft: false,
88 authorLogin: 'alice',
89 headSha: SHA,
90 headRef: 'fix-chat',
91 headRepo: 'alice/ai',
92 maintainerCanModify: false,
93 labels: ['bug'],
94 files: [
95 { path: 'src/chat.ts', patch: '@@ -1,2 +1,3 @@\n line' },
96 { path: 'src/index.ts', patch: '@@ -4,1 +4,2 @@\n other' },
97 ],
98 })
99 })
100​
101 it('keeps a null author and a null body', async () => {
102 const result = await loadPull({
103 pull: { ...samplePull(), user: null, body: null },
104 })
105 expect(result.authorLogin).toBe(null)
106 expect(result.body).toBe(null)
107 })
108​
109 it('stores a null patch when GitHub omits it', async () => {
110 const result = await loadPull({
111 files: [{ filename: 'src/chat.ts' }],
112 })
113 expect(result.files).toEqual([{ path: 'src/chat.ts', patch: null }])
114 })
115​
116 it('throws when the pull payload is missing title', async () => {
117 await expect(
118 loadPull({ pull: { ...samplePull(), title: undefined } }),
119 ).rejects.toThrow(/missing title/)
120 })
121​
122 it('throws when files is not an array', async () => {
123 await expect(loadPull({ files: null })).rejects.toThrow(
124 /did not return an array/,
125 )
126 })
127​
128 it('aggregates every page when the PR has more than 100 files', async () => {
129 const files = []
130 for (let i = 1; i <= 101; i++) {
131 files.push({ filename: `src/f${i}.ts`, patch: `@@ +${i} @@` })
132 }
133 const result = await loadPull({ files })
134 expect(result.files).toHaveLength(101)
135 expect(result.files[0]?.path).toBe('src/f1.ts')
136 expect(result.files[100]?.path).toBe('src/f101.ts')
137 })
138})
139​
140describe('fetchPullRequestDiff', () => {
141 it('concatenates two file patches with --- a/filename headers', async () => {
142 const diff = await fetchPullRequestDiff(createFakeGitHub(), REPO, NUMBER)
143 expect(diff).toBe(
144 [
145 '--- a/src/chat.ts',
146 '@@ -1,2 +1,3 @@',
147 ' line',
148 '--- a/src/index.ts',
149 '@@ -4,1 +4,2 @@',
150 ' other',
151 ].join('\n'),
152 )
153 })
154})