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

RightPanelTabs.test.tsx

327 lines11.4 KB

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