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

threadRoutes.test.ts

199 lines6.6 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 { makeThreadProjectionFixture } from "./test-fixtures";
8​
9import {
10 isThreadRoutePathname,
11 isThreadRouteSnapshotAuthoritative,
12 resolveActiveThreadRouteRef,
13 resolveThreadRouteRenderState,
14 resolveThreadRouteTarget,
15} from "./threadRoutes";
16​
17describe("threadRoutes", () => {
18 it("matches only the thread's own route pathname", () => {
19 const ref = scopeThreadRef("env-1" as never, ThreadId.make("thread-1"));
20​
21 expect(isThreadRoutePathname("/env-1/thread-1", ref)).toBe(true);
22 expect(isThreadRoutePathname("/env-1/thread-1/", ref)).toBe(true);
23 // A pending navigation away from the thread already shows these locations.
24 expect(isThreadRoutePathname("/new", ref)).toBe(false);
25 expect(isThreadRoutePathname("/env-1/thread-2", ref)).toBe(false);
26 expect(isThreadRoutePathname("/env-2/thread-1", ref)).toBe(false);
27 expect(isThreadRoutePathname("/draft/thread-1", ref)).toBe(false);
28 expect(isThreadRoutePathname("/env-1/thread-1/extra", ref)).toBe(false);
29 });
30​
31 it("resolves the backing thread while a draft route is being promoted", () => {
32 const target = resolveThreadRouteTarget({ draftId: "draft-1" });
33​
34 expect(
35 resolveActiveThreadRouteRef(target, {
36 environmentId: "env-1" as never,
37 threadId: ThreadId.make("draft-thread"),
38 promotedTo: scopeThreadRef("env-2" as never, ThreadId.make("server-thread")),
39 }),
40 ).toEqual({
41 environmentId: "env-2",
42 threadId: "server-thread",
43 });
44 });
45​
46 it("does not treat a draft's reserved thread ref as an active sidebar thread", () => {
47 const target = resolveThreadRouteTarget({ draftId: "draft-1" });
48​
49 expect(
50 resolveActiveThreadRouteRef(target, {
51 environmentId: "env-1" as never,
52 threadId: ThreadId.make("draft-thread"),
53 promotedTo: null,
54 }),
55 ).toBeNull();
56 });
57​
58 it.each([
59 { status: "cached", source: "server thread" },
60 { status: "synchronizing", source: "server thread" },
61 { status: "cached", source: "draft" },
62 { status: "synchronizing", source: "draft" },
63 ] as const)(
64 "keeps available $source visible while the shell is $status",
65 ({ status, source }) => {
66 expect(
67 resolveThreadRouteRenderState({
68 bootstrapComplete: isThreadRouteSnapshotAuthoritative(status),
69 serverThreadExists: source === "server thread",
70 serverThreadDeleted: false,
71 unlistedThreadDetail: EMPTY_ENVIRONMENT_THREAD_STATE,
72 draftThreadExists: source === "draft",
73 }),
74 ).toBe("ready");
75 },
76 );
77​
78 it("distinguishes bootstrap loading from a missing thread", () => {
79 expect(
80 resolveThreadRouteRenderState({
81 bootstrapComplete: false,
82 serverThreadExists: false,
83 serverThreadDeleted: false,
84 unlistedThreadDetail: { ...EMPTY_ENVIRONMENT_THREAD_STATE, status: "deleted" },
85 draftThreadExists: false,
86 }),
87 ).toBe("loading");
88 expect(
89 resolveThreadRouteRenderState({
90 bootstrapComplete: true,
91 serverThreadExists: false,
92 serverThreadDeleted: false,
93 unlistedThreadDetail: { ...EMPTY_ENVIRONMENT_THREAD_STATE, status: "deleted" },
94 draftThreadExists: false,
95 }),
96 ).toBe("missing");
97 });
98​
99 it("opens an unlisted server thread once its detail arrives, without waiting for the shell", () => {
100 const route = {
101 bootstrapComplete: isThreadRouteSnapshotAuthoritative("live"),
102 serverThreadExists: false,
103 serverThreadDeleted: false,
104 draftThreadExists: false,
105 };
106​
107 expect(
108 resolveThreadRouteRenderState({
109 ...route,
110 unlistedThreadDetail: EMPTY_ENVIRONMENT_THREAD_STATE,
111 }),
112 ).toBe("loading");
113 expect(
114 resolveThreadRouteRenderState({
115 ...route,
116 unlistedThreadDetail: {
117 ...EMPTY_ENVIRONMENT_THREAD_STATE,
118 status: "synchronizing",
119 },
120 }),
121 ).toBe("loading");
122 expect(
123 resolveThreadRouteRenderState({
124 ...route,
125 unlistedThreadDetail: {
126 ...EMPTY_ENVIRONMENT_THREAD_STATE,
127 status: "synchronizing",
128 error: Option.some("Connection interrupted"),
129 },
130 }),
131 ).toBe("loading");
132 expect(
133 resolveThreadRouteRenderState({
134 ...route,
135 unlistedThreadDetail: {
136 ...EMPTY_ENVIRONMENT_THREAD_STATE,
137 status: "live",
138 data: Option.some(makeThreadProjectionFixture()),
139 },
140 }),
141 ).toBe("ready");
142 });
143​
144 it.each(["missing", "deleted", "archived"] as const)(
145 "redirects an unlisted thread only after detail confirms it is %s",
146 (outcome) => {
147 const route = {
148 bootstrapComplete: isThreadRouteSnapshotAuthoritative("live"),
149 serverThreadExists: false,
150 serverThreadDeleted: false,
151 draftThreadExists: false,
152 };
153 expect(
154 resolveThreadRouteRenderState({
155 ...route,
156 unlistedThreadDetail: {
157 ...EMPTY_ENVIRONMENT_THREAD_STATE,
158 status: "synchronizing",
159 },
160 }),
161 ).toBe("loading");
162​
163 const projection = makeThreadProjectionFixture();
164 const removedAt = DateTime.makeUnsafe("2026-01-02T00:00:00Z");
165 expect(
166 resolveThreadRouteRenderState({
167 ...route,
168 unlistedThreadDetail:
169 outcome === "missing"
170 ? { ...EMPTY_ENVIRONMENT_THREAD_STATE, status: "deleted" }
171 : {
172 ...EMPTY_ENVIRONMENT_THREAD_STATE,
173 status: "live",
174 data: Option.some({
175 ...projection,
176 thread: {
177 ...projection.thread,
178 deletedAt: outcome === "deleted" ? removedAt : null,
179 archivedAt: outcome === "archived" ? removedAt : null,
180 },
181 }),
182 },
183 }),
184 ).toBe("missing");
185 },
186 );
187​
188 it("waits for the live shell before redirecting a deleted thread", () => {
189 expect(
190 resolveThreadRouteRenderState({
191 bootstrapComplete: isThreadRouteSnapshotAuthoritative("cached"),
192 serverThreadExists: true,
193 serverThreadDeleted: true,
194 unlistedThreadDetail: EMPTY_ENVIRONMENT_THREAD_STATE,
195 draftThreadExists: false,
196 }),
197 ).toBe("loading");
198 });
199});