Open History to see the last commit
1// @vitest-environment jsdom
2import { afterEach, describe, expect, it, vi } from "vite-plus/test";
3import {
4 eraseHeldText,
5 holdTextForUrlRouteTarget,
6 isUrlTargetSidebarTextKey,
7 setUrlRouteTargetKey,
8 insertHeldText,
9} from "./components/keptView";
10import { scopeThreadRef } from "@t3tools/client-runtime/environment";
11import { ThreadId } from "@t3tools/contracts";
12import { DraftId } from "./composerDraftStore";
13
14import {
15 buildDraftThreadRouteParams,
16 buildThreadRouteParams,
17 isThreadRoutePathname,
18 isThreadRouteSnapshotAuthoritative,
19 resolveActiveThreadRouteRef,
20 finishWarmingThreadView,
21 keepReleasedThreadView,
22 resolveKeptThreadViews,
23 resolveThreadRouteRenderState,
24 resolveThreadRouteRef,
25 resolveThreadRouteTarget,
26 type KeptThreadView,
27} from "./threadRoutes";
28
29describe("sidebar typing during a kept thread navigation", () => {
30 const takeHeldText = (key: string) => {
31 let taken: string | null = null;
32 insertHeldText(key, (text) => {
33 taken = text;
34 return true;
35 });
36 return taken;
37 };
38 const previous = "server:env-1:previous";
39 const destination = "server:env-1:destination";
40 const item = document.createElement("li");
41 item.setAttribute("data-thread-item", "env-1:destination");
42 item.innerHTML =
43 '<div data-testid="sidebar-row-slim" role="button" tabindex="0"><button>Menu</button></div>';
44 const row = item.querySelector<HTMLElement>("[role=button]")!;
45
46 afterEach(() => {
47 takeHeldText(destination);
48 setUrlRouteTargetKey(null);
49 vi.restoreAllMocks();
50 });
51
52 it("keeps destination text until its composer accepts it, even after a long connection", () => {
53 const clock = vi.spyOn(performance, "now").mockReturnValue(0);
54 setUrlRouteTargetKey(destination);
55 holdTextForUrlRouteTarget(previous, "draft");
56 const insert = vi.fn().mockReturnValue(false);
57 insertHeldText(destination, insert);
58 clock.mockReturnValue(5_000);
59 insertHeldText(destination, insert);
60 expect(insert.mock.calls).toEqual([["draft"], ["draft"]]);
61 expect(takeHeldText(previous)).toBeNull();
62 insert.mockReturnValue(true);
63 insertHeldText(destination, insert);
64 expect(takeHeldText(destination)).toBeNull();
65 expect(insert).toHaveBeenCalledTimes(3);
66 });
67
68 it("holds destination row typing without giving any text to the previous draft", () => {
69 setUrlRouteTargetKey(destination);
70 const key = new KeyboardEvent("keydown", { key: "A", shiftKey: true });
71 row.dispatchEvent(key);
72 expect(isUrlTargetSidebarTextKey(key)).toBe(true);
73 expect(holdTextForUrlRouteTarget(previous, key.key)).toBe(true);
74 expect(takeHeldText(previous)).toBeNull();
75 holdTextForUrlRouteTarget(previous, "b");
76 expect(eraseHeldText(previous)).toBe(true);
77 holdTextForUrlRouteTarget(previous, "c");
78 expect(takeHeldText(destination)).toBe("Ac");
79 expect(takeHeldText(destination)).toBeNull();
80 setUrlRouteTargetKey(previous);
81 expect(isUrlTargetSidebarTextKey(key)).toBe(false);
82 setUrlRouteTargetKey(destination);
83 expect(holdTextForUrlRouteTarget(destination, "a")).toBe(false);
84 row.querySelector("button")!.dispatchEvent(key);
85 expect(isUrlTargetSidebarTextKey(key)).toBe(false);
86 expect(takeHeldText(destination)).toBeNull();
87 });
88
89 it.each([
90 { key: " " },
91 { key: "Enter" },
92 { key: "Tab" },
93 { key: "ArrowDown" },
94 { key: "a", ctrlKey: true },
95 { key: "a", metaKey: true },
96 { key: "a", altKey: true },
97 { key: "a", isComposing: true },
98 ])("leaves activation, navigation, modifiers and composition alone: %j", (init) => {
99 setUrlRouteTargetKey(destination);
100 const key = new KeyboardEvent("keydown", init);
101 row.dispatchEvent(key);
102 expect(isUrlTargetSidebarTextKey(key)).toBe(false);
103 expect(takeHeldText(destination)).toBeNull();
104 });
105});
106
107describe("threadRoutes", () => {
108 it("does not treat cached thread lists as authoritative for deep links", () => {
109 expect(isThreadRouteSnapshotAuthoritative("empty")).toBe(false);
110 expect(isThreadRouteSnapshotAuthoritative("cached")).toBe(false);
111 expect(isThreadRouteSnapshotAuthoritative("synchronizing")).toBe(false);
112 expect(isThreadRouteSnapshotAuthoritative("live")).toBe(true);
113 });
114
115 it("builds canonical thread route params from a scoped ref", () => {
116 const ref = scopeThreadRef("env-1" as never, ThreadId.make("thread-1"));
117
118 expect(buildThreadRouteParams(ref)).toEqual({
119 environmentId: "env-1",
120 threadId: "thread-1",
121 });
122 });
123
124 it("resolves a scoped ref only when both params are present", () => {
125 expect(
126 resolveThreadRouteRef({
127 environmentId: "env-1",
128 threadId: "thread-1",
129 }),
130 ).toEqual({
131 environmentId: "env-1",
132 threadId: "thread-1",
133 });
134
135 expect(resolveThreadRouteRef({ environmentId: "env-1" })).toBeNull();
136 expect(resolveThreadRouteRef({ threadId: "thread-1" })).toBeNull();
137 });
138
139 it("matches only the thread's own route pathname", () => {
140 const ref = scopeThreadRef("env-1" as never, ThreadId.make("thread-1"));
141
142 expect(isThreadRoutePathname("/env-1/thread-1", ref)).toBe(true);
143 expect(isThreadRoutePathname("/env-1/thread-1/", ref)).toBe(true);
144 // A pending navigation away from the thread already shows these locations.
145 expect(isThreadRoutePathname("/new", ref)).toBe(false);
146 expect(isThreadRoutePathname("/env-1/thread-2", ref)).toBe(false);
147 expect(isThreadRoutePathname("/env-2/thread-1", ref)).toBe(false);
148 expect(isThreadRoutePathname("/draft/thread-1", ref)).toBe(false);
149 expect(isThreadRoutePathname("/env-1/thread-1/extra", ref)).toBe(false);
150 });
151
152 it("builds canonical draft route params from a draft id", () => {
153 expect(buildDraftThreadRouteParams(DraftId.make("draft-1"))).toEqual({
154 draftId: "draft-1",
155 });
156 });
157
158 it("resolves draft and server route targets", () => {
159 expect(
160 resolveThreadRouteTarget({
161 environmentId: "env-1",
162 threadId: "thread-1",
163 }),
164 ).toEqual({
165 kind: "server",
166 threadRef: {
167 environmentId: "env-1",
168 threadId: "thread-1",
169 },
170 });
171
172 expect(
173 resolveThreadRouteTarget({
174 draftId: "draft-1",
175 }),
176 ).toEqual({
177 kind: "draft",
178 draftId: "draft-1",
179 });
180 });
181
182 it("resolves the backing thread while a draft route is being promoted", () => {
183 const target = resolveThreadRouteTarget({ draftId: "draft-1" });
184
185 expect(
186 resolveActiveThreadRouteRef(target, {
187 environmentId: "env-1" as never,
188 threadId: ThreadId.make("draft-thread"),
189 promotedTo: scopeThreadRef("env-2" as never, ThreadId.make("server-thread")),
190 }),
191 ).toEqual({
192 environmentId: "env-2",
193 threadId: "server-thread",
194 });
195 });
196
197 it("does not treat a draft's reserved thread ref as an active sidebar thread", () => {
198 const target = resolveThreadRouteTarget({ draftId: "draft-1" });
199
200 expect(
201 resolveActiveThreadRouteRef(target, {
202 environmentId: "env-1" as never,
203 threadId: ThreadId.make("draft-thread"),
204 promotedTo: null,
205 }),
206 ).toBeNull();
207 });
208
209 it("renders authoritative server-thread shells when bootstrap is complete", () => {
210 expect(
211 resolveThreadRouteRenderState({
212 bootstrapComplete: true,
213 serverThreadExists: true,
214 serverThreadDeleted: false,
215 draftThreadExists: false,
216 }),
217 ).toBe("ready");
218 });
219
220 it("renders server threads and local drafts when they are ready", () => {
221 expect(
222 resolveThreadRouteRenderState({
223 bootstrapComplete: true,
224 serverThreadExists: true,
225 serverThreadDeleted: false,
226 draftThreadExists: false,
227 }),
228 ).toBe("ready");
229 expect(
230 resolveThreadRouteRenderState({
231 bootstrapComplete: true,
232 serverThreadExists: false,
233 serverThreadDeleted: false,
234 draftThreadExists: true,
235 }),
236 ).toBe("ready");
237 });
238
239 it.each([
240 { status: "cached", source: "server thread" },
241 { status: "synchronizing", source: "server thread" },
242 { status: "cached", source: "draft" },
243 { status: "synchronizing", source: "draft" },
244 ] as const)(
245 "keeps available $source visible while the shell is $status",
246 ({ status, source }) => {
247 expect(
248 resolveThreadRouteRenderState({
249 bootstrapComplete: isThreadRouteSnapshotAuthoritative(status),
250 serverThreadExists: source === "server thread",
251 serverThreadDeleted: false,
252 draftThreadExists: source === "draft",
253 }),
254 ).toBe("ready");
255 },
256 );
257
258 it("distinguishes bootstrap loading from a missing thread", () => {
259 expect(
260 resolveThreadRouteRenderState({
261 bootstrapComplete: false,
262 serverThreadExists: false,
263 serverThreadDeleted: false,
264 draftThreadExists: false,
265 }),
266 ).toBe("loading");
267 expect(
268 resolveThreadRouteRenderState({
269 bootstrapComplete: true,
270 serverThreadExists: false,
271 serverThreadDeleted: false,
272 draftThreadExists: false,
273 }),
274 ).toBe("missing");
275 });
276
277 it("redirects deleted server threads", () => {
278 expect(
279 resolveThreadRouteRenderState({
280 bootstrapComplete: true,
281 serverThreadExists: true,
282 serverThreadDeleted: true,
283 draftThreadExists: false,
284 }),
285 ).toBe("missing");
286 });
287
288 it("waits for the live shell before redirecting a deleted thread", () => {
289 expect(
290 resolveThreadRouteRenderState({
291 bootstrapComplete: isThreadRouteSnapshotAuthoritative("cached"),
292 serverThreadExists: true,
293 serverThreadDeleted: true,
294 draftThreadExists: false,
295 }),
296 ).toBe("loading");
297 });
298});
299
300describe("resolveKeptThreadViews", () => {
301 const serverView = (id: string): Omit<KeptThreadView, "visit"> => ({
302 key: `env-1:${id}`,
303 threadKey: `env-1:${id}`,
304 environmentId: "env-1" as never,
305 threadId: ThreadId.make(id),
306 draftId: null,
307 });
308 // Opens each thread in turn, keeping released threads and finishing their
309 // warm-up right away, as idle time would.
310 const visit = (ids: ReadonlyArray<string>, limit?: number) =>
311 ids.reduce<ReadonlyArray<KeptThreadView>>((views, id) => {
312 const update = resolveKeptThreadViews(views, serverView(id), limit);
313 if (update.released === null) return update.views;
314 const kept = keepReleasedThreadView(update.views, update.released, limit);
315 const warming = kept.find((view) => view.warming === true);
316 return warming === undefined ? kept : finishWarmingThreadView(kept, warming.key);
317 }, []);
318
319 it("returns the same list when the shown thread is shown again", () => {
320 const views = visit(["a"]);
321 expect(resolveKeptThreadViews(views, serverView("a")).views).toBe(views);
322 });
323
324 it("opens a new thread in the shown view and releases the thread it showed", () => {
325 const first = resolveKeptThreadViews([], serverView("a")).views;
326 const update = resolveKeptThreadViews(first, serverView("b"));
327 expect(update.views.map((view) => [view.key, view.threadId])).toEqual([
328 ["view:env-1:a:1", "b"],
329 ]);
330 expect(update.released?.threadId).toBe("a");
331 });
332
333 it("keeps a released thread in a warming view, then reveals it on a revisit", () => {
334 const first = resolveKeptThreadViews([], serverView("a")).views;
335 const update = resolveKeptThreadViews(first, serverView("b"));
336 const kept = keepReleasedThreadView(update.views, update.released!);
337 expect(kept.map((view) => [view.threadId, view.warming === true])).toEqual([
338 ["b", false],
339 ["a", true],
340 ]);
341 const revisit = resolveKeptThreadViews(kept, serverView("a"));
342 expect(revisit.released).toBeNull();
343 expect(revisit.views.map((view) => view.key)).toEqual(kept.map((view) => view.key));
344 expect(revisit.views.find((view) => view.threadId === "a")?.warming).toBe(false);
345 });
346
347 it("drops the least recently shown hidden view to keep a released thread in a full pool", () => {
348 const views = visit(["a", "b", "c"], 3);
349 expect(views.map((view) => view.threadId).toSorted()).toEqual(["a", "b", "c"]);
350 const next = visit(["a", "b", "c", "d"], 3);
351 expect(next.map((view) => view.threadId).toSorted()).toEqual(["b", "c", "d"]);
352 });
353
354 it("skips keeping a released thread that is shown again", () => {
355 const first = resolveKeptThreadViews([], serverView("a")).views;
356 const update = resolveKeptThreadViews(first, serverView("b"));
357 const back = resolveKeptThreadViews(update.views, serverView("a"));
358 expect(keepReleasedThreadView(back.views, update.released!)).toBe(back.views);
359 });
360
361 it("keeps a promoted draft's view, and its key, on the thread route", () => {
362 const draftId = DraftId.make("draft-1");
363 const draft = resolveKeptThreadViews([], { ...serverView("t"), key: draftId, draftId }).views;
364 const promoted = resolveKeptThreadViews(draft, serverView("t")).views;
365 expect(promoted).toHaveLength(1);
366 expect(promoted[0]).toMatchObject({ key: draftId, draftId: null, threadId: "t" });
367 });
368
369 it("drops a draft's view once another view is shown", () => {
370 const draftId = DraftId.make("draft-1");
371 const views = resolveKeptThreadViews(visit(["a"]), {
372 ...serverView("t"),
373 key: draftId,
374 draftId,
375 }).views;
376 expect(
377 resolveKeptThreadViews(views, serverView("a")).views.map((view) => view.threadId),
378 ).toEqual(["a"]);
379 });
380});