|
| 1 | +import { resolveAndValidateSubagents, SubagentResolutionError, type AgentVersionEntry } from '../subagent-resolution' |
| 2 | + |
| 3 | +describe('resolveAndValidateSubagents', () => { |
| 4 | + const requestedPublisherId = 'me' |
| 5 | + |
| 6 | + function makeAgents(entries: { id: string; version: string; spawnableAgents?: string[] }[]): AgentVersionEntry[] { |
| 7 | + return entries.map((e) => ({ id: e.id, version: e.version, data: { id: e.id, version: e.version, spawnableAgents: e.spawnableAgents } })) |
| 8 | + } |
| 9 | + |
| 10 | + test('simple same-publisher id resolves to batch version when present', async () => { |
| 11 | + const agents = makeAgents([ |
| 12 | + { id: 'file-explorer', version: '1.0.10', spawnableAgents: ['file-picker'] }, |
| 13 | + { id: 'file-picker', version: '1.0.11' }, |
| 14 | + ]) |
| 15 | + |
| 16 | + const exists = (full: string) => full === 'me/file-picker@1.0.11' || full === 'me/file-explorer@1.0.10' |
| 17 | + const latest = async () => null |
| 18 | + |
| 19 | + await resolveAndValidateSubagents({ |
| 20 | + agents, |
| 21 | + requestedPublisherId, |
| 22 | + existsInSamePublisher: exists, |
| 23 | + getLatestPublishedVersion: latest, |
| 24 | + }) |
| 25 | + |
| 26 | + expect(agents[0].data.spawnableAgents).toEqual(['me/file-picker@1.0.11']) |
| 27 | + }) |
| 28 | + |
| 29 | + test('simple same-publisher id resolves to latest published when not in batch', async () => { |
| 30 | + const agents = makeAgents([ |
| 31 | + { id: 'file-explorer', version: '1.0.10', spawnableAgents: ['file-picker'] }, |
| 32 | + ]) |
| 33 | + |
| 34 | + const exists = (full: string) => full === 'me/file-explorer@1.0.10' || full === 'me/file-picker@1.0.9' |
| 35 | + const latest = async (pub: string, id: string) => (pub === 'me' && id === 'file-picker' ? '1.0.9' : null) |
| 36 | + |
| 37 | + await resolveAndValidateSubagents({ agents, requestedPublisherId, existsInSamePublisher: exists, getLatestPublishedVersion: latest }) |
| 38 | + |
| 39 | + expect(agents[0].data.spawnableAgents).toEqual(['me/file-picker@1.0.9']) |
| 40 | + }) |
| 41 | + |
| 42 | + test('fully-qualified same-publisher refs are kept and validated', async () => { |
| 43 | + const agents = makeAgents([ |
| 44 | + { id: 'file-explorer', version: '1.0.10', spawnableAgents: ['me/file-picker@1.0.8'] }, |
| 45 | + ]) |
| 46 | + |
| 47 | + const exists = (full: string) => full === 'me/file-picker@1.0.8' || full === 'me/file-explorer@1.0.10' |
| 48 | + const latest = async () => null |
| 49 | + |
| 50 | + await resolveAndValidateSubagents({ agents, requestedPublisherId, existsInSamePublisher: exists, getLatestPublishedVersion: latest }) |
| 51 | + |
| 52 | + expect(agents[0].data.spawnableAgents).toEqual(['me/file-picker@1.0.8']) |
| 53 | + }) |
| 54 | + |
| 55 | + test('cross-publisher simple refs resolve to latest without same-publisher validation', async () => { |
| 56 | + const agents = makeAgents([ |
| 57 | + { id: 'file-explorer', version: '1.0.10', spawnableAgents: ['other/file-picker'] }, |
| 58 | + ]) |
| 59 | + |
| 60 | + const exists = (full: string) => full === 'me/file-explorer@1.0.10' |
| 61 | + const latest = async (pub: string, id: string) => (pub === 'other' && id === 'file-picker' ? '2.0.1' : null) |
| 62 | + |
| 63 | + await resolveAndValidateSubagents({ agents, requestedPublisherId, existsInSamePublisher: exists, getLatestPublishedVersion: latest }) |
| 64 | + |
| 65 | + expect(agents[0].data.spawnableAgents).toEqual(['other/file-picker@2.0.1']) |
| 66 | + }) |
| 67 | + |
| 68 | + test('throws when simple ref has no published versions', async () => { |
| 69 | + const agents = makeAgents([ |
| 70 | + { id: 'file-explorer', version: '1.0.10', spawnableAgents: ['missing'] }, |
| 71 | + ]) |
| 72 | + |
| 73 | + const exists = (full: string) => full === 'me/file-explorer@1.0.10' |
| 74 | + const latest = async () => null |
| 75 | + |
| 76 | + await expect( |
| 77 | + resolveAndValidateSubagents({ agents, requestedPublisherId, existsInSamePublisher: exists, getLatestPublishedVersion: latest }) |
| 78 | + ).rejects.toBeInstanceOf(SubagentResolutionError) |
| 79 | + }) |
| 80 | + |
| 81 | + test('throws when fully-qualified same-publisher ref does not exist', async () => { |
| 82 | + const agents = makeAgents([ |
| 83 | + { id: 'file-explorer', version: '1.0.10', spawnableAgents: ['me/file-picker@1.0.0'] }, |
| 84 | + ]) |
| 85 | + |
| 86 | + const exists = (full: string) => full === 'me/file-explorer@1.0.10' // not the picker |
| 87 | + const latest = async () => null |
| 88 | + |
| 89 | + await expect( |
| 90 | + resolveAndValidateSubagents({ agents, requestedPublisherId, existsInSamePublisher: exists, getLatestPublishedVersion: latest }) |
| 91 | + ).rejects.toBeInstanceOf(SubagentResolutionError) |
| 92 | + }) |
| 93 | +}) |
0 commit comments