|
| 1 | +// @vitest-environment jsdom |
| 2 | + |
| 3 | +import { EnvironmentId, ThreadId, type ScopedThreadRef } from "@t3tools/contracts"; |
| 4 | +import { act } from "react"; |
| 5 | +import { createRoot, type Root } from "react-dom/client"; |
| 6 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test"; |
| 7 | + |
| 8 | +import { selectThreadRightPanelState, useRightPanelStore } from "~/rightPanelStore"; |
| 9 | + |
| 10 | +import { RightPanelTabs } from "./RightPanelTabs"; |
| 11 | + |
| 12 | +const threadRef: ScopedThreadRef = { |
| 13 | + environmentId: EnvironmentId.make("environment-a"), |
| 14 | + threadId: ThreadId.make("thread-a"), |
| 15 | +}; |
| 16 | + |
| 17 | +let root: Root; |
| 18 | +let container: HTMLDivElement; |
| 19 | + |
| 20 | +beforeEach(() => { |
| 21 | + vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true); |
| 22 | + vi.stubGlobal( |
| 23 | + "ResizeObserver", |
| 24 | + class { |
| 25 | + observe() {} |
| 26 | + disconnect() {} |
| 27 | + }, |
| 28 | + ); |
| 29 | + // jsdom lacks the Web Animations API that the tab bar's scroll area waits on. |
| 30 | + Element.prototype.getAnimations ??= () => []; |
| 31 | + useRightPanelStore.setState({ byThreadKey: {} }); |
| 32 | + container = document.createElement("div"); |
| 33 | + document.body.append(container); |
| 34 | + root = createRoot(container); |
| 35 | +}); |
| 36 | + |
| 37 | +afterEach(async () => { |
| 38 | + await act(async () => root.unmount()); |
| 39 | + container.remove(); |
| 40 | + vi.unstubAllGlobals(); |
| 41 | +}); |
| 42 | + |
| 43 | +// Shows the thread's surfaces from the real right-panel store and opens the |
| 44 | +// terminal into it the way ChatView's addTerminalSurface does. |
| 45 | +function Harness({ terminalAvailable }: { terminalAvailable: boolean }) { |
| 46 | + const surfaces = useRightPanelStore( |
| 47 | + (state) => selectThreadRightPanelState(state.byThreadKey, threadRef).surfaces, |
| 48 | + ); |
| 49 | + return ( |
| 50 | + <RightPanelTabs |
| 51 | + mode="inline" |
| 52 | + surfaces={surfaces} |
| 53 | + environmentId={threadRef.environmentId} |
| 54 | + activeSurfaceId={surfaces[0]?.id ?? null} |
| 55 | + pendingSurfaceIds={new Set()} |
| 56 | + previewSessions={{}} |
| 57 | + desktopByTabId={{}} |
| 58 | + terminalLabelsById={new Map()} |
| 59 | + onActivate={() => undefined} |
| 60 | + onCloseSurface={() => undefined} |
| 61 | + onCloseOtherSurfaces={() => undefined} |
| 62 | + onCloseSurfacesToRight={() => undefined} |
| 63 | + onCloseAllSurfaces={() => undefined} |
| 64 | + onCopyFilePath={() => undefined} |
| 65 | + panels={{ |
| 66 | + preview: { available: false, onOpen: () => undefined }, |
| 67 | + diff: { available: false, onOpen: () => undefined }, |
| 68 | + terminal: { |
| 69 | + available: terminalAvailable, |
| 70 | + onOpen: () => useRightPanelStore.getState().openTerminal(threadRef, "term-1"), |
| 71 | + }, |
| 72 | + }} |
| 73 | + onAddBrowserInProfile={() => undefined} |
| 74 | + onAddPullRequest={() => undefined} |
| 75 | + onAddPullRequests={() => undefined} |
| 76 | + onAddFiles={() => undefined} |
| 77 | + onAddDevice={() => undefined} |
| 78 | + filesAvailable={false} |
| 79 | + pullRequestAvailable={false} |
| 80 | + pullRequestsAvailable={false} |
| 81 | + deviceAvailable={false} |
| 82 | + > |
| 83 | + {null} |
| 84 | + </RightPanelTabs> |
| 85 | + ); |
| 86 | +} |
| 87 | + |
| 88 | +function launcherRow(label: string): HTMLElement { |
| 89 | + const launcher = container.querySelector('[aria-label="Open a surface"]'); |
| 90 | + // Unavailable rows render as aria-disabled elements rather than buttons. |
| 91 | + const row = [ |
| 92 | + ...(launcher?.querySelectorAll<HTMLElement>('button, [aria-disabled="true"]') ?? []), |
| 93 | + ].find((element) => element.textContent?.startsWith(label)); |
| 94 | + if (!row) throw new Error(`No launcher row ${label}`); |
| 95 | + return row; |
| 96 | +} |
| 97 | + |
| 98 | +async function click(element: Element) { |
| 99 | + await act(async () => { |
| 100 | + element.dispatchEvent(new MouseEvent("pointerdown", { bubbles: true })); |
| 101 | + element.dispatchEvent(new MouseEvent("mousedown", { bubbles: true })); |
| 102 | + element.dispatchEvent(new MouseEvent("pointerup", { bubbles: true })); |
| 103 | + element.dispatchEvent(new MouseEvent("mouseup", { bubbles: true })); |
| 104 | + element.dispatchEvent(new MouseEvent("click", { bubbles: true })); |
| 105 | + }); |
| 106 | +} |
| 107 | + |
| 108 | +const threadSurfaces = () => |
| 109 | + selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, threadRef).surfaces; |
| 110 | + |
| 111 | +describe("opening Terminal from the launcher", () => { |
| 112 | + it("opens a terminal surface from the row when a project allows it", async () => { |
| 113 | + await act(async () => root.render(<Harness terminalAvailable />)); |
| 114 | + |
| 115 | + await click(launcherRow("Terminal")); |
| 116 | + expect(threadSurfaces()).toMatchObject([{ kind: "terminal", activeTerminalId: "term-1" }]); |
| 117 | + expect(container.querySelector('[aria-label="Open a surface"]')).toBeNull(); |
| 118 | + expect(container.textContent).toContain("Terminal 1"); |
| 119 | + }); |
| 120 | + |
| 121 | + it("keeps the row disabled and opens nothing without a project", async () => { |
| 122 | + await act(async () => root.render(<Harness terminalAvailable={false} />)); |
| 123 | + |
| 124 | + const row = launcherRow("Terminal"); |
| 125 | + expect(row.getAttribute("aria-disabled")).toBe("true"); |
| 126 | + await click(row); |
| 127 | + expect(threadSurfaces()).toEqual([]); |
| 128 | + expect(container.querySelector('[aria-label="Open a surface"]')).not.toBeNull(); |
| 129 | + }); |
| 130 | +}); |
0 commit comments