Open History to see the last commit
1import { describe, expect, it } from 'vitest'
2import { globToRegExp, matchesGlob } from './glob'
3import { computeLoad, requiresCoreReview, routeIssue, routePR } from './route'
4import { config, daysAgo } from './fixtures'
5
6describe('glob matcher', () => {
7 it.each([
8 ['packages/ai/src/core/chat.ts', 'packages/ai/**', true],
9 ['packages/ai-client/src/index.ts', 'packages/ai/**', false],
10 ['packages/ai-sandbox-docker/src/x.ts', 'packages/ai-sandbox*/**', true],
11 ['packages/ai-sandbox/src/x.ts', 'packages/ai-sandbox*/**', true],
12 ['packages/ai-solid/src/x.ts', 'packages/ai-sandbox*/**', false],
13 ['.changeset/two-dogs.md', '.changeset/*.md', true],
14 ['.changeset/nested/two-dogs.md', '.changeset/*.md', false],
15 ['testing/e2e/tests/chat.spec.ts', 'testing/e2e/**', true],
16 ['docs/a/b/c.md', 'docs/**', true],
17 ['packages/ai/src/core/chat.ts', 'packages/*/src/**', true],
18 ['packages/ai/package.json', 'packages/*/src/**', false],
19 ['deep/path/file.ts', '**/file.ts', true],
20 ])('%s vs %s → %s', (path, glob, expected) => {
21 expect(matchesGlob(path, glob)).toBe(expected)
22 })
23
24 it('anchors patterns (no substring matches)', () => {
25 expect(globToRegExp('docs/**').test('notdocs/a.md')).toBe(false)
26 })
27})
28
29describe('routePR', () => {
30 const noLoad = computeLoad(config, [])
31
32 it('routes by area ownership', () => {
33 expect(
34 routePR(['packages/ai-sandbox/src/x.ts'], 'someone', config, noLoad, 1),
35 ).toBe('tom')
36 expect(routePR(['docs/guide.md'], 'someone', config, noLoad, 1)).toBe(
37 'jack',
38 )
39 })
40
41 it('never assigns the author to their own PR', () => {
42 expect(
43 routePR(['packages/ai-sandbox/src/x.ts'], 'tom', config, noLoad, 1),
44 ).not.toBe('tom')
45 })
46
47 it('falls back to least-loaded rotation when no area matches', () => {
48 const load = computeLoad(config, [['tom'], ['tom'], ['alem']])
49 expect(routePR(['README.md'], 'someone', config, load, 7)).toBe('jack')
50 })
51
52 it('rotates deterministically among equally-loaded candidates', () => {
53 const a = routePR(['README.md'], 'someone', config, noLoad, 1)
54 const b = routePR(['README.md'], 'someone', config, noLoad, 2)
55 expect(a).not.toBe(b)
56 })
57
58 it('assigns PRs and issues even when everyone exceeds the old caps', () => {
59 const full = computeLoad(
60 config,
61 Array.from({ length: 20 }, () => ['tom', 'alem', 'jack']),
62 )
63 expect(routePR(['docs/x.md'], 'someone', config, full, 1)).toBe('jack')
64 expect(routeIssue('ai-client', 'someone', config, full, 1)).toBe('alem')
65 })
66})
67
68describe('routeIssue', () => {
69 const noLoad = computeLoad(config, [])
70
71 it('routes by package name mentioned in text', () => {
72 expect(
73 routeIssue(
74 'bug in @tanstack/ai-sandbox process spawn',
75 'someone',
76 config,
77 noLoad,
78 1,
79 ),
80 ).toBe('tom')
81 })
82
83 it('matches bare package tokens with word boundaries', () => {
84 expect(
85 routeIssue('ai-client reconnect loop', 'someone', config, noLoad, 1),
86 ).toBe('alem')
87 })
88
89 it('falls back to rotation when nothing matches', () => {
90 expect(
91 routeIssue('something vague', 'someone', config, noLoad, 3),
92 ).not.toBeNull()
93 })
94})
95
96describe('core ownership and balanced areas', () => {
97 const codeowners =
98 '.github/ @TanStack/tanstack-core\nscripts/ @TanStack/tanstack-core\nscripts/*.models.json\n/nx.json @TanStack/tanstack-core'
99 const roster = {
100 ...config,
101 maintainers: config.maintainers.map((m) => ({
102 ...m,
103 github: m.github === 'alem' ? 'AlemTuzlak' : m.github,
104 })),
105 }
106
107 it.each([
108 ['.github/workflows/test.yml', true],
109 ['scripts/maintainer/route.ts', true],
110 ['scripts/openai.models.json', false],
111 ['scripts/nested/openai.models.json', true],
112 ['nx.json', true],
113 ['nested/nx.json', false],
114 ['packages/ai/src/chat.ts', false],
115 ])('resolves CODEOWNERS for %s', (path, expected) => {
116 expect(requiresCoreReview([path], codeowners)).toBe(expected)
117 })
118
119 it('assigns core work to Alem above his cap; author exception uses load', () => {
120 const load = new Map([
121 ['AlemTuzlak', 100],
122 ['tom', 3],
123 ['jack', 1],
124 ])
125 expect(
126 routePR(['scripts/a.ts'], 'someone', roster, load, 1, codeowners),
127 ).toBe('AlemTuzlak')
128 expect(
129 routePR(['scripts/a.ts'], 'ALEMTUZLAK', roster, load, 1, codeowners),
130 ).toBe('jack')
131 })
132
133 it('balances load before area preference and never self-assigns', () => {
134 const load = new Map([
135 ['tom', 100],
136 ['alem', 1],
137 ['jack', 0],
138 ])
139 expect(
140 routePR(['packages/ai-sandbox/x.ts'], 'someone', config, load, 1),
141 ).toBe('jack')
142 expect(routeIssue('ai-sandbox', 'someone', config, load, 1)).toBe('jack')
143 const solo = { ...config, maintainers: [config.maintainers[0]!] }
144 expect(routePR([], 'TOM', solo, load, 1)).toBeNull()
145 expect(routeIssue('', 'TOM', solo, load, 1)).toBeNull()
146 })
147
148 it('uses merged PR authors and reviewers for file/package experience without configured areas', () => {
149 const noAreas = {
150 ...config,
151 maintainers: config.maintainers.map((m) => ({ ...m, areas: [] })),
152 }
153 const history = [
154 {
155 type: 'pr' as const,
156 number: 10,
157 title: 'merged',
158 url: '',
159 author: 'TOM',
160 authorIsBot: false,
161 createdAt: daysAgo(5),
162 closedAt: daysAgo(2),
163 mergedAt: daysAgo(2),
164 files: ['packages/ai-client/src/old.ts', 'docs/topic.md'],
165 timeline: [],
166 },
167 ]
168 expect(
169 routePR(
170 ['packages/ai-client/src/new.ts'],
171 'someone',
172 noAreas,
173 new Map(),
174 1,
175 '',
176 history,
177 ),
178 ).toBe('tom')
179 expect(
180 routePR(['docs/topic.md'], 'someone', noAreas, new Map(), 1, '', history),
181 ).toBe('tom')
182 history[0]!.author = 'outside'
183 const reviewed = [
184 {
185 ...history[0]!,
186 timeline: [
187 {
188 kind: 'review' as const,
189 actor: 'jack',
190 isBot: false,
191 at: daysAgo(3),
192 reviewState: 'APPROVED',
193 },
194 ],
195 },
196 ]
197 expect(
198 routePR(
199 ['packages/ai-client/src/new.ts'],
200 'someone',
201 noAreas,
202 new Map(),
203 1,
204 '',
205 reviewed,
206 ),
207 ).toBe('jack')
208 expect(
209 routePR(
210 ['packages/ai-client/src/new.ts'],
211 'someone',
212 noAreas,
213 new Map(),
214 1,
215 '',
216 [{ ...history[0]!, mergedAt: null }],
217 ),
218 ).toBe('alem')
219 })
220})