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

RightPanelTabs.test.tsx

324 lines10.8 KB

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