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

threadRoutes.test.ts

269 lines9.5 KB

1import {
2 createMemoryHistory,
3 createRootRoute,
4 createRoute,
5 createRouter,
6} from "@tanstack/react-router";
7import { describe, expect, it } from "vite-plus/test";
8import { scopeThreadRef } from "@t3tools/client-runtime/environment";
9import { EMPTY_ENVIRONMENT_THREAD_STATE } from "@t3tools/client-runtime/state/threads";
10import { ThreadId } from "@t3tools/contracts";
11import * as DateTime from "effect/DateTime";
12import * as Option from "effect/Option";
13import { linkedThreadRouteParams } from "./linkedThreadSurface";
14import { makeThreadProjectionFixture } from "./test-fixtures";
15​
16import {
17 isThreadRoutePathname,
18 isThreadRouteSnapshotAuthoritative,
19 resolveActiveThreadRouteRef,
20 resolveThreadRouteRenderState,
21 resolveThreadRouteTarget,
22} from "./threadRoutes";
23​
24describe("threadRoutes", () => {
25 it("matches only the thread's own route pathname", () => {
26 const ref = scopeThreadRef("env-1" as never, ThreadId.make("thread-1"));
27​
28 expect(isThreadRoutePathname("/env-1/thread-1", ref)).toBe(true);
29 expect(isThreadRoutePathname("/env-1/thread-1/", ref)).toBe(true);
30 expect(isThreadRoutePathname("/new", ref)).toBe(false);
31 expect(isThreadRoutePathname("/env-1/thread-2", ref)).toBe(false);
32 expect(isThreadRoutePathname("/env-2/thread-1", ref)).toBe(false);
33 expect(isThreadRoutePathname("/draft/thread-1", ref)).toBe(false);
34 expect(isThreadRoutePathname("/env-1/thread-1/extra", ref)).toBe(false);
35​
36 const encodedRef = scopeThreadRef("env space" as never, ThreadId.make("mcp:example/part"));
37 expect(isThreadRoutePathname("/env%20space/mcp%3Aexample%2Fpart", encodedRef)).toBe(true);
38 expect(isThreadRoutePathname("/env%20space/mcp%3Aexample%2Fpart/", encodedRef)).toBe(true);
39 expect(isThreadRoutePathname("/env%20space/mcp%3Aexample/part", encodedRef)).toBe(false);
40 expect(isThreadRoutePathname("/env%20space/mcp%253Aexample%2Fpart", encodedRef)).toBe(false);
41 expect(isThreadRoutePathname("/env%20space/mcp%ZZexample", encodedRef)).toBe(false);
42 expect(isThreadRoutePathname("/env%20space/mcp%3Aexample%2Fpart/extra", encodedRef)).toBe(
43 false,
44 );
45 expect(
46 isThreadRoutePathname(
47 "/env-1/mcp%253Aexample",
48 scopeThreadRef("env-1" as never, ThreadId.make("mcp%3Aexample")),
49 ),
50 ).toBe(true);
51 });
52​
53 it("matches encoded MCP thread IDs from router navigation", async () => {
54 const root = createRootRoute();
55 const thread = createRoute({ getParentRoute: () => root, path: "$environmentId/$threadId" });
56 const newThread = createRoute({ getParentRoute: () => root, path: "new" });
57 const router = createRouter({
58 routeTree: root.addChildren([thread, newThread]),
59 history: createMemoryHistory({ initialEntries: ["/new"] }),
60 });
61 await router.load();
62 const ref = scopeThreadRef("local" as never, ThreadId.make("mcp:example"));
63 await router.navigate({
64 to: "/$environmentId/$threadId",
65 params: { environmentId: ref.environmentId, threadId: ref.threadId },
66 });
67 expect(router.state.location.pathname).toBe("/local/mcp%3Aexample");
68 expect(router.state.matches.at(-1)?.params).toEqual(ref);
69 expect(isThreadRoutePathname(router.state.location.pathname, ref)).toBe(true);
70 await router.navigate({ to: "/new" });
71 expect(isThreadRoutePathname(router.state.location.pathname, ref)).toBe(false);
72 });
73​
74 it("does not take a linked environment's thread route for a thread this client can open", () => {
75 // The layout reads every match's params untyped, as here.
76 const params: Record<string, string> = linkedThreadRouteParams({
77 viaEnvironmentId: "env-1" as never,
78 environmentId: "env-2" as never,
79 threadId: ThreadId.make("thread-there"),
80 });
81​
82 expect(resolveThreadRouteTarget(params)).toBeNull();
83 });
84​
85 it("resolves the backing thread while a draft route is being promoted", () => {
86 const target = resolveThreadRouteTarget({ draftId: "draft-1" });
87​
88 expect(
89 resolveActiveThreadRouteRef(target, {
90 environmentId: "env-1" as never,
91 threadId: ThreadId.make("draft-thread"),
92 promotedTo: scopeThreadRef("env-2" as never, ThreadId.make("server-thread")),
93 }),
94 ).toEqual({
95 environmentId: "env-2",
96 threadId: "server-thread",
97 });
98 expect(
99 resolveActiveThreadRouteRef(
100 target,
101 null,
102 scopeThreadRef("env-2" as never, ThreadId.make("server-thread")),
103 ),
104 ).toEqual({ environmentId: "env-2", threadId: "server-thread" });
105 });
106​
107 it("does not treat a draft's reserved thread ref as an active sidebar thread", () => {
108 const target = resolveThreadRouteTarget({ draftId: "draft-1" });
109​
110 expect(
111 resolveActiveThreadRouteRef(target, {
112 environmentId: "env-1" as never,
113 threadId: ThreadId.make("draft-thread"),
114 promotedTo: null,
115 }),
116 ).toBeNull();
117 });
118​
119 it.each([
120 { status: "cached", source: "server thread" },
121 { status: "cached", source: "draft" },
122 ] as const)(
123 "keeps available $source visible while the shell is $status",
124 ({ status, source }) => {
125 expect(
126 resolveThreadRouteRenderState({
127 bootstrapComplete: isThreadRouteSnapshotAuthoritative(status),
128 serverThreadExists: source === "server thread",
129 serverThreadDeleted: false,
130 unlistedThreadDetail: EMPTY_ENVIRONMENT_THREAD_STATE,
131 draftThreadExists: source === "draft",
132 }),
133 ).toBe("ready");
134 },
135 );
136​
137 it("distinguishes bootstrap loading from a missing thread", () => {
138 expect(
139 resolveThreadRouteRenderState({
140 bootstrapComplete: false,
141 serverThreadExists: false,
142 serverThreadDeleted: false,
143 unlistedThreadDetail: { ...EMPTY_ENVIRONMENT_THREAD_STATE, status: "deleted" },
144 draftThreadExists: false,
145 }),
146 ).toBe("loading");
147 expect(
148 resolveThreadRouteRenderState({
149 bootstrapComplete: true,
150 serverThreadExists: false,
151 serverThreadDeleted: false,
152 unlistedThreadDetail: { ...EMPTY_ENVIRONMENT_THREAD_STATE, status: "deleted" },
153 draftThreadExists: false,
154 }),
155 ).toBe("missing");
156 // A link to an environment this app has no connection to never bootstraps.
157 expect(
158 resolveThreadRouteRenderState({
159 bootstrapComplete: false,
160 serverThreadExists: false,
161 serverThreadDeleted: false,
162 unlistedThreadDetail: EMPTY_ENVIRONMENT_THREAD_STATE,
163 draftThreadExists: false,
164 environmentUnreachable: true,
165 }),
166 ).toBe("unreachable");
167 });
168​
169 it("opens an unlisted server thread once its detail arrives, without waiting for the shell", () => {
170 const route = {
171 bootstrapComplete: isThreadRouteSnapshotAuthoritative("live"),
172 serverThreadExists: false,
173 serverThreadDeleted: false,
174 draftThreadExists: false,
175 };
176​
177 expect(
178 resolveThreadRouteRenderState({
179 ...route,
180 unlistedThreadDetail: EMPTY_ENVIRONMENT_THREAD_STATE,
181 }),
182 ).toBe("loading");
183 expect(
184 resolveThreadRouteRenderState({
185 ...route,
186 unlistedThreadDetail: {
187 ...EMPTY_ENVIRONMENT_THREAD_STATE,
188 status: "synchronizing",
189 },
190 }),
191 ).toBe("loading");
192 expect(
193 resolveThreadRouteRenderState({
194 ...route,
195 unlistedThreadDetail: {
196 ...EMPTY_ENVIRONMENT_THREAD_STATE,
197 status: "synchronizing",
198 error: Option.some("Connection interrupted"),
199 },
200 }),
201 ).toBe("loading");
202 expect(
203 resolveThreadRouteRenderState({
204 ...route,
205 unlistedThreadDetail: {
206 ...EMPTY_ENVIRONMENT_THREAD_STATE,
207 status: "live",
208 data: Option.some(makeThreadProjectionFixture()),
209 },
210 }),
211 ).toBe("ready");
212 });
213​
214 it.each(["missing", "deleted", "archived"] as const)(
215 "redirects an unlisted thread only after detail confirms it is %s",
216 (outcome) => {
217 const route = {
218 bootstrapComplete: isThreadRouteSnapshotAuthoritative("live"),
219 serverThreadExists: false,
220 serverThreadDeleted: false,
221 draftThreadExists: false,
222 };
223 expect(
224 resolveThreadRouteRenderState({
225 ...route,
226 unlistedThreadDetail: {
227 ...EMPTY_ENVIRONMENT_THREAD_STATE,
228 status: "synchronizing",
229 },
230 }),
231 ).toBe("loading");
232​
233 const projection = makeThreadProjectionFixture();
234 const removedAt = DateTime.makeUnsafe("2026-01-02T00:00:00Z");
235 expect(
236 resolveThreadRouteRenderState({
237 ...route,
238 unlistedThreadDetail:
239 outcome === "missing"
240 ? { ...EMPTY_ENVIRONMENT_THREAD_STATE, status: "deleted" }
241 : {
242 ...EMPTY_ENVIRONMENT_THREAD_STATE,
243 status: "live",
244 data: Option.some({
245 ...projection,
246 thread: {
247 ...projection.thread,
248 deletedAt: outcome === "deleted" ? removedAt : null,
249 archivedAt: outcome === "archived" ? removedAt : null,
250 },
251 }),
252 },
253 }),
254 ).toBe("missing");
255 },
256 );
257​
258 it("waits for the live shell before redirecting a deleted thread", () => {
259 expect(
260 resolveThreadRouteRenderState({
261 bootstrapComplete: isThreadRouteSnapshotAuthoritative("cached"),
262 serverThreadExists: true,
263 serverThreadDeleted: true,
264 unlistedThreadDetail: EMPTY_ENVIRONMENT_THREAD_STATE,
265 draftThreadExists: false,
266 }),
267 ).toBe("loading");
268 });
269});