Repository navigation
Expand file tree
/
Copy pathagent-source-github.test.ts
More file actions
280 lines (238 loc) · 9.17 KB
/
Copy pathagent-source-github.test.ts
File metadata and controls
280 lines (238 loc) · 9.17 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
/**
* Tests for GitHubAgentSource (M5-4, Issue #127)
*/
import { describe, it, expect, vi } from 'vitest';
import {
GitHubAgentSource,
type GitHubFetcher,
type AgentManifest,
type AgentDefinition,
parseCharterMetadata,
} from '@bradygaster/squad-sdk/config';
// --- Mock fetcher helper ---
function makeFetcher(
dirs: Array<{ name: string; type: 'file' | 'dir' }> = [],
files: Record<string, string> = {},
): GitHubFetcher {
return {
listDirectory: vi.fn(async () => dirs),
getFileContent: vi.fn(async (_o, _r, path) => files[path] ?? null),
};
}
const CHARTER_AGENT1 = `## Identity
**Name:** Agent1
**Role:** Prompt Engineer
## Model
**Preferred:** claude-sonnet-4.5
## Tools
- edit
- grep
`;
const CHARTER_AGENT2 = `## Identity
**Name:** Agent2
**Role:** Code Architect
`;
// --- Tests ---
describe('GitHubAgentSource', () => {
describe('constructor', () => {
it('should parse owner/repo format', () => {
const source = new GitHubAgentSource('acme/squad-team');
expect(source.name).toBe('github');
expect(source.type).toBe('github');
});
it('should throw on invalid repo format', () => {
expect(() => new GitHubAgentSource('invalid')).toThrow('Invalid repo format');
expect(() => new GitHubAgentSource('')).toThrow('Invalid repo format');
expect(() => new GitHubAgentSource('a/b/c')).toThrow('Invalid repo format');
});
it('should accept optional ref and pathPrefix', () => {
const fetcher = makeFetcher();
const source = new GitHubAgentSource('acme/repo', {
ref: 'develop',
pathPrefix: 'custom/agents',
fetcher,
});
expect(source.type).toBe('github');
});
});
describe('listAgents', () => {
it('should return manifests for agent directories with charters', async () => {
const fetcher = makeFetcher(
[
{ name: 'agent1', type: 'dir' },
{ name: 'agent2', type: 'dir' },
],
{
'.squad/agents/agent1/charter.md': CHARTER_AGENT1,
'.squad/agents/agent2/charter.md': CHARTER_AGENT2,
},
);
const source = new GitHubAgentSource('acme/repo', { fetcher });
const agents = await source.listAgents();
expect(agents).toHaveLength(2);
expect(agents[0].name).toBe('Agent1');
expect(agents[0].role).toBe('Prompt Engineer');
expect(agents[0].source).toBe('github');
expect(agents[1].name).toBe('Agent2');
});
it('should skip files (non-directories)', async () => {
const fetcher = makeFetcher(
[
{ name: 'agent1', type: 'dir' },
{ name: 'README.md', type: 'file' },
],
{ '.squad/agents/agent1/charter.md': CHARTER_AGENT1 },
);
const source = new GitHubAgentSource('acme/repo', { fetcher });
const agents = await source.listAgents();
expect(agents).toHaveLength(1);
});
it('should skip directories without charter.md', async () => {
const fetcher = makeFetcher(
[{ name: 'empty-agent', type: 'dir' }],
{},
);
const source = new GitHubAgentSource('acme/repo', { fetcher });
const agents = await source.listAgents();
expect(agents).toHaveLength(0);
});
it('should return empty array with default fetcher (no config)', async () => {
const source = new GitHubAgentSource('acme/repo');
const agents = await source.listAgents();
expect(agents).toEqual([]);
});
it('should use custom pathPrefix', async () => {
const fetcher = makeFetcher(
[{ name: 'agent1', type: 'dir' }],
{ 'custom/path/agent1/charter.md': CHARTER_AGENT1 },
);
const source = new GitHubAgentSource('acme/repo', {
pathPrefix: 'custom/path',
fetcher,
});
const agents = await source.listAgents();
expect(agents).toHaveLength(1);
expect(fetcher.listDirectory).toHaveBeenCalledWith('acme', 'repo', 'custom/path', undefined);
});
it('should pass branch ref to fetcher', async () => {
const fetcher = makeFetcher([], {});
const source = new GitHubAgentSource('acme/repo', { ref: 'develop', fetcher });
await source.listAgents();
expect(fetcher.listDirectory).toHaveBeenCalledWith('acme', 'repo', '.squad/agents', 'develop');
});
// Regression test: when parallel charter fetching was introduced, the
// initial pass swallowed all per-item rejections (mapWithLimitSettled).
// That hid 403/auth/rate-limit/network failures behind silently smaller
// result sets. listAgents must continue to PROPAGATE thrown fetcher
// errors, matching the prior sequential `for...await` behavior.
it('propagates a thrown fetcher error instead of silently dropping the agent', async () => {
const fetcher: GitHubFetcher = {
listDirectory: vi.fn(async () => [
{ name: 'agent1', type: 'dir' },
{ name: 'agent2', type: 'dir' },
]),
getFileContent: vi.fn(async (_o, _r, path) => {
if (path.includes('agent2')) {
// Simulate a transient GitHub failure (e.g. secondary rate limit)
const err = new Error('GitHub API: 403 secondary rate limit');
(err as Error & { status?: number }).status = 403;
throw err;
}
return CHARTER_AGENT1;
}),
};
const source = new GitHubAgentSource('acme/repo', { fetcher });
await expect(source.listAgents()).rejects.toThrow(/secondary rate limit/);
});
it('still skips agents whose charter.md returns null (missing file)', async () => {
const fetcher = makeFetcher(
[
{ name: 'agent1', type: 'dir' },
{ name: 'agent-no-charter', type: 'dir' },
],
{ '.squad/agents/agent1/charter.md': CHARTER_AGENT1 },
);
const source = new GitHubAgentSource('acme/repo', { fetcher });
const agents = await source.listAgents();
expect(agents).toHaveLength(1);
expect(agents[0]!.name).toBe('Agent1');
});
});
describe('getAgent', () => {
it('should return full agent definition', async () => {
const fetcher = makeFetcher([], {
'.squad/agents/agent1/charter.md': CHARTER_AGENT1,
});
const source = new GitHubAgentSource('acme/repo', { fetcher });
const agent = await source.getAgent('agent1');
expect(agent).not.toBeNull();
expect(agent!.name).toBe('Agent1');
expect(agent!.role).toBe('Prompt Engineer');
expect(agent!.charter).toBe(CHARTER_AGENT1);
expect(agent!.model).toBe('claude-sonnet-4.5');
expect(agent!.tools).toEqual(['edit', 'grep']);
expect(agent!.source).toBe('github');
});
it('should include history when available', async () => {
const fetcher = makeFetcher([], {
'.squad/agents/agent1/charter.md': CHARTER_AGENT1,
'.squad/agents/agent1/history.md': '# History\n- Created team',
});
const source = new GitHubAgentSource('acme/repo', { fetcher });
const agent = await source.getAgent('agent1');
expect(agent!.history).toBe('# History\n- Created team');
});
it('should return null when charter not found', async () => {
const fetcher = makeFetcher([], {});
const source = new GitHubAgentSource('acme/repo', { fetcher });
const agent = await source.getAgent('nonexistent');
expect(agent).toBeNull();
});
it('should use directory name when charter has no name', async () => {
const fetcher = makeFetcher([], {
'.squad/agents/my-agent/charter.md': '# Just a charter\nSome content.',
});
const source = new GitHubAgentSource('acme/repo', { fetcher });
const agent = await source.getAgent('my-agent');
expect(agent).not.toBeNull();
expect(agent!.name).toBe('my-agent');
expect(agent!.role).toBe('agent');
});
});
describe('getCharter', () => {
it('should return raw charter content', async () => {
const fetcher = makeFetcher([], {
'.squad/agents/agent1/charter.md': CHARTER_AGENT1,
});
const source = new GitHubAgentSource('acme/repo', { fetcher });
const charter = await source.getCharter('agent1');
expect(charter).toBe(CHARTER_AGENT1);
});
it('should return null when charter missing', async () => {
const fetcher = makeFetcher([], {});
const source = new GitHubAgentSource('acme/repo', { fetcher });
const charter = await source.getCharter('missing');
expect(charter).toBeNull();
});
});
describe('parseCharterMetadata', () => {
it('should extract name and role from Identity section', () => {
const meta = parseCharterMetadata(CHARTER_AGENT1);
expect(meta.name).toBe('Agent1');
expect(meta.role).toBe('Prompt Engineer');
});
it('should extract model preference', () => {
const meta = parseCharterMetadata(CHARTER_AGENT1);
expect(meta.model).toBe('claude-sonnet-4.5');
});
it('should extract tools list', () => {
const meta = parseCharterMetadata(CHARTER_AGENT1);
expect(meta.tools).toEqual(['edit', 'grep']);
});
it('should handle empty content', () => {
const meta = parseCharterMetadata('');
expect(meta.name).toBeUndefined();
expect(meta.role).toBeUndefined();
});
});
});