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

RightPanelTabs.test.tsx

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