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

RightPanelTabs.test.tsx

280 lines9.9 KB

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