|
| 1 | +import { afterEach, describe, expect, it, vi } from 'vitest' |
| 2 | +import { getTree } from '../../layers/registry/server/utils/github-client' |
| 3 | + |
| 4 | +afterEach(() => vi.unstubAllGlobals()) |
| 5 | + |
| 6 | +describe('getTree', () => { |
| 7 | + it('reconstructs truncated trees from immutable subtree SHAs', async () => { |
| 8 | + const answers = new Map([ |
| 9 | + ['root?recursive=1', { sha: 'root', truncated: true, tree: [{ path: 'partial', type: 'blob', sha: 'discard' }] }], |
| 10 | + ['root', { sha: 'root', tree: [{ path: 'skills', type: 'tree', sha: 'skills-tree' }] }], |
| 11 | + ['skills-tree?recursive=1', { sha: 'skills-tree', truncated: true, tree: [] }], |
| 12 | + ['skills-tree', { sha: 'skills-tree', tree: [{ path: 'one', type: 'tree', sha: 'one-tree' }] }], |
| 13 | + ['one-tree?recursive=1', { sha: 'one-tree', tree: [{ path: 'SKILL.md', type: 'blob', sha: 'skill', size: 120 }, { path: 'references/a.md', type: 'blob', sha: 'reference', size: 42 }] }], |
| 14 | + ]) |
| 15 | + const reads: string[] = [] |
| 16 | + vi.stubGlobal('fetch', vi.fn(async (url: string) => { |
| 17 | + const key = url.split('/git/trees/')[1]! |
| 18 | + reads.push(key) |
| 19 | + const answer = answers.get(key) |
| 20 | + if (!answer) |
| 21 | + throw new Error(`Unexpected tree read: ${key}`) |
| 22 | + return Response.json(answer) |
| 23 | + })) |
| 24 | + |
| 25 | + const result = await getTree('acme', 'large', 'root', {}, { expandTruncated: true }) |
| 26 | + |
| 27 | + expect(result.data).toEqual({ sha: 'root', truncated: false, tree: [ |
| 28 | + { path: 'skills', type: 'tree', sha: 'skills-tree' }, |
| 29 | + { path: 'skills/one', type: 'tree', sha: 'one-tree' }, |
| 30 | + { path: 'skills/one/SKILL.md', type: 'blob', sha: 'skill', size: 120 }, |
| 31 | + { path: 'skills/one/references/a.md', type: 'blob', sha: 'reference', size: 42 }, |
| 32 | + ] }) |
| 33 | + expect(reads).toEqual([...answers.keys()]) |
| 34 | + }) |
| 35 | + |
| 36 | + it('returns a partial tree with one request unless durable refresh opts in', async () => { |
| 37 | + const tree = { sha: 'root', truncated: true, tree: [{ path: 'SKILL.md', type: 'blob', sha: 'skill', size: 120 }] } |
| 38 | + const fetchMock = vi.fn(async () => Response.json(tree)) |
| 39 | + vi.stubGlobal('fetch', fetchMock) |
| 40 | + |
| 41 | + const result = await getTree('acme', 'large', 'root', {}) |
| 42 | + |
| 43 | + expect(result.data).toEqual(tree) |
| 44 | + expect(fetchMock).toHaveBeenCalledTimes(1) |
| 45 | + }) |
| 46 | + |
| 47 | + it('propagates a subtree read failure without presenting a partial tree', async () => { |
| 48 | + let reads = 0 |
| 49 | + vi.stubGlobal('fetch', vi.fn(async () => { |
| 50 | + reads++ |
| 51 | + return reads === 1 |
| 52 | + ? Response.json({ sha: 'root', truncated: true, tree: [] }) |
| 53 | + : new Response('', { status: 429 }) |
| 54 | + })) |
| 55 | + |
| 56 | + const result = await getTree('acme', 'large', 'root', {}, { expandTruncated: true }) |
| 57 | + |
| 58 | + expect(result.status).toBe(429) |
| 59 | + expect(result.data).toBeNull() |
| 60 | + }) |
| 61 | + |
| 62 | + it('stops bounded traversal without accepting an incomplete inventory', async () => { |
| 63 | + const fetchMock = vi.fn(async (url: string) => Response.json({ |
| 64 | + sha: url.split('/git/trees/')[1]!.split('?')[0], |
| 65 | + truncated: url.includes('?'), |
| 66 | + tree: url.includes('?') ? [] : [{ path: 'nested', type: 'tree', sha: 'next' }], |
| 67 | + })) |
| 68 | + vi.stubGlobal('fetch', fetchMock) |
| 69 | + |
| 70 | + const result = await getTree('acme', 'large', 'root', {}, { expandTruncated: true }) |
| 71 | + |
| 72 | + expect(result.data?.truncated).toBe(true) |
| 73 | + expect(fetchMock.mock.calls.length).toBeGreaterThan(1) |
| 74 | + expect(fetchMock.mock.calls.length).toBeLessThanOrEqual(64) |
| 75 | + }) |
| 76 | +}) |
0 commit comments