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

MarkdownThreadLink.browser.test.tsx

134 lines4.7 KB

1import { RegistryContext } from "@effect/atom-react";
2import { EnvironmentId, ProjectId, ThreadId } from "@t3tools/contracts";
3import * as Cause from "effect/Cause";
4import { AsyncResult, Atom, AtomRegistry } from "effect/reactivity";
5import { expect, it, vi } from "vite-plus/test";
6​
7import { act, render } from "../../../test/render";
8import { MarkdownThreadLink } from "./MarkdownThreadLink";
9​
10const state = vi.hoisted(() => ({
11 reply: null as unknown,
12 remote: null as unknown,
13}));
14vi.mock(import("../../state/entities"), () => ({
15 useThreadShell: ((ref: { environmentId: string } | null) =>
16 ref?.environmentId === "remote" ? state.remote : null) as never,
17 useThreadRefs: (enabled = true) =>
18 enabled
19 ? [{ environmentId: EnvironmentId.make("remote"), threadId: ThreadId.make("older-thread") }]
20 : [],
21 useProject: (ref: { projectId: string } | null) =>
22 ref?.projectId === "essay-project" ? ({ title: "Essays" } as never) : null,
23}));
24vi.mock(import("../ProjectFavicon"), () => ({
25 ProjectFavicon: () => (
26 <span role="img" aria-label="Essays project icon">
27 P
28 </span>
29 ),
30}));
31vi.mock(import("../contextChipParts"), () => ({
32 UnresolvedChip: ({ label }: { label: string }) => <span>{label}</span>,
33}));
34vi.mock(import("../../state/orchestration"), () => ({
35 orchestrationEnvironment: { threadLinkMetadata: () => state.reply } as never,
36}));
37​
38function setup() {
39 const reply = Atom.make<
40 AsyncResult.AsyncResult<Record<string, { title: string; projectId: ProjectId | null }>, string>
41 >(AsyncResult.initial());
42 state.reply = reply;
43 state.remote = {
44 environmentId: EnvironmentId.make("remote"),
45 id: ThreadId.make("older-thread"),
46 title: "Remote thread",
47 projectId: ProjectId.make("essay-project"),
48 };
49 return { reply, registry: AtomRegistry.make() };
51​
52it.each([
53 { legacy: false, encoded: false },
54 { legacy: true, encoded: false },
55 { legacy: false, encoded: true },
56])(
57 "resolves an unloaded thread with its icon and the correct destination, %j",
58 async ({ legacy, encoded }) => {
59 const { reply, registry } = setup();
60 using _registry = { [Symbol.dispose]: () => registry.dispose() };
61 const environmentId = EnvironmentId.make("environment");
62 const threadId = ThreadId.make(encoded ? "older%2Dthread" : "older-thread");
63 await using screen = await render(
64 <RegistryContext value={registry}>
65 <MarkdownThreadLink
66 environmentId={environmentId}
67 link={{ environmentId: legacy ? null : environmentId, threadId }}
68 label={null}
69 />
70 </RegistryContext>,
71 );
72 await expect
73 .element(screen.getByRole("link", { name: "Thread, Thread", exact: true }))
74 .toBeVisible();
75 await act(() => registry.set(reply, AsyncResult.failure(Cause.fail("offline"))));
76 await expect
77 .element(screen.getByRole("link", { name: "Thread, Thread", exact: true }))
78 .toBeVisible();
79 await act(() =>
80 registry.set(
81 reply,
82 AsyncResult.success({
83 [threadId]: { title: "Older essay", projectId: ProjectId.make("essay-project") },
84 ...(encoded
85 ? {
86 "older-thread": {
87 title: "Decoded essay",
88 projectId: ProjectId.make("essay-project"),
89 },
90 }
91 : {}),
92 }),
93 ),
94 );
95 const link = screen.getByRole("link", { name: "Thread, Older essay" });
96 await expect.element(link).toBeVisible();
97 await expect.element(screen.getByRole("img", { name: "Essays project icon" })).toBeVisible();
98 await link.click();
99 await vi.waitFor(() =>
100 expect(screen.router?.state.location.pathname).toBe(
101 `/environment/${encodeURIComponent(threadId)}`,
102 ),
103 );
104 if (encoded) {
105 await act(() =>
106 registry.set(
107 reply,
108 AsyncResult.success({
109 "older-thread": { title: "Decoded essay", projectId: ProjectId.make("essay-project") },
110 }),
111 ),
112 );
113 const decoded = screen.getByRole("link", { name: "Thread, Decoded essay" });
114 await expect.element(decoded).toBeVisible();
115 await decoded.click();
116 await vi.waitFor(() =>
117 expect(screen.router?.state.location.pathname).toBe("/environment/older-thread"),
118 );
119 }
120 await act(() => registry.set(reply, AsyncResult.success({})));
121 if (legacy) {
122 const remote = screen.getByRole("link", { name: "Thread, Remote thread" });
123 await expect.element(remote).toBeVisible();
124 await remote.click();
125 await vi.waitFor(() =>
126 expect(screen.router?.state.location.pathname).toBe("/remote/older-thread"),
127 );
128 } else {
129 await expect
130 .element(screen.getByText("Thread no longer available", { exact: true }))
131 .toBeVisible();
132 }
133 },
134);