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

labels.test.ts

141 lines4.0 KB

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')
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')
38​
39function sorted(labels: Iterable<string>) {
40 return [...labels].sort()
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 // "404" elsewhere in the message (a PR number in the path) is not a 404.
76 throw new Error(
77 `GitHub REST DELETE ${path} → HTTP ${deleteStatus}: boom on /issues/1404`,
78 )
79 }
80 const name = decodeURIComponent(
81 path.slice(`${issueLabelsPath}/`.length),
82 )
83 if (!issueLabels.has(name)) {
84 throw new Error(`GitHub REST DELETE ${path} → HTTP 404: Not Found`)
85 }
86 issueLabels.delete(name)
87 return null
88 }
89​
90 throw new Error(`unexpected ${method} ${path}`)
91 },
92 } satisfies GitHubClient
93​
94 return { client, issueLabels, repoLabels }
96​
97describe('REVIEW_LABELS', () => {
98 it('names the three mutually exclusive review states', () => {
99 expect(REVIEW_LABELS.map((label) => label.name)).toEqual([
100 'ai-rejected',
101 'ai-needs-work',
102 'ai-ready',
103 ])
104 })
105})
106​
107describe('setReviewState', () => {
108 it('replaces ai-rejected with ai-ready and leaves ready-to-merge', async () => {
109 const github = createFakeGitHub([
110 'ai-rejected',
111 'ready-to-merge',
112 'waiting-on: maintainer',
113 ])
114​
115 await setReviewState(github.client, REPO, ISSUE, 'ai-ready')
116​
117 expect(sorted(github.issueLabels)).toEqual([
118 'ai-ready',
119 'ready-to-merge',
120 'waiting-on: maintainer',
121 ])
122 })
123​
124 it('ignores HTTP 422 when a repo label already exists', async () => {
125 const github = createFakeGitHub([], {
126 repoLabels: REVIEW_LABELS.map((label) => label.name).slice(),
127 })
128​
129 await setReviewState(github.client, REPO, ISSUE, 'ai-rejected')
130​
131 expect(sorted(github.issueLabels)).toEqual(['ai-rejected'])
132 })
133​
134 it('throws when deleting a sibling label fails with a non-404 error', async () => {
135 const github = createFakeGitHub(['ai-rejected'], { deleteStatus: 500 })
136​
137 await expect(
138 setReviewState(github.client, REPO, ISSUE, 'ai-ready'),
139 ).rejects.toThrow(/HTTP 500/)
140 })
141})