import { beforeEach, describe, expect, it, vi } from "vitest"; import { render, screen, waitFor } from "@testing-library/react"; import userEvent from "@testing-library/user-event"; import type { ButtonHTMLAttributes, InputHTMLAttributes, LabelHTMLAttributes, ReactNode, } from "react"; interface MockButtonProps extends ButtonHTMLAttributes { children: ReactNode; } interface MockInputProps extends InputHTMLAttributes { children?: ReactNode; } interface MockLabelProps extends LabelHTMLAttributes { children: ReactNode; } interface MockSession { id: string; providerId: string; providerType: string; status: "active" | "paused"; createdAt: Date; updatedAt: Date; } const mockApiPost = vi.fn<(endpoint: string, body?: unknown) => Promise<{ message: string }>>(); vi.mock("@/lib/api/client", () => ({ apiPost: (endpoint: string, body?: unknown): Promise<{ message: string }> => mockApiPost(endpoint, body), })); vi.mock("@/components/ui/button", () => ({ Button: ({ children, ...props }: MockButtonProps): React.JSX.Element => ( ), })); vi.mock("@/components/ui/input", () => ({ Input: ({ ...props }: MockInputProps): React.JSX.Element => , })); vi.mock("@/components/ui/label", () => ({ Label: ({ children, ...props }: MockLabelProps): React.JSX.Element => ( ), })); import { KillAllDialog } from "./KillAllDialog"; function makeSession(overrides: Partial): MockSession { return { id: "session-1", providerId: "internal", providerType: "internal", status: "active", createdAt: new Date("2026-03-07T10:00:00.000Z"), updatedAt: new Date("2026-03-07T10:01:00.000Z"), ...overrides, }; } describe("KillAllDialog", (): void => { beforeEach((): void => { vi.clearAllMocks(); mockApiPost.mockResolvedValue({ message: "killed" }); }); it("renders trigger button and requires exact confirmation text", async (): Promise => { const user = userEvent.setup(); render(); await user.click(screen.getByRole("button", { name: "Kill All" })); const confirmInput = screen.getByLabelText("Type KILL ALL to confirm"); const confirmButton = screen.getByRole("button", { name: "Kill All Agents" }); expect(confirmButton).toBeDisabled(); await user.type(confirmInput, "kill all"); expect(confirmButton).toBeDisabled(); await user.clear(confirmInput); await user.type(confirmInput, "KILL ALL"); expect(confirmButton).toBeEnabled(); }); it("kills only internal sessions by default and invokes completion callback", async (): Promise => { const onComplete = vi.fn<() => void>(); const user = userEvent.setup(); render( ); await user.click(screen.getByRole("button", { name: "Kill All" })); await user.type(screen.getByLabelText("Type KILL ALL to confirm"), "KILL ALL"); await user.click(screen.getByRole("button", { name: "Kill All Agents" })); await waitFor((): void => { expect(mockApiPost).toHaveBeenCalledWith("/api/mission-control/sessions/internal-1/kill", { force: true, }); }); expect(mockApiPost).not.toHaveBeenCalledWith("/api/mission-control/sessions/external-1/kill", { force: true, }); expect(onComplete).toHaveBeenCalledTimes(1); }); it("kills all providers when all scope is selected", async (): Promise => { const user = userEvent.setup(); render( ); await user.click(screen.getByRole("button", { name: "Kill All" })); await user.click(screen.getByRole("radio", { name: /All providers \(2\)/ })); await user.type(screen.getByLabelText("Type KILL ALL to confirm"), "KILL ALL"); await user.click(screen.getByRole("button", { name: "Kill All Agents" })); await waitFor((): void => { expect(mockApiPost).toHaveBeenCalledWith("/api/mission-control/sessions/internal-2/kill", { force: true, }); expect(mockApiPost).toHaveBeenCalledWith("/api/mission-control/sessions/external-2/kill", { force: true, }); }); }); it("shows empty-scope warning when internal sessions are unavailable", async (): Promise => { const user = userEvent.setup(); render( ); await user.click(screen.getByRole("button", { name: "Kill All" })); await user.type(screen.getByLabelText("Type KILL ALL to confirm"), "KILL ALL"); expect(screen.getByText("No sessions in the selected scope.")).toBeInTheDocument(); expect(screen.getByRole("button", { name: "Kill All Agents" })).toBeDisabled(); }); });