import { useState } from "react"; import { act, cleanup, fireEvent, render, screen } from "@testing-library/react"; import { afterEach, describe, expect, it, vi } from "vitest"; import { SessionSearchDialog } from "@/components/SessionSearchDialog"; import { useAppViewport } from "@/hooks/useAppViewport"; import type { ChatSummary } from "@/lib/types"; function session(index: number): ChatSummary { return { key: `websocket:chat-${index}`, channel: "websocket", chatId: `chat-${index}`, createdAt: null, updatedAt: null, title: `Chat ${index}`, preview: `Preview ${index}`, }; } describe("SessionSearchDialog", () => { it("windows large result sets and selects an item reached beyond the first window", () => { const onSelect = vi.fn(); render( session(i))} activeKey={null} loading={false} onOpenChange={() => {}} onSelect={onSelect} />); expect(screen.getAllByRole("button").length).toBeLessThanOrEqual(24); const input = screen.getByRole("textbox", { name: "Search" }); for (let i = 0; i < 40; i++) fireEvent.keyDown(input, { key: "ArrowDown" }); fireEvent.keyDown(input, { key: "Enter" }); expect(onSelect).toHaveBeenCalledWith("websocket:chat-40"); expect(screen.getAllByRole("button").length).toBeLessThanOrEqual(24); }); afterEach(() => { cleanup(); vi.unstubAllGlobals(); vi.restoreAllMocks(); }); it("shares keyboard rotation and dismissal with the body portal without losing a draft", () => { vi.stubGlobal("matchMedia", vi.fn(() => ({ matches: true, addEventListener: vi.fn(), removeEventListener: vi.fn(), }))); const viewport = Object.assign(new EventTarget(), { height: 428, offsetTop: 0, scale: 1 }); vi.stubGlobal("visualViewport", viewport); const onSelect = vi.fn(); function SearchApp() { useAppViewport(); const [open, setOpen] = useState(true); return <>