The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import "~/index.css"; |
| 2 | |
| 3 | import { RegistryContext } from "@effect/atom-react"; |
| 4 | import { scopeThreadRef } from "@t3tools/client-runtime/environment"; |
| 5 | import { type DeviceServiceState, type EnvironmentId, ThreadId } from "@t3tools/contracts"; |
| 6 | import { AsyncResult, Atom } from "effect/reactivity"; |
| 7 | import { useState } from "react"; |
| 8 | import { expect, it, vi } from "vite-plus/test"; |
| 9 | |
| 10 | import { act, render } from "../../../test/render"; |
| 11 | import { |
| 12 | type PreviewMiniPlayerSource, |
| 13 | selectThreadPreviewMiniPlayer, |
| 14 | usePreviewMiniPlayerStore, |
| 15 | } from "../../previewMiniPlayerStore"; |
| 16 | import { appAtomRegistry } from "../../rpc/atomRegistry"; |
| 17 | import { deviceEnvironment } from "../../state/device"; |
| 18 | import { ChatCanvas } from "../chat/ChatCanvas"; |
| 19 | import { createChatCanvasStore } from "../chat/canvasStore"; |
| 20 | import { deviceStreamScreenAtom } from "../device/deviceStreamSession"; |
| 21 | import { ThreadPreviewMiniPlayer, useThreadPreviewCanvasSource } from "./ThreadPreviewMiniPlayer"; |
| 22 | |
| 23 | vi.mock(import("../device/DeviceStreamView"), () => ({ |
| 24 | DeviceStreamView: (({ viewId }: { viewId: string }) => ( |
| 25 | <div data-testid="stream" data-view-id={viewId} /> |
| 26 | )) as never, |
| 27 | })); |
| 28 | |
| 29 | const threadRef = scopeThreadRef("env-1" as EnvironmentId, ThreadId.make("thread-1")); |
| 30 | const phone: PreviewMiniPlayerSource = { |
| 31 | kind: "device", |
| 32 | hostId: "local", |
| 33 | deviceId: "phone", |
| 34 | platform: "ios", |
| 35 | name: "Phone", |
| 36 | }; |
| 37 | |
| 38 | function Harness() { |
| 39 | const [store] = useState(createChatCanvasStore); |
| 40 | const miniPlayer = usePreviewMiniPlayerStore((state) => |
| 41 | selectThreadPreviewMiniPlayer(state.byThreadKey, threadRef), |
| 42 | ); |
| 43 | const preview = useThreadPreviewCanvasSource(threadRef, miniPlayer); |
| 44 | return ( |
| 45 | <ChatCanvas store={store} threadRef={threadRef} preview={preview} composerOverlayElement={null}> |
| 46 | {miniPlayer ? ( |
| 47 | <ThreadPreviewMiniPlayer threadRef={threadRef} miniPlayer={miniPlayer} /> |
| 48 | ) : null} |
| 49 | </ChatCanvas> |
| 50 | ); |
| 51 | } |
| 52 | |
| 53 | it("holds its frame through a source change and stops when the gesture is interrupted", async () => { |
| 54 | using _state = vi |
| 55 | .spyOn(deviceEnvironment, "state") |
| 56 | .mockReturnValue(Atom.make<AsyncResult.AsyncResult<DeviceServiceState>>(AsyncResult.initial())); |
| 57 | using _store = { |
| 58 | [Symbol.dispose]: () => usePreviewMiniPlayerStore.getState().removeThread(threadRef), |
| 59 | }; |
| 60 | usePreviewMiniPlayerStore.getState().open(threadRef, phone); |
| 61 | const container = document.createElement("div"); |
| 62 | container.style.cssText = "display:flex;position:fixed;inset:0;width:1200px;height:800px"; |
| 63 | await using screen = await render( |
| 64 | <RegistryContext.Provider value={appAtomRegistry}> |
| 65 | <Harness /> |
| 66 | </RegistryContext.Provider>, |
| 67 | { container }, |
| 68 | ); |
| 69 | const player = screen.container.querySelector<HTMLElement>("[data-preview-mini-player]")!; |
| 70 | const handle = (direction: string) => |
| 71 | player.querySelector<HTMLElement>(`[data-preview-mini-player-resize="${direction}"]`)!; |
| 72 | const rect = () => { |
| 73 | const { left, top, right, bottom } = player.getBoundingClientRect(); |
| 74 | return { left, top, right, bottom }; |
| 75 | }; |
| 76 | const point = (target: HTMLElement, type: string, x: number, y: number, buttons: number) => |
| 77 | act(() => |
| 78 | target.dispatchEvent( |
| 79 | new PointerEvent(type, { |
| 80 | pointerId: 1, |
| 81 | pointerType: "mouse", |
| 82 | isPrimary: true, |
| 83 | button: type === "pointermove" ? -1 : 0, |
| 84 | buttons, |
| 85 | clientX: x, |
| 86 | clientY: y, |
| 87 | bubbles: true, |
| 88 | cancelable: true, |
| 89 | }), |
| 90 | ), |
| 91 | ); |
| 92 | const viewId = screen.getByTestId("stream").element().getAttribute("data-view-id")!; |
| 93 | const portrait = rect(); |
| 94 | |
| 95 | // Held without moving while the device rotates: the frame waits for the release. |
| 96 | await point(handle("northwest"), "pointerdown", portrait.left, portrait.top, 1); |
| 97 | await act(() => |
| 98 | appAtomRegistry.set(deviceStreamScreenAtom(viewId), { |
| 99 | width: 1_000, |
| 100 | height: 2_000, |
| 101 | orientation: "landscape_left", |
| 102 | }), |
| 103 | ); |
| 104 | expect(rect()).toEqual(portrait); |
| 105 | |
| 106 | // The release never arrived, so the next buttonless move ends the gesture where it was held. |
| 107 | await point(handle("northwest"), "pointermove", portrait.left - 200, portrait.top - 200, 0); |
| 108 | const landscape = rect(); |
| 109 | expect({ right: landscape.right, bottom: landscape.bottom }).toEqual({ |
| 110 | right: portrait.right, |
| 111 | bottom: portrait.bottom, |
| 112 | }); |
| 113 | expect(landscape.right - landscape.left).toBe(2 * (landscape.bottom - landscape.top)); |
| 114 | await point(handle("northwest"), "pointermove", portrait.left - 300, portrait.top - 300, 1); |
| 115 | expect(rect()).toEqual(landscape); |
| 116 | |
| 117 | await point(handle("west"), "pointerdown", landscape.left, landscape.top + 10, 1); |
| 118 | await act(() => window.dispatchEvent(new Event("blur"))); |
| 119 | await point(handle("west"), "pointermove", landscape.left - 100, landscape.top + 10, 1); |
| 120 | expect(rect()).toEqual(landscape); |
| 121 | }); |