-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Expand file tree
/
Copy pathgithub.test.ts
More file actions
663 lines (614 loc) · 26.3 KB
/
Copy pathgithub.test.ts
File metadata and controls
663 lines (614 loc) · 26.3 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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
/**
* @vitest-environment node
*/
import { afterEach, describe, expect, it, vi } from 'vitest'
import {
beginListingCheckpoint,
listingFingerprint,
runResumableListing,
} from '@/lib/knowledge/connectors/listing-checkpoint'
import { githubConnector } from '@/connectors/github/github'
import type { ExternalDocument } from '@/connectors/types'
import { PER_MEMBER_LISTING_CONTEXT } from '@/connectors/utils'
vi.mock('@/lib/core/rate-limiter/provider-capacity', () => ({
acquireProviderCapacity: vi.fn(async () => ({ settle: vi.fn(async () => 0) })),
}))
const source = { repository: 'owner/repo', branch: 'main' }
function treeFile(path: string, sha = path, size = 20) {
return { path, sha, size, mode: '100644', type: 'blob' }
}
function treeResponse(tree: ReturnType<typeof treeFile>[], truncated = false, sha = 'tree-sha') {
return new Response(JSON.stringify({ sha, tree, truncated }), { status: 200 })
}
describe('githubConnector member listing', () => {
afterEach(() => vi.unstubAllGlobals())
it('resolves a member source default branch once and reuses it during hydration', async () => {
const fetchMock = vi
.fn()
.mockResolvedValueOnce(new Response(JSON.stringify({ default_branch: 'master' })))
.mockResolvedValueOnce(treeResponse([treeFile('readme.md', 'sha')]))
.mockResolvedValueOnce(new Response('text'))
vi.stubGlobal('fetch', fetchMock)
const context: Record<string, unknown> = { ...PER_MEMBER_LISTING_CONTEXT }
const config = { repository: 'owner/repo' }
const listing = await githubConnector.listDocuments('member-token', config, undefined, context)
const hydrated = await githubConnector.getDocument('member-token', config, 'readme.md', context)
expect(fetchMock.mock.calls.map(([url]) => url)).toEqual([
'https://api.github.com/repos/owner/repo',
'https://api.github.com/repos/owner/repo/git/trees/master?recursive=1',
'https://api.github.com/repos/owner/repo/git/blobs/sha',
])
expect(listing.documents[0]?.metadata?.branch).toBe('master')
expect(hydrated?.metadata?.branch).toBe('master')
expect(hydrated?.contentHash).toBe(listing.documents[0]?.contentHash)
})
it.each(['master', 'develop'])(
'uses the actual %s default for an installation content pass with a blank branch',
async (defaultBranch) => {
const fetchMock = vi
.fn()
.mockResolvedValueOnce(new Response(JSON.stringify({ default_branch: defaultBranch })))
.mockResolvedValueOnce(treeResponse([treeFile('readme.md', 'sha')]))
.mockResolvedValueOnce(new Response('text'))
vi.stubGlobal('fetch', fetchMock)
const context = {}
const config = { repository: 'owner/repo', githubRepositoryId: '101', branch: ' ' }
const listing = await githubConnector.listDocuments(
'installation-token',
config,
undefined,
context
)
const hydrated = await githubConnector.getDocument(
'installation-token',
config,
'readme.md',
context
)
expect(fetchMock.mock.calls.map(([url]) => url)).toEqual([
'https://api.github.com/repos/owner/repo',
`https://api.github.com/repos/owner/repo/git/trees/${defaultBranch}?recursive=1`,
'https://api.github.com/repos/owner/repo/git/blobs/sha',
])
expect(listing.documents[0]?.metadata?.branch).toBe(defaultBranch)
expect(hydrated?.metadata?.branch).toBe(defaultBranch)
expect(hydrated?.contentHash).toBe(listing.documents[0]?.contentHash)
}
)
it('preserves the default main branch for existing general KB sources', async () => {
const fetchMock = vi.fn().mockResolvedValue(treeResponse([]))
vi.stubGlobal('fetch', fetchMock)
await githubConnector.listDocuments('pat', { repository: 'owner/repo' })
expect(fetchMock.mock.calls[0]?.[0]).toBe(
'https://api.github.com/repos/owner/repo/git/trees/main?recursive=1'
)
})
it('uses an explicitly configured member branch without a repository metadata lookup', async () => {
const fetchMock = vi.fn().mockResolvedValue(treeResponse([]))
vi.stubGlobal('fetch', fetchMock)
await githubConnector.listDocuments(
'member-token',
{ repository: 'owner/repo', branch: 'release/docs' },
undefined,
{ ...PER_MEMBER_LISTING_CONTEXT }
)
expect(fetchMock.mock.calls[0]?.[0]).toBe(
'https://api.github.com/repos/owner/repo/git/trees/release%2Fdocs?recursive=1'
)
})
it('uses an explicitly configured installation branch without a repository metadata lookup', async () => {
const fetchMock = vi.fn().mockResolvedValue(treeResponse([]))
vi.stubGlobal('fetch', fetchMock)
await githubConnector.listDocuments(
'installation-token',
{ repository: 'owner/repo', githubRepositoryId: '101', branch: 'release/docs' },
undefined,
{}
)
expect(fetchMock.mock.calls[0]?.[0]).toBe(
'https://api.github.com/repos/owner/repo/git/trees/release%2Fdocs?recursive=1'
)
})
it('validates a member source against its actual default branch', async () => {
const fetchMock = vi
.fn()
.mockResolvedValueOnce(new Response(JSON.stringify({ default_branch: 'develop' })))
.mockResolvedValueOnce(new Response('{}'))
vi.stubGlobal('fetch', fetchMock)
await expect(
githubConnector.validateConfig(
'member-token',
{ repository: 'owner/repo' },
{
...PER_MEMBER_LISTING_CONTEXT,
}
)
).resolves.toEqual({ valid: true })
expect(fetchMock.mock.calls[1]?.[0]).toBe(
'https://api.github.com/repos/owner/repo/branches/develop'
)
})
it('lists only metadata under the caller token and retains the hydration hash', async () => {
const fetchMock = vi
.fn()
.mockResolvedValueOnce(treeResponse([treeFile('docs/readme.md', 'blob-sha')]))
.mockResolvedValueOnce(new Response('text'))
vi.stubGlobal('fetch', fetchMock)
const context = {}
const result = await githubConnector.listDocuments('member-token', source, undefined, context)
expect(result.documents[0]).toMatchObject({
externalId: 'docs/readme.md',
content: '',
contentDeferred: true,
contentHash: 'git-sha:blob-sha',
})
expect(fetchMock).toHaveBeenCalledTimes(1)
expect(fetchMock.mock.calls[0]![1].headers.Authorization).toBe('Bearer member-token')
const hydrated = await githubConnector.getDocument(
'member-token',
source,
'docs/readme.md',
context
)
expect(hydrated?.contentHash).toBe(result.documents[0]?.contentHash)
expect(hydrated?.content).toBe('text')
})
it('pages the same tree without refetching a moving branch', async () => {
const files = Array.from({ length: 201 }, (_, index) => treeFile(`file-${index}.md`))
const fetchMock = vi.fn().mockResolvedValue(treeResponse(files))
vi.stubGlobal('fetch', fetchMock)
const context: Record<string, unknown> = {}
const first = await githubConnector.listDocuments('token', source, undefined, context)
const second = await githubConnector.listDocuments('token', source, first.nextCursor, context)
expect(first.documents).toHaveLength(200)
expect(first.hasMore).toBe(true)
expect(second.documents.map((document) => document.externalId)).toEqual(['file-200.md'])
expect(second.hasMore).toBe(false)
expect(fetchMock).toHaveBeenCalledTimes(1)
})
it('replays a pinned tree in a fresh worker even when the branch has moved', async () => {
const original = Array.from({ length: 201 }, (_, index) =>
treeFile(`file-${index}.md`, `blob-${index}`)
)
const fetchMock = vi.fn(async (url: string) => {
if (url.includes('/git/trees/main')) return treeResponse(original)
if (url.includes('/git/trees/tree-sha')) return treeResponse(original)
if (url.includes('/git/blobs/blob-200')) return new Response('original bytes')
throw new Error('Unpinned request escaped the original snapshot')
})
vi.stubGlobal('fetch', fetchMock)
const first = await githubConnector.listDocuments('token', source, undefined, {})
const context = {}
const resumed = await githubConnector.listDocuments('token', source, first.nextCursor, context)
expect(resumed.documents.map((doc) => doc.externalId)).toEqual(['file-200.md'])
expect(
await githubConnector.getDocument('token', source, 'file-200.md', context)
).toMatchObject({
content: 'original bytes',
contentHash: 'git-sha:blob-200',
})
expect(fetchMock.mock.calls.map(([url]) => url)).toEqual([
'https://api.github.com/repos/owner/repo/git/trees/main?recursive=1',
'https://api.github.com/repos/owner/repo/git/trees/tree-sha?recursive=1',
'https://api.github.com/repos/owner/repo/git/blobs/blob-200',
])
})
it('reopens an expired snapshot against the current member default branch without declaring false absence', async () => {
const fetchMock = vi.fn(async (url: string) => {
if (url.endsWith('/git/trees/expired-tree?recursive=1'))
return new Response(null, { status: 404 })
if (url.endsWith('/repos/owner/repo'))
return Response.json({ default_branch: 'current-default' })
if (url.endsWith('/git/trees/current-default?recursive=1'))
return treeResponse([treeFile('still-readable.md')], false, 'current-tree')
if (url.endsWith('/git/trees/deleted-default?recursive=1'))
return new Response(null, { status: 404 })
throw new Error('Unexpected provider request')
})
vi.stubGlobal('fetch', fetchMock)
const context = {
...PER_MEMBER_LISTING_CONTEXT,
githubBranch: 'deleted-default',
filteredTree: [treeFile('stale.md')],
listingCapped: true,
}
const checkpoint = {
...beginListingCheckpoint({
fingerprint: listingFingerprint({ source: 'github' }),
generationId: 'prior-generation',
startedAt: new Date(),
}),
cursor: JSON.stringify({
version: 1,
treeSha: 'expired-tree',
branch: 'deleted-default',
offset: 200,
}),
}
const processPage = vi.fn(async (_documents: ExternalDocument[]) => undefined)
const result = await runResumableListing({
connectorConfig: githubConnector,
sourceConfig: { repository: 'owner/repo' },
syncContext: context,
checkpoint,
deadlineAt: Date.now() + 60_000,
beforePage: async () => {},
getAccessToken: async () => 'fixture-token',
processPage,
saveCheckpoint: async () => {},
})
expect(result).toMatchObject({ complete: true, unsafe: false, listedCount: 1 })
expect(result.generationId).not.toBe('prior-generation')
expect(processPage.mock.calls[0]?.[0]).toEqual([
expect.objectContaining({ externalId: 'still-readable.md' }),
])
expect(fetchMock.mock.calls.map(([url]) => url)).toEqual([
'https://api.github.com/repos/owner/repo/git/trees/expired-tree?recursive=1',
'https://api.github.com/repos/owner/repo',
'https://api.github.com/repos/owner/repo/git/trees/current-default?recursive=1',
])
})
it('restarts legacy offsets rather than skipping entries in a different tree', async () => {
const fetchMock = vi.fn()
vi.stubGlobal('fetch', fetchMock)
const error = await githubConnector
.listDocuments('token', source, '200', {})
.catch((error: unknown) => error)
expect(githubConnector.isListingCursorInvalidError?.(error)).toBe(true)
expect(fetchMock).not.toHaveBeenCalled()
})
it.each([401, 403, 404])(
'classifies repository rejection %i for member access',
async (status) => {
vi.stubGlobal('fetch', vi.fn().mockResolvedValue(new Response(null, { status })))
const error = await githubConnector.listDocuments('token', source).catch((error) => error)
expect(githubConnector.isCredentialInvalidError?.(error)).toBe(status === 401)
expect(githubConnector.isListingScopeUnavailableError?.(error)).toBe(status !== 401)
}
)
it('keeps an SSO denial distinct from invalid credentials', async () => {
vi.stubGlobal(
'fetch',
vi
.fn()
.mockResolvedValue(
new Response(null, { status: 403, headers: { 'x-github-sso': 'required' } })
)
)
const error = await githubConnector.listDocuments('token', source).catch((error) => error)
expect(githubConnector.isListingScopeUnavailableError?.(error)).toBe(true)
expect(githubConnector.isCredentialInvalidError?.(error)).toBe(false)
})
it('does not revoke member access for a rate-limit 403', async () => {
vi.stubGlobal(
'fetch',
vi
.fn()
.mockResolvedValue(new Response(null, { status: 403, headers: { 'retry-after': '3600' } }))
)
const error = await githubConnector.listDocuments('token', source).catch((error) => error)
expect(githubConnector.isListingScopeUnavailableError?.(error)).toBe(false)
expect(githubConnector.isCredentialInvalidError?.(error)).toBe(false)
})
it('does not revoke access for a secondary throttle without rate-limit headers', async () => {
vi.stubGlobal(
'fetch',
vi.fn().mockResolvedValue(
new Response(JSON.stringify({ message: 'You have exceeded a secondary rate limit.' }), {
status: 403,
})
)
)
const error = await githubConnector.listDocuments('token', source).catch((error) => error)
expect(githubConnector.isListingScopeUnavailableError?.(error)).toBe(false)
expect(error).toMatchObject({ rateLimited: true, retryAfterMs: 60_000 })
})
it('prevents deletion reconciliation after GitHub truncates the tree', async () => {
vi.stubGlobal('fetch', vi.fn().mockResolvedValue(treeResponse([treeFile('one.md')], true)))
const context: Record<string, unknown> = {}
await githubConnector.listDocuments('token', source, undefined, context)
expect(context.listingCapped).toBe(true)
})
it('prevents reconciliation after a general KB file cap truncates the listing', async () => {
vi.stubGlobal(
'fetch',
vi.fn().mockResolvedValue(treeResponse([treeFile('one.md'), treeFile('two.md')]))
)
const context: Record<string, unknown> = {}
const result = await githubConnector.listDocuments(
'token',
{ ...source, maxFiles: '1' },
undefined,
context
)
expect(result.documents).toHaveLength(1)
expect(context.listingCapped).toBe(true)
})
it('allows reconciliation after intentional scope filtering', async () => {
vi.stubGlobal(
'fetch',
vi
.fn()
.mockResolvedValue(
treeResponse([treeFile('docs/one.md'), treeFile('docs/two.txt'), treeFile('src/code.ts')])
)
)
const context: Record<string, unknown> = {}
const result = await githubConnector.listDocuments(
'token',
{ ...source, pathPrefix: 'docs/', extensions: 'md' },
undefined,
context
)
expect(result.documents.map((document) => document.externalId)).toEqual(['docs/one.md'])
expect(context.listingCapped).toBeUndefined()
})
it('rejects malformed successful listings instead of treating them as an empty repository', async () => {
vi.stubGlobal('fetch', vi.fn().mockResolvedValue(new Response('{}')))
await expect(githubConnector.listDocuments('token', source)).rejects.toThrow()
})
it.each(['owner/repo?redirect=x', 'owner/../other', 'owner/repo/tree/main', 'owner/.'])(
'rejects invalid repository input %s before sending a token',
async (repository) => {
const fetchMock = vi.fn()
vi.stubGlobal('fetch', fetchMock)
await expect(githubConnector.validateConfig('token', { repository })).resolves.toMatchObject({
valid: false,
})
expect(fetchMock).not.toHaveBeenCalled()
}
)
})
describe('githubConnector.getDocument', () => {
afterEach(() => {
vi.unstubAllGlobals()
})
it('hydrates the immutable large file directly through the blob API', async () => {
const fetchMock = vi
.fn()
.mockResolvedValueOnce(treeResponse([treeFile('docs/large.md', 'blob-sha')]))
.mockResolvedValueOnce(
new Response('large text file', { status: 200, headers: { 'content-length': '15' } })
)
vi.stubGlobal('fetch', fetchMock)
const document = await githubConnector.getDocument(
'token',
{ repository: 'owner/repo', branch: 'main' },
'docs/large.md'
)
expect(fetchMock).toHaveBeenCalledTimes(2)
expect(fetchMock.mock.calls[1][1]).toMatchObject({
headers: expect.objectContaining({ Accept: 'application/vnd.github.raw+json' }),
})
expect(document).toMatchObject({
externalId: 'docs/large.md',
content: 'large text file',
contentDeferred: false,
contentHash: 'git-sha:blob-sha',
})
})
it('returns null only when a listed path is no longer present', async () => {
vi.stubGlobal(
'fetch',
vi
.fn()
.mockResolvedValueOnce(treeResponse([]))
.mockResolvedValueOnce(new Response(null, { status: 404 }))
)
await expect(
githubConnector.getDocument('token', { repository: 'owner/repo' }, 'deleted.md')
).resolves.toBeNull()
})
it('records a blob that exceeds the byte cap as a visible skipped document', async () => {
const fetchMock = vi
.fn()
.mockResolvedValueOnce(treeResponse([treeFile('oversized.md', 'blob-sha')]))
.mockResolvedValueOnce(
new Response('oversized', {
status: 200,
headers: { 'content-length': String(100 * 1024 * 1024 + 1) },
})
)
vi.stubGlobal('fetch', fetchMock)
await expect(
githubConnector.getDocument('token', { repository: 'owner/repo' }, 'oversized.md')
).resolves.toMatchObject({
externalId: 'oversized.md',
content: '',
skippedReason: 'File exceeds the 100MB size limit and was not indexed',
})
})
it('rejects a bodyless blob response instead of misreporting it as oversized', async () => {
const fetchMock = vi
.fn()
.mockResolvedValueOnce(treeResponse([treeFile('missing-body.md', 'blob-sha')]))
.mockResolvedValueOnce(new Response(null, { status: 200 }))
vi.stubGlobal('fetch', fetchMock)
await expect(
githubConnector.getDocument('token', { repository: 'owner/repo' }, 'missing-body.md')
).rejects.toThrow('GitHub git blob blob-sha returned no body')
})
it('retains null hydration for a repository that became unavailable before the tree request', async () => {
const fetchMock = vi.fn().mockResolvedValueOnce(new Response(null, { status: 404 }))
vi.stubGlobal('fetch', fetchMock)
await expect(githubConnector.getDocument('token', source, 'docs/readme.md')).resolves.toBeNull()
expect(fetchMock).toHaveBeenCalledTimes(1)
})
it('surfaces a non-rate-limit 403 as a document failure', async () => {
vi.stubGlobal(
'fetch',
vi
.fn()
.mockResolvedValueOnce(treeResponse([treeFile('private.md')]))
.mockResolvedValueOnce(new Response(null, { status: 403 }))
)
await expect(
githubConnector.getDocument('token', { repository: 'owner/repo' }, 'private.md')
).rejects.toThrow('Failed to fetch git blob private.md: 403')
})
})
describe('githubConnector symlinks', () => {
afterEach(() => vi.unstubAllGlobals())
const link = { ...treeFile('docs/link.md', 'link-sha'), mode: '120000' }
const target = treeFile('docs/target.md', 'target-sha')
it('versions symlink targets while keeping unchanged trees and regular files stable', async () => {
const stable = treeFile('docs/stable.md', 'stable-sha')
const fetchMock = vi.fn()
for (const [revision, text] of [
['one', 'one'],
['two', 'two'],
] as const) {
fetchMock
.mockResolvedValueOnce(treeResponse([link, stable, target], false, `tree-${revision}`))
.mockResolvedValueOnce(new Response('target.md'))
.mockResolvedValueOnce(new Response(text))
}
fetchMock.mockResolvedValueOnce(treeResponse([link, stable, target], false, 'tree-two'))
vi.stubGlobal('fetch', fetchMock)
const firstContext = {}
const first = await githubConnector.listDocuments('token', source, undefined, firstContext)
const firstContent = await githubConnector.getDocument('token', source, link.path, firstContext)
const secondContext = {}
const second = await githubConnector.listDocuments('token', source, undefined, secondContext)
const secondContent = await githubConnector.getDocument(
'token',
source,
link.path,
secondContext
)
const unchanged = await githubConnector.listDocuments('token', source, undefined, {})
expect(firstContent?.content).toBe('one')
expect(secondContent?.content).toBe('two')
expect(firstContent?.contentHash).toBe(first.documents[0].contentHash)
expect(secondContent?.contentHash).toBe(second.documents[0].contentHash)
expect(first.documents[0].contentHash).not.toBe(second.documents[0].contentHash)
expect(unchanged.documents.map((doc) => doc.contentHash)).toEqual(
second.documents.map((doc) => doc.contentHash)
)
expect(first.documents[1].contentHash).toBe(second.documents[1].contentHash)
expect(fetchMock).toHaveBeenCalledTimes(7)
})
it.each(['target.md', '../../outside.md', '/etc/passwd', 'https://example.com/file.md'])(
'skips a deleted, escaping, or external symlink target: %s',
async (targetPath) => {
const fetchMock = vi
.fn()
.mockResolvedValueOnce(treeResponse([link], false, 'target-deleted-tree'))
.mockResolvedValueOnce(new Response(targetPath))
vi.stubGlobal('fetch', fetchMock)
const context = {}
const listing = await githubConnector.listDocuments('token', source, undefined, context)
const hydrated = await githubConnector.getDocument('token', source, link.path, context)
expect(hydrated).toMatchObject({
content: '',
contentDeferred: false,
contentHash: listing.documents[0].contentHash,
skippedReason: 'Symbolic link target is not a repository file',
skippedExistingDisposition: 'replace',
})
expect(fetchMock).toHaveBeenCalledTimes(2)
}
)
it('reads the full target blob when GitHub Contents silently truncates a link at 1 MiB', async () => {
const fullContent = 'x'.repeat(1024 * 1024 + 8192)
const fetchMock = vi
.fn()
.mockResolvedValueOnce(treeResponse([link, { ...target, size: fullContent.length }]))
.mockResolvedValueOnce(new Response('target.md'))
.mockResolvedValueOnce(new Response(fullContent))
vi.stubGlobal('fetch', fetchMock)
const context = {}
const listing = await githubConnector.listDocuments('token', source, undefined, context)
const hydrated = await githubConnector.getDocument('token', source, link.path, context)
expect(hydrated?.content.length).toBe(fullContent.length)
expect(hydrated?.content.endsWith(fullContent.slice(-8192))).toBe(true)
expect(hydrated?.contentHash).toBe(listing.documents[0].contentHash)
expect(hydrated?.metadata?.size).toBe(fullContent.length)
expect(fetchMock.mock.calls.slice(1).map(([url]) => url)).toEqual([
'https://api.github.com/repos/owner/repo/git/blobs/link-sha',
'https://api.github.com/repos/owner/repo/git/blobs/target-sha',
])
})
it('follows an in-repository link chain outside the configured listing prefix', async () => {
const nextLink = { ...treeFile('intermediate.md', 'next-link-sha'), mode: '120000' }
vi.stubGlobal(
'fetch',
vi
.fn()
.mockResolvedValueOnce(treeResponse([link, nextLink, target]))
.mockResolvedValueOnce(new Response('../intermediate.md'))
.mockResolvedValueOnce(new Response('docs/target.md'))
.mockResolvedValueOnce(new Response('complete target'))
)
await expect(
githubConnector.getDocument('token', { ...source, pathPrefix: 'docs/' }, link.path)
).resolves.toMatchObject({ content: 'complete target' })
})
it('bounds cycles without repeatedly fetching the same link', async () => {
const nextLink = { ...treeFile('docs/next.md', 'next-link-sha'), mode: '120000' }
const fetchMock = vi
.fn()
.mockResolvedValueOnce(treeResponse([link, nextLink]))
.mockResolvedValueOnce(new Response('next.md'))
.mockResolvedValueOnce(new Response('link.md'))
vi.stubGlobal('fetch', fetchMock)
await expect(githubConnector.getDocument('token', source, link.path)).resolves.toMatchObject({
content: '',
skippedReason: 'Symbolic link target is not a repository file',
skippedExistingDisposition: 'replace',
})
expect(fetchMock).toHaveBeenCalledTimes(3)
})
it('caps a long acyclic link chain at forty target reads', async () => {
const links = Array.from({ length: 41 }, (_, index) => ({
...treeFile(`link-${index}.md`, `link-sha-${index}`),
mode: '120000',
}))
const fetchMock = vi.fn().mockResolvedValueOnce(treeResponse(links))
for (let index = 1; index <= 40; index++)
fetchMock.mockResolvedValueOnce(new Response(`link-${index}.md`))
vi.stubGlobal('fetch', fetchMock)
await expect(
githubConnector.getDocument('token', source, links[0].path)
).resolves.toMatchObject({
skippedReason: 'Symbolic link target is not a repository file',
skippedExistingDisposition: 'replace',
})
expect(fetchMock).toHaveBeenCalledTimes(41)
})
it('fails hydration when a truncated snapshot may have omitted the target', async () => {
vi.stubGlobal(
'fetch',
vi
.fn()
.mockResolvedValueOnce(treeResponse([link], true))
.mockResolvedValueOnce(new Response('target.md'))
)
await expect(githubConnector.getDocument('token', source, link.path)).rejects.toThrow(
'GitHub tree was truncated before the symbolic link target could be resolved'
)
})
it('preserves binary detection and byte limits for actual symlink target blobs', async () => {
for (const [body, length, reason] of [
['binary\0contents', '15', 'Binary file was not indexed'],
[
'oversized',
String(100 * 1024 * 1024 + 1),
'File exceeds the 100MB size limit and was not indexed',
],
]) {
vi.stubGlobal(
'fetch',
vi
.fn()
.mockResolvedValueOnce(treeResponse([link, target]))
.mockResolvedValueOnce(new Response('target.md'))
.mockResolvedValueOnce(new Response(body, { headers: { 'content-length': length } }))
)
await expect(githubConnector.getDocument('token', source, link.path)).resolves.toMatchObject({
content: '',
skippedReason: reason,
})
}
})
})