Open History to see the last commit
1import { describe, expect, it } from 'vitest'
2import type { GitHubClient } from '../../scripts/maintainer/github.ts'
3import { REVIEW_LABELS, setReviewState } from './labels.ts'
4
5const REPO = 'TanStack/ai'
6const ISSUE = 42
7
8function readLabelName(body: unknown) {
9 if (
10 typeof body === 'object' &&
11 body !== null &&
12 'name' in body &&
13 typeof body.name === 'string'
14 ) {
15 return body.name
16 }
17 throw new Error('label body is missing name')
18}
19
20function readIssueLabelNames(body: unknown) {
21 if (
22 typeof body === 'object' &&
23 body !== null &&
24 'labels' in body &&
25 Array.isArray(body.labels)
26 ) {
27 const names: Array<string> = []
28 for (const label of body.labels) {
29 if (typeof label !== 'string') {
30 throw new Error('labels must be strings')
31 }
32 names.push(label)
33 }
34 return names
35 }
36 throw new Error('issue label body is missing labels')
37}
38
39function sorted(labels: Iterable<string>) {
40 return [...labels].sort()
41}
42
43function createFakeGitHub(
44 initialIssueLabels: Array<string>,
45 options: { repoLabels?: Array<string>; deleteStatus?: number } = {},
46) {
47 const issueLabels = new Set(initialIssueLabels)
48 const repoLabels = new Set(options.repoLabels ?? [])
49 const deleteStatus = options.deleteStatus
50 const repoLabelsPath = `/repos/${REPO}/labels`
51 const issueLabelsPath = `/repos/${REPO}/issues/${ISSUE}/labels`
52
53 const client = {
54 async graphql() {
55 throw new Error('graphql is unused')
56 },
57 async rest(method, path, body) {
58 if (method === 'POST' && path === repoLabelsPath) {
59 const name = readLabelName(body)
60 if (repoLabels.has(name)) {
61 throw new Error(`GitHub REST POST ${path} → HTTP 422: already exists`)
62 }
63 repoLabels.add(name)
64 return null
65 }
66
67 if (method === 'POST' && path === issueLabelsPath) {
68 const names = readIssueLabelNames(body)
69 for (const name of names) issueLabels.add(name)
70 return null
71 }
72
73 if (method === 'DELETE' && path.startsWith(`${issueLabelsPath}/`)) {
74 if (deleteStatus !== undefined) {
75 throw new Error(
76 `GitHub REST DELETE ${path} → HTTP ${deleteStatus}: boom`,
77 )
78 }
79 const name = decodeURIComponent(
80 path.slice(`${issueLabelsPath}/`.length),
81 )
82 if (!issueLabels.has(name)) {
83 throw new Error(`GitHub REST DELETE ${path} → HTTP 404: Not Found`)
84 }
85 issueLabels.delete(name)
86 return null
87 }
88
89 throw new Error(`unexpected ${method} ${path}`)
90 },
91 } satisfies GitHubClient
92
93 return { client, issueLabels, repoLabels }
94}
95
96describe('REVIEW_LABELS', () => {
97 it('names the three mutually exclusive review states', () => {
98 expect(REVIEW_LABELS.map((label) => label.name)).toEqual([
99 'ai-rejected',
100 'ai-needs-work',
101 'ai-ready',
102 ])
103 })
104})
105
106describe('setReviewState', () => {
107 it('replaces ai-rejected with ai-ready and leaves ready-to-merge', async () => {
108 const github = createFakeGitHub([
109 'ai-rejected',
110 'ready-to-merge',
111 'waiting-on: maintainer',
112 ])
113
114 await setReviewState(github.client, REPO, ISSUE, 'ai-ready')
115
116 expect(sorted(github.issueLabels)).toEqual([
117 'ai-ready',
118 'ready-to-merge',
119 'waiting-on: maintainer',
120 ])
121 })
122
123 it('ignores HTTP 422 when a repo label already exists', async () => {
124 const github = createFakeGitHub([], {
125 repoLabels: REVIEW_LABELS.map((label) => label.name).slice(),
126 })
127
128 await setReviewState(github.client, REPO, ISSUE, 'ai-rejected')
129
130 expect(sorted(github.issueLabels)).toEqual(['ai-rejected'])
131 })
132
133 it('throws when deleting a sibling label fails with a non-404 error', async () => {
134 const github = createFakeGitHub(['ai-rejected'], { deleteStatus: 500 })
135
136 await expect(
137 setReviewState(github.client, REPO, ISSUE, 'ai-ready'),
138 ).rejects.toThrow(/HTTP 500/)
139 })
140})