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

secure.test.ts

135 lines4.1 KB

1import { describe, expect, it } from 'vitest'
2import type { GitHubClient } from '../../scripts/maintainer/github.ts'
3import {
4 SECURE_LABEL,
5 approveWaitingWorkflows,
6 setSecureLabel,
7} from './secure.ts'
8​
9const REPO = 'TanStack/ai'
10const ISSUE = 42
11const SHA = 'abc123def456'
12​
13function readLabelName(body: unknown) {
14 if (
15 typeof body === 'object' &&
16 body !== null &&
17 'name' in body &&
18 typeof body.name === 'string'
19 ) {
20 return body.name
21 }
22 throw new Error('label body is missing name')
24​
25function readIssueLabelNames(body: unknown) {
26 if (
27 typeof body === 'object' &&
28 body !== null &&
29 'labels' in body &&
30 Array.isArray(body.labels)
31 ) {
32 return body.labels.filter(
33 (name): name is string => typeof name === 'string',
34 )
35 }
36 throw new Error('issue label body is missing labels')
38​
39function createFakeGitHub(options: { waitingIds?: Array<number> } = {}) {
40 const issueLabels = new Set<string>()
41 const repoLabels = new Set<string>()
42 const approved: Array<number> = []
43 const repoLabelsPath = `/repos/${REPO}/labels`
44 const issueLabelsPath = `/repos/${REPO}/issues/${ISSUE}/labels`
45​
46 const client = {
47 graphql() {
48 throw new Error('graphql is unused')
49 },
50 async rest(method, path, body) {
51 if (method === 'GET' && path.startsWith(`/repos/${REPO}/actions/runs?`)) {
52 const params = new URL(`https://api.github.com${path}`).searchParams
53 if (params.get('head_sha') !== SHA) {
54 return { workflow_runs: [] }
55 }
56 const status = params.get('status')
57 const ids = status === 'waiting' ? (options.waitingIds ?? []) : []
58 return {
59 workflow_runs: ids.map((id) => ({ id, head_sha: SHA, status })),
60 }
61 }
62​
63 if (method === 'POST' && path === repoLabelsPath) {
64 const name = readLabelName(body)
65 if (repoLabels.has(name)) {
66 throw new Error(`GitHub REST POST ${path} → HTTP 422: already exists`)
67 }
68 repoLabels.add(name)
69 return null
70 }
71​
72 if (method === 'POST' && path === issueLabelsPath) {
73 for (const name of readIssueLabelNames(body)) {
74 issueLabels.add(name)
75 }
76 return null
77 }
78​
79 if (method === 'DELETE' && path.startsWith(`${issueLabelsPath}/`)) {
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 const approve =
91 /^\/repos\/TanStack\/ai\/actions\/runs\/(\d+)\/approve$/.exec(path)
92 if (method === 'POST' && approve?.[1] !== undefined) {
93 approved.push(Number(approve[1]))
94 return null
95 }
96​
97 throw new Error(`unexpected ${method} ${path}`)
98 },
99 } satisfies GitHubClient
100​
101 return { client, issueLabels, repoLabels, approved }
103​
104describe('setSecureLabel', () => {
105 it('adds the secure label', async () => {
106 const github = createFakeGitHub()
107 await setSecureLabel(github.client, REPO, ISSUE, true)
108 expect([...github.issueLabels]).toEqual([SECURE_LABEL.name])
109 expect(github.repoLabels.has(SECURE_LABEL.name)).toBe(true)
110 })
111​
112 it('removes the secure label and ignores a missing label', async () => {
113 const github = createFakeGitHub()
114 await setSecureLabel(github.client, REPO, ISSUE, true)
115 await setSecureLabel(github.client, REPO, ISSUE, false)
116 await setSecureLabel(github.client, REPO, ISSUE, false)
117 expect([...github.issueLabels]).toEqual([])
118 })
119})
120​
121describe('approveWaitingWorkflows', () => {
122 it('approves waiting runs for this head SHA', async () => {
123 const github = createFakeGitHub({ waitingIds: [11, 22] })
124 const approved = await approveWaitingWorkflows(github.client, REPO, SHA)
125 expect(approved).toBe(2)
126 expect(github.approved).toEqual([11, 22])
127 })
128​
129 it('approves nothing when no runs wait', async () => {
130 const github = createFakeGitHub({ waitingIds: [] })
131 const approved = await approveWaitingWorkflows(github.client, REPO, SHA)
132 expect(approved).toBe(0)
133 expect(github.approved).toEqual([])
134 })
135})