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

ThreadRelationshipsControl.browser.test.tsx

400 lines13.1 KB

1import * as DateTime from "effect/DateTime";
2import { EnvironmentId, NodeId, ThreadId } from "@t3tools/contracts";
3import * as Cause from "effect/Cause";
4import { AsyncResult } from "effect/reactivity";
5import { expect, it, vi } from "vite-plus/test";
6import { page, userEvent } from "vite-plus/test/browser";
7​
8const state = vi.hoisted(() => ({
9 projection: null as unknown,
10 commandSucceeds: false,
11 relatedShells: [] as unknown[],
12 threadTitles: {} as Record<string, string>,
13 archivedThreads: [] as unknown[],
14}));
15​
16vi.mock(import("../../state/query"), async (original) => ({
17 ...(await original()),
18 useEnvironmentQuery: ((atom: unknown) =>
19 atom === null
20 ? { data: null, isSuccess: false, isPending: false }
21 : { data: state.threadTitles, isSuccess: true, isPending: false }) as never,
22}));
23​
24vi.mock(import("@effect/atom-react"), async (original) => ({
25 ...(await original()),
26 useAtomValue: (() => false) as never,
27}));
28​
29vi.mock(import("../../state/entities"), () => ({
30 useQuietThreadShell: (() => null) as never,
31 useThreadProjection: ((ref: unknown) =>
32 ref === null ? null : { projection: state.projection }) as never,
33 useThreadRelationshipShells: (() =>
34 state.projection === null
35 ? []
36 : [(state.projection as { thread: unknown }).thread, ...state.relatedShells]) as never,
37 useProjects: (() => []) as never,
38 useHiddenThreadKeys: (() => new Set()) as never,
39 useAnyEnvironmentHiding: (() => false) as never,
40}));
41vi.mock(import("../../state/environments"), () => ({
42 useEnvironment: (() => null) as never,
43 useEnvironmentProviders: (() => new Map()) as never,
44}));
45vi.mock(import("../../lib/archivedThreadsState"), () => ({
46 useArchivedThreadSnapshots: (() => ({
47 snapshots: [{ environmentId: "environment", snapshot: { threads: state.archivedThreads } }],
48 })) as never,
49}));
50vi.mock(import("../../state/use-atom-command"), () => ({
51 useAtomCommand: (() => async () =>
52 state.commandSucceeds
53 ? AsyncResult.success(undefined)
54 : AsyncResult.failure(Cause.fail(new Error("Connection lost")))) as never,
55}));
56​
57import { render } from "../../../test/render";
58import { makeThreadProjectionFixture } from "../../test-fixtures";
59import { ToastProvider } from "../ui/toast";
60import { ThreadRelationshipsPanel } from "./ThreadRelationshipsControl";
61​
62const environmentId = EnvironmentId.make("environment");
63const threadId = ThreadId.make("fork");
64​
65function setup() {
66 state.commandSucceeds = false;
67 state.relatedShells = [];
68 state.threadTitles = { source: "Source" };
69 state.archivedThreads = [];
70 state.projection = {
71 thread: {
72 id: threadId,
73 title: "Fork",
74 activeProviderThreadId: null,
75 lineage: { parentThreadId: ThreadId.make("source"), relationshipToParent: "fork" },
76 forkedFrom: null,
77 },
78 runs: [{ id: "run", ordinal: 1, status: "completed" }],
79 subagents: [],
80 contextTransfers: [],
81 providerThreads: [],
82 providerSessions: [{ id: "session", status: "ready" }],
83 };
85​
86it.each([
87 ["merge", "Could not merge back"],
88 ["disconnect", "Could not disconnect agent session"],
89])("reports a rejected %s command and clears the busy state", async (action, title) => {
90 setup();
91 await using screen = await render(
92 <ToastProvider timeout={0}>
93 <ThreadRelationshipsPanel
94 environmentId={environmentId}
95 threadId={threadId}
96 showLineage
97 showSideChats
98 />
99 </ToastProvider>,
100 );
101​
102 if (action === "merge") {
103 await screen.getByRole("button", { name: "Merge back to source conversation" }).click();
104 } else {
105 await screen.getByRole("button", { name: "More thread actions" }).click();
106 await page.getByRole("menuitem", { name: "Disconnect agent session" }).click();
107 }
108​
109 await expect.element(page.getByText(title, { exact: true })).toBeVisible();
110 await expect
111 .element(
112 screen.getByRole("button", {
113 name: action === "merge" ? "Merge back to source conversation" : "More thread actions",
114 }),
115 )
116 .toBeEnabled();
117});
118​
119it.each([
120 ["merge", "Could not merge back"],
121 ["disconnect", "Could not disconnect agent session"],
122])("clears the previous %s error after a successful retry", async (action, title) => {
123 setup();
124 await using screen = await render(
125 <ToastProvider timeout={0}>
126 <ThreadRelationshipsPanel
127 environmentId={environmentId}
128 threadId={threadId}
129 showLineage
130 showSideChats
131 />
132 </ToastProvider>,
133 { router: "/environment/fork" },
134 );
135​
136 const runAction = async () => {
137 if (action === "merge") {
138 await screen.getByRole("button", { name: "Merge back to source conversation" }).click();
139 } else {
140 await screen.getByRole("button", { name: "More thread actions" }).click();
141 await page.getByRole("menuitem", { name: "Disconnect agent session" }).click();
142 }
143 };
144​
145 await runAction();
146 await expect.element(page.getByText(title, { exact: true })).toBeVisible();
147 expect(screen.router?.state.location.pathname).toBe("/environment/fork");
148​
149 state.commandSucceeds = true;
150 await runAction();
151​
152 await expect.element(page.getByText(title, { exact: true })).not.toBeInTheDocument();
153 await vi.waitFor(() => {
154 expect(screen.router?.state.location.pathname).toBe(
155 action === "merge" ? "/environment/source" : "/environment/fork",
156 );
157 });
158 if (action === "disconnect") {
159 await expect.element(screen.getByRole("button", { name: "More thread actions" })).toBeEnabled();
160 }
161});
162​
163it("opens an idle child from Lineage when its shell has not been loaded", async () => {
164 setup();
165 const projection = makeThreadProjectionFixture();
166 const childId = ThreadId.make("omitted-child");
167 const environmentId = EnvironmentId.make("local");
168 state.projection = {
169 ...projection,
170 subagents: [
171 {
172 id: NodeId.make("agent"),
173 threadId: projection.thread.id,
174 runId: null,
175 parentNodeId: NodeId.make("parent"),
176 origin: "app_owned",
177 createdBy: "agent",
178 driver: "codex",
179 providerInstanceId: projection.thread.providerInstanceId,
180 providerThreadId: null,
181 childThreadId: childId,
182 nativeTaskRef: null,
183 prompt: "Inspect the change",
184 title: "Idle child",
185 model: null,
186 status: "completed",
187 result: null,
188 startedAt: null,
189 completedAt: projection.thread.updatedAt,
190 updatedAt: projection.thread.updatedAt,
191 },
192 ],
193 };
194 await using screen = await render(
195 <ThreadRelationshipsPanel
196 environmentId={environmentId}
197 threadId={projection.thread.id}
198 showLineage
199 showSideChats
200 />,
201 );
202 await screen.getByRole("button", { name: /Previous agents/ }).click();
203 const child = screen.getByRole("button", { name: /Idle child/ });
204 await expect.element(child).toBeEnabled();
205 await child.click();
206 await vi.waitFor(() =>
207 expect(screen.router?.state.location.pathname).toBe("/local/omitted-child"),
208 );
209});
210​
211it("shows a lineage thread's full title in its tooltip", async () => {
212 setup();
213 const longTitle = "Compare the lineage tooltip against a title that the row truncates";
214 const sourceThread = {
215 ...(state.projection as { thread: Record<string, unknown> }).thread,
216 id: "source",
217 title: longTitle,
218 lineage: { parentThreadId: null, relationshipToParent: null },
219 };
220 state.relatedShells = [sourceThread];
221 await using _screen = await render(
222 <ThreadRelationshipsPanel
223 environmentId={environmentId}
224 threadId={threadId}
225 showLineage
226 showSideChats={false}
227 />,
228 );
229​
230 await userEvent.hover(page.getByText(longTitle, { exact: true }));
231 await vi.waitFor(() =>
232 expect(document.querySelector('[data-slot="tooltip-popup"]')?.textContent).toContain(longTitle),
233 );
234});
235​
236it("shows a side chat's full title in its tooltip", async () => {
237 setup();
238 const longTitle = "Side chat question with a title that the side chat row truncates";
239 state.relatedShells = [
240 {
241 id: "side",
242 title: longTitle,
243 lineage: {
244 parentThreadId: threadId,
245 relationshipToParent: "fork",
246 rootThreadId: threadId,
247 sideChat: true,
248 },
249 },
250 ];
251 await using _screen = await render(
252 <ThreadRelationshipsPanel
253 environmentId={environmentId}
254 threadId={threadId}
255 showLineage={false}
256 showSideChats
257 />,
258 );
259​
260 await userEvent.hover(page.getByText(longTitle, { exact: true }));
261 await vi.waitFor(() =>
262 expect(document.querySelector('[data-slot="tooltip-popup"]')?.textContent).toContain(longTitle),
263 );
264});
265​
266it("shows a remote subagent's full title in its tooltip even when its environment is unavailable", async () => {
267 setup();
268 const longTitle = "Investigate a remote subagent issue across linked environments";
269 const remoteEnvId = EnvironmentId.make("remote-env");
270 const remoteThreadId = ThreadId.make("remote-thread");
271 const now = DateTime.makeUnsafe("2026-10-31T20:00:00.000Z");
272 state.projection = {
273 ...(state.projection as Record<string, unknown>),
274 subagents: [
275 {
276 id: NodeId.make("remote-agent"),
277 threadId,
278 runId: null,
279 parentNodeId: NodeId.make("parent"),
280 origin: "app_owned",
281 createdBy: "agent",
282 driver: "codex",
283 providerInstanceId: "provider",
284 providerThreadId: null,
285 childThreadId: null,
286 nativeTaskRef: null,
287 prompt: "Run remote task",
288 title: longTitle,
289 model: null,
290 status: "completed",
291 result: null,
292 startedAt: null,
293 completedAt: null,
294 updatedAt: now,
295 remoteChild: {
296 environmentId: remoteEnvId,
297 threadId: remoteThreadId,
298 label: "Remote VM",
299 },
300 },
301 ],
302 };
303 await using screen = await render(
304 <ThreadRelationshipsPanel
305 environmentId={environmentId}
306 threadId={threadId}
307 showLineage
308 showSideChats={false}
309 />,
310 { router: "/environment/fork" },
311 );
312​
313 // A finished remote task sits with the other finished agents, not in its own list.
314 await expect
315 .element(screen.getByRole("button", { name: new RegExp(longTitle) }))
316 .not.toBeInTheDocument();
317 await screen.getByRole("button", { name: /Previous agents/ }).click();
318 const row = screen.getByRole("button", { name: new RegExp(longTitle) });
319 await userEvent.hover(row);
320 await vi.waitFor(() =>
321 expect(document.querySelector('[data-slot="tooltip-popup"]')?.textContent).toContain(longTitle),
322 );
323 expect(document.querySelector('[data-slot="tooltip-popup"]')?.textContent).toContain(
324 "Runs on Remote VM. Connect to it to open the thread there.",
325 );
326​
327 await row.click({ force: true });
328 expect(screen.router?.state.location.pathname).toBe("/environment/fork");
329});
330​
331it("shows a delegated-from thread's full title in its tooltip even when its environment is unavailable", async () => {
332 setup();
333 const longTitle = "Parent task delegated from another machine that has a long description";
334 state.projection = {
335 ...(state.projection as Record<string, unknown>),
336 thread: {
337 ...(state.projection as { thread: Record<string, unknown> }).thread,
338 delegatedFrom: {
339 environmentId: EnvironmentId.make("parent-env"),
340 threadId: ThreadId.make("parent-thread"),
341 title: longTitle,
342 },
343 },
344 };
345 await using screen = await render(
346 <ThreadRelationshipsPanel
347 environmentId={environmentId}
348 threadId={threadId}
349 showLineage
350 showSideChats={false}
351 />,
352 { router: "/environment/fork" },
353 );
354​
355 const row = screen.getByRole("button", { name: new RegExp(longTitle) });
356 await userEvent.hover(row);
357 await vi.waitFor(() =>
358 expect(document.querySelector('[data-slot="tooltip-popup"]')?.textContent).toContain(longTitle),
359 );
360 expect(document.querySelector('[data-slot="tooltip-popup"]')?.textContent).toContain(
361 "Delegated from a linked environment. Connect to it to open the parent thread.",
362 );
363​
364 await row.click({ force: true });
365 expect(screen.router?.state.location.pathname).toBe("/environment/fork");
366});
367​
368it("hides a deleted fork and keeps unloaded and archived forks with their current titles", async () => {
369 setup();
370 const projection = makeThreadProjectionFixture();
371 state.threadTitles = { "unloaded-fork": "Unloaded fork", "archived-fork": "Archived fork" };
372 state.archivedThreads = [
373 {
374 ...projection.thread,
375 id: ThreadId.make("archived-fork"),
376 title: "Old archived title",
377 archivedAt: projection.thread.updatedAt,
378 lineage: { parentThreadId: projection.thread.id, relationshipToParent: "fork" },
379 },
380 ];
381 state.projection = {
382 ...projection,
383 contextTransfers: ["deleted-fork", "unloaded-fork", "archived-fork"].map((id) => ({
384 sourceThreadId: projection.thread.id,
385 targetThreadId: ThreadId.make(id),
386 status: "pending",
387 })),
388 };
389 await using screen = await render(
390 <ThreadRelationshipsPanel
391 environmentId={environmentId}
392 threadId={projection.thread.id}
393 showLineage
394 showSideChats
395 />,
396 );
397 await expect.element(screen.getByText("Unloaded fork", { exact: true })).toBeVisible();
398 await expect.element(screen.getByText("Archived fork", { exact: true })).toBeVisible();
399 await expect.element(screen.getByText("deleted-fork", { exact: true })).not.toBeInTheDocument();
400});