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

ThreadPreviewMiniPlayer.browser.test.tsx

162 lines6.2 KB

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