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