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

threadRoutes.test.ts

309 lines9.7 KB

1import { describe, expect, it } from "vite-plus/test";
2import { scopeThreadRef } from "@t3tools/client-runtime/environment";
3import { EMPTY_ENVIRONMENT_THREAD_STATE } from "@t3tools/client-runtime/state/threads";
4import { ThreadId } from "@t3tools/contracts";
5import * as DateTime from "effect/DateTime";
6import * as Option from "effect/Option";
7import { DraftId } from "./composerDraftStore";
8import { makeThreadProjectionFixture } from "./test-fixtures";
9​
10import {
11 buildDraftThreadRouteParams,
12 buildThreadRouteParams,
13 isThreadRoutePathname,
14 isThreadRouteSnapshotAuthoritative,
15 resolveActiveThreadRouteRef,
16 resolveThreadRouteRenderState,
17 resolveThreadRouteRef,
18 resolveThreadRouteTarget,
19} from "./threadRoutes";
20​
21describe("threadRoutes", () => {
22 it("does not treat cached thread lists as authoritative for deep links", () => {
23 expect(isThreadRouteSnapshotAuthoritative("empty")).toBe(false);
24 expect(isThreadRouteSnapshotAuthoritative("cached")).toBe(false);
25 expect(isThreadRouteSnapshotAuthoritative("synchronizing")).toBe(false);
26 expect(isThreadRouteSnapshotAuthoritative("live")).toBe(true);
27 });
28​
29 it("builds canonical thread route params from a scoped ref", () => {
30 const ref = scopeThreadRef("env-1" as never, ThreadId.make("thread-1"));
31​
32 expect(buildThreadRouteParams(ref)).toEqual({
33 environmentId: "env-1",
34 threadId: "thread-1",
35 });
36 });
37​
38 it("resolves a scoped ref only when both params are present", () => {
39 expect(
40 resolveThreadRouteRef({
41 environmentId: "env-1",
42 threadId: "thread-1",
43 }),
44 ).toEqual({
45 environmentId: "env-1",
46 threadId: "thread-1",
47 });
48​
49 expect(resolveThreadRouteRef({ environmentId: "env-1" })).toBeNull();
50 expect(resolveThreadRouteRef({ threadId: "thread-1" })).toBeNull();
51 });
52​
53 it("matches only the thread's own route pathname", () => {
54 const ref = scopeThreadRef("env-1" as never, ThreadId.make("thread-1"));
55​
56 expect(isThreadRoutePathname("/env-1/thread-1", ref)).toBe(true);
57 expect(isThreadRoutePathname("/env-1/thread-1/", ref)).toBe(true);
58 // A pending navigation away from the thread already shows these locations.
59 expect(isThreadRoutePathname("/new", ref)).toBe(false);
60 expect(isThreadRoutePathname("/env-1/thread-2", ref)).toBe(false);
61 expect(isThreadRoutePathname("/env-2/thread-1", ref)).toBe(false);
62 expect(isThreadRoutePathname("/draft/thread-1", ref)).toBe(false);
63 expect(isThreadRoutePathname("/env-1/thread-1/extra", ref)).toBe(false);
64 });
65​
66 it("builds canonical draft route params from a draft id", () => {
67 expect(buildDraftThreadRouteParams(DraftId.make("draft-1"))).toEqual({
68 draftId: "draft-1",
69 });
70 });
71​
72 it("resolves draft and server route targets", () => {
73 expect(
74 resolveThreadRouteTarget({
75 environmentId: "env-1",
76 threadId: "thread-1",
77 }),
78 ).toEqual({
79 kind: "server",
80 threadRef: {
81 environmentId: "env-1",
82 threadId: "thread-1",
83 },
84 });
85​
86 expect(
87 resolveThreadRouteTarget({
88 draftId: "draft-1",
89 }),
90 ).toEqual({
91 kind: "draft",
92 draftId: "draft-1",
93 });
94 });
95​
96 it("resolves the backing thread while a draft route is being promoted", () => {
97 const target = resolveThreadRouteTarget({ draftId: "draft-1" });
98​
99 expect(
100 resolveActiveThreadRouteRef(target, {
101 environmentId: "env-1" as never,
102 threadId: ThreadId.make("draft-thread"),
103 promotedTo: scopeThreadRef("env-2" as never, ThreadId.make("server-thread")),
104 }),
105 ).toEqual({
106 environmentId: "env-2",
107 threadId: "server-thread",
108 });
109 });
110​
111 it("does not treat a draft's reserved thread ref as an active sidebar thread", () => {
112 const target = resolveThreadRouteTarget({ draftId: "draft-1" });
113​
114 expect(
115 resolveActiveThreadRouteRef(target, {
116 environmentId: "env-1" as never,
117 threadId: ThreadId.make("draft-thread"),
118 promotedTo: null,
119 }),
120 ).toBeNull();
121 });
122​
123 it("renders authoritative server-thread shells when bootstrap is complete", () => {
124 expect(
125 resolveThreadRouteRenderState({
126 bootstrapComplete: true,
127 serverThreadExists: true,
128 serverThreadDeleted: false,
129 unlistedThreadDetail: EMPTY_ENVIRONMENT_THREAD_STATE,
130 draftThreadExists: false,
131 }),
132 ).toBe("ready");
133 });
134​
135 it("renders server threads and local drafts when they are ready", () => {
136 expect(
137 resolveThreadRouteRenderState({
138 bootstrapComplete: true,
139 serverThreadExists: true,
140 serverThreadDeleted: false,
141 unlistedThreadDetail: EMPTY_ENVIRONMENT_THREAD_STATE,
142 draftThreadExists: false,
143 }),
144 ).toBe("ready");
145 expect(
146 resolveThreadRouteRenderState({
147 bootstrapComplete: true,
148 serverThreadExists: false,
149 serverThreadDeleted: false,
150 unlistedThreadDetail: EMPTY_ENVIRONMENT_THREAD_STATE,
151 draftThreadExists: true,
152 }),
153 ).toBe("ready");
154 });
155​
156 it.each([
157 { status: "cached", source: "server thread" },
158 { status: "synchronizing", source: "server thread" },
159 { status: "cached", source: "draft" },
160 { status: "synchronizing", source: "draft" },
161 ] as const)(
162 "keeps available $source visible while the shell is $status",
163 ({ status, source }) => {
164 expect(
165 resolveThreadRouteRenderState({
166 bootstrapComplete: isThreadRouteSnapshotAuthoritative(status),
167 serverThreadExists: source === "server thread",
168 serverThreadDeleted: false,
169 unlistedThreadDetail: EMPTY_ENVIRONMENT_THREAD_STATE,
170 draftThreadExists: source === "draft",
171 }),
172 ).toBe("ready");
173 },
174 );
175​
176 it("distinguishes bootstrap loading from a missing thread", () => {
177 expect(
178 resolveThreadRouteRenderState({
179 bootstrapComplete: false,
180 serverThreadExists: false,
181 serverThreadDeleted: false,
182 unlistedThreadDetail: { ...EMPTY_ENVIRONMENT_THREAD_STATE, status: "deleted" },
183 draftThreadExists: false,
184 }),
185 ).toBe("loading");
186 expect(
187 resolveThreadRouteRenderState({
188 bootstrapComplete: true,
189 serverThreadExists: false,
190 serverThreadDeleted: false,
191 unlistedThreadDetail: { ...EMPTY_ENVIRONMENT_THREAD_STATE, status: "deleted" },
192 draftThreadExists: false,
193 }),
194 ).toBe("missing");
195 });
196​
197 it("opens an unlisted server thread once its detail arrives, without waiting for the shell", () => {
198 const route = {
199 bootstrapComplete: isThreadRouteSnapshotAuthoritative("live"),
200 serverThreadExists: false,
201 serverThreadDeleted: false,
202 draftThreadExists: false,
203 };
204​
205 expect(
206 resolveThreadRouteRenderState({
207 ...route,
208 unlistedThreadDetail: EMPTY_ENVIRONMENT_THREAD_STATE,
209 }),
210 ).toBe("loading");
211 expect(
212 resolveThreadRouteRenderState({
213 ...route,
214 unlistedThreadDetail: {
215 ...EMPTY_ENVIRONMENT_THREAD_STATE,
216 status: "synchronizing",
217 },
218 }),
219 ).toBe("loading");
220 expect(
221 resolveThreadRouteRenderState({
222 ...route,
223 unlistedThreadDetail: {
224 ...EMPTY_ENVIRONMENT_THREAD_STATE,
225 status: "synchronizing",
226 error: Option.some("Connection interrupted"),
227 },
228 }),
229 ).toBe("loading");
230 expect(
231 resolveThreadRouteRenderState({
232 ...route,
233 unlistedThreadDetail: {
234 ...EMPTY_ENVIRONMENT_THREAD_STATE,
235 status: "live",
236 data: Option.some(makeThreadProjectionFixture()),
237 },
238 }),
239 ).toBe("ready");
240 });
241​
242 it.each(["missing", "deleted", "archived"] as const)(
243 "redirects an unlisted thread only after detail confirms it is %s",
244 (outcome) => {
245 const route = {
246 bootstrapComplete: isThreadRouteSnapshotAuthoritative("live"),
247 serverThreadExists: false,
248 serverThreadDeleted: false,
249 draftThreadExists: false,
250 };
251 expect(
252 resolveThreadRouteRenderState({
253 ...route,
254 unlistedThreadDetail: {
255 ...EMPTY_ENVIRONMENT_THREAD_STATE,
256 status: "synchronizing",
257 },
258 }),
259 ).toBe("loading");
260​
261 const projection = makeThreadProjectionFixture();
262 const removedAt = DateTime.makeUnsafe("2026-01-02T00:00:00Z");
263 expect(
264 resolveThreadRouteRenderState({
265 ...route,
266 unlistedThreadDetail:
267 outcome === "missing"
268 ? { ...EMPTY_ENVIRONMENT_THREAD_STATE, status: "deleted" }
269 : {
270 ...EMPTY_ENVIRONMENT_THREAD_STATE,
271 status: "live",
272 data: Option.some({
273 ...projection,
274 thread: {
275 ...projection.thread,
276 deletedAt: outcome === "deleted" ? removedAt : null,
277 archivedAt: outcome === "archived" ? removedAt : null,
278 },
279 }),
280 },
281 }),
282 ).toBe("missing");
283 },
284 );
285​
286 it("redirects deleted server threads", () => {
287 expect(
288 resolveThreadRouteRenderState({
289 bootstrapComplete: true,
290 serverThreadExists: true,
291 serverThreadDeleted: true,
292 unlistedThreadDetail: EMPTY_ENVIRONMENT_THREAD_STATE,
293 draftThreadExists: false,
294 }),
295 ).toBe("missing");
296 });
297​
298 it("waits for the live shell before redirecting a deleted thread", () => {
299 expect(
300 resolveThreadRouteRenderState({
301 bootstrapComplete: isThreadRouteSnapshotAuthoritative("cached"),
302 serverThreadExists: true,
303 serverThreadDeleted: true,
304 unlistedThreadDetail: EMPTY_ENVIRONMENT_THREAD_STATE,
305 draftThreadExists: false,
306 }),
307 ).toBe("loading");
308 });
309});