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