-
Notifications
You must be signed in to change notification settings - Fork 3k
Expand file tree
/
Copy pathToolGroup.test.tsx
More file actions
2176 lines (1981 loc) · 64.6 KB
/
Copy pathToolGroup.test.tsx
File metadata and controls
2176 lines (1981 loc) · 64.6 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
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// @vitest-environment jsdom
import { afterEach, describe, expect, it, vi } from 'vitest';
import { act } from 'react';
import { createRoot, type Root } from 'react-dom/client';
import type { ACPToolCall } from '../../adapters/types';
import type { SessionContentGenerator } from './AssistantMessage';
import { hasActiveAgents } from '../../adapters/toolClassification';
import { I18nProvider } from '../../i18n';
import { WebShellCustomizationProvider } from '../../customization';
import { TranscriptRenderModeProvider } from '../../transcriptRenderMode';
import { SubagentDetailsProvider } from '../../subagentDetailsContext';
import { MonitorDetailsProvider } from '../../monitorDetailsContext';
vi.mock('../../App', async () => {
const { createContext } = await import('react');
return {
TodoTimelineContext: createContext(new Map()),
TodoDetailContext: createContext(new Map()),
};
});
const {
buildUnifiedDiff,
extractDiff,
fencedCodeBlock,
formatSingleToolSummary,
formatToolGroupSummary,
getActiveTool,
getRawFileDiff,
getToolHeaderKind,
hasExpandableContent,
isWebFetchToolName,
languageForPath,
shouldAutoExpand,
ToolGroup,
ToolLine,
} = await import('./ToolGroup');
(
globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }
).IS_REACT_ACT_ENVIRONMENT = true;
const mounted: Array<{ root: Root; container: HTMLElement }> = [];
afterEach(() => {
for (const { root, container } of mounted.splice(0)) {
act(() => root.unmount());
container.remove();
}
});
function makeTool(overrides: Partial<ACPToolCall> = {}): ACPToolCall {
return {
callId: 'call-1',
toolName: 'Shell',
status: 'completed',
...overrides,
};
}
function renderToolLine(
tool: ACPToolCall,
props: Partial<Parameters<typeof ToolLine>[0]> = {},
customization = {},
): HTMLElement {
const container = document.createElement('div');
document.body.appendChild(container);
const root = createRoot(container);
act(() => {
root.render(
<I18nProvider language="en">
<WebShellCustomizationProvider value={customization}>
<ToolLine tool={tool} {...props} />
</WebShellCustomizationProvider>
</I18nProvider>,
);
});
mounted.push({ root, container });
return container;
}
function renderToolGroup(
tools: ACPToolCall[],
customization = {},
thoughts?: Array<{
content: string;
isStreaming?: boolean;
beforeToolCallId?: string;
}>,
compactSummary = false,
onOpenSubagent?: (tool: ACPToolCall) => void,
onOpenMonitor?: (tool: ACPToolCall) => Promise<boolean>,
language: 'en' | 'zh-CN' = 'en',
generateContent?: SessionContentGenerator,
): HTMLElement {
const container = document.createElement('div');
document.body.appendChild(container);
const root = createRoot(container);
act(() => {
const group = (
<ToolGroup
tools={tools}
thoughts={thoughts}
compactSummary={compactSummary}
generateContent={generateContent}
/>
);
root.render(
<I18nProvider language={language}>
<WebShellCustomizationProvider value={customization}>
{onOpenMonitor ? (
<MonitorDetailsProvider onOpen={onOpenMonitor}>
{group}
</MonitorDetailsProvider>
) : onOpenSubagent ? (
<SubagentDetailsProvider onOpen={onOpenSubagent}>
{group}
</SubagentDetailsProvider>
) : (
group
)}
</WebShellCustomizationProvider>
</I18nProvider>,
);
});
mounted.push({ root, container });
return container;
}
const t = (key: string, values?: Record<string, string | number>): string => {
if (key === 'toolGroup.running') {
return Number(values?.count ?? 0) > 1
? `Running ${values?.count ?? 0} tools: ${values?.name ?? 'tool'}`
: `Running ${values?.name ?? 'tool'}`;
}
if (key === 'toolGroup.summary') {
return `Ran ${values?.count ?? 0} tool${values?.count === 1 ? '' : 's'}`;
}
if (key === 'toolGroup.summary.ranAgents') {
return `Ran ${values?.count ?? 0} agent${values?.count === 1 ? '' : 's'}`;
}
if (key === 'toolGroup.summary.editedFiles') {
return `Edited ${values?.count ?? 0} files`;
}
if (key === 'toolGroup.summary.ranCommands') {
return `Ran ${values?.count ?? 0} commands`;
}
if (key === 'toolGroup.summary.readFiles') {
return `Read ${values?.count ?? 0} files`;
}
if (key === 'toolGroup.summary.searched') {
return `Searched ${values?.count ?? 0} times`;
}
if (key === 'toolGroup.summary.updatedTodos') {
return `Updated todos ${values?.count ?? 0} times`;
}
if (key === 'toolGroup.summary.provideInformation') {
return 'Provide information';
}
if (key === 'toolGroup.summary.askedQuestions') {
return `Asked ${values?.count ?? 0} question${values?.count === 1 ? '' : 's'}`;
}
if (key === 'toolGroup.summary.otherTools') {
return `Called ${values?.count ?? 0} other tools`;
}
return key;
};
const zhT = (key: string, values?: Record<string, string | number>): string => {
if (key === 'toolName.readfile') return '读取文件';
return t(key, values);
};
describe('tool group summary logic', () => {
it('counts agents separately only for compact summaries', () => {
const tools = [
makeTool({ callId: 'agent-1', toolName: 'Agent' }),
makeTool({ callId: 'agent-2', toolName: 'Agent' }),
makeTool({ callId: 'read', toolName: 'Read' }),
];
expect(formatToolGroupSummary(tools, t, undefined, true)).toBe(
'Ran 2 agents · Ran 1 tool',
);
expect(formatToolGroupSummary(tools, t)).toBe(
'Read 1 files Called 2 other tools',
);
});
it('uses the active tool in running summaries', () => {
const tools = [
makeTool({ callId: 'done', status: 'completed' }),
makeTool({
callId: 'active',
toolName: 'ReadFile',
status: 'in_progress',
}),
];
expect(hasActiveAgents(tools)).toBe(true);
expect(getActiveTool(tools).callId).toBe('active');
expect(formatToolGroupSummary(tools, t)).toBe('Running ReadFile');
});
it('uses a static summary when only background agents remain active', () => {
const tools = [
makeTool({ callId: 'done', status: 'completed' }),
makeTool({
callId: 'background',
toolName: 'agent',
status: 'pending',
args: { run_in_background: true },
rawOutput: { type: 'task_execution', status: 'background' },
}),
];
expect(formatToolGroupSummary(tools, t)).toBe('subagent.background');
});
it('keeps a foreground active tool ahead of a background agent', () => {
const tools = [
makeTool({
callId: 'background',
toolName: 'agent',
status: 'pending',
args: { run_in_background: true },
}),
makeTool({
callId: 'foreground',
toolName: 'ReadFile',
status: 'in_progress',
}),
];
expect(formatToolGroupSummary(tools, t)).toBe('Running ReadFile');
});
it('describes every active foreground tool until all tools finish', () => {
const tools = [
makeTool({
callId: 'read',
toolName: 'ReadFile',
status: 'in_progress',
args: { file_path: 'package.json' },
}),
makeTool({
callId: 'search',
toolName: 'grep',
status: 'pending',
args: { pattern: 'ToolGroup' },
}),
makeTool({ callId: 'done', status: 'completed' }),
];
const summary = formatToolGroupSummary(tools, t);
expect(summary).toContain('ReadFile package.json');
expect(summary).toContain('ToolGroup');
expect(summary).toContain('Running 2 tools:');
});
it('keeps workspace-relative paths in multi-tool summaries', () => {
const tools = [
makeTool({
callId: 'first',
toolName: 'ReadFile',
status: 'in_progress',
args: { file_path: '/workspace/src/index.ts' },
}),
makeTool({
callId: 'second',
toolName: 'ReadFile',
status: 'pending',
args: { file_path: '/workspace/test/index.ts' },
}),
];
expect(formatToolGroupSummary(tools, t, '/workspace')).toBe(
'Running 2 tools: ReadFile src/index.ts · ReadFile test/index.ts',
);
});
it('excludes a running background agent from a multi-tool summary', () => {
const tools = [
makeTool({
callId: 'agent',
toolName: 'agent',
status: 'in_progress',
args: { run_in_background: true },
}),
makeTool({
callId: 'read',
toolName: 'ReadFile',
status: 'in_progress',
args: { file_path: 'package.json' },
}),
makeTool({
callId: 'search',
toolName: 'grep',
status: 'pending',
args: { pattern: 'ToolGroup' },
}),
];
const summary = formatToolGroupSummary(tools, t);
expect(summary).toBe(
"Running 2 tools: ReadFile package.json · Grep 'ToolGroup' in path './'",
);
});
it('localizes active tool names in running summaries', () => {
const tools = [
makeTool({
callId: 'active',
toolName: 'ReadFile',
status: 'in_progress',
}),
];
expect(formatToolGroupSummary(tools, zhT)).toBe('Running 读取文件');
});
it('asks for information while AskUserQuestion is running', () => {
const tools = [
makeTool({
toolName: 'ask_user_question',
status: 'in_progress',
args: { questions: [{}, {}] },
}),
];
expect(formatToolGroupSummary(tools, t)).toBe('Provide information');
});
it('summarizes completed tool groups by common action type', () => {
const tools = [
makeTool({ callId: 'shell', status: 'completed' }),
makeTool({ callId: 'read', toolName: 'ReadFile', status: 'completed' }),
makeTool({ callId: 'edit', toolName: 'edit', status: 'completed' }),
makeTool({ callId: 'grep', toolName: 'grep', status: 'completed' }),
makeTool({
callId: 'todo',
toolName: 'todo_write',
status: 'completed',
}),
makeTool({
callId: 'ask',
toolName: 'ask_user_question',
status: 'completed',
args: { questions: [{}, {}] },
}),
];
expect(hasActiveAgents(tools)).toBe(false);
expect(getActiveTool(tools).callId).toBe('ask');
expect(formatToolGroupSummary(tools, t)).toBe(
'Edited 1 files Ran 1 commands Read 1 files Searched 1 times Updated todos 1 times Asked 2 questions',
);
});
it('formats a single shell summary as only the semantic description', () => {
expect(
formatSingleToolSummary(
makeTool({
toolName: 'run_shell_command',
args: {
command: 'dataworks-infra workspace list',
description: '查询用户工作空间列表',
timeout: 30000,
},
}),
t,
),
).toBe('查询用户工作空间列表');
});
it('falls back to command text for shell summaries without descriptions', () => {
expect(
formatSingleToolSummary(
makeTool({
toolName: 'Shell',
args: { command: 'npm run build', timeout: 30000 },
}),
t,
),
).toBe('Shell npm run build');
});
it('uses only skill names in single tool summaries', () => {
expect(
formatSingleToolSummary(
makeTool({
toolName: 'skill',
title:
'Skill: Use skill: "qc-helper" with args: "weather in Hangzhou next 5 days"',
args: {
skill: 'qc-helper',
args: 'weather in Hangzhou next 5 days',
},
}),
t,
),
).toBe('Skill qc-helper');
});
it('uses action summaries for single todo and ask-user tools', () => {
expect(
formatSingleToolSummary(makeTool({ toolName: 'todo_write' }), t),
).toBe('Updated todos 1 times');
expect(
formatSingleToolSummary(
makeTool({
toolName: 'ask_user_question',
args: { questions: [{}, {}, {}] },
}),
t,
),
).toBe('Asked 3 questions');
expect(
formatSingleToolSummary(
makeTool({
toolName: 'ask_user_question',
status: 'in_progress',
args: { questions: [{}, {}, {}] },
}),
t,
),
).toBe('Provide information');
});
it('counts legacy or empty AskUserQuestion inputs as one question', () => {
expect(
formatSingleToolSummary(makeTool({ toolName: 'ask_user_question' }), t),
).toBe('Asked 1 question');
expect(
formatSingleToolSummary(
makeTool({
toolName: 'ask_user_question',
args: { questions: [] },
}),
t,
),
).toBe('Asked 1 question');
});
it('truncates long single tool descriptions in the chat summary', () => {
const summary = formatSingleToolSummary(
makeTool({
toolName: 'Shell',
args: { command: 'x'.repeat(200) },
}),
t,
);
expect(summary.length).toBeLessThan(140);
expect(summary).toContain('...');
});
it('lets custom tool header extras render single-tool chat summaries', () => {
const container = renderToolGroup(
[
makeTool({
toolName: 'run_shell_command',
args: {
command: 'dataworks-infra workspace list',
description: '查询用户工作空间列表',
timeout: 30000,
},
}),
],
{
renderToolHeaderExtra: (info) => (
<span data-testid="custom-summary">
{info.kind}:{info.description}
</span>
),
},
);
const summary = container.querySelector('button');
expect(summary?.textContent).not.toContain('Shell');
expect(summary?.textContent).toContain('shell:查询用户工作空间列表');
expect(summary?.textContent).not.toContain('timeout: 30000ms');
});
it('opens a completed MCP App result in the session transcript', () => {
const container = renderToolGroup([
makeTool({
toolName: 'mcp__demo__show_dashboard',
rawOutput: {
type: 'mcp_app',
serverName: 'demo',
resourceUri: 'ui://demo/dashboard',
html: '<main>Dashboard</main>',
toolResult: { content: [] },
toolArguments: {},
fallbackText: 'Dashboard ready',
},
}),
]);
expect(
container.querySelector('button')?.getAttribute('aria-expanded'),
).toBe('true');
expect(container.textContent).toContain('Dashboard ready');
});
it('keeps an MCP App open when multiple tools share a summary', () => {
const container = renderToolGroup([
makeTool({ callId: 'read', toolName: 'read_file' }),
makeTool({
callId: 'app',
toolName: 'mcp__demo__show_dashboard',
rawOutput: {
type: 'mcp_app',
serverName: 'demo',
resourceUri: 'ui://demo/dashboard',
html: '<main>Dashboard</main>',
toolResult: { content: [] },
toolArguments: {},
fallbackText: 'Dashboard ready',
},
}),
]);
expect(
container.querySelector('button')?.getAttribute('aria-expanded'),
).toBe('true');
expect(container.textContent).toContain('Dashboard ready');
});
it('renders fallbackText for a compacted MCP App without mounting the iframe', () => {
const container = renderToolLine(
makeTool({
toolName: 'mcp__demo__show_dashboard',
rawOutput: {
type: 'mcp_app',
serverName: 'demo',
resourceUri: 'ui://demo/dashboard',
html: '',
toolResult: {},
toolArguments: {},
fallbackText: 'Dashboard ready',
},
}),
);
expect(container.textContent).toContain('Dashboard ready');
expect(container.querySelector('iframe')).toBeNull();
expect(container.querySelector('[data-testid="mcp-app"]')).toBeNull();
});
it('keeps an MCP App open in a summary-only row', () => {
const container = renderToolLine(
makeTool({
toolName: 'mcp__demo__show_dashboard',
rawOutput: {
type: 'mcp_app',
serverName: 'demo',
resourceUri: 'ui://demo/dashboard',
html: '<main>Dashboard</main>',
toolResult: { content: [] },
toolArguments: {},
fallbackText: 'Dashboard ready',
},
}),
{ summaryOnly: true },
);
expect(container.textContent).toContain('Dashboard ready');
});
it('uses action descriptions for shell rows inside grouped summaries', () => {
const container = renderToolGroup([
makeTool({
callId: 'shell',
toolName: 'run_shell_command',
title:
'Shell: dataworks-infra workspace list [timeout: 30000ms] (查询用户工作空间列表)',
args: {
command: 'dataworks-infra workspace list',
description: '查询用户工作空间列表',
timeout: 30000,
},
}),
makeTool({
callId: 'read',
toolName: 'read_file',
args: { file_path: 'README.md' },
}),
]);
act(() => container.querySelector('button')?.click());
expect(container.textContent).toContain('Shell');
expect(container.textContent).toContain('查询用户工作空间列表');
expect(container.textContent).not.toContain(
'dataworks-infra workspace list',
);
expect(container.textContent).not.toContain('timeout: 30000ms');
});
});
describe('tool output session links', () => {
function renderSessionLinkTool(readonly: boolean): HTMLElement {
const container = document.createElement('div');
document.body.appendChild(container);
const root = createRoot(container);
const toolLine = (
<ToolLine
tool={makeTool({
toolName: 'custom_tool',
rawOutput: '[child](qwen-session://child-session)',
})}
forceExpanded
/>
);
act(() => {
root.render(
<I18nProvider language="en">
{readonly ? (
<TranscriptRenderModeProvider value="readonly">
{toolLine}
</TranscriptRenderModeProvider>
) : (
toolLine
)}
</I18nProvider>,
);
});
mounted.push({ root, container });
return container;
}
it('keeps interactive tool session links clickable by default', () => {
const handler = vi.fn();
window.addEventListener('qwen:open-session', handler);
const container = renderSessionLinkTool(false);
const link = container.querySelector('a[role="button"]');
expect(link?.textContent).toBe('child');
act(() => {
link?.dispatchEvent(
new MouseEvent('click', { bubbles: true, cancelable: true }),
);
});
expect(handler).toHaveBeenCalledOnce();
window.removeEventListener('qwen:open-session', handler);
});
it('renders tool session links as inert text in readonly mode', () => {
const handler = vi.fn();
window.addEventListener('qwen:open-session', handler);
const container = renderSessionLinkTool(true);
expect(container.querySelector('a[role="button"]')).toBeNull();
expect(container.textContent).toContain('child');
expect(handler).not.toHaveBeenCalled();
window.removeEventListener('qwen:open-session', handler);
});
});
describe('tool expandability', () => {
it('only marks tools with actual detail views as expandable by output', () => {
expect(
hasExpandableContent(
makeTool({
toolName: 'Shell',
content: [{ type: 'content', content: { text: 'first\nsecond' } }],
}),
),
).toBe(true);
expect(
hasExpandableContent(
makeTool({
toolName: 'list_directory',
rawOutput: 'a\nb',
}),
),
).toBe(false);
});
it('does not expand skill rows that only have the skill name', () => {
expect(
hasExpandableContent(
makeTool({
toolName: 'skill',
title: 'Skill: Use skill: "review"',
args: { skill: 'review' },
}),
),
).toBe(false);
expect(
hasExpandableContent(
makeTool({
toolName: 'skill',
args: { skill: 'review' },
content: [
{
type: 'content',
content: { type: 'text', text: '# Code Review' },
},
],
}),
),
).toBe(true);
});
});
describe('tool kind logic', () => {
it('classifies common tool names for summary icons', () => {
expect(getToolHeaderKind(makeTool({ toolName: 'Shell' }))).toBe('shell');
expect(getToolHeaderKind(makeTool({ toolName: 'web_fetch' }))).toBe(
'fetch',
);
expect(getToolHeaderKind(makeTool({ toolName: 'ReadFile' }))).toBe('read');
expect(getToolHeaderKind(makeTool({ toolName: 'edit' }))).toBe('edit');
expect(getToolHeaderKind(makeTool({ toolName: 'write_file' }))).toBe(
'write',
);
expect(getToolHeaderKind(makeTool({ toolName: 'todo_write' }))).toBe(
'todo',
);
expect(getToolHeaderKind(makeTool({ toolName: 'ask_user_question' }))).toBe(
'ask',
);
});
it('recognizes web fetch aliases', () => {
expect(isWebFetchToolName('web_fetch')).toBe(true);
expect(isWebFetchToolName('WebFetch')).toBe(true);
expect(isWebFetchToolName('fetch')).toBe(true);
expect(isWebFetchToolName('ReadFile')).toBe(false);
});
it('auto-expands verbose tools only while active or failed', () => {
expect(
shouldAutoExpand(makeTool({ toolName: 'Shell', status: 'in_progress' })),
).toBe(true);
expect(
shouldAutoExpand(makeTool({ toolName: 'edit', status: 'failed' })),
).toBe(true);
expect(
shouldAutoExpand(makeTool({ toolName: 'Shell', status: 'completed' })),
).toBe(false);
expect(
shouldAutoExpand(makeTool({ toolName: 'glob', status: 'in_progress' })),
).toBe(false);
});
});
describe('tool row rendering', () => {
it('renders the aggregate summary for a multi-tool group', () => {
const container = renderToolGroup([
makeTool({
callId: 'read',
toolName: 'ReadFile',
status: 'in_progress',
args: { file_path: 'package.json' },
}),
makeTool({
callId: 'search',
toolName: 'grep',
status: 'pending',
args: { pattern: 'ToolGroup' },
}),
]);
expect(container.querySelector('button')?.textContent).toContain(
'package.json',
);
expect(container.querySelector('button')?.textContent).toContain(
'ToolGroup',
);
});
it('does not show elapsed time in a running summary', () => {
const container = renderToolGroup([
makeTool({ status: 'in_progress', startTime: 1_000 }),
makeTool({ callId: 'done', status: 'completed' }),
]);
expect(container.querySelector('button')?.textContent).not.toMatch(
/\d+[sm]/,
);
});
it('keeps elapsed time updating in a running tool row', () => {
vi.useFakeTimers();
vi.setSystemTime(6_000);
try {
const container = renderToolLine(
makeTool({
toolName: 'ReadFile',
status: 'in_progress',
startTime: 1_000,
}),
);
expect(container.textContent).toContain('5s');
act(() => {
vi.advanceTimersByTime(1_000);
});
expect(container.textContent).toContain('6s');
} finally {
vi.useRealTimers();
}
});
it('shows live elapsed time after expanding a single-tool group', () => {
vi.useFakeTimers();
vi.setSystemTime(6_000);
try {
const container = renderToolGroup(
[
makeTool({
toolName: 'ReadFile',
status: 'in_progress',
startTime: 1_000,
}),
],
{
renderToolHeaderExtra: (info) =>
info.elapsed ? <span>custom {info.elapsed}</span> : null,
},
);
const summary = container.querySelector('button');
expect(summary?.textContent).not.toContain('5s');
act(() => summary?.click());
const content = container.querySelector(
'[class*="chatSummaryContentClip"]',
);
expect(content?.className).not.toContain('chatSummaryContentCollapsed');
expect(content?.textContent).toContain('custom 5s');
act(() => {
vi.advanceTimersByTime(1_000);
});
expect(content?.textContent).toContain('custom 6s');
} finally {
vi.useRealTimers();
}
});
it('does not show elapsed time after a tool completes', () => {
const container = renderToolLine(
makeTool({
toolName: 'ReadFile',
status: 'completed',
startTime: 1_000,
endTime: 6_000,
}),
);
expect(container.textContent).not.toContain('5s');
});
it('keeps completed elapsed data available to custom header renderers', () => {
const container = renderToolLine(
makeTool({
toolName: 'ReadFile',
status: 'completed',
startTime: 1_000,
endTime: 6_000,
}),
{},
{ renderToolHeaderExtra: (info) => <span>{info.elapsed}</span> },
);
expect(container.textContent).toContain('5s');
});
it.each([
['completed', undefined],
['failed', 'Agent process failed'],
] as const)('shows meta for a %s agent', (status, reason) => {
const container = renderToolLine(
makeTool({
toolName: 'Task',
status,
startTime: 1_000,
endTime: 6_000,
rawOutput: {
type: 'task_execution',
executionSummary: { outputTokens: 1_200 },
reason,
},
}),
);
expect(container.textContent).toContain('5s');
expect(container.textContent).toContain('1.2k tokens');
if (reason) expect(container.textContent).toContain(reason);
});
it('shows a tool-kind icon on every expanded group row', () => {
const container = renderToolGroup([
makeTool({ callId: 'read', toolName: 'ReadFile' }),
makeTool({ callId: 'edit', toolName: 'edit' }),
]);
const summary = container.querySelector('button');
act(() => summary?.click());
const rows = container.querySelectorAll(
'[class*="chatSummaryGroup"] [class*="lineMain"]',
);
expect(rows).toHaveLength(2);
for (const row of rows) {
expect(
row.querySelector('svg[class*="chatSummaryToolIcon"]'),
).not.toBeNull();
}
});
it('keeps the failed label out of the collapsed chat summary', () => {
const container = renderToolGroup([
makeTool({ toolName: 'Shell', status: 'failed' }),
]);
const summary = container.querySelector('button');
expect(summary?.textContent).toContain('Shell');
expect(summary?.textContent).not.toContain('Failed');
expect(summary?.querySelector('[class*="iconError"]')).toBeNull();
});
it('shows the error icon in a failed tool line header', () => {
const container = renderToolLine(
makeTool({ toolName: 'Shell', status: 'failed' }),
);
const errorIcon = container.querySelector('[class*="iconError"]');
expect(errorIcon).not.toBeNull();
expect(errorIcon?.getAttribute('role')).toBe('img');
expect(errorIcon?.getAttribute('aria-label')).toBe('Failed');
expect(errorIcon?.querySelector('svg')).not.toBeNull();
expect(container.textContent).not.toContain('Failed');
});
it('shows an error icon instead of the failed label on expanded tool rows', () => {
const container = renderToolGroup([
makeTool({
toolName: 'Shell',
status: 'failed',
content: [{ type: 'content', content: { text: 'boom' } }],
}),
makeTool({ callId: 'call-2', toolName: 'Grep', status: 'completed' }),
]);
const summary = container.querySelector('button') as HTMLButtonElement;
act(() => summary.click());
const errorIcon = container.querySelector('[class*="iconError"]');
expect(errorIcon).not.toBeNull();
expect(errorIcon?.querySelector('svg')).not.toBeNull();
expect(errorIcon?.textContent).not.toContain('Failed');
});
it('shows an error icon in the expanded single-tool card title', () => {
const container = renderToolGroup([
makeTool({
toolName: 'Shell',
status: 'failed',
content: [{ type: 'content', content: { text: 'boom' } }],
}),
]);
const summary = container.querySelector('button') as HTMLButtonElement;
act(() => summary.click());
const titleRow = container.querySelector('[class*="expandedCardTitleRow"]');
expect(titleRow).not.toBeNull();
expect(titleRow?.querySelector('[class*="iconError"] svg')).not.toBeNull();
expect(titleRow?.textContent).not.toContain('Failed');
});
it('renders no status icon in the expanded completed tool card title', () => {
const container = renderToolGroup([
makeTool({
toolName: 'Shell',
status: 'completed',
content: [{ type: 'content', content: { text: 'ok' } }],
}),
]);
const summary = container.querySelector('button') as HTMLButtonElement;
act(() => summary.click());
const titleRow = container.querySelector('[class*="expandedCardTitleRow"]');
expect(titleRow).not.toBeNull();
expect(titleRow?.querySelector('[class*="iconError"]')).toBeNull();
});
it('shows an error icon in the expanded failed todo card title', () => {
const container = renderToolGroup([
makeTool({
toolName: 'todo_write',
status: 'failed',
args: {
todos: [{ id: '1', content: 'Check UI', status: 'in_progress' }],
},
}),