-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent-coder.test.js
More file actions
512 lines (391 loc) · 14.5 KB
/
agent-coder.test.js
File metadata and controls
512 lines (391 loc) · 14.5 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
/**
* Coder Agent Tests
*
* Comprehensive test suite for the CoderAgent class covering:
* - Constructor and initialization
* - Implementation result parsing
* - Fix result parsing
* - Text fallback parsing
* - State management
* - Clarification requests
* - Statistics
*/
import { describe, it, beforeEach, afterEach, mock } from 'node:test';
import assert from 'node:assert';
import agentCore from './agent-core.js';
import { CoderAgent, IMPL_STATUS, FIX_STATUS } from './agent-coder.js';
describe('CoderAgent - Constants', () => {
it('should export IMPL_STATUS constants', () => {
assert.strictEqual(IMPL_STATUS.COMPLETE, 'complete');
assert.strictEqual(IMPL_STATUS.BLOCKED, 'blocked');
assert.strictEqual(IMPL_STATUS.NEEDS_CLARIFICATION, 'needs_clarification');
});
it('should export FIX_STATUS constants', () => {
assert.strictEqual(FIX_STATUS.FIXED, 'fixed');
assert.strictEqual(FIX_STATUS.STILL_FAILING, 'still_failing');
assert.strictEqual(FIX_STATUS.BLOCKED, 'blocked');
});
});
describe('CoderAgent - Constructor and Initialization', () => {
beforeEach(() => {
agentCore.reset();
});
it('should create instance with default options', () => {
const coder = new CoderAgent();
assert.strictEqual(coder.name, 'coder');
assert.strictEqual(coder.model, 'opus');
assert.strictEqual(coder.fallbackModel, 'sonnet');
});
it('should create instance with custom options', () => {
const coder = new CoderAgent({
model: 'sonnet',
fallbackModel: 'haiku',
allowExisting: true
});
assert.strictEqual(coder.model, 'sonnet');
assert.strictEqual(coder.fallbackModel, 'haiku');
});
it('should register agent with agent core', () => {
const coder = new CoderAgent();
const agent = agentCore.getAgent('coder');
assert.ok(agent);
assert.strictEqual(agent.name, 'coder');
assert.strictEqual(agent.model, 'opus');
});
it('should initialize agent state correctly', () => {
const coder = new CoderAgent();
assert.strictEqual(coder.agent.state.tasksImplemented, 0);
assert.strictEqual(coder.agent.state.fixesApplied, 0);
assert.strictEqual(coder.agent.state.linesOfCode, 0);
assert.strictEqual(coder.agent.state.testsWritten, 0);
assert.strictEqual(coder.agent.state.blockedCount, 0);
});
it('should set up subscriptions to other agents', () => {
const coder = new CoderAgent({
subscribesTo: ['supervisor', 'planner']
});
assert.deepStrictEqual(coder.agent.subscribesTo, ['supervisor', 'planner']);
});
it('should register tools with agent core', () => {
const coder = new CoderAgent();
assert.ok(coder.agent.tools.length > 0);
assert.ok(coder.agent.tools.some(t => t.name === 'implementationComplete'));
assert.ok(coder.agent.tools.some(t => t.name === 'fixComplete'));
});
});
describe('CoderAgent - Implementation Result Parsing', () => {
let coder;
beforeEach(() => {
agentCore.reset();
coder = new CoderAgent();
});
it('should parse implementation from structuredOutput.toolCall.arguments', () => {
const result = {
structuredOutput: {
toolCall: {
name: 'implementationComplete',
arguments: {
status: 'complete',
summary: 'Implementation done',
filesModified: ['src/index.js', 'src/utils.js'],
testsAdded: ['test/index.test.js'],
commands: ['npm install']
}
}
}
};
const parsed = coder._parseImplementationResult(result);
assert.strictEqual(parsed.status, 'complete');
assert.strictEqual(parsed.summary, 'Implementation done');
assert.deepStrictEqual(parsed.filesModified, ['src/index.js', 'src/utils.js']);
assert.deepStrictEqual(parsed.testsAdded, ['test/index.test.js']);
});
it('should parse implementation from toolCalls array', () => {
const result = {
toolCalls: [
{
name: 'implementationComplete',
arguments: {
status: 'complete',
summary: 'From toolCalls',
filesModified: ['file.js']
}
}
]
};
const parsed = coder._parseImplementationResult(result);
assert.strictEqual(parsed.summary, 'From toolCalls');
});
it('should fallback to text parsing when structured output unavailable', () => {
const result = {
response: 'Implementation completed. Modified `src/main.js` and `src/helper.js`.'
};
const parsed = coder._parseImplementationResult(result);
assert.ok(parsed.status);
assert.ok(parsed.summary);
});
});
describe('CoderAgent - Text Implementation Parsing', () => {
let coder;
beforeEach(() => {
agentCore.reset();
coder = new CoderAgent();
});
it('should detect complete status from text', () => {
const response = 'Successfully implemented the feature. All done.';
const parsed = coder._parseTextImplementation(response);
assert.strictEqual(parsed.status, IMPL_STATUS.COMPLETE);
});
it('should detect blocked status from text', () => {
const response = 'Implementation blocked due to missing dependencies';
const parsed = coder._parseTextImplementation(response);
assert.strictEqual(parsed.status, IMPL_STATUS.BLOCKED);
assert.ok(parsed.blockReason);
});
it('should detect blocked status from "cannot proceed"', () => {
const response = 'Cannot proceed with the implementation';
const parsed = coder._parseTextImplementation(response);
assert.strictEqual(parsed.status, IMPL_STATUS.BLOCKED);
});
it('should detect blocked status from "unable to"', () => {
const response = 'Unable to complete the task';
const parsed = coder._parseTextImplementation(response);
assert.strictEqual(parsed.status, IMPL_STATUS.BLOCKED);
});
it('should extract file paths from text', () => {
const response = 'Modified `src/index.js` and "lib/helper.js" files';
const parsed = coder._parseTextImplementation(response);
assert.ok(parsed.filesModified.includes('src/index.js'));
assert.ok(parsed.filesModified.includes('lib/helper.js'));
});
it('should identify test files from modified files', () => {
const response = 'Created `test/feature.test.js` and `spec/helper.spec.js`';
const parsed = coder._parseTextImplementation(response);
assert.ok(parsed.testsAdded.includes('test/feature.test.js'));
assert.ok(parsed.testsAdded.includes('spec/helper.spec.js'));
});
it('should truncate long summaries to 500 characters', () => {
const response = 'A'.repeat(1000);
const parsed = coder._parseTextImplementation(response);
assert.strictEqual(parsed.summary.length, 500);
});
});
describe('CoderAgent - Fix Result Parsing', () => {
let coder;
beforeEach(() => {
agentCore.reset();
coder = new CoderAgent();
});
it('should parse fix from structuredOutput.toolCall.arguments', () => {
const result = {
structuredOutput: {
toolCall: {
name: 'fixComplete',
arguments: {
status: 'fixed',
summary: 'Bug fixed',
filesModified: ['src/bug.js'],
testsRun: true,
testsPass: true,
remainingIssues: []
}
}
}
};
const parsed = coder._parseFixResult(result);
assert.strictEqual(parsed.status, 'fixed');
assert.strictEqual(parsed.testsPass, true);
assert.deepStrictEqual(parsed.remainingIssues, []);
});
it('should parse fix from toolCalls array', () => {
const result = {
toolCalls: [
{
name: 'fixComplete',
arguments: {
status: 'still_failing',
summary: 'From toolCalls',
remainingIssues: ['Issue 1']
}
}
]
};
const parsed = coder._parseFixResult(result);
assert.strictEqual(parsed.status, 'still_failing');
assert.deepStrictEqual(parsed.remainingIssues, ['Issue 1']);
});
it('should fallback to text parsing when structured output unavailable', () => {
const result = {
response: 'The tests now pass after the fix.'
};
const parsed = coder._parseFixResult(result);
assert.ok(parsed.status);
});
});
describe('CoderAgent - Text Fix Parsing', () => {
let coder;
beforeEach(() => {
agentCore.reset();
coder = new CoderAgent();
});
it('should detect fixed status from "fixed"', () => {
const response = 'Bug fixed successfully';
const parsed = coder._parseTextFix(response);
assert.strictEqual(parsed.status, FIX_STATUS.FIXED);
assert.strictEqual(parsed.testsPass, true);
});
it('should detect fixed status from "tests pass"', () => {
const response = 'All tests pass now';
const parsed = coder._parseTextFix(response);
assert.strictEqual(parsed.status, FIX_STATUS.FIXED);
});
it('should detect fixed status from "resolved"', () => {
const response = 'Issue resolved';
const parsed = coder._parseTextFix(response);
assert.strictEqual(parsed.status, FIX_STATUS.FIXED);
});
it('should detect blocked status from "blocked"', () => {
const response = 'Fix blocked by external dependency';
const parsed = coder._parseTextFix(response);
assert.strictEqual(parsed.status, FIX_STATUS.BLOCKED);
assert.ok(parsed.blockReason);
});
it('should detect blocked status from "cannot fix"', () => {
const response = 'Cannot fix this issue';
const parsed = coder._parseTextFix(response);
assert.strictEqual(parsed.status, FIX_STATUS.BLOCKED);
});
it('should default to still_failing status', () => {
const response = 'Made some changes but not sure if it works';
const parsed = coder._parseTextFix(response);
assert.strictEqual(parsed.status, FIX_STATUS.STILL_FAILING);
assert.strictEqual(parsed.testsPass, false);
});
it('should set testsRun to true', () => {
const response = 'Test output here';
const parsed = coder._parseTextFix(response);
assert.strictEqual(parsed.testsRun, true);
});
it('should have remaining issues when not fixed', () => {
const response = 'Still failing';
const parsed = coder._parseTextFix(response);
assert.ok(parsed.remainingIssues.length > 0);
});
it('should have empty remaining issues when fixed', () => {
const response = 'All tests pass';
const parsed = coder._parseTextFix(response);
assert.deepStrictEqual(parsed.remainingIssues, []);
});
});
describe('CoderAgent - Clarification Requests', () => {
let coder;
beforeEach(() => {
agentCore.reset();
coder = new CoderAgent();
});
it('should create clarification request', async () => {
const task = { id: 'task-123', description: 'Implement feature' };
const question = 'What format should the output be in?';
const clarification = await coder.requestClarification(task, question);
assert.strictEqual(clarification.taskId, 'task-123');
assert.strictEqual(clarification.taskDescription, 'Implement feature');
assert.strictEqual(clarification.question, question);
assert.ok(clarification.timestamp);
});
it('should add clarification to memory', async () => {
const task = { id: 'task-456', description: 'Test task' };
const question = 'Need clarification';
await coder.requestClarification(task, question);
const memories = coder.agent.memory;
assert.ok(memories.some(m => m.content.includes('clarification')));
});
});
describe('CoderAgent - Statistics', () => {
let coder;
beforeEach(() => {
agentCore.reset();
coder = new CoderAgent();
});
it('should return agent statistics', () => {
const stats = coder.getStats();
assert.strictEqual(stats.name, 'coder');
assert.strictEqual(stats.tasksImplemented, 0);
assert.strictEqual(stats.fixesApplied, 0);
assert.strictEqual(stats.linesOfCode, 0);
assert.strictEqual(stats.testsWritten, 0);
assert.strictEqual(stats.blockedCount, 0);
});
it('should reflect updated state in statistics', () => {
agentCore.updateAgentState('coder', {
tasksImplemented: 5,
fixesApplied: 3,
testsWritten: 10
});
const stats = coder.getStats();
assert.strictEqual(stats.tasksImplemented, 5);
assert.strictEqual(stats.fixesApplied, 3);
assert.strictEqual(stats.testsWritten, 10);
});
});
describe('CoderAgent - Subscription Setup', () => {
beforeEach(() => {
agentCore.reset();
});
it('should set up subscriptions on construction', () => {
const coder = new CoderAgent();
// Verify subscriptions are set up (agent has subscribesTo)
assert.ok(coder.agent.subscribesTo);
assert.ok(coder.agent.subscribesTo.length > 0);
});
it('should use custom subscribesTo when provided', () => {
const coder = new CoderAgent({
subscribesTo: ['supervisor']
});
assert.deepStrictEqual(coder.agent.subscribesTo, ['supervisor']);
});
});
describe('CoderAgent - Tool Definitions', () => {
let coder;
beforeEach(() => {
agentCore.reset();
coder = new CoderAgent();
});
it('should have implementationComplete tool with correct params', () => {
const implTool = coder.agent.tools.find(t => t.name === 'implementationComplete');
assert.ok(implTool);
assert.ok(implTool.params.some(p => p.name === 'status'));
assert.ok(implTool.params.some(p => p.name === 'summary'));
assert.ok(implTool.params.some(p => p.name === 'filesModified'));
assert.ok(implTool.params.some(p => p.name === 'testsAdded'));
assert.ok(implTool.params.some(p => p.name === 'commands'));
assert.ok(implTool.params.some(p => p.name === 'blockReason'));
});
it('should have fixComplete tool with correct params', () => {
const fixTool = coder.agent.tools.find(t => t.name === 'fixComplete');
assert.ok(fixTool);
assert.ok(fixTool.params.some(p => p.name === 'status'));
assert.ok(fixTool.params.some(p => p.name === 'summary'));
assert.ok(fixTool.params.some(p => p.name === 'filesModified'));
assert.ok(fixTool.params.some(p => p.name === 'testsRun'));
assert.ok(fixTool.params.some(p => p.name === 'testsPass'));
assert.ok(fixTool.params.some(p => p.name === 'remainingIssues'));
});
});
describe('CoderAgent - Allow Existing Registration', () => {
beforeEach(() => {
agentCore.reset();
});
it('should allow re-registration with allowExisting option', () => {
new CoderAgent();
// Should not throw
assert.doesNotThrow(() => {
new CoderAgent({ allowExisting: true });
});
});
it('should throw when re-registering without allowExisting', () => {
new CoderAgent();
assert.throws(() => {
new CoderAgent();
}, /already registered/);
});
});