Open History to see the last commit
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: "cached", source: "draft" },
61 ] as const)(
62 "keeps available $source visible while the shell is $status",
63 ({ status, source }) => {
64 expect(
65 resolveThreadRouteRenderState({
66 bootstrapComplete: isThreadRouteSnapshotAuthoritative(status),
67 serverThreadExists: source === "server thread",
68 serverThreadDeleted: false,
69 unlistedThreadDetail: EMPTY_ENVIRONMENT_THREAD_STATE,
70 draftThreadExists: source === "draft",
71 }),
72 ).toBe("ready");
73 },
74 );
75
76 it("distinguishes bootstrap loading from a missing thread", () => {
77 expect(
78 resolveThreadRouteRenderState({
79 bootstrapComplete: false,
80 serverThreadExists: false,
81 serverThreadDeleted: false,
82 unlistedThreadDetail: { ...EMPTY_ENVIRONMENT_THREAD_STATE, status: "deleted" },
83 draftThreadExists: false,
84 }),
85 ).toBe("loading");
86 expect(
87 resolveThreadRouteRenderState({
88 bootstrapComplete: true,
89 serverThreadExists: false,
90 serverThreadDeleted: false,
91 unlistedThreadDetail: { ...EMPTY_ENVIRONMENT_THREAD_STATE, status: "deleted" },
92 draftThreadExists: false,
93 }),
94 ).toBe("missing");
95 });
96
97 it("opens an unlisted server thread once its detail arrives, without waiting for the shell", () => {
98 const route = {
99 bootstrapComplete: isThreadRouteSnapshotAuthoritative("live"),
100 serverThreadExists: false,
101 serverThreadDeleted: false,
102 draftThreadExists: false,
103 };
104
105 expect(
106 resolveThreadRouteRenderState({
107 ...route,
108 unlistedThreadDetail: EMPTY_ENVIRONMENT_THREAD_STATE,
109 }),
110 ).toBe("loading");
111 expect(
112 resolveThreadRouteRenderState({
113 ...route,
114 unlistedThreadDetail: {
115 ...EMPTY_ENVIRONMENT_THREAD_STATE,
116 status: "synchronizing",
117 },
118 }),
119 ).toBe("loading");
120 expect(
121 resolveThreadRouteRenderState({
122 ...route,
123 unlistedThreadDetail: {
124 ...EMPTY_ENVIRONMENT_THREAD_STATE,
125 status: "synchronizing",
126 error: Option.some("Connection interrupted"),
127 },
128 }),
129 ).toBe("loading");
130 expect(
131 resolveThreadRouteRenderState({
132 ...route,
133 unlistedThreadDetail: {
134 ...EMPTY_ENVIRONMENT_THREAD_STATE,
135 status: "live",
136 data: Option.some(makeThreadProjectionFixture()),
137 },
138 }),
139 ).toBe("ready");
140 });
141
142 it.each(["missing", "deleted", "archived"] as const)(
143 "redirects an unlisted thread only after detail confirms it is %s",
144 (outcome) => {
145 const route = {
146 bootstrapComplete: isThreadRouteSnapshotAuthoritative("live"),
147 serverThreadExists: false,
148 serverThreadDeleted: false,
149 draftThreadExists: false,
150 };
151 expect(
152 resolveThreadRouteRenderState({
153 ...route,
154 unlistedThreadDetail: {
155 ...EMPTY_ENVIRONMENT_THREAD_STATE,
156 status: "synchronizing",
157 },
158 }),
159 ).toBe("loading");
160
161 const projection = makeThreadProjectionFixture();
162 const removedAt = DateTime.makeUnsafe("2026-01-02T00:00:00Z");
163 expect(
164 resolveThreadRouteRenderState({
165 ...route,
166 unlistedThreadDetail:
167 outcome === "missing"
168 ? { ...EMPTY_ENVIRONMENT_THREAD_STATE, status: "deleted" }
169 : {
170 ...EMPTY_ENVIRONMENT_THREAD_STATE,
171 status: "live",
172 data: Option.some({
173 ...projection,
174 thread: {
175 ...projection.thread,
176 deletedAt: outcome === "deleted" ? removedAt : null,
177 archivedAt: outcome === "archived" ? removedAt : null,
178 },
179 }),
180 },
181 }),
182 ).toBe("missing");
183 },
184 );
185
186 it("waits for the live shell before redirecting a deleted thread", () => {
187 expect(
188 resolveThreadRouteRenderState({
189 bootstrapComplete: isThreadRouteSnapshotAuthoritative("cached"),
190 serverThreadExists: true,
191 serverThreadDeleted: true,
192 unlistedThreadDetail: EMPTY_ENVIRONMENT_THREAD_STATE,
193 draftThreadExists: false,
194 }),
195 ).toBe("loading");
196 });
197});