@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
tsx 267 lines 10.4 KB
Raw
1 import { RegistryContext } from "@effect/atom-react";
2 import { scopeThreadRef } from "@t3tools/client-runtime/environment";
3 import { EnvironmentId, ProjectId, ThreadId } from "@t3tools/contracts";
4 import * as Cause from "effect/Cause";
5 import { AsyncResult, Atom, AtomRegistry } from "effect/reactivity";
6 import { expect, it, vi } from "vite-plus/test";
7
8 import { act, render } from "../../../test/render";
9 import { isMacPlatform } from "../../lib/utils";
10 import { linkedThreadSurface } from "../../linkedThreadSurface";
11 import { selectThreadRightPanelState, useRightPanelStore } from "../../rightPanelStore";
12 import { MarkdownThreadLink } from "./MarkdownThreadLink";
13
14 const HERE = scopeThreadRef(EnvironmentId.make("environment"), ThreadId.make("here"));
15 const panel = () => selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, HERE);
16
17 const state = vi.hoisted(() => ({
18 reply: null as unknown,
19 remote: null as unknown,
20 cached: null as unknown,
21 linked: new Map<string, string>(),
22 connected: [] as Array<string>,
23 }));
24 vi.mock(import("../../state/entities"), () => ({
25 useThreadShell: ((ref: { environmentId: string } | null) =>
26 ref?.environmentId === "remote"
27 ? state.remote
28 : ref?.environmentId === "saved-vm"
29 ? state.cached
30 : null) as never,
31 useThreadRefs: (enabled = true) =>
32 enabled
33 ? [{ environmentId: EnvironmentId.make("remote"), threadId: ThreadId.make("older-thread") }]
34 : [],
35 useProject: (ref: { projectId: string } | null) =>
36 ref?.projectId === "essay-project" ? ({ title: "Essays" } as never) : null,
37 }));
38 vi.mock(import("../../state/environments"), () => ({
39 // The click rule reads the client setting, whose module asks for these.
40 useEnvironments: (() => []) as never,
41 usePrimaryEnvironment: (() => null) as never,
42 useEnvironmentIdentities: () => [
43 { environmentId: EnvironmentId.make("environment"), label: "Here" },
44 { environmentId: EnvironmentId.make("remote"), label: "Remote" },
45 { environmentId: EnvironmentId.make("saved-vm"), label: "Saved VM" },
46 ],
47 useConnectedEnvironmentIds: () => state.connected as never,
48 }));
49 vi.mock(import("../../state/peerLinks"), () => ({
50 useLinkedEnvironments: ((environmentId: string | null) =>
51 environmentId === null ? new Map() : state.linked) as never,
52 }));
53 vi.mock(import("../ProjectFavicon"), () => ({
54 ProjectFavicon: () => (
55 <span role="img" aria-label="Essays project icon">
56 P
57 </span>
58 ),
59 }));
60 vi.mock(import("../contextChipParts"), () => ({
61 UnresolvedChip: ({ label }: { label: string }) => <span>{label}</span>,
62 }));
63 vi.mock(import("../../state/orchestration"), () => ({
64 orchestrationEnvironment: { threadLinkMetadata: () => state.reply } as never,
65 }));
66
67 function setup() {
68 const reply = Atom.make<
69 AsyncResult.AsyncResult<Record<string, { title: string; projectId: ProjectId | null }>, string>
70 >(AsyncResult.initial());
71 state.reply = reply;
72 state.linked = new Map();
73 state.cached = null;
74 state.connected = ["environment", "remote"];
75 useRightPanelStore.setState({ byThreadKey: {} });
76 state.remote = {
77 environmentId: EnvironmentId.make("remote"),
78 id: ThreadId.make("older-thread"),
79 title: "Remote thread",
80 projectId: ProjectId.make("essay-project"),
81 lineage: {
82 rootThreadId: ThreadId.make("older-thread"),
83 parentThreadId: null,
84 relationshipToParent: null,
85 },
86 };
87 return { reply, registry: AtomRegistry.make() };
88 }
89
90 it.each([
91 { legacy: false, encoded: false },
92 { legacy: true, encoded: false },
93 { legacy: false, encoded: true },
94 ])(
95 "resolves an unloaded thread with its icon and the correct destination, %j",
96 async ({ legacy, encoded }) => {
97 const { reply, registry } = setup();
98 using _registry = { [Symbol.dispose]: () => registry.dispose() };
99 const environmentId = EnvironmentId.make("environment");
100 const threadId = ThreadId.make(encoded ? "older%2Dthread" : "older-thread");
101 await using screen = await render(
102 <RegistryContext value={registry}>
103 <MarkdownThreadLink
104 environmentId={environmentId}
105 link={{ environmentId: legacy ? null : environmentId, threadId }}
106 label={null}
107 />
108 </RegistryContext>,
109 );
110 await expect
111 .element(screen.getByRole("link", { name: "Thread, Thread", exact: true }))
112 .toBeVisible();
113 await act(() => registry.set(reply, AsyncResult.failure(Cause.fail("offline"))));
114 await expect
115 .element(screen.getByRole("link", { name: "Thread, Thread", exact: true }))
116 .toBeVisible();
117 await act(() =>
118 registry.set(
119 reply,
120 AsyncResult.success({
121 [threadId]: { title: "Older essay", projectId: ProjectId.make("essay-project") },
122 ...(encoded
123 ? {
124 "older-thread": {
125 title: "Decoded essay",
126 projectId: ProjectId.make("essay-project"),
127 },
128 }
129 : {}),
130 }),
131 ),
132 );
133 const link = screen.getByRole("link", { name: "Thread, Older essay" });
134 await expect.element(link).toBeVisible();
135 await expect.element(screen.getByRole("img", { name: "Essays project icon" })).toBeVisible();
136 await link.click();
137 await vi.waitFor(() =>
138 expect(screen.router?.state.location.pathname).toBe(
139 `/environment/${encodeURIComponent(threadId)}`,
140 ),
141 );
142 if (encoded) {
143 await act(() =>
144 registry.set(
145 reply,
146 AsyncResult.success({
147 "older-thread": { title: "Decoded essay", projectId: ProjectId.make("essay-project") },
148 }),
149 ),
150 );
151 const decoded = screen.getByRole("link", { name: "Thread, Decoded essay" });
152 await expect.element(decoded).toBeVisible();
153 await decoded.click();
154 await vi.waitFor(() =>
155 expect(screen.router?.state.location.pathname).toBe("/environment/older-thread"),
156 );
157 }
158 await act(() => registry.set(reply, AsyncResult.success({})));
159 if (legacy) {
160 const remote = screen.getByRole("link", { name: "Thread, Remote thread" });
161 await expect.element(remote).toBeVisible();
162 await remote.click();
163 await vi.waitFor(() =>
164 expect(screen.router?.state.location.pathname).toBe("/remote/older-thread"),
165 );
166 } else {
167 await expect
168 .element(screen.getByText("Thread no longer available", { exact: true }))
169 .toBeVisible();
170 }
171 },
172 );
173
174 it("names a thread on a machine this client has no connection to, without waiting on it", async () => {
175 const { registry } = setup();
176 using _registry = { [Symbol.dispose]: () => registry.dispose() };
177 await using screen = await render(
178 <RegistryContext value={registry}>
179 <MarkdownThreadLink
180 environmentId={EnvironmentId.make("environment")}
181 link={{ environmentId: EnvironmentId.make("peer"), threadId: ThreadId.make("peer-thread") }}
182 label={null}
183 />
184 </RegistryContext>,
185 );
186 await expect
187 .element(screen.getByText("Thread on another machine", { exact: true }))
188 .toBeVisible();
189 await expect.element(screen.getByRole("link")).not.toBeInTheDocument();
190 });
191
192 it.for([
193 { name: "a machine only this environment links to", linkedId: "linked-vm" },
194 { name: "a saved machine this client is not connected to", linkedId: "saved-vm" },
195 ])("opens a thread on $name as a read through the link", async ({ linkedId }) => {
196 const { registry } = setup();
197 using _registry = { [Symbol.dispose]: () => registry.dispose() };
198 state.linked = new Map([[linkedId, "fs-17"]]);
199 const environmentId = EnvironmentId.make("environment");
200 const link = { environmentId: EnvironmentId.make(linkedId), threadId: ThreadId.make("there") };
201 await using screen = await render(
202 <RegistryContext value={registry}>
203 <MarkdownThreadLink environmentId={environmentId} threadRef={HERE} link={link} label={null} />
204 </RegistryContext>,
205 );
206 const chip = screen.getByRole("button", { name: "Thread, Thread on fs-17" });
207 await chip.click();
208 const surface = linkedThreadSurface({
209 viaEnvironmentId: environmentId,
210 ...link,
211 label: "fs-17",
212 title: null,
213 });
214 expect(panel()).toMatchObject({ isOpen: true, activeSurfaceId: surface.id, surfaces: [surface] });
215 await chip.click();
216 expect(panel().surfaces).toHaveLength(1);
217 expect(screen.router?.state.location.pathname).toBe("/");
218 // The modifier reads it on the main screen instead, through the same environment.
219 await chip.click({ modifiers: [isMacPlatform(navigator.platform) ? "Meta" : "Control"] });
220 await vi.waitFor(() =>
221 expect(screen.router?.state.location.pathname).toBe(`/linked/environment/${linkedId}/there`),
222 );
223 expect(panel().surfaces).toHaveLength(1);
224 });
225
226 it("reads through the link when a disconnected machine's thread is only cached here", async () => {
227 const { registry } = setup();
228 using _registry = { [Symbol.dispose]: () => registry.dispose() };
229 state.linked = new Map([["saved-vm", "fs-17"]]);
230 state.cached = { id: ThreadId.make("there"), title: "Cached title", projectId: null };
231 const environmentId = EnvironmentId.make("environment");
232 const link = { environmentId: EnvironmentId.make("saved-vm"), threadId: ThreadId.make("there") };
233 await using screen = await render(
234 <RegistryContext value={registry}>
235 <MarkdownThreadLink environmentId={environmentId} threadRef={HERE} link={link} label={null} />
236 </RegistryContext>,
237 );
238 await screen.getByRole("button", { name: "Thread, Cached title" }).click();
239 expect(panel().surfaces).toEqual([
240 linkedThreadSurface({
241 viaEnvironmentId: environmentId,
242 ...link,
243 label: "fs-17",
244 title: "Cached title",
245 }),
246 ]);
247 });
248
249 it("opens a linked machine's thread directly once this client is connected to it", async () => {
250 const { registry, reply } = setup();
251 using _registry = { [Symbol.dispose]: () => registry.dispose() };
252 state.linked = new Map([["saved-vm", "fs-17"]]);
253 state.connected = ["environment", "saved-vm"];
254 registry.set(reply, AsyncResult.success({ there: { title: "Fix the build", projectId: null } }));
255 await using screen = await render(
256 <RegistryContext value={registry}>
257 <MarkdownThreadLink
258 environmentId={EnvironmentId.make("environment")}
259 threadRef={HERE}
260 link={{ environmentId: EnvironmentId.make("saved-vm"), threadId: ThreadId.make("there") }}
261 label={null}
262 />
263 </RegistryContext>,
264 );
265 await expect.element(screen.getByText("Fix the build")).toBeVisible();
266 await expect.element(screen.getByRole("button")).not.toBeInTheDocument();
267 });