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

threadRoutes.test.ts

156 lines4.2 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 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("renders authoritative server-thread shells when bootstrap is complete", () => {
98 expect(
99 resolveThreadRouteRenderState({
100 bootstrapComplete: true,
101 serverThreadExists: true,
102 serverThreadDeleted: false,
103 draftThreadExists: false,
104 }),
105 ).toBe("ready");
106 });
107​
108 it("renders server threads and local drafts when they are ready", () => {
109 expect(
110 resolveThreadRouteRenderState({
111 bootstrapComplete: true,
112 serverThreadExists: true,
113 serverThreadDeleted: false,
114 draftThreadExists: false,
115 }),
116 ).toBe("ready");
117 expect(
118 resolveThreadRouteRenderState({
119 bootstrapComplete: true,
120 serverThreadExists: false,
121 serverThreadDeleted: false,
122 draftThreadExists: true,
123 }),
124 ).toBe("ready");
125 });
126​
127 it("distinguishes bootstrap loading from a missing thread", () => {
128 expect(
129 resolveThreadRouteRenderState({
130 bootstrapComplete: false,
131 serverThreadExists: false,
132 serverThreadDeleted: false,
133 draftThreadExists: false,
134 }),
135 ).toBe("loading");
136 expect(
137 resolveThreadRouteRenderState({
138 bootstrapComplete: true,
139 serverThreadExists: false,
140 serverThreadDeleted: false,
141 draftThreadExists: false,
142 }),
143 ).toBe("missing");
144 });
145​
146 it("redirects deleted server threads", () => {
147 expect(
148 resolveThreadRouteRenderState({
149 bootstrapComplete: true,
150 serverThreadExists: true,
151 serverThreadDeleted: true,
152 draftThreadExists: false,
153 }),
154 ).toBe("missing");
155 });
156});