import { act, render, screen, waitFor } from "@testing-library/react"; import userEvent from "@testing-library/user-event"; import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; import { ChatList } from "@/components/ChatList"; import type { ChatSummary } from "@/lib/types"; import { mockBrowserFocus } from "./browser-focus"; const topic: ChatSummary = { key: "websocket:review", chatId: "review", channel: "websocket", title: "Review", preview: "", createdAt: "2026-10-04T10:00:00Z", updatedAt: "2026-10-04T10:00:00Z", }; function SidebarFocusCase() { return <> ; } describe("sidebar action focus", () => { let restoreBrowserFocus: () => void; beforeEach(() => { restoreBrowserFocus = mockBrowserFocus(); }); afterEach(() => { restoreBrowserFocus(); localStorage.removeItem("nanobot-webui.collapsed-pane-groups.v1"); }); it.each([ "Topic actions for Review", "Topic actions for Group", "Research pane actions", "Topic actions for Photos", ])("returns Escape focus to %s and allows continued tabbing", async (name) => { const user = userEvent.setup(); render(); const trigger = screen.getByRole("button", { name }); trigger.focus(); await user.keyboard("{Enter}"); await screen.findByRole("menu"); await user.keyboard("{Escape}"); await waitFor(() => expect(screen.queryByRole("menu")).not.toBeInTheDocument()); await waitFor(() => expect(trigger).toHaveFocus()); await user.tab(); expect(trigger).not.toHaveFocus(); expect(document.body).not.toHaveFocus(); }); it("does not reuse Escape focus restoration when the next action opens Rename", async () => { const user = userEvent.setup(); render(); const trigger = screen.getByRole("button", { name: "Topic actions for Review" }); trigger.focus(); await user.keyboard("{Enter}{Escape}"); await waitFor(() => expect(trigger).toHaveFocus()); await user.keyboard("{Enter}"); await user.click(await screen.findByRole("menuitem", { name: "Rename" })); const input = await screen.findByPlaceholderText("Topic name"); await waitFor(() => expect(screen.queryByRole("menu")).not.toBeInTheDocument()); await waitFor(() => expect(input).toHaveFocus()); await user.keyboard("Updated"); expect(input).toHaveValue("Updated"); expect(screen.queryByRole("dialog")).not.toBeInTheDocument(); await user.keyboard("{Escape}"); expect(screen.getByRole("button", { name: "Review" })).toHaveFocus(); }); it("keeps an outside click focused on its destination", async () => { const user = userEvent.setup(); render(); const trigger = screen.getByRole("button", { name: "Topic actions for Review" }); trigger.focus(); await user.keyboard("{Enter}"); await screen.findByRole("menu"); const outside = screen.getByRole("button", { name: "Outside destination" }); await user.click(outside); await waitFor(() => expect(screen.queryByRole("menu")).not.toBeInTheDocument()); expect(outside).toHaveFocus(); }); it("returns Escape from Move to to the root action button", async () => { const user = userEvent.setup(); render(); const trigger = screen.getByRole("button", { name: "Topic actions for Review" }); trigger.focus(); await user.keyboard("{Enter}"); const move = await screen.findByRole("menuitem", { name: "Move to" }); act(() => move.focus()); await user.keyboard("{ArrowRight}"); await screen.findByRole("menuitem", { name: "Group 2/4" }); await user.keyboard("{Escape}"); await waitFor(() => expect(screen.queryByRole("menu")).not.toBeInTheDocument()); await waitFor(() => expect(trigger).toHaveFocus()); }); it("keeps ArrowLeft within the parent menu, then hands Rename its input focus", async () => { const user = userEvent.setup(); render(); const trigger = screen.getByRole("button", { name: "Topic actions for Review" }); trigger.focus(); await user.keyboard("{Enter}"); const move = await screen.findByRole("menuitem", { name: "Move to" }); act(() => move.focus()); await user.keyboard("{ArrowRight}"); await screen.findByRole("menuitem", { name: "Group 2/4" }); await user.keyboard("{ArrowLeft}"); await waitFor(() => expect(move).toHaveFocus()); expect(screen.queryByRole("menuitem", { name: "Group 2/4" })).not.toBeInTheDocument(); await user.click(screen.getByRole("menuitem", { name: "Rename" })); const input = await screen.findByPlaceholderText("Topic name"); await waitFor(() => expect(screen.queryByRole("menu")).not.toBeInTheDocument()); expect(input).toHaveFocus(); }); it("allows Select to remove the trigger without restoring focus to it", async () => { const user = userEvent.setup(); render(); const trigger = screen.getByRole("button", { name: "Topic actions for Review" }); trigger.focus(); await user.keyboard("{Enter}"); await user.click(await screen.findByRole("menuitem", { name: "Select" })); await waitFor(() => expect(trigger).not.toBeInTheDocument()); expect(trigger).not.toHaveFocus(); await user.keyboard("{Escape}"); expect(screen.getByRole("button", { name: "Topic actions for Review" })).toBeInTheDocument(); }); });