git clone https://garage44.eu/jeroen/midas.git.. /web/src/lib/tools.test.ts
import {expect, test} from 'bun:test'
import {toolCalls} from '@/lib/tools'
import type {LogItem} from '@/lib/stream'
const item = (n: number, fields: Partial<LogItem>): LogItem => ({n, at: n, kind: 'tool', ...fields}) as LogItem
test("a subagent's calls go under the call that started it; updates fold in", () => {
const {calls, children} = toolCalls([
item(1, {id: 'a', title: 'Task'}),
item(2, {id: 'b', title: 'Read x', parent: 'a'}),
item(3, {id: 'c', title: 'Grep', _meta: {parentToolCallId: 'a'}} as Partial<LogItem>),
item(4, {kind: 'tool_update', id: 'b', status: 'completed'}),
item(5, {id: 'd', title: 'Bash', parent: 'gone'}),
])
expect(children.get('a')?.map((t) => t.id)).toEqual(['b', 'c'])
expect(calls.get('b')?.status).toBe('completed')
// Its parent is not in the log: it stays a call of its own.
expect([...children.values()].flat().some((t) => t.id === 'd')).toBe(false)
})
test('without the field, nothing nests', () => {
expect(toolCalls([item(1, {id: 'a'}), item(2, {id: 'b'})]).children.size).toBe(0)
})