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

threadRoutes.test.ts

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