import assert from "node:assert/strict"; import { readFileSync } from "node:fs"; import test from "node:test"; const storage = new Map(); Object.defineProperty(globalThis, "localStorage", { configurable: true, value: { getItem: (key: string) => storage.get(key) ?? null, setItem: (key: string, value: string) => storage.set(key, value), removeItem: (key: string) => storage.delete(key), }, }); Object.defineProperty(globalThis, "requestAnimationFrame", { configurable: true, value: (callback: (time: number) => void) => setTimeout(() => callback(Date.now()), 0) as unknown as number, }); const { computeHostTreeTabGutter, resolveWorkspaceSessionTabDropTarget, shouldKeepHostTreeToggleSurface, shouldShowHostTreeToggle, } = await import("./TopTabs.tsx"); const { WORKSPACE_SESSION_DRAG_TYPE, dataTransferHasType, getTopTabInsertionTarget, getWorkspaceSessionDragId, hasWorkspaceSessionDrag, isPointInsideRect, } = await import("../application/state/terminalDragData.ts"); const { activateLogViewTab, createTopTabSessionDoubleClickHandler, formatSessionTopTabLabel, formatSessionTopTabTooltip, resolveSessionTabCodingCliIconState, stopCloseButtonDoubleClickPropagation, } = await import("./top-tabs/TopTabItems.tsx"); const { activeTabStore } = await import("../application/state/activeTabStore.ts"); const indexCss = readFileSync(new URL("../index.css", import.meta.url), "utf8"); const topTabsSource = readFileSync(new URL("./TopTabs.tsx", import.meta.url), "utf8"); const topTabItemsSource = readFileSync(new URL("./top-tabs/TopTabItems.tsx", import.meta.url), "utf8"); const topTabsQuickControlsSource = readFileSync(new URL("./TopTabsQuickControls.tsx", import.meta.url), "utf8"); const syncStatusButtonSource = readFileSync(new URL("./SyncStatusButton.tsx", import.meta.url), "utf8"); const switchSource = readFileSync(new URL("./ui/switch.tsx", import.meta.url), "utf8"); const appViewSource = readFileSync(new URL("../application/app/AppView.tsx", import.meta.url), "utf8"); const appSource = [ readFileSync(new URL("../App.tsx", import.meta.url), "utf8"), readFileSync(new URL("../application/app/AppSideEffects.tsx", import.meta.url), "utf8"), readFileSync(new URL("../application/app/hosts/TerminalHost.tsx", import.meta.url), "utf8"), readFileSync(new URL("../application/app/hosts/ChromeHost.tsx", import.meta.url), "utf8"), ].join("\n"); const externalMcpToggleSource = readFileSync(new URL("../application/state/useExternalMcpToggleState.ts", import.meta.url), "utf8"); const zhTwAiSource = readFileSync(new URL("../application/i18n/locales/zh-TW/ai.ts", import.meta.url), "utf8"); const terminalViewSource = readFileSync(new URL("./terminal/TerminalView.tsx", import.meta.url), "utf8"); test("host tree tab gutter fills the remaining sidebar width", () => { assert.equal(computeHostTreeTabGutter(280, 120), 160); }); test("host tree tab gutter never goes negative", () => { assert.equal(computeHostTreeTabGutter(120, 280), 0); }); test("host tree tab surface stays mounted when root pages are active", () => { assert.equal(shouldKeepHostTreeToggleSurface({ enabled: true, activeWorkTabCount: 2, }), true); }); test("host tree tab surface is hidden without work tabs", () => { assert.equal(shouldKeepHostTreeToggleSurface({ enabled: true, activeWorkTabCount: 0, }), false); }); test("host tree tab layout transitions match the sidebar timing", () => { const hostTreeCss = [ ".top-tab-root-label", ".top-tab-host-tree-toggle-slot", ].map((selector) => { const start = indexCss.indexOf(selector); assert.notEqual(start, -1); const end = indexCss.indexOf("}", start); return indexCss.slice(start, end); }).join("\n"); const gutterStart = indexCss.indexOf(".top-tab-host-tree-gutter"); assert.notEqual(gutterStart, -1); const gutterEnd = indexCss.indexOf("}", gutterStart); const gutterCss = indexCss.slice(gutterStart, gutterEnd); assert.match(hostTreeCss, /width 220ms cubic-bezier\(0\.4, 0, 0\.2, 1\)/); assert.match(hostTreeCss, /max-width 220ms cubic-bezier\(0\.4, 0, 0\.2, 1\)/); assert.doesNotMatch(hostTreeCss, /transition:\s*none/); assert.doesNotMatch(hostTreeCss, /280ms/); assert.doesNotMatch(gutterCss, /transition/); assert.match(indexCss, /\.top-tab-host-tree-gutter-exit[\s\S]*transition: width 220ms/); }); test("host tree toggle appears with opacity only and no bounce animation", () => { assert.doesNotMatch(indexCss, /top-tab-host-tree-toggle-pop/); assert.doesNotMatch(indexCss, /@keyframes\s+pop-in/); const start = indexCss.indexOf(".top-tab-host-tree-toggle-slot"); assert.notEqual(start, -1); const end = indexCss.indexOf("}", start); const toggleSlotCss = indexCss.slice(start, end); assert.match(toggleSlotCss, /opacity 220ms ease/); assert.doesNotMatch(toggleSlotCss, /transform/); assert.doesNotMatch(toggleSlotCss, /scale/); }); test("host tree toggle exposes a custom CSS hook", () => { assert.match(topTabsSource, /data-section="top-tabs-host-tree-toggle"/); }); test("quick switcher plus button exposes a custom CSS hook", () => { assert.match(topTabsSource, /data-section="top-tabs-quick-switcher-toggle"/); }); test("top tabs keep cloud sync separate from quick controls", () => { assert.match(topTabsSource, /SyncStatusButton/); assert.match(topTabsSource, /TopTabsQuickControls/); assert.match(topTabsSource, /onOpenSettings=\{onOpenSettings\}/); assert.match(topTabsSource, /onSyncNow=\{onSyncNow\}/); assert.match(topTabsSource, /theme=\{theme\}/); assert.match(topTabsSource, /themePreference=\{themePreference\}/); assert.match(topTabsSource, /onThemeChange=\{onThemeChange\}/); assert.match(topTabsSource, /externalMcpEnabled=\{externalMcpEnabled\}/); assert.match(topTabsSource, /windowOpacity=\{windowOpacity\}/); assert.match(topTabsSource, /showExternalMcpToggle=\{showExternalMcpToggle\}/); assert.doesNotMatch(topTabsSource, /WindowOpacityButton/); assert.doesNotMatch(topTabsSource, /onToggleTheme/); const syncButtonUsage = topTabsSource.match(//); assert.ok(syncButtonUsage, 'expected a SyncStatusButton usage'); assert.doesNotMatch(syncButtonUsage[0], /externalMcpEnabled=/); assert.doesNotMatch(syncButtonUsage[0], /windowOpacity=/); assert.doesNotMatch(syncButtonUsage[0], /onThemeChange=/); const quickControlsUsage = topTabsSource.match(//); assert.ok(quickControlsUsage, 'expected a TopTabsQuickControls usage'); assert.match(quickControlsUsage[0], /showExternalMcpToggle=\{showExternalMcpToggle\}/); assert.match(quickControlsUsage[0], /externalMcpEnabled=\{externalMcpEnabled\}/); }); test("quick controls panel hosts External MCP, opacity, and theme", () => { assert.match(topTabsQuickControlsSource, /data-section="top-tabs-quick-controls"/); assert.match(topTabsQuickControlsSource, /className="w-72 p-0 app-no-drag"/); assert.match(topTabsQuickControlsSource, /topTabs\.controlPanel/); assert.doesNotMatch(topTabsQuickControlsSource, /topTabs\.controlPanel\.description/); assert.match(topTabsQuickControlsSource, /externalMcpEnabled/); assert.match(topTabsQuickControlsSource, /onToggleExternalMcp/); assert.match(topTabsQuickControlsSource, /windowOpacity/); assert.match(topTabsQuickControlsSource, /setWindowOpacity/); assert.match(topTabsQuickControlsSource, /themePreference/); assert.match(topTabsQuickControlsSource, /onThemeChange/); assert.match(topTabsQuickControlsSource, /topTabs\.controlPanel\.theme\.system/); assert.match(topTabsQuickControlsSource, /aria-pressed=\{themePreference === option\.value\}/); assert.match(topTabsQuickControlsSource, /onClick=\{\(\) => onThemeChange\(option\.value\)\}/); assert.match(topTabsQuickControlsSource, / { assert.match(zhTwAiSource, /'topTabs\.externalMcp\.enable':/); assert.match(zhTwAiSource, /'topTabs\.externalMcp\.disable':/); assert.match(zhTwAiSource, /'topTabs\.controlPanel':/); assert.match(zhTwAiSource, /'topTabs\.controlPanel\.externalMcp':/); }); test("AppView hides External MCP toggle in peer session windows", () => { assert.match(appViewSource, /hash\.startsWith\('#\/session-window'\)/); assert.match(appViewSource, /showExternalMcpToggle=\{!isPeerSessionWindow\}/); }); test("External MCP top-bar status sync waits for App startup reconcile", () => { assert.match(appSource, /await syncExternalMcpStartupStateOnce\(netcattyBridge\.get\(\)\)/); assert.match(appSource, /markExternalMcpStartupReady\(\)/); assert.ok( appSource.indexOf("await syncExternalMcpStartupStateOnce(netcattyBridge.get())") < appSource.indexOf("markExternalMcpStartupReady()"), "startup ready must be marked only after await syncExternalMcpStartupStateOnce", ); assert.match(externalMcpToggleSource, /shouldWaitForExternalMcpStartupReady/); assert.match(externalMcpToggleSource, /waitForExternalMcpStartupReady\(/); assert.match(externalMcpToggleSource, /!status\.enabled && !status\.error/); assert.match(externalMcpToggleSource, /if \(isPeerSessionWindow \|\| !enabled\) return;/); }); test("SessionTabIcon checks custom host icon appearance before distro logos", () => { assert.match(topTabItemsSource, /resolveHostIconAppearance\(host\)/); assert.ok( topTabItemsSource.indexOf("resolveHostIconAppearance(host)") < topTabItemsSource.indexOf("getEffectiveHostDistro(host)"), "custom host icon should be checked before distro fallback", ); }); test("disabling dynamic titles freezes a stored coding CLI icon and stops title fallback", () => { assert.deepEqual( resolveSessionTabCodingCliIconState({ codingCliProviderId: "claude", dynamicTitle: "⠋ Reading files", }, undefined, "off"), { provider: { id: "claude", label: "Claude Code", command: "claude", titleHints: ["claude code", "claude"], iconKey: "claude", }, activityPhase: "idle", }, ); assert.equal( resolveSessionTabCodingCliIconState({ dynamicTitle: "OpenAI Codex", }, undefined, "off"), null, ); assert.equal( resolveSessionTabCodingCliIconState({ startupCommand: "claude", }, undefined, "off")?.provider.id, "claude", ); assert.equal( resolveSessionTabCodingCliIconState({ dynamicTitle: "OpenAI Codex", }, undefined, "agent")?.provider.id, "codex", ); }); test("session top tab double click dispatches the configured behavior", () => { const copiedSessionIds: string[] = []; const duplicatedSessionIds: string[] = []; const handlers = (["duplicate", "copy", "disabled"] as const).map((behavior) => createTopTabSessionDoubleClickHandler({ behavior, onCopySession: (sessionId) => copiedSessionIds.push(sessionId), onDuplicateSession: (sessionId) => duplicatedSessionIds.push(sessionId), sessionId: `session-${behavior}`, }), ); handlers.forEach((handler) => handler({} as Parameters[0])); assert.deepEqual(duplicatedSessionIds, ["session-duplicate"]); assert.deepEqual(copiedSessionIds, ["session-copy"]); assert.match(topTabItemsSource, /behavior: tabDoubleClickBehavior/); assert.match(topTabItemsSource, /onDoubleClick=\{handleDoubleClick\}/); }); test("duplicate double-click behavior falls back to copying when unavailable", () => { const copiedSessionIds: string[] = []; const handleDoubleClick = createTopTabSessionDoubleClickHandler({ behavior: "duplicate", onCopySession: (sessionId) => copiedSessionIds.push(sessionId), sessionId: "session-1", }); handleDoubleClick({} as Parameters[0]); assert.deepEqual(copiedSessionIds, ["session-1"]); }); test("draggable top-tab close buttons cancel the drag gesture on mousedown", () => { const closeButtonLabels = [ "tabs.closePluginViewAria", "Close editor tab", "tabs.closeSessionAria", "tabs.closeLogViewAria", ]; for (const label of closeButtonLabels) { const labelIndex = topTabItemsSource.indexOf(label); assert.notEqual(labelIndex, -1, `missing close label ${label}`); const buttonStart = topTabItemsSource.lastIndexOf("", labelIndex); const openingTag = topTabItemsSource.slice(buttonStart, buttonEnd); assert.match( openingTag, /onMouseDown=\{handleTabCloseButtonMouseDown\}/, `${label} close button must guard mousedown`, ); } assert.equal( topTabItemsSource.match(/onMouseDown=\{handleTabCloseButtonMouseDown\}/g)?.length, 4, ); const workspaceSource = topTabItemsSource.slice( topTabItemsSource.indexOf("export const WorkspaceTopTab"), topTabItemsSource.indexOf("WorkspaceTopTab.displayName"), ); assert.equal(workspaceSource.includes("handleTabCloseButtonMouseDown"), false); assert.equal(workspaceSource.includes(" { const tabTypes = ["plugin-view", "editor", "session", "workspace", "logView"]; for (const tabType of tabTypes) { const marker = `data-tab-type="${tabType}"`; const start = topTabItemsSource.indexOf(marker); assert.notEqual(start, -1, marker); const classNameAt = topTabItemsSource.indexOf("className={cn(", start); assert.notEqual(classNameAt, -1, tabType); const openingHandlers = topTabItemsSource.slice(start, classNameAt); assert.match(openingHandlers, /onMouseDown=\{handleTabDragThresholdMouseDown\}/, tabType); assert.match(openingHandlers, /onMouseUp=\{\(event\) => handleTabDragThresholdRelease\(event,/, tabType); assert.match( openingHandlers, /onDragStart=\{\(event\) => \{\s*handleTabDragThresholdDragStart\(event\);\s*onTabDragStart\(event,/, tabType, ); assert.match( openingHandlers, /onDrag=\{\(event\) => \{\s*if \(!handleTabDragThresholdDrag\(event\)\) return;\s*onTabDragStart\(event,/, tabType, ); assert.match( openingHandlers, /onDragEnd=\{\(event\) => \{\s*handleTabDragThresholdRelease\(event,[\s\S]*?onTabDragEnd\(\);\s*\}\}/, tabType, ); assert.equal(openingHandlers.includes("handleTabCloseButtonMouseDown"), false, tabType); assert.equal(openingHandlers.includes("preventDefault"), false, tabType); } assert.equal(topTabItemsSource.match(/onMouseDown=\{handleTabMiddleMouseDown\}/g)?.length ?? 0, 0); assert.equal(topTabItemsSource.match(/handleTabDragThresholdRelease\(/g)?.length, 10); }); test("session close button double click stays on the close button", () => { let stopPropagationCount = 0; stopCloseButtonDoubleClickPropagation({ stopPropagation: () => { stopPropagationCount += 1; }, }); assert.equal(stopPropagationCount, 1); assert.match(topTabItemsSource, /onDoubleClick=\{stopCloseButtonDoubleClickPropagation\}/); }); test("main window controls keep mouse presses from taking focus", () => { const windowControlsSource = topTabItemsSource.slice( topTabItemsSource.indexOf("export const WindowControls"), topTabItemsSource.indexOf("WindowControls.displayName"), ); assert.match( windowControlsSource, /const preventWindowControlFocus: React\.MouseEventHandler = \(event\) => \{\s*event\.preventDefault\(\);\s*\};/, ); assert.equal(windowControlsSource.match(/onMouseDown=\{preventWindowControlFocus\}/g)?.length, 2); const closeButton = windowControlsSource.match( /