Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

ThreadRelationshipsControl.agents.test.tsx

497 lines16.4 KB

1import { act, cloneElement, type ReactElement, type ReactNode } from "react";
2import { create, type ReactTestRenderer } from "react-test-renderer";
3import { EnvironmentId, ThreadId, type OrchestrationV2ContextTransfer } 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("Done");
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, "Not reported"],
270 ["", "Not reported"],
271 [" ", "Not reported"],
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 if (!model?.trim()) expect(text()).not.toContain("My GPT");
284 }
285​
286 for (const driver of [
287 "codex",
288 "claudeAgent",
289 "cursor",
290 "opencode",
291 "grok",
292 "antigravity",
293 "pi",
294 "acpRegistry",
295 ]) {
296 state.projection = {
297 ...projection,
298 subagents: [{ ...projection.subagents[0], driver, model: null }],
299 };
300 await act(async () => renderer.update(cloneElement(panel)));
301 expect(text()).toContain("Not reported");
302 expect(text()).not.toContain("My GPT");
303 }
304​
305 state.projection = {
306 ...projection,
307 subagents: [
308 {
309 ...projection.subagents[0],
310 progress: "Checking the latest changes",
311 result: "Old intermediate result",
312 },
313 ],
314 };
315 await act(async () => renderer.update(cloneElement(panel)));
316 expect(text()).toContain("Checking the latest changes");
317 expect(text()).not.toContain("Old intermediate result");
318 const result = "Final checks passed. " + "More detail. ".repeat(50) + "Hidden tail";
319 state.projection = {
320 ...projection,
321 subagents: [
322 { ...projection.subagents[0], status: "failed", progress: "Stale progress", result },
323 ],
324 };
325 await act(async () => renderer.update(cloneElement(panel)));
326 await act(async () =>
327 renderer.root.findByProps({ type: "button", "aria-expanded": false }).props.onClick(),
328 );
329 expect(text()).toContain("Final checks passed.");
330 expect(text()).not.toContain("Stale progress");
331 expect(text()).not.toContain("Hidden tail");
332 expect(text()).not.toContain(result);
333 state.projection = projection;
334​
335 child.worktreePath = "/main/worktrees/checker";
336 state.shells = [{ environmentId: "test", source: { ...child } }];
337 await act(async () => renderer.update(cloneElement(panel)));
338 expect(text()).toContain("Worktree");
339 expect(text()).toContain("checker");
340 expect(text()).not.toContain("/main/worktrees");
341 expect(text()).not.toContain("Project");
342​
343 child.branch = "fix/checker";
344 state.shells = [{ environmentId: "test", source: { ...child } }];
345 await act(async () => renderer.update(cloneElement(panel)));
346 expect(text()).toContain("Branch");
347 expect(text()).toContain("fix/checker");
348 expect(text()).not.toContain("Worktree");
349 expect(text()).not.toContain("/main/worktrees");
350​
351 child.projectId = "other";
352 child.worktreePath = null;
353 child.branch = null;
354 state.shells = [{ environmentId: "test", source: { ...child } }];
355 await act(async () => renderer.update(cloneElement(panel)));
356 expect(text()).toContain("Other project");
357 expect(text()).toContain("Workspace");
358 expect(text()).toContain("other");
359 expect(text()).not.toContain("/other");
360 expect(text()).not.toContain("Wrong environment");
361 expect(text()).not.toContain("Worktree");
362​
363 state.shells = [];
364 state.configs.clear();
365 await act(async () => renderer.update(cloneElement(panel)));
366 expect(text()).toContain("GPT-5.4");
367 expect(text()).not.toContain("gpt-5.4");
368 expect(text()).not.toContain("Project");
369 expect(text()).not.toContain("Workspace");
370​
371 for (const [driver, model, expected] of [
372 ["codex", "gpt-5.3-codex-spark", "GPT-5.3-Codex-Spark"],
373 ["codex", "custom/model-v2", "custom/model-v2"],
374 ["claudeAgent", "gpt-5.4", "GPT-5.4"],
375 ["claudeAgent", "claude-opus-4-6", "Claude Opus 4.6"],
376 ["cursor", "composer-2", "Composer 2"],
377 ["grok", "grok-4-fast", "Grok 4 Fast"],
378 ["antigravity", "gemini-3.8-flash-high", "Gemini 3.8 Flash High"],
379 ["opencode", "anthropic/claude-sonnet-4-6", "anthropic/Claude Sonnet 4.6"],
380 ["codex", null, "Not reported"],
381 ] as const) {
382 state.projection = {
383 ...projection,
384 subagents: [{ ...projection.subagents[0], driver, model }],
385 };
386 await act(async () => renderer.update(cloneElement(panel)));
387 expect(text()).toContain(expected);
388 }
389});
390​
391it("shows the parent's own visible status as parent and child activity change", async () => {
392 vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
393 const environmentId = EnvironmentId.make("test");
394 const parent = {
395 id: "parent",
396 title: "Parent conversation",
397 status: "completed",
398 activityRunStatus: "running",
399 lineage: { parentThreadId: null, relationshipToParent: null },
400 };
401 const child = {
402 id: "child",
403 title: "Current fork",
404 status: "completed",
405 lineage: { parentThreadId: "parent", relationshipToParent: "fork" },
406 };
407 state.projection = {
408 thread: { ...child, activeProviderThreadId: null },
409 runs: [],
410 providerThreads: [],
411 providerSessions: [],
412 contextTransfers: [],
413 subagents: [],
414 };
415 const shells = (parentSource: typeof parent, childSource: typeof child) => [
416 { environmentId, source: parentSource },
417 { environmentId, source: childSource },
418 ];
419 state.shells = shells(parent, child);
420 const panel = (
421 <ThreadRelationshipsPanel environmentId={environmentId} threadId={ThreadId.make("child")} />
422 );
423 await act(async () => {
424 renderer = create(panel);
425 });
426 const visibleText = () =>
427 renderer.root
428 .findAll((node) => typeof node.type === "string" && node.props.className !== "sr-only")
429 .flatMap((node) => node.children.filter((child) => typeof child === "string"))
430 .join(" ");
431 expect(visibleText()).toContain("Parent conversation");
432 expect(visibleText()).toContain("Running");
433 state.shells = shells(
434 { ...parent, activityRunStatus: "completed" },
435 { ...child, status: "running" },
436 );
437 await act(async () => renderer.update(cloneElement(panel)));
438 expect(visibleText()).toContain("Done");
439 expect(visibleText()).not.toContain("Running");
440});
441​
442it.each(["source", "target"])(
443 "shows transfer lifecycle states when viewing the %s thread",
444 async (currentThreadId) => {
445 vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
446 const environmentId = EnvironmentId.make("test");
447 const threads = ["source", "target"].map((id) => ({
448 id,
449 title: `${id} conversation`,
450 status: "running",
451 activityRunStatus: "running",
452 lineage: { parentThreadId: null, relationshipToParent: null },
453 }));
454 state.shells = threads.map((source) => ({ environmentId, source }));
455 const labels: Record<OrchestrationV2ContextTransfer["status"], string> = {
456 pending: "Queued",
457 resolved_native: "Resolved (native)",
458 resolved_portable: "Resolved (portable)",
459 failed: "Failed",
460 consumed: "Consumed",
461 superseded: "Superseded",
462 };
463 const panel = (
464 <ThreadRelationshipsPanel
465 environmentId={environmentId}
466 threadId={ThreadId.make(currentThreadId)}
467 />
468 );
469 for (const [status, label] of Object.entries(labels)) {
470 state.projection = {
471 thread: {
472 ...threads.find((thread) => thread.id === currentThreadId),
473 activeProviderThreadId: null,
474 },
475 runs: [],
476 providerThreads: [],
477 providerSessions: [],
478 subagents: [],
479 contextTransfers: [{ sourceThreadId: "source", targetThreadId: "target", status }],
480 };
481 await act(async () => {
482 if (status === "pending") renderer = create(panel);
483 else renderer.update(cloneElement(panel));
484 });
485 const visibleText = renderer.root
486 .findAll((node) => typeof node.type === "string")
487 .flatMap((node) => node.children.filter((child) => typeof child === "string"))
488 .join(" ");
489 expect(visibleText).toContain(
490 currentThreadId === "source" ? "target conversation" : "source conversation",
491 );
492 expect(visibleText).toContain(label);
493 expect(visibleText).not.toContain("Unknown");
494 expect(visibleText).not.toContain("Running");
495 }
496 },
497);