Open History to see the last commit
1import type { DesktopPreviewFavicon, PreviewSessionSnapshot } from "@t3tools/contracts";
2import { DEFAULT_RESOLVED_KEYBINDINGS } from "@t3tools/shared/keybindings";
3import { act } from "react";
4import { createRoot, type Root } from "react-dom/client";
5import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
6
7const setAudioMuted = vi.hoisted(() => vi.fn(async () => undefined));
8
9vi.mock(import("~/hooks/useTheme"), async (original) => ({
10 ...(await original()),
11 useTheme: () => ({ theme: "light", resolvedTheme: "light", themeHalves: null }) as never,
12}));
13vi.mock(import("~/browser/browserDefaults"), async (original) => ({
14 ...(await original()),
15 useBrowserDefaults: () => ({ profiles: [] }) as never,
16}));
17vi.mock(import("./preview/previewBridge"), () => ({
18 previewBridge: { setAudioMuted } as never,
19}));
20
21import { RightPanelTabs } from "./RightPanelTabs";
22
23// A real 1x1 PNG, so the browser keeps it instead of falling back on a decode error.
24const PIXEL_A =
25 "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg==";
26const PIXEL_B =
27 "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg==";
28
29const previewSurface = {
30 id: "browser:tab-1" as const,
31 kind: "preview" as const,
32 resourceId: "tab-1",
33};
34const secondSurface = {
35 id: "browser:tab-2" as const,
36 kind: "preview" as const,
37 resourceId: "tab-2",
38};
39const sessions: Readonly<Record<string, PreviewSessionSnapshot>> = {
40 "tab-1": {
41 threadId: "thread-1",
42 tabId: "tab-1",
43 navStatus: { _tag: "Success", url: "http://24x.xf.local/", title: "Local site" },
44 canGoBack: false,
45 canGoForward: false,
46 updatedAt: "2026-08-09T00:00:00.000Z",
47 },
48 "tab-2": {
49 threadId: "thread-1",
50 tabId: "tab-2",
51 navStatus: { _tag: "Success", url: "http://24x.xf.local/admin", title: "Admin" },
52 canGoBack: false,
53 canGoForward: false,
54 updatedAt: "2026-08-09T00:00:00.000Z",
55 },
56};
57
58const favicon = (dataUrl: string, pageUrl: string): DesktopPreviewFavicon => ({
59 dataUrl,
60 pageUrl,
61 capturedAt: 1,
62});
63
64function overlay(
65 icon: DesktopPreviewFavicon | null,
66 audio?: { audible?: boolean; audioMuted?: boolean },
67) {
68 return {
69 hasWebContents: true,
70 canGoBack: false,
71 canGoForward: false,
72 loading: false,
73 zoomFactor: 1,
74 pictureInPicture: false,
75 colorScheme: "system" as const,
76 audioMuted: audio?.audioMuted ?? false,
77 audible: audio?.audible ?? false,
78 controller: "none" as const,
79 favicon: icon,
80 };
81}
82
83let root: Root;
84let container: HTMLDivElement;
85const noop = () => undefined;
86
87beforeEach(() => {
88 vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
89 setAudioMuted.mockClear();
90 container = document.createElement("div");
91 document.body.append(container);
92 root = createRoot(container);
93});
94
95afterEach(async () => {
96 await act(() => root.unmount());
97 container.remove();
98 vi.unstubAllGlobals();
99});
100
101async function renderTabs(
102 first: DesktopPreviewFavicon | null,
103 second?: DesktopPreviewFavicon,
104 audio?: { audible?: boolean; audioMuted?: boolean },
105 previewRuntimeTabId: ((tabId: string) => string) | null = (tabId) => `runtime:${tabId}`,
106) {
107 await act(() =>
108 root.render(
109 <RightPanelTabs
110 mode="inline"
111 open
112 keybindings={DEFAULT_RESOLVED_KEYBINDINGS}
113 getShortcutContext={() => ({
114 terminalFocus: false,
115 terminalOpen: false,
116 previewFocus: false,
117 previewOpen: false,
118 isWeb: true,
119 isDesktop: false,
120 })}
121 surfaces={second ? [previewSurface, secondSurface] : [previewSurface]}
122 environmentId={null}
123 activeSurfaceId={previewSurface.id}
124 pendingSurfaceIds={new Set()}
125 previewSessions={sessions}
126 desktopByTabId={{
127 "tab-1": overlay(first, audio),
128 ...(second ? { "tab-2": overlay(second) } : {}),
129 }}
130 {...(previewRuntimeTabId ? { previewRuntimeTabId } : {})}
131 terminalLabelsById={new Map()}
132 onActivate={noop}
133 onCloseSurface={noop}
134 onCloseOtherSurfaces={noop}
135 onCloseSurfacesToRight={noop}
136 onCloseAllSurfaces={noop}
137 onCopyFilePath={noop}
138 onAddBrowser={noop}
139 onAddBrowserInProfile={noop}
140 onAddTerminal={noop}
141 onAddPullRequest={noop}
142 onAddPullRequests={noop}
143 onAddDiff={noop}
144 onAddFiles={noop}
145 onAddDevice={noop}
146 browserAvailable
147 terminalAvailable={false}
148 diffAvailable={false}
149 filesAvailable={false}
150 pullRequestAvailable={false}
151 pullRequestsAvailable={false}
152 deviceAvailable={false}
153 >
154 <div>content</div>
155 </RightPanelTabs>,
156 ),
157 );
158}
159
160const imageSources = () => [...container.querySelectorAll("img")].map((image) => image.src);
161const button = (name: string) =>
162 [...container.querySelectorAll("button")].find((element) => element.ariaLabel === name);
163
164describe("preview tab favicon", () => {
165 it("prefers a live capture and never asks a public provider about a private hostname", async () => {
166 await renderTabs(favicon(PIXEL_A, "http://24x.xf.local/"));
167 expect(imageSources()).toEqual([PIXEL_A]);
168
169 await renderTabs(null);
170 expect(imageSources()).toEqual([]);
171 });
172
173 it("keeps route-specific captures isolated between live tabs on one origin", async () => {
174 await renderTabs(
175 favicon(PIXEL_A, "http://24x.xf.local/"),
176 favicon(PIXEL_B, "http://24x.xf.local/admin"),
177 );
178 expect(imageSources()).toEqual([PIXEL_A, PIXEL_B]);
179 });
180
181 it("hides a capture while the server session still describes another origin", async () => {
182 await renderTabs(favicon(PIXEL_A, "https://example.com/"));
183 expect(imageSources()).not.toContain(PIXEL_A);
184 });
185});
186
187describe("preview tab audio toggle", () => {
188 it.each([
189 { audible: false, audioMuted: false },
190 { audible: false, audioMuted: true },
191 ])("is absent for a silent tab (muted=$audioMuted)", async (audio) => {
192 await renderTabs(null, undefined, audio);
193 expect(button("Mute Local site")).toBeUndefined();
194 expect(button("Unmute Local site")).toBeUndefined();
195 });
196
197 it.each([
198 { audioMuted: false, label: "Mute Local site", nextMuted: true },
199 { audioMuted: true, label: "Unmute Local site", nextMuted: false },
200 ])("$label addresses the desktop by runtime tab id", async ({ audioMuted, label, nextMuted }) => {
201 await renderTabs(null, undefined, { audible: true, audioMuted });
202 await act(() => button(label)!.click());
203 expect(setAudioMuted).toHaveBeenCalledExactlyOnceWith("runtime:tab-1", nextMuted);
204 });
205
206 it("hides the toggle when no runtime tab id can be resolved", async () => {
207 await renderTabs(null, undefined, { audible: true }, null);
208 expect(button("Mute Local site")).toBeUndefined();
209 });
210});