The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { |
| 2 | createMemoryHistory, |
| 3 | createRootRoute, |
| 4 | createRoute, |
| 5 | createRouter, |
| 6 | } from "@tanstack/react-router"; |
| 7 | import { describe, expect, it } from "vite-plus/test"; |
| 8 | import { scopeThreadRef } from "@t3tools/client-runtime/environment"; |
| 9 | import { EMPTY_ENVIRONMENT_THREAD_STATE } from "@t3tools/client-runtime/state/threads"; |
| 10 | import { ThreadId } from "@t3tools/contracts"; |
| 11 | import * as DateTime from "effect/DateTime"; |
| 12 | import * as Option from "effect/Option"; |
| 13 | import { linkedThreadRouteParams } from "./linkedThreadSurface"; |
| 14 | import { makeThreadProjectionFixture } from "./test-fixtures"; |
| 15 | |
| 16 | import { |
| 17 | isThreadRoutePathname, |
| 18 | isThreadRouteSnapshotAuthoritative, |
| 19 | resolveActiveThreadRouteRef, |
| 20 | resolveThreadRouteRenderState, |
| 21 | resolveThreadRouteTarget, |
| 22 | } from "./threadRoutes"; |
| 23 | |
| 24 | describe("threadRoutes", () => { |
| 25 | it("matches only the thread's own route pathname", () => { |
| 26 | const ref = scopeThreadRef("env-1" as never, ThreadId.make("thread-1")); |
| 27 | |
| 28 | expect(isThreadRoutePathname("/env-1/thread-1", ref)).toBe(true); |
| 29 | expect(isThreadRoutePathname("/env-1/thread-1/", ref)).toBe(true); |
| 30 | expect(isThreadRoutePathname("/new", ref)).toBe(false); |
| 31 | expect(isThreadRoutePathname("/env-1/thread-2", ref)).toBe(false); |
| 32 | expect(isThreadRoutePathname("/env-2/thread-1", ref)).toBe(false); |
| 33 | expect(isThreadRoutePathname("/draft/thread-1", ref)).toBe(false); |
| 34 | expect(isThreadRoutePathname("/env-1/thread-1/extra", ref)).toBe(false); |
| 35 | |
| 36 | const encodedRef = scopeThreadRef("env space" as never, ThreadId.make("mcp:example/part")); |
| 37 | expect(isThreadRoutePathname("/env%20space/mcp%3Aexample%2Fpart", encodedRef)).toBe(true); |
| 38 | expect(isThreadRoutePathname("/env%20space/mcp%3Aexample%2Fpart/", encodedRef)).toBe(true); |
| 39 | expect(isThreadRoutePathname("/env%20space/mcp%3Aexample/part", encodedRef)).toBe(false); |
| 40 | expect(isThreadRoutePathname("/env%20space/mcp%253Aexample%2Fpart", encodedRef)).toBe(false); |
| 41 | expect(isThreadRoutePathname("/env%20space/mcp%ZZexample", encodedRef)).toBe(false); |
| 42 | expect(isThreadRoutePathname("/env%20space/mcp%3Aexample%2Fpart/extra", encodedRef)).toBe( |
| 43 | false, |
| 44 | ); |
| 45 | expect( |
| 46 | isThreadRoutePathname( |
| 47 | "/env-1/mcp%253Aexample", |
| 48 | scopeThreadRef("env-1" as never, ThreadId.make("mcp%3Aexample")), |
| 49 | ), |
| 50 | ).toBe(true); |
| 51 | }); |
| 52 | |
| 53 | it("matches encoded MCP thread IDs from router navigation", async () => { |
| 54 | const root = createRootRoute(); |
| 55 | const thread = createRoute({ getParentRoute: () => root, path: "$environmentId/$threadId" }); |
| 56 | const newThread = createRoute({ getParentRoute: () => root, path: "new" }); |
| 57 | const router = createRouter({ |
| 58 | routeTree: root.addChildren([thread, newThread]), |
| 59 | history: createMemoryHistory({ initialEntries: ["/new"] }), |
| 60 | }); |
| 61 | await router.load(); |
| 62 | const ref = scopeThreadRef("local" as never, ThreadId.make("mcp:example")); |
| 63 | await router.navigate({ |
| 64 | to: "/$environmentId/$threadId", |
| 65 | params: { environmentId: ref.environmentId, threadId: ref.threadId }, |
| 66 | }); |
| 67 | expect(router.state.location.pathname).toBe("/local/mcp%3Aexample"); |
| 68 | expect(router.state.matches.at(-1)?.params).toEqual(ref); |
| 69 | expect(isThreadRoutePathname(router.state.location.pathname, ref)).toBe(true); |
| 70 | await router.navigate({ to: "/new" }); |
| 71 | expect(isThreadRoutePathname(router.state.location.pathname, ref)).toBe(false); |
| 72 | }); |
| 73 | |
| 74 | it("does not take a linked environment's thread route for a thread this client can open", () => { |
| 75 | // The layout reads every match's params untyped, as here. |
| 76 | const params: Record<string, string> = linkedThreadRouteParams({ |
| 77 | viaEnvironmentId: "env-1" as never, |
| 78 | environmentId: "env-2" as never, |
| 79 | threadId: ThreadId.make("thread-there"), |
| 80 | }); |
| 81 | |
| 82 | expect(resolveThreadRouteTarget(params)).toBeNull(); |
| 83 | }); |
| 84 | |
| 85 | it("resolves the backing thread while a draft route is being promoted", () => { |
| 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: scopeThreadRef("env-2" as never, ThreadId.make("server-thread")), |
| 93 | }), |
| 94 | ).toEqual({ |
| 95 | environmentId: "env-2", |
| 96 | threadId: "server-thread", |
| 97 | }); |
| 98 | expect( |
| 99 | resolveActiveThreadRouteRef( |
| 100 | target, |
| 101 | null, |
| 102 | scopeThreadRef("env-2" as never, ThreadId.make("server-thread")), |
| 103 | ), |
| 104 | ).toEqual({ environmentId: "env-2", threadId: "server-thread" }); |
| 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.each([ |
| 120 | { status: "cached", source: "server thread" }, |
| 121 | { status: "cached", source: "draft" }, |
| 122 | ] as const)( |
| 123 | "keeps available $source visible while the shell is $status", |
| 124 | ({ status, source }) => { |
| 125 | expect( |
| 126 | resolveThreadRouteRenderState({ |
| 127 | bootstrapComplete: isThreadRouteSnapshotAuthoritative(status), |
| 128 | serverThreadExists: source === "server thread", |
| 129 | serverThreadDeleted: false, |
| 130 | unlistedThreadDetail: EMPTY_ENVIRONMENT_THREAD_STATE, |
| 131 | draftThreadExists: source === "draft", |
| 132 | }), |
| 133 | ).toBe("ready"); |
| 134 | }, |
| 135 | ); |
| 136 | |
| 137 | it("distinguishes bootstrap loading from a missing thread", () => { |
| 138 | expect( |
| 139 | resolveThreadRouteRenderState({ |
| 140 | bootstrapComplete: false, |
| 141 | serverThreadExists: false, |
| 142 | serverThreadDeleted: false, |
| 143 | unlistedThreadDetail: { ...EMPTY_ENVIRONMENT_THREAD_STATE, status: "deleted" }, |
| 144 | draftThreadExists: false, |
| 145 | }), |
| 146 | ).toBe("loading"); |
| 147 | expect( |
| 148 | resolveThreadRouteRenderState({ |
| 149 | bootstrapComplete: true, |
| 150 | serverThreadExists: false, |
| 151 | serverThreadDeleted: false, |
| 152 | unlistedThreadDetail: { ...EMPTY_ENVIRONMENT_THREAD_STATE, status: "deleted" }, |
| 153 | draftThreadExists: false, |
| 154 | }), |
| 155 | ).toBe("missing"); |
| 156 | }); |
| 157 | |
| 158 | it("opens an unlisted server thread once its detail arrives, without waiting for the shell", () => { |
| 159 | const route = { |
| 160 | bootstrapComplete: isThreadRouteSnapshotAuthoritative("live"), |
| 161 | serverThreadExists: false, |
| 162 | serverThreadDeleted: false, |
| 163 | draftThreadExists: false, |
| 164 | }; |
| 165 | |
| 166 | expect( |
| 167 | resolveThreadRouteRenderState({ |
| 168 | ...route, |
| 169 | unlistedThreadDetail: EMPTY_ENVIRONMENT_THREAD_STATE, |
| 170 | }), |
| 171 | ).toBe("loading"); |
| 172 | expect( |
| 173 | resolveThreadRouteRenderState({ |
| 174 | ...route, |
| 175 | unlistedThreadDetail: { |
| 176 | ...EMPTY_ENVIRONMENT_THREAD_STATE, |
| 177 | status: "synchronizing", |
| 178 | }, |
| 179 | }), |
| 180 | ).toBe("loading"); |
| 181 | expect( |
| 182 | resolveThreadRouteRenderState({ |
| 183 | ...route, |
| 184 | unlistedThreadDetail: { |
| 185 | ...EMPTY_ENVIRONMENT_THREAD_STATE, |
| 186 | status: "synchronizing", |
| 187 | error: Option.some("Connection interrupted"), |
| 188 | }, |
| 189 | }), |
| 190 | ).toBe("loading"); |
| 191 | expect( |
| 192 | resolveThreadRouteRenderState({ |
| 193 | ...route, |
| 194 | unlistedThreadDetail: { |
| 195 | ...EMPTY_ENVIRONMENT_THREAD_STATE, |
| 196 | status: "live", |
| 197 | data: Option.some(makeThreadProjectionFixture()), |
| 198 | }, |
| 199 | }), |
| 200 | ).toBe("ready"); |
| 201 | }); |
| 202 | |
| 203 | it.each(["missing", "deleted", "archived"] as const)( |
| 204 | "redirects an unlisted thread only after detail confirms it is %s", |
| 205 | (outcome) => { |
| 206 | const route = { |
| 207 | bootstrapComplete: isThreadRouteSnapshotAuthoritative("live"), |
| 208 | serverThreadExists: false, |
| 209 | serverThreadDeleted: false, |
| 210 | draftThreadExists: false, |
| 211 | }; |
| 212 | expect( |
| 213 | resolveThreadRouteRenderState({ |
| 214 | ...route, |
| 215 | unlistedThreadDetail: { |
| 216 | ...EMPTY_ENVIRONMENT_THREAD_STATE, |
| 217 | status: "synchronizing", |
| 218 | }, |
| 219 | }), |
| 220 | ).toBe("loading"); |
| 221 | |
| 222 | const projection = makeThreadProjectionFixture(); |
| 223 | const removedAt = DateTime.makeUnsafe("2026-01-02T00:00:00Z"); |
| 224 | expect( |
| 225 | resolveThreadRouteRenderState({ |
| 226 | ...route, |
| 227 | unlistedThreadDetail: |
| 228 | outcome === "missing" |
| 229 | ? { ...EMPTY_ENVIRONMENT_THREAD_STATE, status: "deleted" } |
| 230 | : { |
| 231 | ...EMPTY_ENVIRONMENT_THREAD_STATE, |
| 232 | status: "live", |
| 233 | data: Option.some({ |
| 234 | ...projection, |
| 235 | thread: { |
| 236 | ...projection.thread, |
| 237 | deletedAt: outcome === "deleted" ? removedAt : null, |
| 238 | archivedAt: outcome === "archived" ? removedAt : null, |
| 239 | }, |
| 240 | }), |
| 241 | }, |
| 242 | }), |
| 243 | ).toBe("missing"); |
| 244 | }, |
| 245 | ); |
| 246 | |
| 247 | it("waits for the live shell before redirecting a deleted thread", () => { |
| 248 | expect( |
| 249 | resolveThreadRouteRenderState({ |
| 250 | bootstrapComplete: isThreadRouteSnapshotAuthoritative("cached"), |
| 251 | serverThreadExists: true, |
| 252 | serverThreadDeleted: true, |
| 253 | unlistedThreadDetail: EMPTY_ENVIRONMENT_THREAD_STATE, |
| 254 | draftThreadExists: false, |
| 255 | }), |
| 256 | ).toBe("loading"); |
| 257 | }); |
| 258 | }); |