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

ThreadPreviewMiniPlayer.browser.test.tsx

198 lines7.9 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 { ComposerCommandMenuLayer } from "../chat/ComposerCommandMenuLayer";
25import { createChatCanvasStore } from "../chat/canvasStore";
26import { deviceStreamScreenAtom } from "../device/deviceStreamSession";
27import { ThreadPreviewMiniPlayer, useThreadPreviewCanvasSource } from "./ThreadPreviewMiniPlayer";
28​
29vi.mock(import("../device/DeviceStreamView"), () => ({
30 DeviceStreamView: (({ viewId }: { viewId: string }) => (
31 <div data-testid="stream" data-view-id={viewId} />
32 )) as never,
33}));
34​
35const threadRef = scopeThreadRef("env-1" as EnvironmentId, ThreadId.make("thread-1"));
36const defaultThreadRef = threadRef;
37const phone: PreviewMiniPlayerSource = {
38 kind: "device",
39 hostId: "local",
40 deviceId: "phone",
41 platform: "ios",
42 name: "Phone",
43};
44​
45function Harness({ threadRef = defaultThreadRef }: { readonly threadRef?: ScopedThreadRef }) {
46 const [store] = useState(createChatCanvasStore);
47 const miniPlayer = usePreviewMiniPlayerStore((state) =>
48 selectThreadPreviewMiniPlayer(state.byThreadKey, threadRef),
49 );
50 const preview = useThreadPreviewCanvasSource(threadRef, miniPlayer);
51 return (
52 <ChatCanvas
53 store={store}
54 rightPanelOpen={false}
55 threadRef={threadRef}
56 preview={preview}
57 composerOverlayElement={null}
58 >
59 {miniPlayer ? (
60 <ThreadPreviewMiniPlayer threadRef={threadRef} miniPlayer={miniPlayer} />
61 ) : null}
62 </ChatCanvas>
63 );
65​
66it("holds its frame through a source change and stops when the gesture is interrupted", async () => {
67 using _state = vi
68 .spyOn(deviceEnvironment, "state")
69 .mockReturnValue(Atom.make<AsyncResult.AsyncResult<DeviceServiceState>>(AsyncResult.initial()));
70 using _store = {
71 [Symbol.dispose]: () => usePreviewMiniPlayerStore.getState().removeThread(threadRef),
72 };
73 usePreviewMiniPlayerStore.getState().open(threadRef, phone);
74 const container = document.createElement("div");
75 container.style.cssText = "display:flex;position:fixed;inset:0;width:1200px;height:800px";
76 await using screen = await render(
77 <RegistryContext.Provider value={appAtomRegistry}>
78 <Harness />
79 </RegistryContext.Provider>,
80 { container },
81 );
82 const player = screen.container.querySelector<HTMLElement>("[data-preview-mini-player]")!;
83 const handle = (direction: string) =>
84 player.querySelector<HTMLElement>(`[data-preview-mini-player-resize="${direction}"]`)!;
85 const rect = () => {
86 const { left, top, right, bottom } = player.getBoundingClientRect();
87 return { left, top, right, bottom };
88 };
89 const point = (target: HTMLElement, type: string, x: number, y: number, buttons: number) =>
90 act(() =>
91 target.dispatchEvent(
92 new PointerEvent(type, {
93 pointerId: 1,
94 pointerType: "mouse",
95 isPrimary: true,
96 button: type === "pointermove" ? -1 : 0,
97 buttons,
98 clientX: x,
99 clientY: y,
100 bubbles: true,
101 cancelable: true,
102 }),
103 ),
104 );
105 const viewId = screen.getByTestId("stream").element().getAttribute("data-view-id")!;
106 const portrait = rect();
107​
108 // Held without moving while the device rotates: the frame waits for the release.
109 await point(handle("northwest"), "pointerdown", portrait.left, portrait.top, 1);
110 await act(() =>
111 appAtomRegistry.set(deviceStreamScreenAtom(viewId), {
112 width: 1_000,
113 height: 2_000,
114 orientation: "landscape_left",
115 }),
116 );
117 expect(rect()).toEqual(portrait);
118​
119 // The release never arrived, so the next buttonless move ends the gesture where it was held.
120 await point(handle("northwest"), "pointermove", portrait.left - 200, portrait.top - 200, 0);
121 const landscape = rect();
122 expect({ right: landscape.right, bottom: landscape.bottom }).toEqual({
123 right: portrait.right,
124 bottom: portrait.bottom,
125 });
126 expect(landscape.right - landscape.left).toBe(2 * (landscape.bottom - landscape.top));
127 await point(handle("northwest"), "pointermove", portrait.left - 300, portrait.top - 300, 1);
128 expect(rect()).toEqual(landscape);
129​
130 await point(handle("west"), "pointerdown", landscape.left, landscape.top + 10, 1);
131 await act(() => window.dispatchEvent(new Event("blur")));
132 await point(handle("west"), "pointermove", landscape.left - 100, landscape.top + 10, 1);
133 expect(rect()).toEqual(landscape);
134});
135​
136it("keeps one stream when switching between threads floating the same device", async () => {
137 using _state = vi
138 .spyOn(deviceEnvironment, "state")
139 .mockReturnValue(Atom.make<AsyncResult.AsyncResult<DeviceServiceState>>(AsyncResult.initial()));
140 const otherThreadRef = scopeThreadRef("env-1" as EnvironmentId, ThreadId.make("thread-2"));
141 using _store = {
142 [Symbol.dispose]: () => {
143 usePreviewMiniPlayerStore.getState().removeThread(threadRef);
144 usePreviewMiniPlayerStore.getState().removeThread(otherThreadRef);
145 },
146 };
147 usePreviewMiniPlayerStore.getState().open(threadRef, phone);
148 usePreviewMiniPlayerStore.getState().open(otherThreadRef, phone);
149 const harness = (ref: ScopedThreadRef) => (
150 <RegistryContext.Provider value={appAtomRegistry}>
151 <Harness threadRef={ref} />
152 </RegistryContext.Provider>
153 );
154 const container = document.createElement("div");
155 container.style.cssText = "display:flex;position:fixed;inset:0;width:1200px;height:800px";
156 await using screen = await render(harness(threadRef), { container });
157 const viewId = () => screen.getByTestId("stream").element().getAttribute("data-view-id");
158 const first = viewId();
159​
160 // The stream session is keyed by view, so a new view id reconnects the device.
161 await screen.rerender(harness(otherThreadRef));
162 expect(viewId()).toBe(first);
163});
164​
165it("paints the composer suggestion menu over the floating player", async () => {
166 using _state = vi
167 .spyOn(deviceEnvironment, "state")
168 .mockReturnValue(Atom.make<AsyncResult.AsyncResult<DeviceServiceState>>(AsyncResult.initial()));
169 using _store = {
170 [Symbol.dispose]: () => usePreviewMiniPlayerStore.getState().removeThread(threadRef),
171 };
172 usePreviewMiniPlayerStore.getState().open(threadRef, phone);
173 const container = document.createElement("div");
174 container.style.cssText = "display:flex;height:100vh";
175 const composer = document.createElement("div");
176 composer.dataset.chatComposerForm = "true";
177 composer.style.cssText =
178 "position:fixed;left:0;right:0;bottom:0;height:80px;--chat-composer-drawer-inset:0rem";
179 document.body.append(composer);
180 using _composer = { [Symbol.dispose]: () => composer.remove() };
181 await using screen = await render(
182 <RegistryContext.Provider value={appAtomRegistry}>
183 <Harness />
184 <ComposerCommandMenuLayer anchor={composer}>
185 <div data-testid="menu" style={{ height: 600 }} />
186 </ComposerCommandMenuLayer>
187 </RegistryContext.Provider>,
188 { container },
189 );
190 const player = screen.container.querySelector<HTMLElement>("[data-preview-mini-player]")!;
191 const menu = document.querySelector<HTMLElement>("[data-testid=menu]")!;
192 const a = player.getBoundingClientRect();
193 const b = menu.getBoundingClientRect();
194 const x = (Math.max(a.left, b.left) + Math.min(a.right, b.right)) / 2;
195 const y = (Math.max(a.top, b.top) + Math.min(a.bottom, b.bottom)) / 2;
196 expect(x).toBeLessThan(Math.min(a.right, b.right));
197 expect(menu.contains(document.elementFromPoint(x, y))).toBe(true);
198});