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

ThreadRelationshipsControl.browser.test.tsx

185 lines6.1 KB

1import { EnvironmentId, NodeId, ThreadId } from "@t3tools/contracts";
2import * as Cause from "effect/Cause";
3import { AsyncResult } from "effect/reactivity";
4import { expect, it, vi } from "vite-plus/test";
5import { page } from "vite-plus/test/browser";
6​
7const state = vi.hoisted(() => ({ projection: null as unknown, commandSucceeds: false }));
8​
9vi.mock(import("@effect/atom-react"), async (original) => ({
10 ...(await original()),
11 useAtomValue: (() => false) as never,
12}));
13​
14vi.mock(import("../../state/entities"), () => ({
15 useQuietThreadShell: (() => null) as never,
16 useThreadProjection: ((ref: unknown) =>
17 ref === null ? null : { projection: state.projection }) as never,
18 useThreadRelationshipShells: (() =>
19 state.projection === null ? [] : [(state.projection as { thread: unknown }).thread]) as never,
20 useProjects: (() => []) as never,
21}));
22vi.mock(import("../../state/environments"), () => ({
23 useEnvironment: (() => null) as never,
24 useEnvironmentProviders: (() => new Map()) as never,
25}));
26vi.mock(import("../../lib/archivedThreadsState"), () => ({
27 useArchivedThreadSnapshots: (() => ({ snapshots: [] })) as never,
28}));
29vi.mock(import("../../state/use-atom-command"), () => ({
30 useAtomCommand: (() => async () =>
31 state.commandSucceeds
32 ? AsyncResult.success(undefined)
33 : AsyncResult.failure(Cause.fail(new Error("Connection lost")))) as never,
34}));
35​
36import { render } from "../../../test/render";
37import { makeThreadProjectionFixture } from "../../test-fixtures";
38import { ToastProvider } from "../ui/toast";
39import { ThreadRelationshipsPanel } from "./ThreadRelationshipsControl";
40​
41const environmentId = EnvironmentId.make("environment");
42const threadId = ThreadId.make("fork");
43​
44function setup() {
45 state.commandSucceeds = false;
46 state.projection = {
47 thread: {
48 id: threadId,
49 title: "Fork",
50 activeProviderThreadId: null,
51 lineage: { parentThreadId: ThreadId.make("source"), relationshipToParent: "fork" },
52 forkedFrom: null,
53 },
54 runs: [{ id: "run", ordinal: 1, status: "completed" }],
55 subagents: [],
56 contextTransfers: [],
57 providerThreads: [],
58 providerSessions: [{ id: "session", status: "ready" }],
59 };
61​
62it.each([
63 ["merge", "Could not merge back"],
64 ["disconnect", "Could not disconnect agent session"],
65])("reports a rejected %s command and clears the busy state", async (action, title) => {
66 setup();
67 await using screen = await render(
68 <ToastProvider timeout={0}>
69 <ThreadRelationshipsPanel
70 environmentId={environmentId}
71 threadId={threadId}
72 showLineage
73 showSideChats
74 />
75 </ToastProvider>,
76 );
77​
78 if (action === "merge") {
79 await screen.getByRole("button", { name: "Merge back to source conversation" }).click();
80 } else {
81 await screen.getByRole("button", { name: "More thread actions" }).click();
82 await page.getByRole("menuitem", { name: "Disconnect agent session" }).click();
83 }
84​
85 await expect.element(page.getByText(title, { exact: true })).toBeVisible();
86 await expect
87 .element(
88 screen.getByRole("button", {
89 name: action === "merge" ? "Merge back to source conversation" : "More thread actions",
90 }),
91 )
92 .toBeEnabled();
93});
94​
95it.each([
96 ["merge", "Could not merge back"],
97 ["disconnect", "Could not disconnect agent session"],
98])("clears the previous %s error after a successful retry", async (action, title) => {
99 setup();
100 await using screen = await render(
101 <ToastProvider timeout={0}>
102 <ThreadRelationshipsPanel
103 environmentId={environmentId}
104 threadId={threadId}
105 showLineage
106 showSideChats
107 />
108 </ToastProvider>,
109 { router: "/environment/fork" },
110 );
111​
112 const runAction = async () => {
113 if (action === "merge") {
114 await screen.getByRole("button", { name: "Merge back to source conversation" }).click();
115 } else {
116 await screen.getByRole("button", { name: "More thread actions" }).click();
117 await page.getByRole("menuitem", { name: "Disconnect agent session" }).click();
118 }
119 };
120​
121 await runAction();
122 await expect.element(page.getByText(title, { exact: true })).toBeVisible();
123 expect(screen.router?.state.location.pathname).toBe("/environment/fork");
124​
125 state.commandSucceeds = true;
126 await runAction();
127​
128 await expect.element(page.getByText(title, { exact: true })).not.toBeInTheDocument();
129 await vi.waitFor(() => {
130 expect(screen.router?.state.location.pathname).toBe(
131 action === "merge" ? "/environment/source" : "/environment/fork",
132 );
133 });
134 if (action === "disconnect") {
135 await expect.element(screen.getByRole("button", { name: "More thread actions" })).toBeEnabled();
136 }
137});
138​
139it("opens an idle child from Lineage when its shell has not been loaded", async () => {
140 setup();
141 const projection = makeThreadProjectionFixture();
142 const childId = ThreadId.make("omitted-child");
143 const environmentId = EnvironmentId.make("local");
144 state.projection = {
145 ...projection,
146 subagents: [
147 {
148 id: NodeId.make("agent"),
149 threadId: projection.thread.id,
150 runId: null,
151 parentNodeId: NodeId.make("parent"),
152 origin: "app_owned",
153 createdBy: "agent",
154 driver: "codex",
155 providerInstanceId: projection.thread.providerInstanceId,
156 providerThreadId: null,
157 childThreadId: childId,
158 nativeTaskRef: null,
159 prompt: "Inspect the change",
160 title: "Idle child",
161 model: null,
162 status: "completed",
163 result: null,
164 startedAt: null,
165 completedAt: projection.thread.updatedAt,
166 updatedAt: projection.thread.updatedAt,
167 },
168 ],
169 };
170 await using screen = await render(
171 <ThreadRelationshipsPanel
172 environmentId={environmentId}
173 threadId={projection.thread.id}
174 showLineage
175 showSideChats
176 />,
177 );
178 await screen.getByRole("button", { name: /Previous agents/ }).click();
179 const child = screen.getByRole("button", { name: /Idle child/ });
180 await expect.element(child).toBeEnabled();
181 await child.click();
182 await vi.waitFor(() =>
183 expect(screen.router?.state.location.pathname).toBe("/local/omitted-child"),
184 );
185});