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

RightPanelTabs.test.tsx

330 lines11.5 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 onAddAgents={() => undefined}
126 onAddDevice={() => undefined}
127 liveAgentCount={0}
128 browserAvailable
129 terminalAvailable={false}
130 diffAvailable={false}
131 filesAvailable={false}
132 pullRequestAvailable={false}
133 pullRequestsAvailable={false}
134 agentsAvailable={false}
135 deviceAvailable={false}
136 >
137 <div>content</div>
138 </RightPanelTabs>,
139 );
141​
142describe("RightPanelTabs preview favicon", () => {
143 it("prefers a live capture and never asks Google about a private hostname", () => {
144 const captured = renderTabs(favicon("data:image/png;base64,AAAA", "http://24x.xf.local/"));
145 expect(captured).toContain("data:image/png;base64,AAAA");
146 expect(captured).not.toContain("s2/favicons");
147 expect(renderTabs(null)).not.toContain("s2/favicons");
148 });
149​
150 it("keeps route-specific captures isolated between live tabs on one origin", () => {
151 const html = renderTabs(
152 favicon("data:image/png;base64,AAAA", "http://24x.xf.local/"),
153 favicon("data:image/png;base64,BBBB", "http://24x.xf.local/admin"),
154 );
155 expect(html).toContain("data:image/png;base64,AAAA");
156 expect(html).toContain("data:image/png;base64,BBBB");
157 });
158​
159 it("hides a capture while the server session still describes another origin", () => {
160 const html = renderTabs(favicon("data:image/png;base64,AAAA", "https://example.com/"));
161 expect(html).not.toContain("data:image/png;base64,AAAA");
162 });
163});
164​
165describe("surface shortcuts", () => {
166 const actions = [
167 { shortcut: "B", available: true, label: "Browser" },
168 { shortcut: "D", available: false, label: "Diff" },
169 ] as const;
170​
171 it("matches available surface shortcuts case-insensitively", () => {
172 expect(surfaceShortcutActionForKey(actions, shortcutEvent("b"))).toBe(actions[0]);
173 expect(surfaceShortcutActionForKey(actions, shortcutEvent("B"))).toBe(actions[0]);
174 });
175​
176 it("does not activate unavailable surfaces", () => {
177 expect(surfaceShortcutActionForKey(actions, shortcutEvent("d"))).toBeNull();
178 });
179​
180 it("leaves modified, composing, and already-handled key events alone", () => {
181 expect(surfaceShortcutActionForKey(actions, shortcutEvent("b", { metaKey: true }))).toBeNull();
182 expect(
183 surfaceShortcutActionForKey(actions, shortcutEvent("b", { isComposing: true })),
184 ).toBeNull();
185 expect(
186 surfaceShortcutActionForKey(actions, shortcutEvent("b", { defaultPrevented: true })),
187 ).toBeNull();
188 });
189});
190​
191describe("surface shortcut typing contexts", () => {
192 // Selector-aware stub: closest() answers only tokens the combined selector
193 // would actually match, mirroring how the browser resolves it.
194 const makeTarget = (matches: string | null) => ({
195 closest(selectors: string) {
196 if (matches === null || !selectors.includes(matches)) return null;
197 return {};
198 },
199 });
200​
201 it("treats form fields and every editable region as typing contexts", () => {
202 expect(surfaceShortcutTargetsTypingContext(makeTarget("input"))).toBe(true);
203 expect(surfaceShortcutTargetsTypingContext(makeTarget("textarea"))).toBe(true);
204 expect(surfaceShortcutTargetsTypingContext(makeTarget("select"))).toBe(true);
205 // The chat composer is a contenteditable that sits empty until a draft
206 // exists; launcher letters claimed from it redirected prompts into shells.
207 // The :not clause sees past contenteditable="false" islands to an editable
208 // host around them, so nested editors stay protected too.
209 expect(surfaceShortcutTargetsTypingContext(makeTarget("[contenteditable]"))).toBe(true);
210 });
211​
212 it("claims letters when focus sits outside any editable region", () => {
213 expect(surfaceShortcutTargetsTypingContext(null)).toBe(false);
214 expect(surfaceShortcutTargetsTypingContext(makeTarget(null))).toBe(false);
215 });
216});
217​
218describe("RightPanelTabs audio indicator", () => {
219 // A muted tab only shows the indicator while it is actually making sound:
220 // arming mute on a quiet tab is deliberate and stays invisible until there
221 // is something to suppress.
222 const cases = [
223 { audible: false, audioMuted: false, label: null },
224 { audible: false, audioMuted: true, label: null },
225 { audible: true, audioMuted: false, label: "Mute Local site" },
226 { audible: true, audioMuted: true, label: "Unmute Local site" },
227 ] as const;
228​
229 it.each(cases)("audible=$audible muted=$audioMuted", ({ audible, audioMuted, label }) => {
230 const html = renderTabs(null, undefined, { audible, audioMuted });
231 if (label === null) {
232 expect(html).not.toContain("Mute Local site");
233 expect(html).not.toContain("Unmute Local site");
234 } else {
235 expect(html).toContain(`aria-label="${label}"`);
236 }
237 });
238​
239 it("addresses the desktop by runtime tab id, never the server session id", () => {
240 // Session ids are only unique per server process; sending one to the
241 // Electron manager raises PreviewTabNotFoundError and silently no-ops.
242 const seen: string[] = [];
243 renderTabs(null, undefined, { audible: true }, (tabId) => {
244 seen.push(tabId);
245 return `runtime:${tabId}`;
246 });
247 expect(seen).toContain("tab-1");
248 });
249​
250 it("hides the toggle when no runtime tab id can be resolved", () => {
251 const html = renderTabs(null, undefined, { audible: true }, null);
252 expect(html).not.toContain("Mute Local site");
253 });
254});
255​
256describe("tabMuteMenuItem", () => {
257 const overlay = (audioMuted: boolean) =>
258 ({ audioMuted, audible: false }) as Parameters<typeof tabMuteMenuItem>[0]["overlay"];
259​
260 it("stays disabled until the desktop tab exists", () => {
261 // The server session id resolves before the preview manager finishes
262 // createTab. Muting in that window fails with an error nobody surfaces.
263 expect(tabMuteMenuItem({ overlay: null, canResolveRuntimeTabId: true })).toEqual({
264 label: "Mute tab",
265 disabled: true,
266 });
267 });
268​
269 it("stays disabled when no runtime tab id can be resolved", () => {
270 expect(tabMuteMenuItem({ overlay: overlay(false), canResolveRuntimeTabId: false })).toEqual({
271 label: "Mute tab",
272 disabled: true,
273 });
274 });
275​
276 it("offers mute and unmute once the tab is addressable", () => {
277 expect(tabMuteMenuItem({ overlay: overlay(false), canResolveRuntimeTabId: true })).toEqual({
278 label: "Mute tab",
279 disabled: false,
280 });
281 expect(tabMuteMenuItem({ overlay: overlay(true), canResolveRuntimeTabId: true })).toEqual({
282 label: "Unmute tab",
283 disabled: false,
284 });
285 });
286});
287​
288describe("pull request tab snapshots", () => {
289 const environmentId = EnvironmentId.make("local");
290 const link: ThreadPullRequestLink = {
291 host: "github.com",
292 repository: "acme/api",
293 number: 7,
294 url: "https://github.com/acme/api/pull/7",
295 source: "manual",
296 linkedAt: "2026-01-01T00:00:00Z",
297 stack: null,
298 snapshot: null,
299 };
300 it("keeps unknown linked state authoritative and scopes matches to environment and host", () => {
301 const threads = [{ environmentId, pullRequests: [link] }];
302 expect(resolvePullRequestTabLink(threads, environmentId, "github.com", link)).toBe(link);
303 expect(
304 resolvePullRequestTabLink(threads, EnvironmentId.make("remote"), "github.com", link),
305 ).toBeUndefined();
306 expect(
307 resolvePullRequestTabLink(threads, environmentId, "github.enterprise.test", link),
308 ).toBeUndefined();
309 });
310 it("uses the newest snapshot when several threads link the same PR", () => {
311 const snapshot = {
312 state: "merged" as const,
313 title: "API",
314 headBranch: "api",
315 baseBranch: "main",
316 isDraft: false,
317 updatedAt: null,
318 syncedAt: "2026-02-01T00:00:00Z",
319 };
320 const newer = { ...link, snapshot };
321 expect(
322 resolvePullRequestTabLink(
323 [{ environmentId, pullRequests: [link, newer] }],
324 environmentId,
325 "github.com",
326 link,
327 ),
328 ).toBe(newer);
329 });
330});