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

PreviewView.test.tsx

330 lines10.0 KB

1import { EnvironmentId, ThreadId } from "@t3tools/contracts";
2import { renderToStaticMarkup } from "react-dom/server";
3import { beforeEach, describe, expect, it, vi } from "vite-plus/test";
4​
5const mocks = vi.hoisted(() => ({
6 navigate: vi.fn(async (_tabId: string, _url: string): Promise<void> => undefined),
7 rememberPreviewUrl: vi.fn(),
8 readPreparedConnection: vi.fn(() => ({ httpBaseUrl: "http://172.25.85.75:3773" })),
9 submittedUrl: null as ((url: string) => void) | null,
10 emptyStateUrl: null as ((url: string) => void) | null,
11 togglePictureInPicture: null as (() => void) | null,
12 toggleNativePictureInPicture: null as (() => void) | null,
13 pictureInPicturePressed: false,
14 miniPlayerTabId: null as string | null,
15 openMiniPlayer: vi.fn(),
16 closeMiniPlayer: vi.fn(),
17 closeRightPanel: vi.fn(),
18 openPictureInPicture: vi.fn(async (_tabId: string): Promise<void> => undefined),
19 closePictureInPicture: vi.fn(async (_tabId: string): Promise<void> => undefined),
20 pictureInPicture: false,
21 showEmptyState: false,
22}));
23​
24vi.mock("~/state/session", () => ({
25 readPreparedConnection: mocks.readPreparedConnection,
26}));
27​
28vi.mock("~/composerDraftStore", () => ({
29 useComposerDraftStore: (
30 select: (store: { addPreviewAnnotation: () => void; addImage: () => void }) => unknown,
31 ) => select({ addPreviewAnnotation: vi.fn(), addImage: vi.fn() }),
32}));
33​
34vi.mock("~/lib/previewAnnotation", () => ({
35 previewAnnotationScreenshotFile: vi.fn(),
36}));
37​
38vi.mock("~/localApi", () => ({
39 ensureLocalApi: vi.fn(),
40}));
41​
42vi.mock("~/previewStateStore", () => ({
43 rememberPreviewUrl: mocks.rememberPreviewUrl,
44 updatePreviewServerSnapshot: vi.fn(),
45 useThreadPreviewState: () => ({
46 activeTabId: "tab-1",
47 desktopByTabId: {
48 "tab-1": {
49 hasWebContents: true,
50 canGoBack: false,
51 canGoForward: false,
52 loading: false,
53 zoomFactor: 1,
54 pictureInPicture: mocks.pictureInPicture,
55 colorScheme: "system",
56 controller: "none",
57 },
58 },
59 recentlySeenUrls: [],
60 sessions: mocks.showEmptyState
61 ? {}
62 : {
63 "tab-1": {
64 threadId: "thread-1",
65 tabId: "tab-1",
66 navStatus: {
67 _tag: "Success",
68 url: "http://example.com/",
69 title: "Example",
70 },
71 canGoBack: false,
72 canGoForward: false,
73 updatedAt: "2026-07-13T00:00:00.000Z",
74 },
75 },
76 }),
77}));
78​
79vi.mock("~/state/environments", () => ({
80 useEnvironment: () => ({ label: "WSL" }),
81 useEnvironmentHttpBaseUrl: () => "http://172.25.85.75:3773",
82}));
83​
84vi.mock("~/state/preview", () => ({
85 previewEnvironment: { open: {}, resize: {} },
86}));
87​
88vi.mock("~/state/use-atom-command", () => ({
89 useAtomCommand: () => vi.fn(),
90}));
91​
92vi.mock("~/browser/browserRecording", () => ({
93 findActiveBrowserRecordingRuntimeTabId: vi.fn(() => null),
94 startBrowserRecording: vi.fn(),
95 stopBrowserRecording: vi.fn(),
96 useActiveBrowserRecordingTabIds: () => new Set(),
97}));
98​
99vi.mock("~/browser/browserSurfaceStore", () => ({
100 useBrowserSurfaceStore: (
101 select: (state: { byTabId: Record<string, { rect?: unknown }> }) => unknown,
102 ) => select({ byTabId: {} }),
103}));
104​
105vi.mock("~/previewMiniPlayerStore", () => {
106 const usePreviewMiniPlayerStore = Object.assign(
107 (select: (state: unknown) => unknown) =>
108 select({
109 byThreadKey: mocks.miniPlayerTabId
110 ? {
111 "environment-1:thread-1": {
112 tabId: mocks.miniPlayerTabId,
113 position: null,
114 },
115 }
116 : {},
117 }),
118 {
119 getState: () => ({
120 open: mocks.openMiniPlayer,
121 close: mocks.closeMiniPlayer,
122 }),
123 },
124 );
125 return {
126 selectThreadPreviewMiniPlayer: (
127 byThreadKey: Record<string, { tabId: string; position: null }>,
128 ) => byThreadKey["environment-1:thread-1"] ?? null,
129 usePreviewMiniPlayerStore,
130 };
131});
132​
133vi.mock("~/rightPanelStore", () => ({
134 useRightPanelStore: {
135 getState: () => ({ close: mocks.closeRightPanel }),
136 },
137}));
138​
139vi.mock("~/components/ui/toast", () => ({
140 stackedThreadToast: vi.fn(),
141 toastManager: { add: vi.fn() },
142}));
143​
144vi.mock("./previewBridge", () => ({
145 previewBridge: {
146 navigate: mocks.navigate,
147 pictureInPicture: {
148 open: mocks.openPictureInPicture,
149 close: mocks.closePictureInPicture,
150 },
151 },
152}));
153​
154vi.mock("./PreviewChromeRow", () => ({
155 PreviewChromeRow: (props: {
156 onSubmit: (url: string) => void;
157 onPictureInPicture?: () => void;
158 pictureInPicture?: boolean;
159 trailingActions?: {
160 props: { onNativePictureInPicture?: () => void };
161 };
162 }) => {
163 mocks.submittedUrl = props.onSubmit;
164 mocks.togglePictureInPicture = props.onPictureInPicture ?? null;
165 mocks.toggleNativePictureInPicture =
166 props.trailingActions?.props.onNativePictureInPicture ?? null;
167 mocks.pictureInPicturePressed = props.pictureInPicture ?? false;
168 return null;
169 },
170}));
171​
172vi.mock("./PreviewEmptyState", () => ({
173 PreviewEmptyState: (props: { onOpenUrl: (url: string) => void }) => {
174 mocks.emptyStateUrl = props.onOpenUrl;
175 return null;
176 },
177}));
178vi.mock("./PreviewMoreMenu", () => ({
179 PreviewMoreMenu: (props: { onNativePictureInPicture: () => void }) => {
180 mocks.toggleNativePictureInPicture = props.onNativePictureInPicture;
181 return null;
182 },
183}));
184vi.mock("./PreviewUnreachable", () => ({ PreviewUnreachable: () => null }));
185vi.mock("./ZoomIndicator", () => ({ ZoomIndicator: () => null }));
186vi.mock("./AgentBrowserCursor", () => ({ AgentBrowserCursor: () => null }));
187vi.mock("~/browser/BrowserSurfaceSlot", () => ({ BrowserSurfaceSlot: () => null }));
188vi.mock("./useLoadingProgress", () => ({ useLoadingProgress: () => 0 }));
189vi.mock("./usePreviewSession", () => ({ usePreviewSession: vi.fn() }));
190​
191import { PreviewView } from "./PreviewView";
192import { previewRuntimeTabId } from "~/browser/previewRuntimeTabId";
193​
194const TEST_THREAD_REF = {
195 environmentId: EnvironmentId.make("environment-1"),
196 threadId: ThreadId.make("thread-1"),
197} as const;
198const TEST_RUNTIME_TAB_ID = previewRuntimeTabId(TEST_THREAD_REF, null, "tab-1");
199​
200describe("PreviewView navigation", () => {
201 beforeEach(() => {
202 mocks.navigate.mockClear();
203 mocks.rememberPreviewUrl.mockClear();
204 mocks.readPreparedConnection.mockClear();
205 mocks.submittedUrl = null;
206 mocks.emptyStateUrl = null;
207 mocks.togglePictureInPicture = null;
208 mocks.toggleNativePictureInPicture = null;
209 mocks.pictureInPicturePressed = false;
210 mocks.miniPlayerTabId = null;
211 mocks.openMiniPlayer.mockClear();
212 mocks.closeMiniPlayer.mockClear();
213 mocks.closeRightPanel.mockClear();
214 mocks.openPictureInPicture.mockClear();
215 mocks.closePictureInPicture.mockClear();
216 mocks.pictureInPicture = false;
217 mocks.showEmptyState = false;
218 });
219​
220 it.each([
221 [
222 "https://localhost:8000/dashboard?mode=test#top",
223 "https://localhost:8000/dashboard?mode=test#top",
224 ],
225 ["localhost:5173/app", "http://localhost:5173/app"],
226 ])("preserves a direct localhost URL in a WSL environment", async (submitted, expected) => {
227 renderToStaticMarkup(
228 <PreviewView
229 threadRef={{
230 environmentId: EnvironmentId.make("environment-1"),
231 threadId: ThreadId.make("thread-1"),
232 }}
233 tabId="tab-1"
234 visible
235 />,
236 );
237​
238 expect(mocks.submittedUrl).not.toBeNull();
239 mocks.submittedUrl?.(submitted);
240​
241 await vi.waitFor(() =>
242 expect(mocks.navigate).toHaveBeenCalledWith(TEST_RUNTIME_TAB_ID, expected),
243 );
244 expect(mocks.rememberPreviewUrl).toHaveBeenCalledWith(
245 {
246 environmentId: "environment-1",
247 threadId: "thread-1",
248 },
249 expected,
250 );
251 });
252​
253 it("maps an empty-state localhost server onto the WSL host", async () => {
254 mocks.showEmptyState = true;
255 renderToStaticMarkup(
256 <PreviewView
257 threadRef={{
258 environmentId: EnvironmentId.make("environment-1"),
259 threadId: ThreadId.make("thread-1"),
260 }}
261 tabId="tab-1"
262 visible
263 />,
264 );
265​
266 expect(mocks.emptyStateUrl).not.toBeNull();
267 mocks.emptyStateUrl?.("http://localhost:5173/app?mode=test#top");
268​
269 await vi.waitFor(() =>
270 expect(mocks.navigate).toHaveBeenCalledWith(
271 TEST_RUNTIME_TAB_ID,
272 "http://172.25.85.75:5173/app?mode=test#top",
273 ),
274 );
275 expect(mocks.rememberPreviewUrl).toHaveBeenCalledWith(
276 {
277 environmentId: "environment-1",
278 threadId: "thread-1",
279 },
280 "http://172.25.85.75:5173/app?mode=test#top",
281 );
282 });
283​
284 it("opens and closes a thread-scoped floating preview for the active tab", async () => {
285 const props = {
286 threadRef: {
287 environmentId: EnvironmentId.make("environment-1"),
288 threadId: ThreadId.make("thread-1"),
289 },
290 tabId: "tab-1",
291 visible: true,
292 } as const;
293​
294 renderToStaticMarkup(<PreviewView {...props} />);
295 expect(mocks.pictureInPicturePressed).toBe(false);
296 mocks.togglePictureInPicture?.();
297 expect(mocks.openMiniPlayer).toHaveBeenCalledWith(props.threadRef, "tab-1");
298 expect(mocks.closeRightPanel).toHaveBeenCalledWith(props.threadRef);
299​
300 mocks.miniPlayerTabId = "tab-1";
301 renderToStaticMarkup(<PreviewView {...props} />);
302 expect(mocks.pictureInPicturePressed).toBe(true);
303 mocks.togglePictureInPicture?.();
304 expect(mocks.closeMiniPlayer).toHaveBeenCalledWith(props.threadRef);
305 });
306​
307 it("keeps the native preview window as a secondary action", async () => {
308 const props = {
309 threadRef: {
310 environmentId: EnvironmentId.make("environment-1"),
311 threadId: ThreadId.make("thread-1"),
312 },
313 tabId: "tab-1",
314 visible: true,
315 } as const;
316​
317 renderToStaticMarkup(<PreviewView {...props} />);
318 mocks.toggleNativePictureInPicture?.();
319 await vi.waitFor(() =>
320 expect(mocks.openPictureInPicture).toHaveBeenCalledWith(TEST_RUNTIME_TAB_ID),
321 );
322​
323 mocks.pictureInPicture = true;
324 renderToStaticMarkup(<PreviewView {...props} />);
325 mocks.toggleNativePictureInPicture?.();
326 await vi.waitFor(() =>
327 expect(mocks.closePictureInPicture).toHaveBeenCalledWith(TEST_RUNTIME_TAB_ID),
328 );
329 });
330});