-
Notifications
You must be signed in to change notification settings - Fork 261
Expand file tree
/
Copy pathsyncSearchContexts.test.ts
More file actions
458 lines (396 loc) · 17.2 KB
/
syncSearchContexts.test.ts
File metadata and controls
458 lines (396 loc) · 17.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
import { beforeEach, describe, expect, test, vi } from 'vitest';
import type { PrismaClient } from '@sourcebot/db';
import { repoMetadataSchema } from '@sourcebot/shared';
vi.mock('@sourcebot/shared', async (importOriginal) => {
const actual = await importOriginal<typeof import('@sourcebot/shared')>();
return {
...actual,
createLogger: vi.fn(() => ({
info: vi.fn(),
warn: vi.fn(),
error: vi.fn(),
debug: vi.fn(),
})),
SOURCEBOT_SUPPORT_EMAIL: 'support@sourcebot.dev',
};
});
vi.mock('../entitlements.js', () => ({
hasEntitlement: vi.fn(() => Promise.resolve(true)),
getPlan: vi.fn(() => Promise.resolve('enterprise')),
}));
import { syncSearchContexts } from './syncSearchContexts.js';
// Helper to build a repo record with GitLab topics stored in metadata.
const makeGitLabRepo = (id: number, name: string, topics: string[] = []) => ({
id,
name,
metadata: {
gitConfig: {},
codeHostMetadata: {
gitlab: { topics },
},
} satisfies ReturnType<typeof repoMetadataSchema.parse>,
});
// Keep the old name as an alias for backwards compatibility within this test file.
const makeRepo = makeGitLabRepo;
// Helper to build a repo record with GitHub topics stored in metadata.
const makeGitHubRepo = (id: number, name: string, topics: string[] = []) => ({
id,
name,
metadata: {
gitConfig: {},
codeHostMetadata: {
github: { topics },
},
} satisfies ReturnType<typeof repoMetadataSchema.parse>,
});
// Helper to build a repo record with no codeHostMetadata (e.g. GitHub repo).
const makeRepoNoTopics = (id: number, name: string) => ({
id,
name,
metadata: {
gitConfig: {},
},
});
const buildDb = (overrides: Partial<{
repoFindMany: unknown[];
connectionFindMany: unknown[];
searchContextFindUnique: unknown;
searchContextFindMany: unknown[];
}> = {}): PrismaClient => ({
repo: {
findMany: vi.fn().mockResolvedValue(overrides.repoFindMany ?? []),
},
connection: {
findMany: vi.fn().mockResolvedValue(overrides.connectionFindMany ?? []),
},
searchContext: {
findUnique: vi.fn().mockResolvedValue(overrides.searchContextFindUnique ?? null),
findMany: vi.fn().mockResolvedValue(overrides.searchContextFindMany ?? []),
upsert: vi.fn().mockResolvedValue({}),
delete: vi.fn().mockResolvedValue({}),
},
} as unknown as PrismaClient);
describe('syncSearchContexts - includeTopics', () => {
test('includes repos whose topics match an includeTopics entry', async () => {
const backendRepo = makeRepo(1, 'gitlab.example.com/org/backend', ['backend']);
const frontendRepo = makeRepo(2, 'gitlab.example.com/org/frontend', ['frontend']);
const db = buildDb({ repoFindMany: [backendRepo, frontendRepo] });
await syncSearchContexts({
contexts: {
myContext: { includeTopics: ['backend'] },
},
orgId: 1,
db,
});
const upsertCall = vi.mocked(db.searchContext.upsert).mock.calls[0][0];
const connectedIds = upsertCall.create.repos.connect.map((r: { id: number }) => r.id);
expect(connectedIds).toContain(1);
expect(connectedIds).not.toContain(2);
});
test('excludes repos that have no topics when includeTopics is set', async () => {
const repoWithTopics = makeRepo(1, 'gitlab.example.com/org/api', ['backend']);
const repoNoTopics = makeRepoNoTopics(2, 'gitlab.example.com/org/misc');
const db = buildDb({ repoFindMany: [repoWithTopics, repoNoTopics] });
await syncSearchContexts({
contexts: {
myContext: { includeTopics: ['backend'] },
},
orgId: 1,
db,
});
const upsertCall = vi.mocked(db.searchContext.upsert).mock.calls[0][0];
const connectedIds = upsertCall.create.repos.connect.map((r: { id: number }) => r.id);
expect(connectedIds).toContain(1);
expect(connectedIds).not.toContain(2);
});
test('includes a repo that matches any one of multiple includeTopics', async () => {
const repo = makeRepo(1, 'gitlab.example.com/org/service', ['core']);
const db = buildDb({ repoFindMany: [repo] });
await syncSearchContexts({
contexts: {
myContext: { includeTopics: ['backend', 'core'] },
},
orgId: 1,
db,
});
const upsertCall = vi.mocked(db.searchContext.upsert).mock.calls[0][0];
const connectedIds = upsertCall.create.repos.connect.map((r: { id: number }) => r.id);
expect(connectedIds).toContain(1);
});
test('supports glob patterns in includeTopics', async () => {
const repo1 = makeRepo(1, 'gitlab.example.com/org/api', ['core-api']);
const repo2 = makeRepo(2, 'gitlab.example.com/org/worker', ['core-worker']);
const repo3 = makeRepo(3, 'gitlab.example.com/org/ui', ['frontend']);
const db = buildDb({ repoFindMany: [repo1, repo2, repo3] });
await syncSearchContexts({
contexts: {
myContext: { includeTopics: ['core-*'] },
},
orgId: 1,
db,
});
const upsertCall = vi.mocked(db.searchContext.upsert).mock.calls[0][0];
const connectedIds = upsertCall.create.repos.connect.map((r: { id: number }) => r.id);
expect(connectedIds).toContain(1);
expect(connectedIds).toContain(2);
expect(connectedIds).not.toContain(3);
});
test('includeTopics matching is case-insensitive', async () => {
const repo = makeRepo(1, 'gitlab.example.com/org/service', ['Backend']);
const db = buildDb({ repoFindMany: [repo] });
await syncSearchContexts({
contexts: {
myContext: { includeTopics: ['backend'] },
},
orgId: 1,
db,
});
const upsertCall = vi.mocked(db.searchContext.upsert).mock.calls[0][0];
const connectedIds = upsertCall.create.repos.connect.map((r: { id: number }) => r.id);
expect(connectedIds).toContain(1);
});
});
describe('syncSearchContexts - excludeTopics', () => {
test('excludes repos whose topics match an excludeTopics entry', async () => {
const backendRepo = makeRepo(1, 'gitlab.example.com/org/backend', ['backend']);
const deprecatedRepo = makeRepo(2, 'gitlab.example.com/org/old', ['deprecated']);
const db = buildDb({ repoFindMany: [backendRepo, deprecatedRepo] });
await syncSearchContexts({
contexts: {
myContext: {
include: ['gitlab.example.com/org/**'],
excludeTopics: ['deprecated'],
},
},
orgId: 1,
db,
});
const upsertCall = vi.mocked(db.searchContext.upsert).mock.calls[0][0];
const connectedIds = upsertCall.create.repos.connect.map((r: { id: number }) => r.id);
expect(connectedIds).toContain(1);
expect(connectedIds).not.toContain(2);
});
test('does not exclude repos that have no topics when excludeTopics is set', async () => {
const repoNoTopics = makeRepoNoTopics(1, 'gitlab.example.com/org/misc');
const db = buildDb({ repoFindMany: [repoNoTopics] });
await syncSearchContexts({
contexts: {
myContext: {
include: ['gitlab.example.com/org/**'],
excludeTopics: ['deprecated'],
},
},
orgId: 1,
db,
});
const upsertCall = vi.mocked(db.searchContext.upsert).mock.calls[0][0];
const connectedIds = upsertCall.create.repos.connect.map((r: { id: number }) => r.id);
expect(connectedIds).toContain(1);
});
test('supports glob patterns in excludeTopics', async () => {
const repo1 = makeRepo(1, 'gitlab.example.com/org/api', ['archived-2023']);
const repo2 = makeRepo(2, 'gitlab.example.com/org/worker', ['backend']);
const db = buildDb({ repoFindMany: [repo1, repo2] });
await syncSearchContexts({
contexts: {
myContext: {
include: ['gitlab.example.com/org/**'],
excludeTopics: ['archived-*'],
},
},
orgId: 1,
db,
});
const upsertCall = vi.mocked(db.searchContext.upsert).mock.calls[0][0];
const connectedIds = upsertCall.create.repos.connect.map((r: { id: number }) => r.id);
expect(connectedIds).not.toContain(1);
expect(connectedIds).toContain(2);
});
test('excludeTopics matching is case-insensitive', async () => {
const repo = makeRepo(1, 'gitlab.example.com/org/old', ['Deprecated']);
const db = buildDb({ repoFindMany: [repo] });
await syncSearchContexts({
contexts: {
myContext: {
include: ['gitlab.example.com/org/**'],
excludeTopics: ['deprecated'],
},
},
orgId: 1,
db,
});
const upsertCall = vi.mocked(db.searchContext.upsert).mock.calls[0][0];
const connectedIds = upsertCall.create.repos.connect.map((r: { id: number }) => r.id);
expect(connectedIds).not.toContain(1);
});
});
describe('syncSearchContexts - includeTopics + excludeTopics combined', () => {
test('excludeTopics removes repos that were added by includeTopics', async () => {
const activeBackend = makeRepo(1, 'gitlab.example.com/org/api', ['backend']);
const deprecatedBackend = makeRepo(2, 'gitlab.example.com/org/old-api', ['backend', 'deprecated']);
const db = buildDb({ repoFindMany: [activeBackend, deprecatedBackend] });
await syncSearchContexts({
contexts: {
myContext: {
includeTopics: ['backend'],
excludeTopics: ['deprecated'],
},
},
orgId: 1,
db,
});
const upsertCall = vi.mocked(db.searchContext.upsert).mock.calls[0][0];
const connectedIds = upsertCall.create.repos.connect.map((r: { id: number }) => r.id);
expect(connectedIds).toContain(1);
expect(connectedIds).not.toContain(2);
});
});
describe('syncSearchContexts - includeTopics combined with include globs', () => {
test('includeTopics is additive with include globs (union)', async () => {
const globRepo = makeRepo(1, 'gitlab.example.com/org/explicitly-included', []);
const topicRepo = makeRepo(2, 'gitlab.example.com/other/topic-matched', ['backend']);
const db = buildDb({ repoFindMany: [globRepo, topicRepo] });
await syncSearchContexts({
contexts: {
myContext: {
include: ['gitlab.example.com/org/**'],
includeTopics: ['backend'],
},
},
orgId: 1,
db,
});
const upsertCall = vi.mocked(db.searchContext.upsert).mock.calls[0][0];
const connectedIds = upsertCall.create.repos.connect.map((r: { id: number }) => r.id);
expect(connectedIds).toContain(1);
expect(connectedIds).toContain(2);
});
test('does not duplicate repos matched by both include globs and includeTopics', async () => {
const repo = makeRepo(1, 'gitlab.example.com/org/api', ['backend']);
const db = buildDb({ repoFindMany: [repo] });
await syncSearchContexts({
contexts: {
myContext: {
include: ['gitlab.example.com/org/**'],
includeTopics: ['backend'],
},
},
orgId: 1,
db,
});
const upsertCall = vi.mocked(db.searchContext.upsert).mock.calls[0][0];
const connectedIds = upsertCall.create.repos.connect.map((r: { id: number }) => r.id);
expect(connectedIds).toHaveLength(1);
expect(connectedIds).toContain(1);
});
});
describe('syncSearchContexts - GitHub includeTopics', () => {
test('includes GitHub repos whose topics match an includeTopics entry', async () => {
const backendRepo = makeGitHubRepo(1, 'github.com/org/backend', ['backend']);
const frontendRepo = makeGitHubRepo(2, 'github.com/org/frontend', ['frontend']);
const db = buildDb({ repoFindMany: [backendRepo, frontendRepo] });
await syncSearchContexts({
contexts: {
myContext: { includeTopics: ['backend'] },
},
orgId: 1,
db,
});
const upsertCall = vi.mocked(db.searchContext.upsert).mock.calls[0][0];
const connectedIds = upsertCall.create.repos.connect.map((r: { id: number }) => r.id);
expect(connectedIds).toContain(1);
expect(connectedIds).not.toContain(2);
});
test('GitHub includeTopics supports glob patterns', async () => {
const repo1 = makeGitHubRepo(1, 'github.com/org/api', ['core-api']);
const repo2 = makeGitHubRepo(2, 'github.com/org/ui', ['frontend']);
const db = buildDb({ repoFindMany: [repo1, repo2] });
await syncSearchContexts({
contexts: {
myContext: { includeTopics: ['core-*'] },
},
orgId: 1,
db,
});
const upsertCall = vi.mocked(db.searchContext.upsert).mock.calls[0][0];
const connectedIds = upsertCall.create.repos.connect.map((r: { id: number }) => r.id);
expect(connectedIds).toContain(1);
expect(connectedIds).not.toContain(2);
});
test('GitHub includeTopics matching is case-insensitive', async () => {
const repo = makeGitHubRepo(1, 'github.com/org/service', ['Backend']);
const db = buildDb({ repoFindMany: [repo] });
await syncSearchContexts({
contexts: {
myContext: { includeTopics: ['backend'] },
},
orgId: 1,
db,
});
const upsertCall = vi.mocked(db.searchContext.upsert).mock.calls[0][0];
const connectedIds = upsertCall.create.repos.connect.map((r: { id: number }) => r.id);
expect(connectedIds).toContain(1);
});
});
describe('syncSearchContexts - GitHub excludeTopics', () => {
test('excludes GitHub repos whose topics match an excludeTopics entry', async () => {
const activeRepo = makeGitHubRepo(1, 'github.com/org/api', ['backend']);
const deprecatedRepo = makeGitHubRepo(2, 'github.com/org/old', ['deprecated']);
const db = buildDb({ repoFindMany: [activeRepo, deprecatedRepo] });
await syncSearchContexts({
contexts: {
myContext: {
include: ['github.com/org/**'],
excludeTopics: ['deprecated'],
},
},
orgId: 1,
db,
});
const upsertCall = vi.mocked(db.searchContext.upsert).mock.calls[0][0];
const connectedIds = upsertCall.create.repos.connect.map((r: { id: number }) => r.id);
expect(connectedIds).toContain(1);
expect(connectedIds).not.toContain(2);
});
});
describe('syncSearchContexts - mixed GitHub and GitLab repos', () => {
test('includeTopics matches repos from both GitHub and GitLab', async () => {
const gitlabRepo = makeGitLabRepo(1, 'gitlab.example.com/org/api', ['backend']);
const githubRepo = makeGitHubRepo(2, 'github.com/org/service', ['backend']);
const untaggedRepo = makeGitHubRepo(3, 'github.com/org/ui', ['frontend']);
const db = buildDb({ repoFindMany: [gitlabRepo, githubRepo, untaggedRepo] });
await syncSearchContexts({
contexts: {
myContext: { includeTopics: ['backend'] },
},
orgId: 1,
db,
});
const upsertCall = vi.mocked(db.searchContext.upsert).mock.calls[0][0];
const connectedIds = upsertCall.create.repos.connect.map((r: { id: number }) => r.id);
expect(connectedIds).toContain(1);
expect(connectedIds).toContain(2);
expect(connectedIds).not.toContain(3);
});
test('excludeTopics applies to repos from both GitHub and GitLab', async () => {
const gitlabDeprecated = makeGitLabRepo(1, 'gitlab.example.com/org/old', ['deprecated']);
const githubDeprecated = makeGitHubRepo(2, 'github.com/org/old', ['deprecated']);
const activeRepo = makeGitHubRepo(3, 'github.com/org/active', ['backend']);
const db = buildDb({ repoFindMany: [gitlabDeprecated, githubDeprecated, activeRepo] });
await syncSearchContexts({
contexts: {
myContext: {
include: ['gitlab.example.com/**', 'github.com/**'],
excludeTopics: ['deprecated'],
},
},
orgId: 1,
db,
});
const upsertCall = vi.mocked(db.searchContext.upsert).mock.calls[0][0];
const connectedIds = upsertCall.create.repos.connect.map((r: { id: number }) => r.id);
expect(connectedIds).not.toContain(1);
expect(connectedIds).not.toContain(2);
expect(connectedIds).toContain(3);
});
});