parser.test.js
8.58 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
import { describe, it, expect } from 'vitest';
const { Parser } = require('../../core/parser');
describe('Parser', () => {
// ── parseCalls: 正则解析(向后兼容) ──
describe('parseCalls (regex)', () => {
it('should parse single agent call', () => {
const input = `
一些分析内容...
[CALL: coder]
实现一个 add 函数
[/CALL]
其他内容...
`;
const result = Parser.parseCalls(input);
expect(result).toHaveLength(1);
expect(result[0]).toEqual({
agent: 'coder',
task: '实现一个 add 函数'
});
});
it('should return only the last call when multiple calls exist (serial decision constraint)', () => {
const input = `
[CALL: coder]
写代码
[/CALL]
[CALL: verifier]
验证代码
[/CALL]
`;
const result = Parser.parseCalls(input);
// 串行决策约束:只返回最后一个 CALL
expect(result).toHaveLength(1);
expect(result[0].agent).toBe('verifier');
});
it('should return empty array when no calls', () => {
const input = '没有任何调用标记的内容';
const result = Parser.parseCalls(input);
expect(result).toEqual([]);
});
it('should handle agent name with spaces', () => {
const input = '[CALL: frontend-tester ]\n测试任务\n[/CALL]';
const result = Parser.parseCalls(input);
expect(result[0].agent).toBe('frontend-tester');
});
it('should preserve multiline task content', () => {
const input = `[CALL: coder]
第一行
第二行
第三行
[/CALL]`;
const result = Parser.parseCalls(input);
expect(result[0].task).toBe('第一行\n第二行\n第三行');
});
it('should skip placeholder agent names', () => {
const input = `
[CALL: agent_name]
这是示例
[/CALL]
[CALL: coder]
真实任务
[/CALL]
`;
const result = Parser.parseCalls(input);
expect(result).toHaveLength(1);
expect(result[0].agent).toBe('coder');
});
});
// ── parseCalls: JSON 解析 (3.1) ──
describe('parseCalls (JSON)', () => {
it('should parse single call from JSON code block', () => {
const input = `
分析完成,调用 coder 执行:
\`\`\`json
{ "agent": "coder", "task": "实现一个 add 函数" }
\`\`\`
`;
const result = Parser.parseCalls(input);
expect(result).toHaveLength(1);
expect(result[0].agent).toBe('coder');
expect(result[0].task).toBe('实现一个 add 函数');
});
it('should parse multiple calls from JSON code block', () => {
const input = `
\`\`\`json
{
"calls": [
{ "agent": "coder", "task": "写代码" },
{ "agent": "verifier", "task": "验证" }
]
}
\`\`\`
`;
// 无 agents 配置时仍然串行约束
const result = Parser.parseCalls(input);
expect(result).toHaveLength(1);
expect(result[0].agent).toBe('verifier');
});
it('should prefer JSON over regex when both present', () => {
const input = `
\`\`\`json
{ "agent": "coder", "task": "JSON 任务" }
\`\`\`
[CALL: verifier]
正则任务
[/CALL]
`;
const result = Parser.parseCalls(input);
// JSON 优先
expect(result).toHaveLength(1);
expect(result[0].agent).toBe('coder');
expect(result[0].task).toBe('JSON 任务');
});
it('should fallback to regex when JSON is invalid', () => {
const input = `
\`\`\`json
{ invalid json content
\`\`\`
[CALL: coder]
正则任务
[/CALL]
`;
const result = Parser.parseCalls(input);
expect(result).toHaveLength(1);
expect(result[0].agent).toBe('coder');
expect(result[0].task).toBe('正则任务');
});
});
// ── parseCalls: parallelGroup (3.5) ──
describe('parseCalls (parallelGroup)', () => {
const agentsWithParallel = {
coder: { type: 'executor' },
'validator-style': { type: 'validator', parallelGroup: 'audit' },
'validator-test': { type: 'validator', parallelGroup: 'audit' },
'validator-security': { type: 'validator', parallelGroup: 'audit' },
};
it('should allow parallel calls for same parallelGroup', () => {
const input = `
[CALL: validator-style]
检查样式
[/CALL]
[CALL: validator-test]
运行测试
[/CALL]
[CALL: validator-security]
安全审计
[/CALL]
`;
const result = Parser.parseCalls(input, { agents: agentsWithParallel });
// 同一个 parallelGroup,允许全部返回
expect(result).toHaveLength(3);
});
it('should still enforce serial constraint for ungrouped agents', () => {
const input = `
[CALL: coder]
写代码
[/CALL]
[CALL: validator-style]
检查样式
[/CALL]
`;
const result = Parser.parseCalls(input, { agents: agentsWithParallel });
// coder 没有 parallelGroup,严格串行
expect(result).toHaveLength(1);
expect(result[0].agent).toBe('validator-style');
});
it('should enforce serial when no agents config provided', () => {
const input = `
[CALL: validator-style]
检查样式
[/CALL]
[CALL: validator-test]
运行测试
[/CALL]
`;
const result = Parser.parseCalls(input); // 没有 agents 参数
expect(result).toHaveLength(1);
expect(result[0].agent).toBe('validator-test');
});
it('should work with JSON format and parallelGroup', () => {
const input = `
\`\`\`json
{
"calls": [
{ "agent": "validator-style", "task": "检查样式" },
{ "agent": "validator-test", "task": "运行测试" }
]
}
\`\`\`
`;
const result = Parser.parseCalls(input, { agents: agentsWithParallel });
// 同 parallelGroup,允许并行
expect(result).toHaveLength(2);
});
});
// ── parseResult ──
describe('parseResult', () => {
it('should parse DONE status', () => {
const input = `
执行完成
[RESULT: DONE]
已创建 src/add.js 文件
[/RESULT]
`;
const result = Parser.parseResult(input);
expect(result).toEqual({
status: 'DONE',
message: '已创建 src/add.js 文件'
});
});
it('should parse PASS status', () => {
const input = '[RESULT: PASS]\n所有测试通过\n[/RESULT]';
const result = Parser.parseResult(input);
expect(result.status).toBe('PASS');
expect(result.message).toBe('所有测试通过');
});
it('should parse FAIL status', () => {
const input = '[RESULT: FAIL]\n测试失败:add(1,2) 返回 null\n[/RESULT]';
const result = Parser.parseResult(input);
expect(result.status).toBe('FAIL');
expect(result.message).toContain('测试失败');
});
it('should parse ERROR status', () => {
const input = '[RESULT: ERROR]\n文件不存在\n[/RESULT]';
const result = Parser.parseResult(input);
expect(result.status).toBe('ERROR');
});
it('should return ERROR when no result marker', () => {
const input = '没有结果标记的输出';
const result = Parser.parseResult(input);
expect(result.status).toBe('ERROR');
expect(result.message).toContain('No result marker');
});
it('should handle status with extra spaces', () => {
const input = '[RESULT: PASS ]\n通过\n[/RESULT]';
const result = Parser.parseResult(input);
expect(result.status).toBe('PASS');
});
});
// ── parseResult: JSON 解析 (3.1) ──
describe('parseResult (JSON)', () => {
it('should parse result from JSON code block', () => {
const input = `
执行完成:
\`\`\`json
{ "status": "DONE", "message": "已创建文件" }
\`\`\`
`;
const result = Parser.parseResult(input);
expect(result.status).toBe('DONE');
expect(result.message).toBe('已创建文件');
});
it('should prefer JSON over regex for result', () => {
const input = `
\`\`\`json
{ "status": "PASS", "message": "JSON 结果" }
\`\`\`
[RESULT: FAIL]
正则结果
[/RESULT]
`;
const result = Parser.parseResult(input);
// JSON 优先
expect(result.status).toBe('PASS');
expect(result.message).toBe('JSON 结果');
});
it('should fallback to regex when JSON result is invalid', () => {
const input = `
\`\`\`json
{ "invalid": "no status field" }
\`\`\`
[RESULT: DONE]
正则兜底
[/RESULT]
`;
const result = Parser.parseResult(input);
expect(result.status).toBe('DONE');
expect(result.message).toBe('正则兜底');
});
it('should reject invalid status values in JSON', () => {
const input = `
\`\`\`json
{ "status": "RUNNING", "message": "非法状态" }
\`\`\`
[RESULT: DONE]
正则兜底
[/RESULT]
`;
const result = Parser.parseResult(input);
// RUNNING 不是合法状态,JSON 解析应忽略
expect(result.status).toBe('DONE');
});
});
});