Open History to see the last commit
1import { describe, expect, it } from 'vitest'
2import type { GitHubClient } from '../../scripts/maintainer/github.ts'
3import { fetchPullRequest } from './pr.ts'
4
5const REPO = 'TanStack/ai'
6const NUMBER = 42
7const SHA = 'a'.repeat(40)
8const BASE_SHA = 'b'.repeat(40)
9
10function samplePull() {
11 return {
12 number: NUMBER,
13 state: 'open',
14 title: 'Fix chat crash',
15 body: 'Handle empty messages.',
16 html_url: 'https://github.com/TanStack/ai/pull/42',
17 draft: false,
18 user: { login: 'alice' },
19 base: { sha: BASE_SHA, ref: 'main', repo: { full_name: REPO } },
20 head: {
21 sha: SHA,
22 ref: 'fix-chat',
23 repo: {
24 full_name: 'alice/ai',
25 owner: { login: 'alice' },
26 },
27 user: { login: 'alice' },
28 },
29 maintainer_can_modify: false,
30 labels: [{ name: 'bug' }],
31 }
32}
33
34function sampleFiles() {
35 return [
36 {
37 filename: 'src/chat.ts',
38 status: 'modified',
39 patch: '@@ -1,2 +1,3 @@\n line',
40 },
41 {
42 filename: 'src/index.ts',
43 status: 'modified',
44 patch: '@@ -4,1 +4,2 @@\n other',
45 },
46 ]
47}
48
49function createFakeGitHub(
50 options: {
51 pull?: unknown
52 files?: unknown
53 } = {},
54) {
55 const pull = options.pull === undefined ? samplePull() : options.pull
56 const files = options.files === undefined ? sampleFiles() : options.files
57 const pullPath = `/repos/${REPO}/pulls/${NUMBER}`
58 const filesPath = `/repos/${REPO}/pulls/${NUMBER}/files`
59
60 const client = {
61 async graphql() {
62 throw new Error('graphql is unused')
63 },
64 async rest(method, path) {
65 if (method === 'GET' && path === pullPath) return pull
66 if (method === 'GET' && path.startsWith(`${filesPath}?`)) {
67 if (!Array.isArray(files)) return files
68 const params = new URL(`https://api.github.com${path}`).searchParams
69 const page = Number(params.get('page') ?? '1')
70 const perPage = Number(params.get('per_page') ?? '100')
71 const start = (page - 1) * perPage
72 return files.slice(start, start + perPage)
73 }
74 throw new Error(`unexpected ${method} ${path}`)
75 },
76 } satisfies GitHubClient
77
78 return client
79}
80
81function loadPull(options?: { pull?: unknown; files?: unknown }) {
82 return fetchPullRequest(createFakeGitHub(options), REPO, NUMBER)
83}
84
85describe('fetchPullRequest', () => {
86 it('returns author, sha, maintainerCanModify false, and two file paths', async () => {
87 expect(await loadPull()).toEqual({
88 number: 42,
89 state: 'open',
90 baseRepo: REPO,
91 title: 'Fix chat crash',
92 body: 'Handle empty messages.',
93 htmlUrl: 'https://github.com/TanStack/ai/pull/42',
94 isDraft: false,
95 authorLogin: 'alice',
96 baseSha: BASE_SHA,
97 baseRef: 'main',
98 headSha: SHA,
99 headRef: 'fix-chat',
100 headRepo: 'alice/ai',
101 maintainerCanModify: false,
102 labels: ['bug'],
103 })
104 })
105
106 it('keeps a null author and a null body', async () => {
107 const result = await loadPull({
108 pull: { ...samplePull(), user: null, body: null },
109 })
110 expect(result.authorLogin).toBe(null)
111 expect(result.body).toBe(null)
112 })
113
114 it('throws when the pull payload is missing title', async () => {
115 await expect(
116 loadPull({ pull: { ...samplePull(), title: undefined } }),
117 ).rejects.toThrow(/missing title/)
118 })
119
120 it('does not read the mutable GitHub file list', async () => {
121 const result = await loadPull({ files: null })
122 expect(result.headSha).toBe(SHA)
123 expect(result).not.toHaveProperty('files')
124 })
125})