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

ThreadPreviewMiniPlayer.browser.test.tsx

156 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 store={store} threadRef={threadRef} preview={preview} composerOverlayElement={null}>
52 {miniPlayer ? (
53 <ThreadPreviewMiniPlayer threadRef={threadRef} miniPlayer={miniPlayer} />
54 ) : null}
55 </ChatCanvas>
56 );
58​
59it("holds its frame through a source change and stops when the gesture is interrupted", async () => {
60 using _state = vi
61 .spyOn(deviceEnvironment, "state")
62 .mockReturnValue(Atom.make<AsyncResult.AsyncResult<DeviceServiceState>>(AsyncResult.initial()));
63 using _store = {
64 [Symbol.dispose]: () => usePreviewMiniPlayerStore.getState().removeThread(threadRef),
65 };
66 usePreviewMiniPlayerStore.getState().open(threadRef, phone);
67 const container = document.createElement("div");
68 container.style.cssText = "display:flex;position:fixed;inset:0;width:1200px;height:800px";
69 await using screen = await render(
70 <RegistryContext.Provider value={appAtomRegistry}>
71 <Harness />
72 </RegistryContext.Provider>,
73 { container },
74 );
75 const player = screen.container.querySelector<HTMLElement>("[data-preview-mini-player]")!;
76 const handle = (direction: string) =>
77 player.querySelector<HTMLElement>(`[data-preview-mini-player-resize="${direction}"]`)!;
78 const rect = () => {
79 const { left, top, right, bottom } = player.getBoundingClientRect();
80 return { left, top, right, bottom };
81 };
82 const point = (target: HTMLElement, type: string, x: number, y: number, buttons: number) =>
83 act(() =>
84 target.dispatchEvent(
85 new PointerEvent(type, {
86 pointerId: 1,
87 pointerType: "mouse",
88 isPrimary: true,
89 button: type === "pointermove" ? -1 : 0,
90 buttons,
91 clientX: x,
92 clientY: y,
93 bubbles: true,
94 cancelable: true,
95 }),
96 ),
97 );
98 const viewId = screen.getByTestId("stream").element().getAttribute("data-view-id")!;
99 const portrait = rect();
100​
101 // Held without moving while the device rotates: the frame waits for the release.
102 await point(handle("northwest"), "pointerdown", portrait.left, portrait.top, 1);
103 await act(() =>
104 appAtomRegistry.set(deviceStreamScreenAtom(viewId), {
105 width: 1_000,
106 height: 2_000,
107 orientation: "landscape_left",
108 }),
109 );
110 expect(rect()).toEqual(portrait);
111​
112 // The release never arrived, so the next buttonless move ends the gesture where it was held.
113 await point(handle("northwest"), "pointermove", portrait.left - 200, portrait.top - 200, 0);
114 const landscape = rect();
115 expect({ right: landscape.right, bottom: landscape.bottom }).toEqual({
116 right: portrait.right,
117 bottom: portrait.bottom,
118 });
119 expect(landscape.right - landscape.left).toBe(2 * (landscape.bottom - landscape.top));
120 await point(handle("northwest"), "pointermove", portrait.left - 300, portrait.top - 300, 1);
121 expect(rect()).toEqual(landscape);
122​
123 await point(handle("west"), "pointerdown", landscape.left, landscape.top + 10, 1);
124 await act(() => window.dispatchEvent(new Event("blur")));
125 await point(handle("west"), "pointermove", landscape.left - 100, landscape.top + 10, 1);
126 expect(rect()).toEqual(landscape);
127});
128​
129it("keeps one stream when switching between threads floating the same device", async () => {
130 using _state = vi
131 .spyOn(deviceEnvironment, "state")
132 .mockReturnValue(Atom.make<AsyncResult.AsyncResult<DeviceServiceState>>(AsyncResult.initial()));
133 const otherThreadRef = scopeThreadRef("env-1" as EnvironmentId, ThreadId.make("thread-2"));
134 using _store = {
135 [Symbol.dispose]: () => {
136 usePreviewMiniPlayerStore.getState().removeThread(threadRef);
137 usePreviewMiniPlayerStore.getState().removeThread(otherThreadRef);
138 },
139 };
140 usePreviewMiniPlayerStore.getState().open(threadRef, phone);
141 usePreviewMiniPlayerStore.getState().open(otherThreadRef, phone);
142 const harness = (ref: ScopedThreadRef) => (
143 <RegistryContext.Provider value={appAtomRegistry}>
144 <Harness threadRef={ref} />
145 </RegistryContext.Provider>
146 );
147 const container = document.createElement("div");
148 container.style.cssText = "display:flex;position:fixed;inset:0;width:1200px;height:800px";
149 await using screen = await render(harness(threadRef), { container });
150 const viewId = () => screen.getByTestId("stream").element().getAttribute("data-view-id");
151 const first = viewId();
152​
153 // The stream session is keyed by view, so a new view id reconnects the device.
154 await screen.rerender(harness(otherThreadRef));
155 expect(viewId()).toBe(first);
156});