The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 03:37 UTC
| 1 | import { scopedThreadKey, scopeThreadRef } from "@t3tools/client-runtime/environment"; |
| 2 | import { |
| 3 | type EnvironmentId, |
| 4 | type PreviewEvent, |
| 5 | type PreviewSessionSnapshot, |
| 6 | ThreadId, |
| 7 | } from "@t3tools/contracts"; |
| 8 | import { beforeEach, describe, expect, it } from "vite-plus/test"; |
| 9 | |
| 10 | import { |
| 11 | __testing, |
| 12 | applyPreviewDesktopState, |
| 13 | applyPreviewServerEvent as applyPreviewServerEventImpl, |
| 14 | applyPreviewServerSnapshot, |
| 15 | beginPreviewSessionClose, |
| 16 | cancelPreviewSessionClose, |
| 17 | previewStateAtom, |
| 18 | readThreadPreviewState, |
| 19 | reconcilePreviewServerSessions, |
| 20 | resetPreviewStateForTests, |
| 21 | setActivePreviewTab, |
| 22 | } from "./previewStateStore"; |
| 23 | import { appAtomRegistry } from "./rpc/atomRegistry"; |
| 24 | |
| 25 | const environmentId = "env-1" as EnvironmentId; |
| 26 | const ref = scopeThreadRef(environmentId, ThreadId.make("thread-1")); |
| 27 | |
| 28 | const makeSnapshot = (overrides: Partial<PreviewSessionSnapshot> = {}): PreviewSessionSnapshot => ({ |
| 29 | threadId: "thread-1", |
| 30 | tabId: "tab_a", |
| 31 | navStatus: { _tag: "Loading", url: "http://localhost:5173/", title: "" }, |
| 32 | canGoBack: false, |
| 33 | canGoForward: false, |
| 34 | updatedAt: "2026-01-01T00:00:00.000Z", |
| 35 | ...overrides, |
| 36 | }); |
| 37 | |
| 38 | type PreviewEventDraft = PreviewEvent extends infer Event |
| 39 | ? Event extends { readonly revision: number } |
| 40 | ? Omit<Event, "revision" | "serverEpoch"> |
| 41 | : never |
| 42 | : never; |
| 43 | |
| 44 | const serverEpoch = "server-a"; |
| 45 | let nextServerRevision = 0; |
| 46 | const applyPreviewServerEvent = (eventRef: typeof ref, event: PreviewEventDraft): void => { |
| 47 | nextServerRevision += 1; |
| 48 | applyPreviewServerEventImpl(eventRef, { |
| 49 | ...event, |
| 50 | serverEpoch, |
| 51 | revision: nextServerRevision, |
| 52 | } as PreviewEvent); |
| 53 | }; |
| 54 | |
| 55 | beforeEach(() => { |
| 56 | nextServerRevision = 0; |
| 57 | resetPreviewStateForTests(); |
| 58 | }); |
| 59 | |
| 60 | describe("previewStateStore (single-tab)", () => { |
| 61 | it("resized event updates tab viewport without changing the active tab", () => { |
| 62 | const active = makeSnapshot({ tabId: "tab_a" }); |
| 63 | const background = makeSnapshot({ tabId: "tab_b" }); |
| 64 | applyPreviewServerSnapshot(ref, background); |
| 65 | applyPreviewServerSnapshot(ref, active); |
| 66 | |
| 67 | applyPreviewServerEvent(ref, { |
| 68 | type: "resized", |
| 69 | threadId: "thread-1", |
| 70 | tabId: background.tabId, |
| 71 | createdAt: "2026-01-01T00:00:01.000Z", |
| 72 | snapshot: { |
| 73 | ...background, |
| 74 | viewport: { _tag: "preset", presetId: "pixel-8", width: 412, height: 915 }, |
| 75 | updatedAt: "2026-01-01T00:00:01.000Z", |
| 76 | }, |
| 77 | }); |
| 78 | |
| 79 | const state = readThreadPreviewState(ref); |
| 80 | expect(state.activeTabId).toBe(active.tabId); |
| 81 | expect(state.sessions[background.tabId]?.viewport).toEqual({ |
| 82 | _tag: "preset", |
| 83 | presetId: "pixel-8", |
| 84 | width: 412, |
| 85 | height: 915, |
| 86 | }); |
| 87 | }); |
| 88 | |
| 89 | it("failed event for a non-active tab is ignored", () => { |
| 90 | const snapshot = makeSnapshot({ tabId: "tab_a" }); |
| 91 | applyPreviewServerEvent(ref, { |
| 92 | type: "opened", |
| 93 | threadId: "thread-1", |
| 94 | tabId: snapshot.tabId, |
| 95 | createdAt: snapshot.updatedAt, |
| 96 | snapshot, |
| 97 | }); |
| 98 | applyPreviewServerEvent(ref, { |
| 99 | type: "failed", |
| 100 | threadId: "thread-1", |
| 101 | tabId: "tab_b", |
| 102 | createdAt: "2026-01-01T00:00:01.000Z", |
| 103 | url: "http://localhost:9999/", |
| 104 | title: "", |
| 105 | code: -105, |
| 106 | description: "ERR_NAME_NOT_RESOLVED", |
| 107 | }); |
| 108 | const state = readThreadPreviewState(ref); |
| 109 | expect(state.snapshot?.navStatus._tag).toBe("Loading"); |
| 110 | }); |
| 111 | |
| 112 | it("optimistically removes a session before the server close event arrives", () => { |
| 113 | const first = makeSnapshot({ tabId: "tab_a" }); |
| 114 | const second = makeSnapshot({ |
| 115 | tabId: "tab_b", |
| 116 | updatedAt: "2026-01-01T00:00:01.000Z", |
| 117 | }); |
| 118 | applyPreviewServerSnapshot(ref, first); |
| 119 | applyPreviewServerSnapshot(ref, second); |
| 120 | |
| 121 | beginPreviewSessionClose(ref, second.tabId); |
| 122 | |
| 123 | const state = readThreadPreviewState(ref); |
| 124 | expect(Object.keys(state.sessions)).toEqual([first.tabId]); |
| 125 | expect(state.activeTabId).toBe(first.tabId); |
| 126 | expect(state.snapshot?.tabId).toBe(first.tabId); |
| 127 | }); |
| 128 | |
| 129 | it("treats a late server close event after optimistic removal as a no-op", () => { |
| 130 | const snapshot = makeSnapshot(); |
| 131 | applyPreviewServerSnapshot(ref, snapshot); |
| 132 | beginPreviewSessionClose(ref, snapshot.tabId); |
| 133 | |
| 134 | applyPreviewServerEvent(ref, { |
| 135 | type: "closed", |
| 136 | threadId: "thread-1", |
| 137 | tabId: snapshot.tabId, |
| 138 | createdAt: "2026-01-01T00:00:01.000Z", |
| 139 | }); |
| 140 | |
| 141 | const state = readThreadPreviewState(ref); |
| 142 | expect(state.sessions).toEqual({}); |
| 143 | expect(state.snapshot).toBeNull(); |
| 144 | }); |
| 145 | |
| 146 | it("does not resurrect an intentionally closed tab from a stale list snapshot", () => { |
| 147 | const snapshot = makeSnapshot(); |
| 148 | applyPreviewServerSnapshot(ref, snapshot); |
| 149 | beginPreviewSessionClose(ref, snapshot.tabId); |
| 150 | |
| 151 | applyPreviewServerSnapshot(ref, snapshot); |
| 152 | |
| 153 | const state = readThreadPreviewState(ref); |
| 154 | expect(state.sessions).toEqual({}); |
| 155 | expect(state.snapshot).toBeNull(); |
| 156 | }); |
| 157 | |
| 158 | it("can restore a suppressed tab after a failed close", () => { |
| 159 | const snapshot = makeSnapshot(); |
| 160 | applyPreviewServerSnapshot(ref, snapshot); |
| 161 | beginPreviewSessionClose(ref, snapshot.tabId); |
| 162 | |
| 163 | cancelPreviewSessionClose(ref, snapshot, snapshot.tabId); |
| 164 | |
| 165 | const state = readThreadPreviewState(ref); |
| 166 | expect(state.sessions).toEqual({ [snapshot.tabId]: snapshot }); |
| 167 | expect(state.snapshot).toEqual(snapshot); |
| 168 | }); |
| 169 | |
| 170 | it("does not publish duplicate desktop browser state", () => { |
| 171 | const snapshot = makeSnapshot(); |
| 172 | applyPreviewServerSnapshot(ref, snapshot); |
| 173 | const overlay = { |
| 174 | hasWebContents: true, |
| 175 | navStatus: { _tag: "Idle" as const }, |
| 176 | canGoBack: true, |
| 177 | canGoForward: false, |
| 178 | loading: false, |
| 179 | zoomFactor: 1, |
| 180 | pictureInPicture: false, |
| 181 | colorScheme: "system" as const, |
| 182 | audioMuted: false, |
| 183 | audible: false, |
| 184 | controller: "none" as const, |
| 185 | favicon: { |
| 186 | dataUrl: "data:image/png;base64,AA==", |
| 187 | pageUrl: "https://example.com", |
| 188 | capturedAt: 1, |
| 189 | }, |
| 190 | }; |
| 191 | let updateCount = 0; |
| 192 | const unsubscribe = appAtomRegistry.subscribe(previewStateAtom(scopedThreadKey(ref)), () => { |
| 193 | updateCount += 1; |
| 194 | }); |
| 195 | |
| 196 | applyPreviewDesktopState(ref, snapshot.tabId, overlay); |
| 197 | applyPreviewDesktopState(ref, snapshot.tabId, { ...overlay, favicon: { ...overlay.favicon } }); |
| 198 | const loaded = { |
| 199 | ...overlay, |
| 200 | navStatus: { _tag: "Success" as const, url: "https://example.com/", title: "Example" }, |
| 201 | }; |
| 202 | applyPreviewDesktopState(ref, snapshot.tabId, loaded); |
| 203 | applyPreviewDesktopState(ref, snapshot.tabId, { |
| 204 | ...loaded, |
| 205 | navStatus: { ...loaded.navStatus }, |
| 206 | }); |
| 207 | applyPreviewDesktopState(ref, snapshot.tabId, { |
| 208 | ...loaded, |
| 209 | navStatus: { ...loaded.navStatus, url: "https://example.com/next" }, |
| 210 | }); |
| 211 | unsubscribe(); |
| 212 | |
| 213 | expect(updateCount).toBe(3); |
| 214 | }); |
| 215 | |
| 216 | it("retains multiple tabs and switches active desktop state", () => { |
| 217 | const first = makeSnapshot(); |
| 218 | const second = { ...makeSnapshot(), tabId: "tab_2", updatedAt: "2026-01-02T00:00:00.000Z" }; |
| 219 | applyPreviewServerSnapshot(ref, first); |
| 220 | applyPreviewServerSnapshot(ref, second); |
| 221 | applyPreviewDesktopState(ref, first.tabId, { |
| 222 | hasWebContents: true, |
| 223 | navStatus: { _tag: "Idle" as const }, |
| 224 | canGoBack: true, |
| 225 | canGoForward: false, |
| 226 | loading: false, |
| 227 | zoomFactor: 1, |
| 228 | pictureInPicture: false, |
| 229 | colorScheme: "system", |
| 230 | audioMuted: false, |
| 231 | audible: false, |
| 232 | controller: "none", |
| 233 | favicon: null, |
| 234 | }); |
| 235 | setActivePreviewTab(ref, first.tabId); |
| 236 | |
| 237 | const state = readThreadPreviewState(ref); |
| 238 | expect(Object.keys(state.sessions)).toEqual([first.tabId, second.tabId]); |
| 239 | expect(state.snapshot?.tabId).toBe(first.tabId); |
| 240 | expect(state.desktopOverlay?.canGoBack).toBe(true); |
| 241 | }); |
| 242 | |
| 243 | it("reconciles an authoritative session list without focusing a background tab", () => { |
| 244 | const active = makeSnapshot({ tabId: "tab_a" }); |
| 245 | const stale = makeSnapshot({ |
| 246 | tabId: "tab_stale", |
| 247 | updatedAt: "2026-01-01T00:00:01.000Z", |
| 248 | }); |
| 249 | applyPreviewServerSnapshot(ref, stale); |
| 250 | applyPreviewServerSnapshot(ref, active); |
| 251 | applyPreviewDesktopState(ref, stale.tabId, { |
| 252 | hasWebContents: true, |
| 253 | navStatus: { _tag: "Idle" as const }, |
| 254 | canGoBack: false, |
| 255 | canGoForward: false, |
| 256 | loading: false, |
| 257 | zoomFactor: 1, |
| 258 | pictureInPicture: false, |
| 259 | colorScheme: "system", |
| 260 | audioMuted: false, |
| 261 | audible: false, |
| 262 | controller: "none", |
| 263 | favicon: null, |
| 264 | }); |
| 265 | |
| 266 | reconcilePreviewServerSessions(ref, { sessions: [active], serverEpoch, revision: 1 }); |
| 267 | |
| 268 | const state = readThreadPreviewState(ref); |
| 269 | expect(Object.keys(state.sessions)).toEqual([active.tabId]); |
| 270 | expect(state.activeTabId).toBe(active.tabId); |
| 271 | expect(state.snapshot).toEqual(active); |
| 272 | expect(state.desktopByTabId[stale.tabId]).toBeUndefined(); |
| 273 | }); |
| 274 | |
| 275 | it("keeps the state object when a refetched list changes nothing", () => { |
| 276 | reconcilePreviewServerSessions(ref, { sessions: [], serverEpoch, revision: 1 }); |
| 277 | const loaded = readThreadPreviewState(ref); |
| 278 | |
| 279 | reconcilePreviewServerSessions(ref, { sessions: [], serverEpoch, revision: 1 }); |
| 280 | expect(readThreadPreviewState(ref)).toBe(loaded); |
| 281 | |
| 282 | const snapshot = makeSnapshot(); |
| 283 | reconcilePreviewServerSessions(ref, { sessions: [snapshot], serverEpoch, revision: 2 }); |
| 284 | expect(readThreadPreviewState(ref).sessions).toEqual({ [snapshot.tabId]: snapshot }); |
| 285 | }); |
| 286 | |
| 287 | it("ignores a list response older than the latest server event", () => { |
| 288 | const snapshot = makeSnapshot(); |
| 289 | applyPreviewServerEvent(ref, { |
| 290 | type: "opened", |
| 291 | threadId: "thread-1", |
| 292 | tabId: snapshot.tabId, |
| 293 | createdAt: snapshot.updatedAt, |
| 294 | snapshot, |
| 295 | }); |
| 296 | |
| 297 | reconcilePreviewServerSessions(ref, { sessions: [], serverEpoch, revision: 0 }); |
| 298 | |
| 299 | expect(readThreadPreviewState(ref).sessions).toEqual({ [snapshot.tabId]: snapshot }); |
| 300 | expect(readThreadPreviewState(ref).listLoaded).toBe(false); |
| 301 | |
| 302 | const olderTab = makeSnapshot({ tabId: "tab_before_event" }); |
| 303 | reconcilePreviewServerSessions(ref, { |
| 304 | sessions: [olderTab, snapshot], |
| 305 | serverEpoch, |
| 306 | revision: 2, |
| 307 | }); |
| 308 | expect(readThreadPreviewState(ref).listLoaded).toBe(true); |
| 309 | expect(readThreadPreviewState(ref).sessions).toEqual({ |
| 310 | [olderTab.tabId]: olderTab, |
| 311 | [snapshot.tabId]: snapshot, |
| 312 | }); |
| 313 | }); |
| 314 | |
| 315 | it("does not resurrect a tab from an event older than its close", () => { |
| 316 | const snapshot = makeSnapshot(); |
| 317 | applyPreviewServerEvent(ref, { |
| 318 | type: "opened", |
| 319 | threadId: "thread-1", |
| 320 | tabId: snapshot.tabId, |
| 321 | createdAt: snapshot.updatedAt, |
| 322 | snapshot, |
| 323 | }); |
| 324 | applyPreviewServerEvent(ref, { |
| 325 | type: "closed", |
| 326 | threadId: "thread-1", |
| 327 | tabId: snapshot.tabId, |
| 328 | createdAt: "2026-01-01T00:00:01.000Z", |
| 329 | }); |
| 330 | |
| 331 | applyPreviewServerEventImpl(ref, { |
| 332 | type: "opened", |
| 333 | threadId: "thread-1", |
| 334 | tabId: snapshot.tabId, |
| 335 | createdAt: snapshot.updatedAt, |
| 336 | serverEpoch, |
| 337 | revision: 1, |
| 338 | snapshot, |
| 339 | }); |
| 340 | |
| 341 | expect(readThreadPreviewState(ref).sessions).toEqual({}); |
| 342 | }); |
| 343 | |
| 344 | it("accepts a lower revision from a newly restarted server", () => { |
| 345 | const snapshot = makeSnapshot(); |
| 346 | applyPreviewServerEventImpl(ref, { |
| 347 | type: "opened", |
| 348 | threadId: "thread-1", |
| 349 | tabId: snapshot.tabId, |
| 350 | createdAt: snapshot.updatedAt, |
| 351 | serverEpoch, |
| 352 | revision: 12, |
| 353 | snapshot, |
| 354 | }); |
| 355 | |
| 356 | reconcilePreviewServerSessions(ref, { |
| 357 | sessions: [], |
| 358 | serverEpoch: "server-b", |
| 359 | revision: 0, |
| 360 | }); |
| 361 | |
| 362 | const state = readThreadPreviewState(ref); |
| 363 | expect(state.sessions).toEqual({}); |
| 364 | expect(state.serverEpoch).toBe("server-b"); |
| 365 | expect(state.serverRevision).toBe(0); |
| 366 | }); |
| 367 | |
| 368 | it("does not carry raw-tab state across a server restart", () => { |
| 369 | const previous = makeSnapshot({ |
| 370 | navStatus: { _tag: "Success", url: "https://old.example", title: "Old" }, |
| 371 | updatedAt: "2026-01-01T00:00:02.000Z", |
| 372 | }); |
| 373 | applyPreviewServerEventImpl(ref, { |
| 374 | type: "opened", |
| 375 | threadId: "thread-1", |
| 376 | tabId: previous.tabId, |
| 377 | createdAt: previous.updatedAt, |
| 378 | serverEpoch, |
| 379 | revision: 12, |
| 380 | snapshot: previous, |
| 381 | }); |
| 382 | beginPreviewSessionClose(ref, previous.tabId); |
| 383 | applyPreviewDesktopState(ref, previous.tabId, { |
| 384 | hasWebContents: true, |
| 385 | navStatus: { _tag: "Idle" as const }, |
| 386 | canGoBack: false, |
| 387 | canGoForward: false, |
| 388 | loading: false, |
| 389 | zoomFactor: 1, |
| 390 | pictureInPicture: false, |
| 391 | colorScheme: "system", |
| 392 | audioMuted: false, |
| 393 | audible: false, |
| 394 | controller: "none", |
| 395 | favicon: null, |
| 396 | }); |
| 397 | const restarted = makeSnapshot({ |
| 398 | navStatus: { _tag: "Success", url: "https://new.example", title: "New" }, |
| 399 | updatedAt: "2026-01-01T00:00:01.000Z", |
| 400 | }); |
| 401 | reconcilePreviewServerSessions(ref, { |
| 402 | sessions: [restarted], |
| 403 | serverEpoch: "server-b", |
| 404 | revision: 0, |
| 405 | }); |
| 406 | |
| 407 | const state = readThreadPreviewState(ref); |
| 408 | expect(state.sessions[restarted.tabId]).toEqual(restarted); |
| 409 | expect(state.suppressedTabIds).toEqual(new Set()); |
| 410 | expect(state.desktopByTabId).toEqual({}); |
| 411 | expect(state.desktopOverlay).toBeNull(); |
| 412 | }); |
| 413 | |
| 414 | it("does not replace a streamed snapshot with older SWR data", () => { |
| 415 | applyPreviewServerSnapshot( |
| 416 | ref, |
| 417 | makeSnapshot({ |
| 418 | navStatus: { _tag: "Success", url: "http://localhost:5173/new", title: "New" }, |
| 419 | updatedAt: "2026-01-01T00:00:02.000Z", |
| 420 | }), |
| 421 | ); |
| 422 | applyPreviewServerSnapshot( |
| 423 | ref, |
| 424 | makeSnapshot({ |
| 425 | navStatus: { _tag: "Success", url: "http://localhost:5173/old", title: "Old" }, |
| 426 | updatedAt: "2026-01-01T00:00:01.000Z", |
| 427 | }), |
| 428 | ); |
| 429 | |
| 430 | const state = readThreadPreviewState(ref); |
| 431 | expect(state.snapshot?.navStatus).toEqual({ |
| 432 | _tag: "Success", |
| 433 | url: "http://localhost:5173/new", |
| 434 | title: "New", |
| 435 | }); |
| 436 | }); |
| 437 | }); |