import { act, fireEvent, render, screen, waitFor, within } from "@testing-library/react"; import userEvent from "@testing-library/user-event"; import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; import { SubagentTasksProvider, SubagentThreadMessages as ThreadMessages } from "@/components/thread/SubagentTasks"; import { buildDisplayUnits, unitKeysForDisplay } from "@/components/thread/ThreadMessages"; import { ThreadVisibilityContext } from "@/hooks/useThreadVisibility"; import { setAppLanguage } from "@/i18n"; import { DEFAULT_LOCAL_PREFS, LOCAL_PREFS_STORAGE_KEY, writeLocalPreferences } from "@/lib/local-preferences"; import type { ConnectionStatus, InboundEvent, SubagentTaskSnapshot, UIMessage } from "@/lib/types"; const requestMutation = vi.fn(); const chatHandlers = new Map void>>(); const statusHandlers = new Set<(status: ConnectionStatus) => void>(); const client = { requestMutation, onChat: vi.fn((chatId: string, handler: (event: InboundEvent) => void) => { const handlers = chatHandlers.get(chatId) ?? new Set(); chatHandlers.set(chatId, handlers); handlers.add(handler); return () => { handlers.delete(handler); }; }), onStatus: vi.fn((handler: (status: ConnectionStatus) => void) => { statusHandlers.add(handler); handler("open"); return () => { statusHandlers.delete(handler); }; }), }; function emitTask(snapshot: SubagentTaskSnapshot, chatId = "a") { act(() => { chatHandlers.get(chatId)?.forEach((handler) => handler({ event: "subagent_task", chat_id: chatId, task: snapshot })); }); } const messages: UIMessage[] = [ { id: "prompt-a", role: "user", content: "Inspect config", turnId: "turn-a" }, { id: "answer-a", role: "assistant", content: "Main answer", turnId: "turn-a" }, ]; function task(overrides: Partial = {}): SubagentTaskSnapshot { return { task_id: "task-1", revision: 1, label: "Config check", task_description: "Check configuration values", state: "running", phase: "awaiting_model", elapsed_seconds: 2, iteration: 1, tool_events: [], usage: null, receipts: {}, result: null, partial: false, stop_reason: null, error: null, origin_turn_id: "turn-a", origin_message_id: null, created_at: 100, completed_at: null, ...overrides, }; } function response(tasks: SubagentTaskSnapshot[]): Response { return new Response(JSON.stringify({ tasks }), { headers: { "content-type": "application/json" } }); } function childThread(answer: string, active = true): Response { return new Response(JSON.stringify({ schemaVersion: 3, projection: "events", active_turn_id: active ? "child-turn" : null, events: [ { event: "user_message", chat_id: "task-1", starts_turn: true, projection_id: "child-prompt", turn_id: "child-turn", text: "Inspect configuration in the child session", created_at_ms: 100000 }, { event: "message", chat_id: "task-1", projection_id: "child-tool", turn_id: "child-turn", kind: "tool_hint", text: 'read_file({"path":"config.py"})', tool_events: [{ call_id: "read-1", name: "read_file", arguments: { path: "config.py" }, phase: "end", result: "configuration values" }] }, { event: "message", chat_id: "task-1", projection_id: "child-answer", turn_id: "child-turn", text: answer }, ], }), { headers: { "content-type": "application/json" } }); } function layout({ enabled = true, liveEvents = true, historyEnabled = false, sessionKey = "websocket:a", visible = true, threadMessages = messages } = {}) { return