Open History to see the last commit
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 pickElement: vi.fn(),
21 previewAnnotationScreenshotFile: vi.fn(),
22 addPreviewAnnotation: vi.fn(),
23 addImage: vi.fn(),
24 toggleAnnotation: null as (() => void) | null,
25 pictureInPicture: false,
26 showEmptyState: false,
27}));
28
29vi.mock("~/state/session", () => ({
30 readPreparedConnection: mocks.readPreparedConnection,
31}));
32
33vi.mock("~/composerDraftStore", () => ({
34 useComposerDraftStore: (
35 select: (store: { addPreviewAnnotation: () => void; addImage: () => void }) => unknown,
36 ) =>
37 select({
38 addPreviewAnnotation: mocks.addPreviewAnnotation,
39 addImage: mocks.addImage,
40 }),
41}));
42
43vi.mock("~/lib/previewAnnotation", () => ({
44 previewAnnotationScreenshotFile: mocks.previewAnnotationScreenshotFile,
45}));
46
47vi.mock("~/localApi", () => ({
48 ensureLocalApi: vi.fn(),
49}));
50
51vi.mock("~/previewStateStore", () => ({
52 rememberPreviewUrl: mocks.rememberPreviewUrl,
53 updatePreviewServerSnapshot: vi.fn(),
54 useThreadPreviewState: () => ({
55 activeTabId: "tab-1",
56 desktopByTabId: {
57 "tab-1": {
58 hasWebContents: true,
59 canGoBack: false,
60 canGoForward: false,
61 loading: false,
62 zoomFactor: 1,
63 pictureInPicture: mocks.pictureInPicture,
64 colorScheme: "system",
65 controller: "none",
66 },
67 },
68 recentlySeenUrls: [],
69 sessions: mocks.showEmptyState
70 ? {}
71 : {
72 "tab-1": {
73 threadId: "thread-1",
74 tabId: "tab-1",
75 navStatus: {
76 _tag: "Success",
77 url: "http://example.com/",
78 title: "Example",
79 },
80 canGoBack: false,
81 canGoForward: false,
82 updatedAt: "2026-07-13T00:00:00.000Z",
83 },
84 },
85 }),
86}));
87
88vi.mock("~/state/environments", () => ({
89 useEnvironment: () => ({ label: "WSL" }),
90 useEnvironmentHttpBaseUrl: () => "http://172.25.85.75:3773",
91}));
92
93vi.mock("~/state/preview", () => ({
94 previewEnvironment: { open: {}, resize: {} },
95}));
96
97vi.mock("~/state/use-atom-command", () => ({
98 useAtomCommand: () => vi.fn(),
99}));
100
101vi.mock("~/browser/browserRecording", () => ({
102 findActiveBrowserRecordingRuntimeTabId: vi.fn(() => null),
103 startBrowserRecording: vi.fn(),
104 stopBrowserRecording: vi.fn(),
105 useActiveBrowserRecordingTabIds: () => new Set(),
106}));
107
108vi.mock("~/browser/browserSurfaceStore", () => ({
109 useBrowserSurfaceStore: (
110 select: (state: { byTabId: Record<string, { rect?: unknown }> }) => unknown,
111 ) => select({ byTabId: {} }),
112}));
113
114vi.mock("~/previewMiniPlayerStore", () => {
115 const usePreviewMiniPlayerStore = Object.assign(
116 (select: (state: unknown) => unknown) =>
117 select({
118 byThreadKey: mocks.miniPlayerTabId
119 ? {
120 "environment-1:thread-1": {
121 tabId: mocks.miniPlayerTabId,
122 position: null,
123 },
124 }
125 : {},
126 }),
127 {
128 getState: () => ({
129 open: mocks.openMiniPlayer,
130 close: mocks.closeMiniPlayer,
131 }),
132 },
133 );
134 return {
135 selectThreadPreviewMiniPlayer: (
136 byThreadKey: Record<string, { tabId: string; position: null }>,
137 ) => byThreadKey["environment-1:thread-1"] ?? null,
138 usePreviewMiniPlayerStore,
139 };
140});
141
142vi.mock("~/rightPanelStore", () => ({
143 useRightPanelStore: {
144 getState: () => ({ close: mocks.closeRightPanel }),
145 },
146}));
147
148vi.mock("~/components/ui/toast", () => ({
149 stackedThreadToast: vi.fn(),
150 toastManager: { add: vi.fn() },
151}));
152
153vi.mock("./previewBridge", () => ({
154 previewBridge: {
155 navigate: mocks.navigate,
156 pickElement: mocks.pickElement,
157 pictureInPicture: {
158 open: mocks.openPictureInPicture,
159 close: mocks.closePictureInPicture,
160 },
161 },
162}));
163
164vi.mock("./PreviewChromeRow", () => ({
165 PreviewChromeRow: (props: {
166 onSubmit: (url: string) => void;
167 onPickElement?: () => void;
168 onPictureInPicture?: () => void;
169 pictureInPicture?: boolean;
170 trailingActions?: {
171 props: { onNativePictureInPicture?: () => void };
172 };
173 }) => {
174 mocks.submittedUrl = props.onSubmit;
175 mocks.toggleAnnotation = props.onPickElement ?? null;
176 mocks.togglePictureInPicture = props.onPictureInPicture ?? null;
177 mocks.toggleNativePictureInPicture =
178 props.trailingActions?.props.onNativePictureInPicture ?? null;
179 mocks.pictureInPicturePressed = props.pictureInPicture ?? false;
180 return null;
181 },
182}));
183
184vi.mock("./PreviewEmptyState", () => ({
185 PreviewEmptyState: (props: { onOpenUrl: (url: string) => void }) => {
186 mocks.emptyStateUrl = props.onOpenUrl;
187 return null;
188 },
189}));
190vi.mock("./PreviewMoreMenu", () => ({
191 PreviewMoreMenu: (props: { onNativePictureInPicture: () => void }) => {
192 mocks.toggleNativePictureInPicture = props.onNativePictureInPicture;
193 return null;
194 },
195}));
196vi.mock("./PreviewUnreachable", () => ({ PreviewUnreachable: () => null }));
197vi.mock("./ZoomIndicator", () => ({ ZoomIndicator: () => null }));
198vi.mock("./AgentBrowserCursor", () => ({ AgentBrowserCursor: () => null }));
199vi.mock("~/browser/BrowserSurfaceSlot", () => ({ BrowserSurfaceSlot: () => null }));
200vi.mock("./useLoadingProgress", () => ({ useLoadingProgress: () => 0 }));
201vi.mock("./usePreviewSession", () => ({ usePreviewSession: vi.fn() }));
202
203import { PreviewView } from "./PreviewView";
204import { previewRuntimeTabId } from "~/browser/previewRuntimeTabId";
205
206const TEST_THREAD_REF = {
207 environmentId: EnvironmentId.make("environment-1"),
208 threadId: ThreadId.make("thread-1"),
209} as const;
210const TEST_RUNTIME_TAB_ID = previewRuntimeTabId(TEST_THREAD_REF, null, "tab-1");
211
212describe("PreviewView navigation", () => {
213 beforeEach(() => {
214 mocks.navigate.mockClear();
215 mocks.rememberPreviewUrl.mockClear();
216 mocks.readPreparedConnection.mockClear();
217 mocks.submittedUrl = null;
218 mocks.emptyStateUrl = null;
219 mocks.togglePictureInPicture = null;
220 mocks.toggleNativePictureInPicture = null;
221 mocks.pictureInPicturePressed = false;
222 mocks.miniPlayerTabId = null;
223 mocks.openMiniPlayer.mockClear();
224 mocks.closeMiniPlayer.mockClear();
225 mocks.closeRightPanel.mockClear();
226 mocks.openPictureInPicture.mockClear();
227 mocks.closePictureInPicture.mockClear();
228 mocks.pickElement.mockReset();
229 mocks.previewAnnotationScreenshotFile.mockReset();
230 mocks.addPreviewAnnotation.mockClear();
231 mocks.addImage.mockClear();
232 mocks.toggleAnnotation = null;
233 mocks.pictureInPicture = false;
234 mocks.showEmptyState = false;
235 });
236
237 it.each([
238 [
239 "https://localhost:8000/dashboard?mode=test#top",
240 "https://localhost:8000/dashboard?mode=test#top",
241 ],
242 ["localhost:5173/app", "http://localhost:5173/app"],
243 ])("preserves a direct localhost URL in a WSL environment", async (submitted, expected) => {
244 renderToStaticMarkup(
245 <PreviewView
246 threadRef={{
247 environmentId: EnvironmentId.make("environment-1"),
248 threadId: ThreadId.make("thread-1"),
249 }}
250 tabId="tab-1"
251 visible
252 />,
253 );
254
255 expect(mocks.submittedUrl).not.toBeNull();
256 mocks.submittedUrl?.(submitted);
257
258 await vi.waitFor(() =>
259 expect(mocks.navigate).toHaveBeenCalledWith(TEST_RUNTIME_TAB_ID, expected),
260 );
261 expect(mocks.rememberPreviewUrl).toHaveBeenCalledWith(
262 {
263 environmentId: "environment-1",
264 threadId: "thread-1",
265 },
266 expected,
267 );
268 });
269
270 it("maps an empty-state localhost server onto the WSL host", async () => {
271 mocks.showEmptyState = true;
272 renderToStaticMarkup(
273 <PreviewView
274 threadRef={{
275 environmentId: EnvironmentId.make("environment-1"),
276 threadId: ThreadId.make("thread-1"),
277 }}
278 tabId="tab-1"
279 visible
280 />,
281 );
282
283 expect(mocks.emptyStateUrl).not.toBeNull();
284 mocks.emptyStateUrl?.("http://localhost:5173/app?mode=test#top");
285
286 await vi.waitFor(() =>
287 expect(mocks.navigate).toHaveBeenCalledWith(
288 TEST_RUNTIME_TAB_ID,
289 "http://172.25.85.75:5173/app?mode=test#top",
290 ),
291 );
292 expect(mocks.rememberPreviewUrl).toHaveBeenCalledWith(
293 {
294 environmentId: "environment-1",
295 threadId: "thread-1",
296 },
297 "http://172.25.85.75:5173/app?mode=test#top",
298 );
299 });
300
301 it("opens and closes a thread-scoped floating preview for the active tab", async () => {
302 const props = {
303 threadRef: {
304 environmentId: EnvironmentId.make("environment-1"),
305 threadId: ThreadId.make("thread-1"),
306 },
307 tabId: "tab-1",
308 visible: true,
309 } as const;
310
311 renderToStaticMarkup(<PreviewView {...props} />);
312 expect(mocks.pictureInPicturePressed).toBe(false);
313 mocks.togglePictureInPicture?.();
314 expect(mocks.openMiniPlayer).toHaveBeenCalledWith(props.threadRef, "tab-1");
315 expect(mocks.closeRightPanel).toHaveBeenCalledWith(props.threadRef);
316
317 mocks.miniPlayerTabId = "tab-1";
318 renderToStaticMarkup(<PreviewView {...props} />);
319 expect(mocks.pictureInPicturePressed).toBe(true);
320 mocks.togglePictureInPicture?.();
321 expect(mocks.closeMiniPlayer).toHaveBeenCalledWith(props.threadRef);
322 });
323
324 it("keeps the native preview window as a secondary action", async () => {
325 const props = {
326 threadRef: {
327 environmentId: EnvironmentId.make("environment-1"),
328 threadId: ThreadId.make("thread-1"),
329 },
330 tabId: "tab-1",
331 visible: true,
332 } as const;
333
334 renderToStaticMarkup(<PreviewView {...props} />);
335 mocks.toggleNativePictureInPicture?.();
336 await vi.waitFor(() =>
337 expect(mocks.openPictureInPicture).toHaveBeenCalledWith(TEST_RUNTIME_TAB_ID),
338 );
339
340 mocks.pictureInPicture = true;
341 renderToStaticMarkup(<PreviewView {...props} />);
342 mocks.toggleNativePictureInPicture?.();
343 await vi.waitFor(() =>
344 expect(mocks.closePictureInPicture).toHaveBeenCalledWith(TEST_RUNTIME_TAB_ID),
345 );
346 });
347
348 it("forwards Cmd/Ctrl+Enter annotations to the composer send path", async () => {
349 const annotation = {
350 id: "annotation-1",
351 pageUrl: "https://example.com/dashboard",
352 pageTitle: "Dashboard",
353 comment: "Tighten this spacing",
354 elements: [],
355 regions: [],
356 strokes: [],
357 styleChanges: [],
358 screenshot: null,
359 createdAt: "2026-07-27T00:00:00.000Z",
360 };
361 const onSendAnnotation = vi.fn();
362 mocks.pickElement.mockResolvedValue({ annotation, submission: "send" });
363
364 renderToStaticMarkup(
365 <PreviewView
366 threadRef={TEST_THREAD_REF}
367 tabId="tab-1"
368 visible
369 onSendAnnotation={onSendAnnotation}
370 />,
371 );
372 mocks.toggleAnnotation?.();
373
374 await vi.waitFor(() => expect(onSendAnnotation).toHaveBeenCalledWith(annotation, null));
375 expect(mocks.addPreviewAnnotation).toHaveBeenCalledWith(TEST_THREAD_REF, annotation);
376 });
377
378 it("still sends when screenshot attachment conversion fails", async () => {
379 const annotation = {
380 id: "annotation-2",
381 pageUrl: "https://example.com/dashboard",
382 pageTitle: "Dashboard",
383 comment: "Tighten this spacing",
384 elements: [],
385 regions: [],
386 strokes: [],
387 styleChanges: [],
388 screenshot: {
389 dataUrl: "data:image/png;base64,c2NyZWVuc2hvdA==",
390 width: 10,
391 height: 10,
392 cropRect: { x: 0, y: 0, width: 10, height: 10 },
393 },
394 createdAt: "2026-07-27T00:00:00.000Z",
395 };
396 const onSendAnnotation = vi.fn();
397 mocks.pickElement.mockResolvedValue({ annotation, submission: "send" });
398 mocks.previewAnnotationScreenshotFile.mockRejectedValue(new Error("conversion failed"));
399
400 renderToStaticMarkup(
401 <PreviewView
402 threadRef={TEST_THREAD_REF}
403 tabId="tab-1"
404 visible
405 onSendAnnotation={onSendAnnotation}
406 />,
407 );
408 mocks.toggleAnnotation?.();
409
410 await vi.waitFor(() => expect(onSendAnnotation).toHaveBeenCalledWith(annotation, null));
411 expect(mocks.addImage).not.toHaveBeenCalled();
412 });
413});