The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { scopedThreadKey, scopeThreadRef } from "@t3tools/client-runtime/environment"; |
| 2 | import { |
| 3 | type DeviceServiceState, |
| 4 | type EnvironmentId, |
| 5 | type PreviewSessionSnapshot, |
| 6 | ThreadId, |
| 7 | } from "@t3tools/contracts"; |
| 8 | import { describe, expect, it, vi } from "vite-plus/test"; |
| 9 | |
| 10 | import { browserMiniPlayerSource, usePreviewMiniPlayerStore } from "../../previewMiniPlayerStore"; |
| 11 | import { |
| 12 | __testing as previewStateTesting, |
| 13 | applyPreviewServerEvent, |
| 14 | applyPreviewServerSnapshot, |
| 15 | beginPreviewSessionClose, |
| 16 | cancelPreviewSessionClose, |
| 17 | reconcilePreviewServerSessions, |
| 18 | resetPreviewStateForTests, |
| 19 | } from "../../previewStateStore"; |
| 20 | import { useRightPanelStore } from "../../rightPanelStore"; |
| 21 | import { |
| 22 | applyDeviceSnapshot, |
| 23 | floatDeviceSurface, |
| 24 | followPreviewSessions, |
| 25 | resetDeviceSessionBaselinesForTests, |
| 26 | revealServerTabs, |
| 27 | startAgentOpenedPreviews, |
| 28 | } from "./agentOpenedPreviews"; |
| 29 | |
| 30 | vi.mock("../../state/entities", () => ({ |
| 31 | readEnvironmentSupportsServerBrowser: () => true, |
| 32 | })); |
| 33 | |
| 34 | const ref = scopeThreadRef("env-1" as EnvironmentId, ThreadId.make("thread-1")); |
| 35 | const threadKey = scopedThreadKey(ref); |
| 36 | |
| 37 | const device = { |
| 38 | hostId: "local", |
| 39 | id: "pixel", |
| 40 | platform: "android", |
| 41 | name: "Pixel 8", |
| 42 | version: "15", |
| 43 | booted: true, |
| 44 | physical: false, |
| 45 | } as const; |
| 46 | |
| 47 | const session = { |
| 48 | threadId: ref.threadId, |
| 49 | hostId: "local", |
| 50 | deviceId: "pixel", |
| 51 | platform: "android", |
| 52 | openedAt: "2026-01-01T00:00:00.000Z", |
| 53 | } as const; |
| 54 | |
| 55 | const deviceState = (input: { |
| 56 | readonly sessions?: ReadonlyArray< |
| 57 | Omit<typeof session, "openedAt"> & { readonly openedAt: string } |
| 58 | >; |
| 59 | readonly devices?: ReadonlyArray<typeof device>; |
| 60 | }) => |
| 61 | ({ |
| 62 | hosts: [], |
| 63 | hostStatus: "ready", |
| 64 | hostStatuses: {}, |
| 65 | devices: input.devices ?? [device], |
| 66 | sessions: input.sessions ?? [], |
| 67 | onboardingCompleted: true, |
| 68 | agentAccessEnabled: true, |
| 69 | hubBasePath: "/api/device-hub", |
| 70 | revision: 0, |
| 71 | }) as unknown as DeviceServiceState; |
| 72 | |
| 73 | const floating = { sheetLayout: false, autoShowFloatingPreview: true }; |
| 74 | |
| 75 | function setup() { |
| 76 | resetPreviewStateForTests(); |
| 77 | resetDeviceSessionBaselinesForTests(); |
| 78 | usePreviewMiniPlayerStore.setState({ byThreadKey: {} }); |
| 79 | useRightPanelStore.setState({ byThreadKey: {} }); |
| 80 | } |
| 81 | |
| 82 | const miniPlayerSource = () => usePreviewMiniPlayerStore.getState().byThreadKey[threadKey]?.source; |
| 83 | |
| 84 | describe("applyDeviceSnapshot", () => { |
| 85 | it("treats the first snapshot as a baseline and floats sessions that start later", () => { |
| 86 | setup(); |
| 87 | applyDeviceSnapshot(ref.environmentId, deviceState({ sessions: [session] }), floating); |
| 88 | expect(miniPlayerSource()).toBeUndefined(); |
| 89 | |
| 90 | applyDeviceSnapshot(ref.environmentId, deviceState({ sessions: [] }), floating); |
| 91 | applyDeviceSnapshot(ref.environmentId, deviceState({ sessions: [session] }), floating); |
| 92 | expect(miniPlayerSource()).toMatchObject({ kind: "device", deviceId: "pixel" }); |
| 93 | }); |
| 94 | |
| 95 | it("opens a session once its device summary arrives", () => { |
| 96 | setup(); |
| 97 | applyDeviceSnapshot(ref.environmentId, deviceState({}), floating); |
| 98 | applyDeviceSnapshot( |
| 99 | ref.environmentId, |
| 100 | deviceState({ sessions: [session], devices: [] }), |
| 101 | floating, |
| 102 | ); |
| 103 | expect(miniPlayerSource()).toBeUndefined(); |
| 104 | |
| 105 | applyDeviceSnapshot(ref.environmentId, deviceState({ sessions: [session] }), floating); |
| 106 | expect(miniPlayerSource()).toMatchObject({ kind: "device", name: "Pixel 8" }); |
| 107 | }); |
| 108 | |
| 109 | it("opens a panel tab once when floating previews are off, and nothing in sheet layouts", () => { |
| 110 | setup(); |
| 111 | const panel = { sheetLayout: false, autoShowFloatingPreview: false }; |
| 112 | applyDeviceSnapshot(ref.environmentId, deviceState({}), { ...panel, sheetLayout: true }); |
| 113 | applyDeviceSnapshot(ref.environmentId, deviceState({ sessions: [session] }), { |
| 114 | ...panel, |
| 115 | sheetLayout: true, |
| 116 | }); |
| 117 | expect(useRightPanelStore.getState().byThreadKey[threadKey]).toBeUndefined(); |
| 118 | |
| 119 | applyDeviceSnapshot(ref.environmentId, deviceState({}), panel); |
| 120 | applyDeviceSnapshot(ref.environmentId, deviceState({ sessions: [session] }), panel); |
| 121 | applyDeviceSnapshot(ref.environmentId, deviceState({}), panel); |
| 122 | applyDeviceSnapshot(ref.environmentId, deviceState({ sessions: [session] }), panel); |
| 123 | const devices = useRightPanelStore |
| 124 | .getState() |
| 125 | .byThreadKey[threadKey]?.surfaces.filter((surface) => surface.kind === "device"); |
| 126 | expect(devices).toHaveLength(1); |
| 127 | expect(miniPlayerSource()).toBeUndefined(); |
| 128 | }); |
| 129 | |
| 130 | it("closes a floating device when its session ends, even before a baseline", () => { |
| 131 | setup(); |
| 132 | usePreviewMiniPlayerStore.getState().open(ref, { |
| 133 | kind: "device", |
| 134 | hostId: "local", |
| 135 | deviceId: "pixel", |
| 136 | platform: "android", |
| 137 | name: "Pixel 8", |
| 138 | }); |
| 139 | applyDeviceSnapshot(ref.environmentId, deviceState({ sessions: [session] }), floating); |
| 140 | expect(miniPlayerSource()).toMatchObject({ kind: "device" }); |
| 141 | |
| 142 | applyDeviceSnapshot(ref.environmentId, deviceState({ sessions: [] }), floating); |
| 143 | expect(miniPlayerSource()).toBeUndefined(); |
| 144 | }); |
| 145 | |
| 146 | it("opens a session that ended and reopened between two snapshots", () => { |
| 147 | setup(); |
| 148 | applyDeviceSnapshot(ref.environmentId, deviceState({ sessions: [session] }), floating); |
| 149 | expect(miniPlayerSource()).toBeUndefined(); |
| 150 | |
| 151 | const reopened = { ...session, openedAt: "2026-01-01T00:05:00.000Z" }; |
| 152 | applyDeviceSnapshot(ref.environmentId, deviceState({ sessions: [reopened] }), floating); |
| 153 | expect(miniPlayerSource()).toMatchObject({ kind: "device", deviceId: "pixel" }); |
| 154 | }); |
| 155 | |
| 156 | it("opens a device for a thread that had no sessions when the baseline was taken", () => { |
| 157 | setup(); |
| 158 | const otherRef = scopeThreadRef(ref.environmentId, ThreadId.make("thread-2")); |
| 159 | applyDeviceSnapshot(ref.environmentId, deviceState({ sessions: [session] }), floating); |
| 160 | applyDeviceSnapshot( |
| 161 | ref.environmentId, |
| 162 | deviceState({ sessions: [session, { ...session, threadId: otherRef.threadId }] }), |
| 163 | floating, |
| 164 | ); |
| 165 | expect(miniPlayerSource()).toBeUndefined(); |
| 166 | expect( |
| 167 | usePreviewMiniPlayerStore.getState().byThreadKey[scopedThreadKey(otherRef)]?.source, |
| 168 | ).toMatchObject({ kind: "device", deviceId: "pixel" }); |
| 169 | }); |
| 170 | }); |
| 171 | |
| 172 | describe("floatDeviceSurface", () => { |
| 173 | const source = { |
| 174 | kind: "device", |
| 175 | hostId: "local", |
| 176 | deviceId: "pixel", |
| 177 | platform: "android", |
| 178 | name: "Pixel 8", |
| 179 | } as const; |
| 180 | |
| 181 | it("floats a device with a live session, or before device state loads", () => { |
| 182 | setup(); |
| 183 | floatDeviceSurface(ref, source, deviceState({ sessions: [session] })); |
| 184 | expect(miniPlayerSource()).toMatchObject({ kind: "device", deviceId: "pixel" }); |
| 185 | |
| 186 | setup(); |
| 187 | floatDeviceSurface(ref, source, null); |
| 188 | expect(miniPlayerSource()).toMatchObject({ kind: "device", deviceId: "pixel" }); |
| 189 | }); |
| 190 | |
| 191 | it("does not float a device whose session ended", () => { |
| 192 | setup(); |
| 193 | floatDeviceSurface(ref, source, deviceState({ sessions: [] })); |
| 194 | expect(miniPlayerSource()).toBeUndefined(); |
| 195 | }); |
| 196 | }); |
| 197 | |
| 198 | describe("revealServerTabs", () => { |
| 199 | const tab = (tabId: string, force?: boolean): PreviewSessionSnapshot => ({ |
| 200 | threadId: "thread-1", |
| 201 | tabId, |
| 202 | navStatus: { _tag: "Loading", url: "http://localhost:5173/", title: "" }, |
| 203 | canGoBack: false, |
| 204 | canGoForward: false, |
| 205 | updatedAt: "2026-01-01T00:00:00.000Z", |
| 206 | runtime: "server", |
| 207 | reveal: true, |
| 208 | ...(force === undefined ? {} : { revealRequest: { id: "r1", force } }), |
| 209 | }); |
| 210 | const floatingTabId = () => { |
| 211 | const source = miniPlayerSource(); |
| 212 | return source?.kind === "browser" ? source.tabId : null; |
| 213 | }; |
| 214 | |
| 215 | it("floats revealed tabs, and only forced ones when floating previews are off", () => { |
| 216 | setup(); |
| 217 | revealServerTabs(ref, [tab("tab_plain")], { autoShowFloatingPreview: false }); |
| 218 | expect(floatingTabId()).toBeNull(); |
| 219 | revealServerTabs(ref, [tab("tab_forced", true)], { autoShowFloatingPreview: false }); |
| 220 | expect(floatingTabId()).toBe("tab_forced"); |
| 221 | }); |
| 222 | |
| 223 | it("focuses a tab already in the panel instead of floating it when forced", () => { |
| 224 | setup(); |
| 225 | useRightPanelStore.getState().openBrowser(ref, "tab_a"); |
| 226 | const browserSurfaceId = useRightPanelStore.getState().byThreadKey[threadKey]?.activeSurfaceId; |
| 227 | useRightPanelStore.getState().open(ref, "diff"); |
| 228 | |
| 229 | revealServerTabs(ref, [tab("tab_a", false)], { autoShowFloatingPreview: true }); |
| 230 | expect(useRightPanelStore.getState().byThreadKey[threadKey]?.activeSurfaceId).not.toBe( |
| 231 | browserSurfaceId, |
| 232 | ); |
| 233 | const userActionRevision = useRightPanelStore.getState().getUserActionRevision(ref); |
| 234 | revealServerTabs(ref, [tab("tab_a", true)], { autoShowFloatingPreview: true }); |
| 235 | expect(useRightPanelStore.getState().byThreadKey[threadKey]?.activeSurfaceId).toBe( |
| 236 | browserSurfaceId, |
| 237 | ); |
| 238 | expect(floatingTabId()).toBeNull(); |
| 239 | // A user action moves keyboard focus into the panel; an agent reveal must not. |
| 240 | expect(useRightPanelStore.getState().getUserActionRevision(ref)).toBe(userActionRevision); |
| 241 | }); |
| 242 | }); |
| 243 | |
| 244 | describe("followPreviewSessions", () => { |
| 245 | const snapshot = (tabId: string, reveal = true): PreviewSessionSnapshot => ({ |
| 246 | threadId: "thread-1", |
| 247 | tabId, |
| 248 | navStatus: { _tag: "Loading", url: "http://localhost:5173/", title: "" }, |
| 249 | canGoBack: false, |
| 250 | canGoForward: false, |
| 251 | updatedAt: "2026-01-01T00:00:00.000Z", |
| 252 | runtime: "server", |
| 253 | reveal, |
| 254 | }); |
| 255 | const previewState = (sessions: ReadonlyArray<PreviewSessionSnapshot>, listLoaded = true) => ({ |
| 256 | ...previewStateTesting.EMPTY_THREAD_PREVIEW_STATE, |
| 257 | listLoaded, |
| 258 | sessions: Object.fromEntries(sessions.map((entry) => [entry.tabId, entry])), |
| 259 | }); |
| 260 | const browserTabs = () => |
| 261 | useRightPanelStore |
| 262 | .getState() |
| 263 | .byThreadKey[threadKey]?.surfaces.filter((surface) => surface.kind === "preview") |
| 264 | .map((surface) => surface.id) ?? []; |
| 265 | |
| 266 | it("adds visible sessions as tabs and drops tabs whose session ended", () => { |
| 267 | setup(); |
| 268 | useRightPanelStore.getState().openBrowser(ref, "tab_gone"); |
| 269 | followPreviewSessions(ref, previewState([snapshot("tab_a"), snapshot("tab_hidden", false)])); |
| 270 | expect(browserTabs()).toEqual(["browser:tab_a"]); |
| 271 | }); |
| 272 | |
| 273 | it("waits for the first server list before treating missing sessions as closed", () => { |
| 274 | setup(); |
| 275 | useRightPanelStore.getState().openBrowser(ref, "tab_a"); |
| 276 | usePreviewMiniPlayerStore.getState().open(ref, browserMiniPlayerSource("tab_a")); |
| 277 | followPreviewSessions(ref, previewState([], false)); |
| 278 | expect(browserTabs()).toEqual(["browser:tab_a"]); |
| 279 | expect(miniPlayerSource()).toMatchObject({ kind: "browser", tabId: "tab_a" }); |
| 280 | |
| 281 | followPreviewSessions(ref, previewState([])); |
| 282 | expect(browserTabs()).toEqual([]); |
| 283 | expect(miniPlayerSource()).toBeUndefined(); |
| 284 | }); |
| 285 | }); |
| 286 | |
| 287 | describe("startAgentOpenedPreviews", () => { |
| 288 | const serverEpoch = "server-a"; |
| 289 | const serverTab = (overrides: Partial<PreviewSessionSnapshot> = {}): PreviewSessionSnapshot => ({ |
| 290 | threadId: "thread-1", |
| 291 | tabId: "tab_a", |
| 292 | navStatus: { _tag: "Loading", url: "http://localhost:5173/", title: "" }, |
| 293 | canGoBack: false, |
| 294 | canGoForward: false, |
| 295 | updatedAt: "2026-01-01T00:00:00.000Z", |
| 296 | runtime: "server", |
| 297 | reveal: true, |
| 298 | ...overrides, |
| 299 | }); |
| 300 | const following = () => { |
| 301 | setup(); |
| 302 | return { [Symbol.dispose]: startAgentOpenedPreviews() }; |
| 303 | }; |
| 304 | const navigated = (revision: number, snapshot: PreviewSessionSnapshot) => |
| 305 | applyPreviewServerEvent(ref, { |
| 306 | type: "navigated", |
| 307 | threadId: "thread-1", |
| 308 | tabId: snapshot.tabId, |
| 309 | createdAt: snapshot.updatedAt, |
| 310 | snapshot, |
| 311 | serverEpoch, |
| 312 | revision, |
| 313 | }); |
| 314 | const floatingTabId = () => { |
| 315 | const source = miniPlayerSource(); |
| 316 | return source?.kind === "browser" ? source.tabId : null; |
| 317 | }; |
| 318 | const browserTabs = (threadKeyToRead = threadKey) => |
| 319 | useRightPanelStore |
| 320 | .getState() |
| 321 | .byThreadKey[threadKeyToRead]?.surfaces.filter((surface) => surface.kind === "preview") |
| 322 | .map((surface) => surface.id); |
| 323 | |
| 324 | it("treats the first list as a baseline and floats tabs opened later", () => { |
| 325 | using _following = following(); |
| 326 | reconcilePreviewServerSessions(ref, { |
| 327 | sessions: [serverTab({ tabId: "tab_restored" })], |
| 328 | serverEpoch, |
| 329 | revision: 1, |
| 330 | }); |
| 331 | expect(floatingTabId()).toBeNull(); |
| 332 | expect(browserTabs()).toEqual(["browser:tab_restored"]); |
| 333 | |
| 334 | const opened = serverTab({ tabId: "tab_agent" }); |
| 335 | applyPreviewServerEvent(ref, { |
| 336 | type: "opened", |
| 337 | threadId: "thread-1", |
| 338 | tabId: opened.tabId, |
| 339 | createdAt: opened.updatedAt, |
| 340 | snapshot: opened, |
| 341 | serverEpoch, |
| 342 | revision: 2, |
| 343 | }); |
| 344 | expect(floatingTabId()).toBe("tab_agent"); |
| 345 | }); |
| 346 | |
| 347 | it("reveals an existing tab only for a new reveal request", () => { |
| 348 | using _following = following(); |
| 349 | const tab = serverTab({ revealRequest: { id: "r1", force: false } }); |
| 350 | reconcilePreviewServerSessions(ref, { sessions: [tab], serverEpoch, revision: 1 }); |
| 351 | |
| 352 | navigated(2, { ...tab, updatedAt: "2026-01-01T00:00:01.000Z" }); |
| 353 | expect(floatingTabId()).toBeNull(); |
| 354 | |
| 355 | navigated(3, { |
| 356 | ...tab, |
| 357 | revealRequest: { id: "r2", force: true }, |
| 358 | updatedAt: "2026-01-01T00:00:02.000Z", |
| 359 | }); |
| 360 | expect(useRightPanelStore.getState().byThreadKey[threadKey]?.activeSurfaceId).toBe( |
| 361 | "browser:tab_a", |
| 362 | ); |
| 363 | }); |
| 364 | |
| 365 | it("never reveals tabs this client opened, desktop tabs, or a tab whose close failed", () => { |
| 366 | using _following = following(); |
| 367 | const tab = serverTab({ tabId: "tab_closing" }); |
| 368 | reconcilePreviewServerSessions(ref, { sessions: [tab], serverEpoch, revision: 1 }); |
| 369 | applyPreviewServerSnapshot(ref, serverTab({ tabId: "tab_own", reveal: undefined })); |
| 370 | applyPreviewServerSnapshot(ref, { ...serverTab({ tabId: "tab_desktop" }), runtime: "desktop" }); |
| 371 | beginPreviewSessionClose(ref, tab.tabId); |
| 372 | cancelPreviewSessionClose(ref, tab, tab.tabId); |
| 373 | expect(floatingTabId()).toBeNull(); |
| 374 | }); |
| 375 | |
| 376 | it("follows background threads and closes the floating browser when its session ends", () => { |
| 377 | using _following = following(); |
| 378 | const other = scopeThreadRef("env-1" as EnvironmentId, ThreadId.make("thread-2")); |
| 379 | reconcilePreviewServerSessions(other, { |
| 380 | sessions: [serverTab({ threadId: "thread-2", tabId: "tab_b" })], |
| 381 | serverEpoch, |
| 382 | revision: 1, |
| 383 | }); |
| 384 | expect(browserTabs(scopedThreadKey(other))).toEqual(["browser:tab_b"]); |
| 385 | |
| 386 | reconcilePreviewServerSessions(ref, { sessions: [], serverEpoch, revision: 1 }); |
| 387 | const opened = serverTab(); |
| 388 | applyPreviewServerEvent(ref, { |
| 389 | type: "opened", |
| 390 | threadId: "thread-1", |
| 391 | tabId: opened.tabId, |
| 392 | createdAt: opened.updatedAt, |
| 393 | snapshot: opened, |
| 394 | serverEpoch, |
| 395 | revision: 2, |
| 396 | }); |
| 397 | expect(floatingTabId()).toBe("tab_a"); |
| 398 | applyPreviewServerEvent(ref, { |
| 399 | type: "closed", |
| 400 | threadId: "thread-1", |
| 401 | tabId: opened.tabId, |
| 402 | createdAt: opened.updatedAt, |
| 403 | serverEpoch, |
| 404 | revision: 3, |
| 405 | }); |
| 406 | expect(floatingTabId()).toBeNull(); |
| 407 | }); |
| 408 | }); |