Open History to see the last commit
1import { act, cloneElement, type ReactElement, type ReactNode } from "react";
2import { create, type ReactTestRenderer } from "react-test-renderer";
3import { EnvironmentId, ThreadId } from "@t3tools/contracts";
4import * as DateTime from "effect/DateTime";
5import { afterEach, expect, it, vi } from "vite-plus/test";
6
7const state = vi.hoisted(() => ({
8 projection: null as unknown,
9 navigate: vi.fn(),
10 shells: [] as unknown[],
11 projects: [] as unknown[],
12 configs: new Map<string, unknown>(),
13 showTooltips: false,
14}));
15
16vi.mock("@tanstack/react-router", () => ({ useNavigate: () => state.navigate }));
17vi.mock("../../state/entities", () => ({
18 useThreadProjection: () => ({ projection: state.projection }),
19 useThreadShells: () => state.shells,
20 useProjects: () => state.projects,
21 useServerConfigs: () => state.configs,
22}));
23vi.mock("../../lib/archivedThreadsState", () => ({
24 useArchivedThreadSnapshots: () => ({ snapshots: [] }),
25}));
26vi.mock("../../state/use-atom-command", () => ({ useAtomCommand: () => vi.fn() }));
27vi.mock("../ui/tooltip", () => ({
28 Tooltip: ({ children }: { children: ReactNode }) => children,
29 TooltipTrigger: ({ render, children }: { render: ReactElement; children: ReactNode }) =>
30 cloneElement(render, {}, children),
31 TooltipPopup: ({ children }: { children: ReactNode }) => (state.showTooltips ? children : null),
32}));
33
34import { ThreadRelationshipsPanel } from "./ThreadRelationshipsControl";
35
36let renderer: ReactTestRenderer;
37
38afterEach(async () => {
39 await act(async () => renderer?.unmount());
40 vi.unstubAllGlobals();
41 state.shells = [];
42 state.projects = [];
43 state.configs.clear();
44 state.showTooltips = false;
45});
46
47it("shows the matching child agent details and refreshes them when the agent settles", async () => {
48 vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
49 const agent = {
50 id: "agent-1",
51 driver: "codex",
52 providerInstanceId: "codex",
53 childThreadId: "child-1",
54 title: "Checker",
55 prompt: "Check the change",
56 model: "gpt-5.4",
57 status: "running",
58 progress: "Running checks",
59 result: null,
60 startedAt: DateTime.makeUnsafe("2026-09-16T12:00:00Z"),
61 completedAt: null,
62 updatedAt: DateTime.makeUnsafe("2026-09-16T12:00:00Z"),
63 };
64 const projection = {
65 thread: {
66 id: "parent",
67 lineage: { relationshipToParent: null },
68 activeProviderThreadId: null,
69 },
70 runs: [],
71 providerThreads: [],
72 providerSessions: [],
73 contextTransfers: [],
74 subagents: [
75 { ...agent, id: "unlinked", childThreadId: null, title: "Unlinked agent" },
76 agent,
77 { ...agent, id: "agent-2", childThreadId: "child-2", title: "Worker", model: "gpt-5.3" },
78 ],
79 };
80 state.projection = projection;
81 const panel = (
82 <ThreadRelationshipsPanel
83 environmentId={EnvironmentId.make("test")}
84 threadId={ThreadId.make("parent")}
85 />
86 );
87 await act(async () => {
88 renderer = create(panel);
89 });
90 const text = () =>
91 renderer.root
92 .findAll((node) => typeof node.type === "string")
93 .flatMap((node) => node.children.filter((child) => typeof child === "string"))
94 .join(" ")
95 .replace(/\s+/g, " ");
96 expect(text()).toContain("Checker");
97 expect(text()).toContain("Lineage · 3 running");
98 expect(text()).toContain("running");
99 expect(text()).not.toContain("gpt-5.4");
100 expect(text()).not.toContain("gpt-5.3");
101 expect(text()).not.toContain("tok");
102 expect(text()).not.toContain("Unlinked agent");
103 expect(text()).not.toContain("Active agents");
104 expect(renderer.root.findAllByProps({ type: "button", "aria-expanded": true })).toHaveLength(0);
105
106 state.projection = {
107 ...projection,
108 subagents: [
109 {
110 ...agent,
111 status: "completed",
112 progress: undefined,
113 result: "All checks passed",
114 completedAt: DateTime.makeUnsafe("2026-09-16T12:02:15Z"),
115 },
116 ],
117 };
118 await act(async () => renderer.update(cloneElement(panel)));
119 expect(renderer.root.findByType("h3").children).toEqual(["Lineage"]);
120 expect(text()).toContain("Previous agents (1)");
121 expect(text()).not.toContain("Checker");
122 await act(async () =>
123 renderer.root.findByProps({ type: "button", "aria-expanded": false }).props.onClick(),
124 );
125 expect(text()).toContain("Checker");
126 expect(text()).toContain("2m 15s");
127 expect(text()).not.toContain("(1)");
128 expect(text()).toContain("completed");
129 expect(text()).not.toContain("running");
130 expect(text()).not.toContain("Worker");
131 await act(async () =>
132 renderer.root.findByProps({ type: "button", "aria-expanded": true }).props.onClick(),
133 );
134 expect(text()).not.toContain("Checker");
135 expect(text()).toContain("Previous agents (1)");
136 await act(async () =>
137 renderer.root.findByProps({ type: "button", "aria-expanded": false }).props.onClick(),
138 );
139 expect(text()).toContain("Checker");
140
141 state.projection = {
142 ...projection,
143 subagents: Array.from({ length: 8 }, (_, index) => ({
144 ...agent,
145 id: `running-agent-${index}`,
146 childThreadId: `running-child-${index}`,
147 })),
148 };
149 await act(async () => renderer.update(cloneElement(panel)));
150 expect(text()).toContain("Lineage · 8 running");
151
152 state.projection = {
153 ...projection,
154 subagents: Array.from({ length: 8 }, (_, index) => ({
155 ...agent,
156 id: `old-agent-${index}`,
157 childThreadId: `old-child-${index}`,
158 status: index === 7 ? "failed" : "completed",
159 title: `Old agent ${index}`,
160 result: index === 7 ? "Earlier build failed" : "Done",
161 completedAt: DateTime.makeUnsafe("2026-09-16T12:02:15Z"),
162 })),
163 };
164 await act(async () => renderer.update(cloneElement(panel)));
165 expect(text()).toContain("1 failed");
166 expect(text()).not.toContain("Old agent 7");
167 await act(async () =>
168 renderer.root
169 .findAllByType("button")
170 .find((button) => button.children.includes("Show "))!
171 .props.onClick(),
172 );
173 expect(text()).toContain("Old agent 7");
174
175 state.projection = {
176 ...projection,
177 subagents: [{ ...agent, childThreadId: null }],
178 };
179 await act(async () => renderer.update(cloneElement(panel)));
180 expect(text()).toContain("Lineage · 1 running");
181});
182
183it("shows readable models and only differing workspace details in agent tooltips", async () => {
184 vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
185 state.showTooltips = true;
186 const parent = {
187 id: "parent",
188 projectId: "main",
189 worktreePath: null,
190 lineage: {},
191 activeProviderThreadId: null,
192 };
193 const child = {
194 id: "child",
195 projectId: "main",
196 worktreePath: null as string | null,
197 branch: null as string | null,
198 title: "Worker",
199 modelSelection: { instanceId: "codex", model: "gpt-5.4" },
200 lineage: { parentThreadId: "parent", relationshipToParent: "subagent" },
201 };
202 state.projects = [
203 { id: "main", environmentId: "test", title: "Main", workspaceRoot: "/main" },
204 {
205 id: "other",
206 environmentId: "elsewhere",
207 title: "Wrong environment",
208 workspaceRoot: "/wrong",
209 },
210 { id: "other", environmentId: "test", title: "Other project", workspaceRoot: "/other" },
211 ];
212 state.shells = [{ environmentId: "test", source: child }];
213 state.configs.set("test", {
214 providers: [
215 {
216 instanceId: "codex",
217 driver: "codex",
218 models: [
219 { slug: "gpt-5.4", name: "My GPT model", shortName: "My GPT", aliases: ["model-alias"] },
220 ],
221 },
222 ],
223 });
224 const projection = {
225 thread: parent,
226 runs: [],
227 providerThreads: [],
228 providerSessions: [],
229 contextTransfers: [],
230 subagents: [
231 {
232 id: "agent",
233 childThreadId: "child",
234 driver: "codex",
235 providerInstanceId: "codex",
236 title: "Worker",
237 prompt: "Check",
238 model: "gpt-5.4",
239 status: "pending",
240 startedAt: null,
241 completedAt: null,
242 updatedAt: DateTime.makeUnsafe("2026-09-16T12:00:00Z"),
243 },
244 ],
245 };
246 state.projection = projection;
247 const panel = (
248 <ThreadRelationshipsPanel
249 environmentId={EnvironmentId.make("test")}
250 threadId={ThreadId.make("parent")}
251 />
252 );
253 await act(async () => {
254 renderer = create(panel);
255 });
256 const text = () =>
257 renderer.root
258 .findAll((node) => typeof node.type === "string")
259 .flatMap((node) => node.children.filter((child) => typeof child === "string"))
260 .join(" ");
261 expect(text()).toContain("My GPT");
262 expect(text()).not.toContain("Tokens");
263 expect(text()).not.toContain("Open subagent");
264 expect(text()).not.toContain("Project");
265 expect(text()).not.toContain("Worktree");
266 expect(text()).not.toContain("Workspace");
267
268 for (const [model, expected] of [
269 [null, "My GPT"],
270 ["", "My GPT"],
271 [" ", "My GPT"],
272 ["model-alias", "My GPT"],
273 ["gpt-5.5", "GPT-5.5"],
274 ["custom/model-v1", "custom/model-v1"],
275 ] as const) {
276 state.projection = {
277 ...projection,
278 subagents: [{ ...projection.subagents[0], model }],
279 };
280 await act(async () => renderer.update(cloneElement(panel)));
281 expect(text()).toContain(expected);
282 expect(text()).not.toContain("Unknown");
283 }
284
285 state.projection = {
286 ...projection,
287 subagents: [
288 {
289 ...projection.subagents[0],
290 progress: "Checking the latest changes",
291 result: "Old intermediate result",
292 },
293 ],
294 };
295 await act(async () => renderer.update(cloneElement(panel)));
296 expect(text()).toContain("Checking the latest changes");
297 expect(text()).not.toContain("Old intermediate result");
298 const result = "Final checks passed. " + "More detail. ".repeat(50) + "Hidden tail";
299 state.projection = {
300 ...projection,
301 subagents: [
302 { ...projection.subagents[0], status: "failed", progress: "Stale progress", result },
303 ],
304 };
305 await act(async () => renderer.update(cloneElement(panel)));
306 await act(async () =>
307 renderer.root.findByProps({ type: "button", "aria-expanded": false }).props.onClick(),
308 );
309 expect(text()).toContain("Final checks passed.");
310 expect(text()).not.toContain("Stale progress");
311 expect(text()).not.toContain("Hidden tail");
312 expect(text()).not.toContain(result);
313 state.projection = projection;
314
315 child.worktreePath = "/main/worktrees/checker";
316 state.shells = [{ environmentId: "test", source: { ...child } }];
317 await act(async () => renderer.update(cloneElement(panel)));
318 expect(text()).toContain("Worktree");
319 expect(text()).toContain("checker");
320 expect(text()).not.toContain("/main/worktrees");
321 expect(text()).not.toContain("Project");
322
323 child.branch = "fix/checker";
324 state.shells = [{ environmentId: "test", source: { ...child } }];
325 await act(async () => renderer.update(cloneElement(panel)));
326 expect(text()).toContain("Branch");
327 expect(text()).toContain("fix/checker");
328 expect(text()).not.toContain("Worktree");
329 expect(text()).not.toContain("/main/worktrees");
330
331 child.projectId = "other";
332 child.worktreePath = null;
333 child.branch = null;
334 state.shells = [{ environmentId: "test", source: { ...child } }];
335 await act(async () => renderer.update(cloneElement(panel)));
336 expect(text()).toContain("Other project");
337 expect(text()).toContain("Workspace");
338 expect(text()).toContain("other");
339 expect(text()).not.toContain("/other");
340 expect(text()).not.toContain("Wrong environment");
341 expect(text()).not.toContain("Worktree");
342
343 state.shells = [];
344 state.configs.clear();
345 await act(async () => renderer.update(cloneElement(panel)));
346 expect(text()).toContain("GPT-5.4");
347 expect(text()).not.toContain("gpt-5.4");
348 expect(text()).not.toContain("Project");
349 expect(text()).not.toContain("Workspace");
350
351 for (const [driver, model, expected] of [
352 ["codex", "gpt-5.3-codex-spark", "GPT-5.3-Codex-Spark"],
353 ["codex", "custom/model-v2", "custom/model-v2"],
354 ["claudeAgent", "gpt-5.4", "GPT-5.4"],
355 ["claudeAgent", "claude-opus-4-6", "Claude Opus 4.6"],
356 ["cursor", "composer-2", "Composer 2"],
357 ["grok", "grok-4-fast", "Grok 4 Fast"],
358 ["antigravity", "gemini-3.8-flash-high", "Gemini 3.8 Flash High"],
359 ["opencode", "anthropic/claude-sonnet-4-6", "anthropic/Claude Sonnet 4.6"],
360 ["codex", null, "Not reported"],
361 ] as const) {
362 state.projection = {
363 ...projection,
364 subagents: [{ ...projection.subagents[0], driver, model }],
365 };
366 await act(async () => renderer.update(cloneElement(panel)));
367 expect(text()).toContain(expected);
368 }
369});