Open History to see the last commit
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 resolveActiveThreadRouteRef,
10 resolveThreadRouteRenderState,
11 resolveThreadRouteRef,
12 resolveThreadRouteTarget,
13} from "./threadRoutes";
14
15describe("threadRoutes", () => {
16 it("builds canonical thread route params from a scoped ref", () => {
17 const ref = scopeThreadRef("env-1" as never, ThreadId.make("thread-1"));
18
19 expect(buildThreadRouteParams(ref)).toEqual({
20 environmentId: "env-1",
21 threadId: "thread-1",
22 });
23 });
24
25 it("resolves a scoped ref only when both params are present", () => {
26 expect(
27 resolveThreadRouteRef({
28 environmentId: "env-1",
29 threadId: "thread-1",
30 }),
31 ).toEqual({
32 environmentId: "env-1",
33 threadId: "thread-1",
34 });
35
36 expect(resolveThreadRouteRef({ environmentId: "env-1" })).toBeNull();
37 expect(resolveThreadRouteRef({ threadId: "thread-1" })).toBeNull();
38 });
39
40 it("builds canonical draft route params from a draft id", () => {
41 expect(buildDraftThreadRouteParams(DraftId.make("draft-1"))).toEqual({
42 draftId: "draft-1",
43 });
44 });
45
46 it("resolves draft and server route targets", () => {
47 expect(
48 resolveThreadRouteTarget({
49 environmentId: "env-1",
50 threadId: "thread-1",
51 }),
52 ).toEqual({
53 kind: "server",
54 threadRef: {
55 environmentId: "env-1",
56 threadId: "thread-1",
57 },
58 });
59
60 expect(
61 resolveThreadRouteTarget({
62 draftId: "draft-1",
63 }),
64 ).toEqual({
65 kind: "draft",
66 draftId: "draft-1",
67 });
68 });
69
70 it("resolves the backing thread while a draft route is being promoted", () => {
71 const target = resolveThreadRouteTarget({ draftId: "draft-1" });
72
73 expect(
74 resolveActiveThreadRouteRef(target, {
75 environmentId: "env-1" as never,
76 threadId: ThreadId.make("draft-thread"),
77 promotedTo: scopeThreadRef("env-2" as never, ThreadId.make("server-thread")),
78 }),
79 ).toEqual({
80 environmentId: "env-2",
81 threadId: "server-thread",
82 });
83 });
84
85 it("does not treat a draft's reserved thread ref as an active sidebar thread", () => {
86 const target = resolveThreadRouteTarget({ draftId: "draft-1" });
87
88 expect(
89 resolveActiveThreadRouteRef(target, {
90 environmentId: "env-1" as never,
91 threadId: ThreadId.make("draft-thread"),
92 promotedTo: null,
93 }),
94 ).toBeNull();
95 });
96
97 it("keeps shell-only server threads in the loading state", () => {
98 expect(
99 resolveThreadRouteRenderState({
100 bootstrapComplete: true,
101 serverThreadShellExists: true,
102 serverThreadDetailExists: false,
103 serverThreadDetailDeleted: false,
104 draftThreadExists: false,
105 }),
106 ).toBe("loading");
107 });
108
109 it("renders server details and local drafts when they are ready", () => {
110 expect(
111 resolveThreadRouteRenderState({
112 bootstrapComplete: true,
113 serverThreadShellExists: true,
114 serverThreadDetailExists: true,
115 serverThreadDetailDeleted: false,
116 draftThreadExists: false,
117 }),
118 ).toBe("ready");
119 expect(
120 resolveThreadRouteRenderState({
121 bootstrapComplete: true,
122 serverThreadShellExists: false,
123 serverThreadDetailExists: false,
124 serverThreadDetailDeleted: false,
125 draftThreadExists: true,
126 }),
127 ).toBe("ready");
128 });
129
130 it("distinguishes bootstrap loading from a missing thread", () => {
131 expect(
132 resolveThreadRouteRenderState({
133 bootstrapComplete: false,
134 serverThreadShellExists: false,
135 serverThreadDetailExists: false,
136 serverThreadDetailDeleted: false,
137 draftThreadExists: false,
138 }),
139 ).toBe("loading");
140 expect(
141 resolveThreadRouteRenderState({
142 bootstrapComplete: true,
143 serverThreadShellExists: false,
144 serverThreadDetailExists: false,
145 serverThreadDetailDeleted: false,
146 draftThreadExists: false,
147 }),
148 ).toBe("missing");
149 });
150
151 it("redirects deleted shell-only threads", () => {
152 expect(
153 resolveThreadRouteRenderState({
154 bootstrapComplete: true,
155 serverThreadShellExists: true,
156 serverThreadDetailExists: false,
157 serverThreadDetailDeleted: true,
158 draftThreadExists: false,
159 }),
160 ).toBe("missing");
161 });
162});