forked from tinyhumansai/openhuman
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathToolTimelineBlock.test.tsx
More file actions
571 lines (530 loc) · 21.3 KB
/
Copy pathToolTimelineBlock.test.tsx
File metadata and controls
571 lines (530 loc) · 21.3 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
import { fireEvent, render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { Provider } from 'react-redux';
import { describe, expect, it, vi } from 'vitest';
import { store } from '../../../../store';
import type { ToolTimelineEntry } from '../../../../store/chatRuntimeSlice';
import { SubagentActivityBlock, ToolTimelineBlock } from '../ToolTimelineBlock';
// #1122 — guards the parent-thread live subagent rendering. The block
// always expands subagent rows so the activity stays visible while the
// run is in flight, even before the subagent emits any prompt detail.
function renderInStore(ui: React.ReactNode) {
return render(<Provider store={store}>{ui}</Provider>);
}
describe('SubagentActivityBlock', () => {
it('renders mode + dedicated-thread + child-turn pills', () => {
renderInStore(
<SubagentActivityBlock
subagent={{
taskId: 't',
agentId: 'researcher',
mode: 'typed',
dedicatedThread: true,
childIteration: 2,
childMaxIterations: 5,
toolCalls: [],
}}
/>
);
const block = screen.getByTestId('subagent-activity');
expect(block.textContent).toContain('typed');
expect(block.textContent).toContain('worker thread');
expect(block.textContent).toContain('turn 2/5');
});
it('renders "step N" when childMaxIterations is null (extended policy)', () => {
renderInStore(
<SubagentActivityBlock
subagent={{ taskId: 't', agentId: 'code_executor', childIteration: 7, toolCalls: [] }}
/>
);
const block = screen.getByTestId('subagent-activity');
expect(block.textContent).toContain('step 7');
expect(block.textContent).not.toContain('/');
});
it('renders final-run statistics on a completed sub-agent', () => {
renderInStore(
<SubagentActivityBlock
subagent={{
taskId: 't',
agentId: 'researcher',
iterations: 3,
elapsedMs: 4200,
toolCalls: [],
}}
/>
);
const block = screen.getByTestId('subagent-activity');
expect(block.textContent).toContain('3 turns');
expect(block.textContent).toContain('4.2s');
});
it('renders one row per child tool call with formatted names, status + timing', () => {
renderInStore(
<SubagentActivityBlock
subagent={{
taskId: 't',
agentId: 'researcher',
toolCalls: [
{ callId: 'c1', toolName: 'web_search', status: 'success', elapsedMs: 312 },
{ callId: 'c2', toolName: 'composio_execute', status: 'running', iteration: 2 },
{ callId: 'c3', toolName: 'file_read', status: 'error', elapsedMs: 50 },
],
}}
/>
);
const calls = screen.getAllByTestId('subagent-tool-call');
expect(calls).toHaveLength(3);
// Human labels + timing, with status as a tinted "Done" / "Failed" /
// "Running" tag instead of a bare ✓/✕ glyph or the raw lowercase word.
expect(calls[0].textContent).toContain('Searching the web');
expect(calls[0].textContent).toContain('Done');
expect(calls[0].textContent).toContain('312ms');
expect(calls[1].textContent).toContain('Composio Execute');
expect(calls[1].textContent).toContain('Running');
expect(calls[1].textContent).not.toContain('·t2');
expect(calls[2].textContent).toContain('Reading file');
expect(calls[2].textContent).toContain('Failed');
expect(calls[2].textContent).toContain('50ms');
});
it('labels cancelled / awaiting-user calls distinctly (not the green "Done" pill)', () => {
renderInStore(
<SubagentActivityBlock
subagent={{
taskId: 't',
agentId: 'researcher',
toolCalls: [
{ callId: 'c1', toolName: 'web_search', status: 'cancelled', elapsedMs: 10 },
{ callId: 'c2', toolName: 'file_read', status: 'awaiting_user' },
],
}}
/>
);
const calls = screen.getAllByTestId('subagent-tool-call');
expect(calls).toHaveLength(2);
// A cancelled / awaiting-user call must NOT read as a successful "Done" step.
expect(calls[0].textContent).toContain('Cancelled');
expect(calls[0].textContent).not.toContain('Done');
expect(calls[1].textContent).toContain('Awaiting input');
expect(calls[1].textContent).not.toContain('Done');
});
it('prefers the server-supplied label + contextual detail for a child tool call', () => {
renderInStore(
<SubagentActivityBlock
subagent={{
taskId: 't',
agentId: 'researcher',
toolCalls: [
{
callId: 'c1',
toolName: 'GMAIL_READ_MESSAGES',
status: 'success',
displayName: 'Reading messages',
detail: 'steven@gmail.com',
},
],
}}
/>
);
const row = screen.getByTestId('subagent-tool-call');
expect(row.textContent).toContain('Reading messages');
expect(row.textContent).toContain('steven@gmail.com');
// Never the raw snake_case slug.
expect(row.textContent).not.toContain('GMAIL_READ_MESSAGES');
});
it('renders every thought inline as quoted prose (reasoning + narration)', () => {
renderInStore(
<SubagentActivityBlock
subagent={{
taskId: 't',
agentId: 'researcher',
toolCalls: [],
transcript: [
{ kind: 'thinking', iteration: 1, text: 'pondering the request' },
{ kind: 'text', iteration: 1, text: 'Here is what I found so far about the topic' },
],
}}
/>
);
const thoughts = screen.getAllByTestId('subagent-thought');
// Both reasoning and visible narration surface as their own prose block —
// shown directly, with no "Thoughts" heading.
expect(thoughts).toHaveLength(2);
expect(thoughts[0].textContent).toContain('pondering the request');
expect(thoughts[0].textContent).not.toContain('Thoughts');
expect(thoughts[1].textContent).toContain('Here is what I found so far');
});
it('renders thoughts and tool calls interleaved in transcript order', () => {
renderInStore(
<SubagentActivityBlock
subagent={{
taskId: 't',
agentId: 'researcher',
toolCalls: [],
transcript: [
{ kind: 'thinking', iteration: 1, text: 'I should search the web first' },
{ kind: 'tool', iteration: 1, callId: 'c1', toolName: 'web_search', status: 'success' },
{ kind: 'text', iteration: 1, text: 'Found three relevant results' },
],
}}
/>
);
const rows = screen.getByTestId('subagent-transcript').children;
// Order is preserved: thought → tool → thought.
expect(rows[0]).toHaveAttribute('data-testid', 'subagent-thought');
expect(rows[0].textContent).toContain('I should search the web first');
expect(rows[1]).toHaveAttribute('data-testid', 'subagent-tool-call');
expect(rows[1].textContent).toContain('Searching the web');
expect(rows[2]).toHaveAttribute('data-testid', 'subagent-thought');
expect(rows[2].textContent).toContain('Found three relevant results');
});
it('shows a thought directly as prose — no heading, no collapse', () => {
renderInStore(
<SubagentActivityBlock
subagent={{
taskId: 't',
agentId: 'researcher',
toolCalls: [],
transcript: [{ kind: 'thinking', iteration: 1, text: 'weighing the options' }],
}}
/>
);
const thought = screen.getByTestId('subagent-thought');
// No collapsible <details>/<summary> and no "Thoughts" heading — the text
// is shown directly.
expect(thought.tagName).not.toBe('DETAILS');
expect(thought.querySelector('summary')).toBeNull();
expect(thought.textContent).toContain('weighing the options');
expect(thought.textContent).not.toContain('Thoughts');
expect(thought.textContent).not.toContain('💭');
});
it('strips a leaked <tool_call> envelope from the thought text', () => {
renderInStore(
<SubagentActivityBlock
subagent={{
taskId: 't',
agentId: 'researcher',
toolCalls: [],
transcript: [
{
kind: 'text',
iteration: 1,
text: 'I\'ll search your Notion for that. <tool_call> {"name": "NOTION_SEARCH", "arguments": {"query": "audit"}} </tool_call>',
},
],
}}
/>
);
const thought = screen.getByTestId('subagent-thought');
expect(thought.textContent).toContain("I'll search your Notion for that.");
// The raw tool-call envelope must not leak into the displayed prose.
expect(thought.textContent).not.toContain('tool_call');
expect(thought.textContent).not.toContain('NOTION_SEARCH');
});
it('skips an all-whitespace thought delta', () => {
renderInStore(
<SubagentActivityBlock
subagent={{
taskId: 't',
agentId: 'researcher',
toolCalls: [],
transcript: [{ kind: 'thinking', iteration: 1, text: ' \n ' }],
}}
/>
);
expect(screen.queryByTestId('subagent-thought')).toBeNull();
});
it('renders the view-processing button only when onView is provided', async () => {
const onView = vi.fn();
const { rerender } = renderInStore(
<SubagentActivityBlock subagent={{ taskId: 't', agentId: 'researcher', toolCalls: [] }} />
);
expect(screen.queryByTestId('subagent-view-processing')).toBeNull();
rerender(
<Provider store={store}>
<SubagentActivityBlock
subagent={{ taskId: 't', agentId: 'researcher', toolCalls: [] }}
onView={onView}
/>
</Provider>
);
const btn = screen.getByTestId('subagent-view-processing');
await userEvent.click(btn);
expect(onView).toHaveBeenCalledTimes(1);
});
it('renders the inline worktree block + actions when worktreePath is set (#3376)', () => {
renderInStore(
<SubagentActivityBlock
subagent={{
taskId: 't',
agentId: 'coder',
toolCalls: [],
worktreePath: '/r/.claude/worktrees/worker-a',
changedFiles: ['src/lib.rs'],
isDirty: true,
}}
/>
);
const block = screen.getByTestId('subagent-worktree');
expect(block).toBeInTheDocument();
// Compact label shows the basename, not the full path.
expect(block).toHaveTextContent('worker-a');
expect(screen.getByTestId('worktree-actions')).toBeInTheDocument();
expect(screen.getByTestId('worktree-remove')).toBeInTheDocument();
});
it('omits the worktree block for a non-isolated subagent', () => {
renderInStore(
<SubagentActivityBlock subagent={{ taskId: 't', agentId: 'researcher', toolCalls: [] }} />
);
expect(screen.queryByTestId('subagent-worktree')).toBeNull();
});
});
describe('ToolTimelineBlock — agentic task insights surface', () => {
it('wraps rows in the "Agentic task insights" group and conveys run state on the name', () => {
const entries: ToolTimelineEntry[] = [
{ id: 'r', name: 'web_search', round: 1, status: 'running', argsBuffer: '{"query":"f1"}' },
{
id: 'd',
name: 'file_read',
round: 1,
status: 'success',
argsBuffer: '{"path":"/a/b.txt"}',
},
];
renderInStore(<ToolTimelineBlock entries={entries} />);
const group = screen.getByTestId('agent-task-insights');
expect(group).toBeInTheDocument();
// Static section label — NOT a duplicate "Working…" string (the live
// state lives on the pulsing row names, not the header).
expect(group.textContent).toContain('Agentic task insights');
expect(group.textContent).not.toContain('Working');
// Two rows on the timeline rail.
expect(screen.getAllByTestId('agent-timeline-row')).toHaveLength(2);
// Running row name pulses; done row name is solid.
const running = screen.getByText('Searching: f1');
const done = screen.getByText('Reading file');
expect(running.className).toContain('animate-pulse');
expect(done.className).not.toContain('animate-pulse');
});
it('renders nothing for an empty timeline', () => {
const { container } = renderInStore(<ToolTimelineBlock entries={[]} />);
expect(container.querySelector('[data-testid="agent-task-insights"]')).toBeNull();
});
it('renders the tool result output inside the expanded row', () => {
const entries: ToolTimelineEntry[] = [
{
id: 'd',
name: 'web_search',
round: 1,
status: 'success',
argsBuffer: '{"query":"f1"}',
result: 'Top result: https://openhuman.dev',
},
];
renderInStore(<ToolTimelineBlock entries={entries} expandAllRows />);
const output = screen.getByTestId('tool-result-output');
expect(output.textContent).toContain('Top result: https://openhuman.dev');
});
it('makes a row expandable on a result alone and omits the block without one', () => {
const entries: ToolTimelineEntry[] = [
// No argsBuffer / detail / subagent — the result is the only body.
{ id: 'a', name: 'run_code', round: 1, status: 'success', result: 'exit 0' },
{ id: 'b', name: 'run_code', round: 2, status: 'success' },
];
renderInStore(<ToolTimelineBlock entries={entries} expandAllRows />);
const outputs = screen.getAllByTestId('tool-result-output');
expect(outputs).toHaveLength(1);
expect(outputs[0].textContent).toBe('exit 0');
});
it('renders the parent live response inside the panel under a Response heading', () => {
const entries: ToolTimelineEntry[] = [
{ id: 'r', name: 'web_search', round: 1, status: 'running', argsBuffer: '{"query":"f1"}' },
];
renderInStore(
<ToolTimelineBlock
entries={entries}
liveResponse="Let me check your Notion for that audit file."
/>
);
const resp = screen.getByTestId('agent-live-response');
expect(resp.textContent).toContain('Response');
expect(resp.textContent).toContain('Let me check your Notion for that audit file.');
});
it('omits the Response block when there is no live response', () => {
const entries: ToolTimelineEntry[] = [
{ id: 'r', name: 'web_search', round: 1, status: 'running' },
];
renderInStore(<ToolTimelineBlock entries={entries} />);
expect(screen.queryByTestId('agent-live-response')).toBeNull();
});
it('strips a leaked <tool_call> envelope from the live response', () => {
const entries: ToolTimelineEntry[] = [
{ id: 'r', name: 'web_search', round: 1, status: 'running' },
];
renderInStore(
<ToolTimelineBlock
entries={entries}
liveResponse={'Searching now. <tool_call> {"name":"X"} </tool_call>'}
/>
);
const resp = screen.getByTestId('agent-live-response');
expect(resp.textContent).toContain('Searching now.');
expect(resp.textContent).not.toContain('tool_call');
});
});
describe('ToolTimelineBlock — subagent rendering', () => {
it('expands a subagent row even without prompt detail and shows child tool calls', () => {
const entry: ToolTimelineEntry = {
id: 'tid:subagent:sub-1:researcher',
name: 'subagent:researcher',
round: 1,
status: 'running',
subagent: {
taskId: 'sub-1',
agentId: 'researcher',
mode: 'typed',
childIteration: 1,
childMaxIterations: 5,
toolCalls: [{ callId: 'cc-1', toolName: 'web_search', status: 'running', iteration: 1 }],
},
};
renderInStore(<ToolTimelineBlock entries={[entry]} />);
const calls = screen.getAllByTestId('subagent-tool-call');
expect(calls).toHaveLength(1);
expect(calls[0].textContent).toContain('Searching the web');
expect(screen.getByTestId('subagent-activity').textContent).toContain('turn 1/5');
});
it('renders a non-subagent row without crashing when there is no detail', () => {
const entry: ToolTimelineEntry = {
id: 'plain',
name: 'list_threads',
round: 0,
status: 'success',
};
renderInStore(<ToolTimelineBlock entries={[entry]} />);
// Plain rows with no detail collapse to a flat label + status pill.
expect(screen.queryByTestId('subagent-activity')).toBeNull();
});
});
// Issue #1624: when a parent timeline entry contains a worker_thread_ref
// envelope, ToolTimelineBlock must propagate the entry's status to the
// rendered WorkerThreadRefCard so the card's badge stays in lockstep
// with the surrounding `<details>` status pill — both are mutated by
// the same subagent_spawned / subagent_completed / subagent_failed
// socket events.
describe('ToolTimelineBlock — worker thread ref status propagation', () => {
const WORKER_REF_DETAIL = `summary text\n[worker_thread_ref]\n${JSON.stringify({
thread_id: 't-worker-1',
label: 'researcher',
agent_id: 'researcher',
task_id: 'task-42',
})}\n[/worker_thread_ref]`;
function entryWithStatus(status: ToolTimelineEntry['status']): ToolTimelineEntry {
return {
id: `tid:subagent:task-42:researcher:${status}`,
name: 'subagent:researcher',
round: 1,
status,
detail: WORKER_REF_DETAIL,
};
}
it('passes `running` to the card when the parent entry is in flight', () => {
renderInStore(<ToolTimelineBlock entries={[entryWithStatus('running')]} />);
const badge = screen.getByTestId('worker-thread-status-badge');
expect(badge.getAttribute('data-status')).toBe('running');
});
it('passes `completed` to the card when the parent entry succeeds', () => {
renderInStore(<ToolTimelineBlock entries={[entryWithStatus('success')]} />);
const badge = screen.getByTestId('worker-thread-status-badge');
expect(badge.getAttribute('data-status')).toBe('completed');
});
it('passes `failed` to the card when the parent entry errors', () => {
renderInStore(<ToolTimelineBlock entries={[entryWithStatus('error')]} />);
const badge = screen.getByTestId('worker-thread-status-badge');
expect(badge.getAttribute('data-status')).toBe('failed');
});
// Defensive fallback: if the entry arrives with an unrecognised status
// (e.g. the union grows in the future, or a malformed payload slips
// through), the card is rendered as label-only so it can never display a
// misleading lifecycle state. The status badge must be absent in that case.
it('omits the status badge when the parent entry has an unknown status', () => {
const malformed = {
...entryWithStatus('success'),
status: 'queued' as unknown as ToolTimelineEntry['status'],
};
renderInStore(<ToolTimelineBlock entries={[malformed]} />);
expect(screen.queryByTestId('worker-thread-status-badge')).toBeNull();
});
});
describe('ToolTimelineBlock — compact chat mode (onViewDetails)', () => {
const entries: ToolTimelineEntry[] = [
// A finished step.
{
id: 'tl-1',
name: 'agent_prepare_context',
round: 1,
status: 'success',
detail: 'fetch X',
result: 'Prepared context from 3 sources.',
},
// The currently-running sub-agent (latest running).
{
id: 'sa-1',
name: 'subagent:researcher',
round: 1,
status: 'running',
subagent: {
taskId: 'task-1',
agentId: 'researcher',
toolCalls: [],
transcript: [{ kind: 'thinking', iteration: 1, text: 'pondering' }],
},
},
];
it('collapses finished steps to a "View details" link but keeps the running step expanded inline', () => {
const onViewDetails = vi.fn();
renderInStore(<ToolTimelineBlock entries={entries} onViewDetails={onViewDetails} />);
// Only the finished step collapses to a "View details →" link.
const links = screen.getAllByTestId('view-details');
expect(links).toHaveLength(1);
// The currently-running sub-agent stays expanded inline in the main UI
// (its activity is visible) — and shows no "View details" link itself.
const activity = screen.getByTestId('subagent-activity');
expect(activity.textContent).toContain('pondering');
expect(screen.getByTestId('tool-result-output').textContent).toContain(
'Prepared context from 3 sources.'
);
// Clicking the finished step's link opens the full-run panel.
fireEvent.click(links[0]);
expect(onViewDetails).toHaveBeenCalledTimes(1);
});
it('collapses an already-finished sub-agent (no longer running) to a "View details" link', () => {
const onViewDetails = vi.fn();
renderInStore(
<ToolTimelineBlock
entries={[
{
id: 'sa-done',
name: 'subagent:researcher',
round: 1,
status: 'success',
subagent: {
taskId: 'task-2',
agentId: 'researcher',
toolCalls: [],
transcript: [{ kind: 'thinking', iteration: 1, text: 'done thinking' }],
},
},
]}
onViewDetails={onViewDetails}
/>
);
// No running step → the finished sub-agent collapses (no inline activity).
expect(screen.getByTestId('view-details')).toBeInTheDocument();
expect(screen.queryByTestId('subagent-activity')).toBeNull();
});
it('still expands inline (no compact link) when onViewDetails is omitted (panel mode)', () => {
renderInStore(<ToolTimelineBlock entries={entries} expandAllRows />);
// Panel/expandable path: sub-agent activity is shown, no "View details" link.
expect(screen.getByTestId('subagent-activity')).toBeInTheDocument();
expect(screen.queryByTestId('view-details')).toBeNull();
});
});