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

threadRoutes.test.ts

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