Open History to see the last commit
1import type { DesktopPreviewFavicon, PreviewSessionSnapshot } from "@t3tools/contracts";
2import { renderToStaticMarkup } from "react-dom/server";
3import { describe, expect, it } from "vite-plus/test";
4
5import { RightPanelTabs, surfaceShortcutActionForKey, tabMuteMenuItem } from "./RightPanelTabs";
6
7function shortcutEvent(
8 key: string,
9 overrides: Partial<Parameters<typeof surfaceShortcutActionForKey>[1]> = {},
10): Parameters<typeof surfaceShortcutActionForKey>[1] {
11 return {
12 key,
13 altKey: false,
14 ctrlKey: false,
15 defaultPrevented: false,
16 isComposing: false,
17 metaKey: false,
18 ...overrides,
19 };
20}
21
22const previewSurface = {
23 id: "browser:tab-1" as const,
24 kind: "preview" as const,
25 resourceId: "tab-1",
26};
27const secondSurface = {
28 id: "browser:tab-2" as const,
29 kind: "preview" as const,
30 resourceId: "tab-2",
31};
32const sessions: Readonly<Record<string, PreviewSessionSnapshot>> = {
33 "tab-1": {
34 threadId: "thread-1",
35 tabId: "tab-1",
36 navStatus: { _tag: "Success", url: "http://24x.xf.local/", title: "Local site" },
37 canGoBack: false,
38 canGoForward: false,
39 updatedAt: "2026-08-09T00:00:00.000Z",
40 },
41 "tab-2": {
42 threadId: "thread-1",
43 tabId: "tab-2",
44 navStatus: { _tag: "Success", url: "http://24x.xf.local/admin", title: "Admin" },
45 canGoBack: false,
46 canGoForward: false,
47 updatedAt: "2026-08-09T00:00:00.000Z",
48 },
49};
50
51const favicon = (dataUrl: string, pageUrl: string): DesktopPreviewFavicon => ({
52 dataUrl,
53 pageUrl,
54 capturedAt: 1,
55});
56
57function overlay(
58 icon: DesktopPreviewFavicon | null,
59 audio?: { audible?: boolean; audioMuted?: boolean },
60) {
61 return {
62 hasWebContents: true,
63 canGoBack: false,
64 canGoForward: false,
65 loading: false,
66 zoomFactor: 1,
67 pictureInPicture: false,
68 colorScheme: "system" as const,
69 audioMuted: audio?.audioMuted ?? false,
70 audible: audio?.audible ?? false,
71 controller: "none" as const,
72 favicon: icon,
73 };
74}
75
76function renderTabs(
77 first: DesktopPreviewFavicon | null,
78 second?: DesktopPreviewFavicon,
79 audio?: { audible?: boolean; audioMuted?: boolean },
80 previewRuntimeTabId: ((tabId: string) => string) | null = (tabId) => `runtime:${tabId}`,
81) {
82 return renderToStaticMarkup(
83 <RightPanelTabs
84 mode="inline"
85 surfaces={second ? [previewSurface, secondSurface] : [previewSurface]}
86 activeSurfaceId={previewSurface.id}
87 pendingSurfaceIds={new Set()}
88 previewSessions={sessions}
89 desktopByTabId={{
90 "tab-1": overlay(first, audio),
91 ...(second ? { "tab-2": overlay(second) } : {}),
92 }}
93 {...(previewRuntimeTabId ? { previewRuntimeTabId } : {})}
94 terminalLabelsById={new Map()}
95 onActivate={() => undefined}
96 onCloseSurface={() => undefined}
97 onCloseOtherSurfaces={() => undefined}
98 onCloseSurfacesToRight={() => undefined}
99 onCloseAllSurfaces={() => undefined}
100 onCopyFilePath={() => undefined}
101 onAddBrowser={() => undefined}
102 onAddTerminal={() => undefined}
103 onAddPullRequest={() => undefined}
104 onAddDiff={() => undefined}
105 onAddFiles={() => undefined}
106 onAddAgents={() => undefined}
107 liveAgentCount={0}
108 browserAvailable
109 terminalAvailable={false}
110 diffAvailable={false}
111 filesAvailable={false}
112 pullRequestAvailable={false}
113 agentsAvailable={false}
114 >
115 <div>content</div>
116 </RightPanelTabs>,
117 );
118}
119
120describe("RightPanelTabs preview favicon", () => {
121 it("prefers a live capture and never asks Google about a private hostname", () => {
122 const captured = renderTabs(favicon("data:image/png;base64,AAAA", "http://24x.xf.local/"));
123 expect(captured).toContain("data:image/png;base64,AAAA");
124 expect(captured).not.toContain("s2/favicons");
125 expect(renderTabs(null)).not.toContain("s2/favicons");
126 });
127
128 it("keeps route-specific captures isolated between live tabs on one origin", () => {
129 const html = renderTabs(
130 favicon("data:image/png;base64,AAAA", "http://24x.xf.local/"),
131 favicon("data:image/png;base64,BBBB", "http://24x.xf.local/admin"),
132 );
133 expect(html).toContain("data:image/png;base64,AAAA");
134 expect(html).toContain("data:image/png;base64,BBBB");
135 });
136
137 it("hides a capture while the server session still describes another origin", () => {
138 const html = renderTabs(favicon("data:image/png;base64,AAAA", "https://example.com/"));
139 expect(html).not.toContain("data:image/png;base64,AAAA");
140 });
141});
142
143describe("surface shortcuts", () => {
144 const actions = [
145 { shortcut: "B", available: true, label: "Browser" },
146 { shortcut: "D", available: false, label: "Diff" },
147 ] as const;
148
149 it("matches available surface shortcuts case-insensitively", () => {
150 expect(surfaceShortcutActionForKey(actions, shortcutEvent("b"))).toBe(actions[0]);
151 expect(surfaceShortcutActionForKey(actions, shortcutEvent("B"))).toBe(actions[0]);
152 });
153
154 it("does not activate unavailable surfaces", () => {
155 expect(surfaceShortcutActionForKey(actions, shortcutEvent("d"))).toBeNull();
156 });
157
158 it("leaves modified, composing, and already-handled key events alone", () => {
159 expect(surfaceShortcutActionForKey(actions, shortcutEvent("b", { metaKey: true }))).toBeNull();
160 expect(
161 surfaceShortcutActionForKey(actions, shortcutEvent("b", { isComposing: true })),
162 ).toBeNull();
163 expect(
164 surfaceShortcutActionForKey(actions, shortcutEvent("b", { defaultPrevented: true })),
165 ).toBeNull();
166 });
167});
168
169describe("RightPanelTabs audio indicator", () => {
170 // A muted tab only shows the indicator while it is actually making sound:
171 // arming mute on a quiet tab is deliberate and stays invisible until there
172 // is something to suppress.
173 const cases = [
174 { audible: false, audioMuted: false, label: null },
175 { audible: false, audioMuted: true, label: null },
176 { audible: true, audioMuted: false, label: "Mute Local site" },
177 { audible: true, audioMuted: true, label: "Unmute Local site" },
178 ] as const;
179
180 it.each(cases)("audible=$audible muted=$audioMuted", ({ audible, audioMuted, label }) => {
181 const html = renderTabs(null, undefined, { audible, audioMuted });
182 if (label === null) {
183 expect(html).not.toContain("Mute Local site");
184 expect(html).not.toContain("Unmute Local site");
185 } else {
186 expect(html).toContain(`aria-label="${label}"`);
187 }
188 });
189
190 it("addresses the desktop by runtime tab id, never the server session id", () => {
191 // Session ids are only unique per server process; sending one to the
192 // Electron manager raises PreviewTabNotFoundError and silently no-ops.
193 const seen: string[] = [];
194 renderTabs(null, undefined, { audible: true }, (tabId) => {
195 seen.push(tabId);
196 return `runtime:${tabId}`;
197 });
198 expect(seen).toContain("tab-1");
199 });
200
201 it("hides the toggle when no runtime tab id can be resolved", () => {
202 const html = renderTabs(null, undefined, { audible: true }, null);
203 expect(html).not.toContain("Mute Local site");
204 });
205});
206
207describe("tabMuteMenuItem", () => {
208 const overlay = (audioMuted: boolean) =>
209 ({ audioMuted, audible: false }) as Parameters<typeof tabMuteMenuItem>[0]["overlay"];
210
211 it("stays disabled until the desktop tab exists", () => {
212 // The server session id resolves before the preview manager finishes
213 // createTab. Muting in that window fails with an error nobody surfaces.
214 expect(tabMuteMenuItem({ overlay: null, canResolveRuntimeTabId: true })).toEqual({
215 label: "Mute tab",
216 disabled: true,
217 });
218 });
219
220 it("stays disabled when no runtime tab id can be resolved", () => {
221 expect(tabMuteMenuItem({ overlay: overlay(false), canResolveRuntimeTabId: false })).toEqual({
222 label: "Mute tab",
223 disabled: true,
224 });
225 });
226
227 it("offers mute and unmute once the tab is addressable", () => {
228 expect(tabMuteMenuItem({ overlay: overlay(false), canResolveRuntimeTabId: true })).toEqual({
229 label: "Mute tab",
230 disabled: false,
231 });
232 expect(tabMuteMenuItem({ overlay: overlay(true), canResolveRuntimeTabId: true })).toEqual({
233 label: "Unmute tab",
234 disabled: false,
235 });
236 });
237});