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

threadRoutes.test.ts

208 lines6.3 KB

1import { describe, expect, it } from "vite-plus/test";
2import { scopeThreadRef } from "@t3tools/client-runtime/environment";
3import { ThreadId } from "@t3tools/contracts";
4import { DraftId } from "./composerDraftStore";
5​
6import {
7 buildDraftThreadRouteParams,
8 buildThreadRouteParams,
9 isThreadRoutePathname,
10 isThreadRouteSnapshotAuthoritative,
11 resolveActiveThreadRouteRef,
12 resolveThreadRouteRenderState,
13 resolveThreadRouteRef,
14 resolveThreadRouteTarget,
15} from "./threadRoutes";
16​
17describe("threadRoutes", () => {
18 it("does not treat cached thread lists as authoritative for deep links", () => {
19 expect(isThreadRouteSnapshotAuthoritative("empty")).toBe(false);
20 expect(isThreadRouteSnapshotAuthoritative("cached")).toBe(false);
21 expect(isThreadRouteSnapshotAuthoritative("synchronizing")).toBe(false);
22 expect(isThreadRouteSnapshotAuthoritative("live")).toBe(true);
23 });
24​
25 it("builds canonical thread route params from a scoped ref", () => {
26 const ref = scopeThreadRef("env-1" as never, ThreadId.make("thread-1"));
27​
28 expect(buildThreadRouteParams(ref)).toEqual({
29 environmentId: "env-1",
30 threadId: "thread-1",
31 });
32 });
33​
34 it("resolves a scoped ref only when both params are present", () => {
35 expect(
36 resolveThreadRouteRef({
37 environmentId: "env-1",
38 threadId: "thread-1",
39 }),
40 ).toEqual({
41 environmentId: "env-1",
42 threadId: "thread-1",
43 });
44​
45 expect(resolveThreadRouteRef({ environmentId: "env-1" })).toBeNull();
46 expect(resolveThreadRouteRef({ threadId: "thread-1" })).toBeNull();
47 });
48​
49 it("matches only the thread's own route pathname", () => {
50 const ref = scopeThreadRef("env-1" as never, ThreadId.make("thread-1"));
51​
52 expect(isThreadRoutePathname("/env-1/thread-1", ref)).toBe(true);
53 expect(isThreadRoutePathname("/env-1/thread-1/", ref)).toBe(true);
54 // A pending navigation away from the thread already shows these locations.
55 expect(isThreadRoutePathname("/new", ref)).toBe(false);
56 expect(isThreadRoutePathname("/env-1/thread-2", ref)).toBe(false);
57 expect(isThreadRoutePathname("/env-2/thread-1", ref)).toBe(false);
58 expect(isThreadRoutePathname("/draft/thread-1", ref)).toBe(false);
59 expect(isThreadRoutePathname("/env-1/thread-1/extra", ref)).toBe(false);
60 });
61​
62 it("builds canonical draft route params from a draft id", () => {
63 expect(buildDraftThreadRouteParams(DraftId.make("draft-1"))).toEqual({
64 draftId: "draft-1",
65 });
66 });
67​
68 it("resolves draft and server route targets", () => {
69 expect(
70 resolveThreadRouteTarget({
71 environmentId: "env-1",
72 threadId: "thread-1",
73 }),
74 ).toEqual({
75 kind: "server",
76 threadRef: {
77 environmentId: "env-1",
78 threadId: "thread-1",
79 },
80 });
81​
82 expect(
83 resolveThreadRouteTarget({
84 draftId: "draft-1",
85 }),
86 ).toEqual({
87 kind: "draft",
88 draftId: "draft-1",
89 });
90 });
91​
92 it("resolves the backing thread while a draft route is being promoted", () => {
93 const target = resolveThreadRouteTarget({ draftId: "draft-1" });
94​
95 expect(
96 resolveActiveThreadRouteRef(target, {
97 environmentId: "env-1" as never,
98 threadId: ThreadId.make("draft-thread"),
99 promotedTo: scopeThreadRef("env-2" as never, ThreadId.make("server-thread")),
100 }),
101 ).toEqual({
102 environmentId: "env-2",
103 threadId: "server-thread",
104 });
105 });
106​
107 it("does not treat a draft's reserved thread ref as an active sidebar thread", () => {
108 const target = resolveThreadRouteTarget({ draftId: "draft-1" });
109​
110 expect(
111 resolveActiveThreadRouteRef(target, {
112 environmentId: "env-1" as never,
113 threadId: ThreadId.make("draft-thread"),
114 promotedTo: null,
115 }),
116 ).toBeNull();
117 });
118​
119 it("renders authoritative server-thread shells when bootstrap is complete", () => {
120 expect(
121 resolveThreadRouteRenderState({
122 bootstrapComplete: true,
123 serverThreadExists: true,
124 serverThreadDeleted: false,
125 draftThreadExists: false,
126 }),
127 ).toBe("ready");
128 });
129​
130 it("renders server threads and local drafts when they are ready", () => {
131 expect(
132 resolveThreadRouteRenderState({
133 bootstrapComplete: true,
134 serverThreadExists: true,
135 serverThreadDeleted: false,
136 draftThreadExists: false,
137 }),
138 ).toBe("ready");
139 expect(
140 resolveThreadRouteRenderState({
141 bootstrapComplete: true,
142 serverThreadExists: false,
143 serverThreadDeleted: false,
144 draftThreadExists: true,
145 }),
146 ).toBe("ready");
147 });
148​
149 it.each([
150 { status: "cached", source: "server thread" },
151 { status: "synchronizing", source: "server thread" },
152 { status: "cached", source: "draft" },
153 { status: "synchronizing", source: "draft" },
154 ] as const)(
155 "keeps available $source visible while the shell is $status",
156 ({ status, source }) => {
157 expect(
158 resolveThreadRouteRenderState({
159 bootstrapComplete: isThreadRouteSnapshotAuthoritative(status),
160 serverThreadExists: source === "server thread",
161 serverThreadDeleted: false,
162 draftThreadExists: source === "draft",
163 }),
164 ).toBe("ready");
165 },
166 );
167​
168 it("distinguishes bootstrap loading from a missing thread", () => {
169 expect(
170 resolveThreadRouteRenderState({
171 bootstrapComplete: false,
172 serverThreadExists: false,
173 serverThreadDeleted: false,
174 draftThreadExists: false,
175 }),
176 ).toBe("loading");
177 expect(
178 resolveThreadRouteRenderState({
179 bootstrapComplete: true,
180 serverThreadExists: false,
181 serverThreadDeleted: false,
182 draftThreadExists: false,
183 }),
184 ).toBe("missing");
185 });
186​
187 it("redirects deleted server threads", () => {
188 expect(
189 resolveThreadRouteRenderState({
190 bootstrapComplete: true,
191 serverThreadExists: true,
192 serverThreadDeleted: true,
193 draftThreadExists: false,
194 }),
195 ).toBe("missing");
196 });
197​
198 it("waits for the live shell before redirecting a deleted thread", () => {
199 expect(
200 resolveThreadRouteRenderState({
201 bootstrapComplete: isThreadRouteSnapshotAuthoritative("cached"),
202 serverThreadExists: true,
203 serverThreadDeleted: true,
204 draftThreadExists: false,
205 }),
206 ).toBe("loading");
207 });
208});