import { cleanup, render, screen } from "@testing-library/react"; import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; import { ToastProvider } from "@/components/toast-provider"; import { GitHubCLIForm } from "./github-cli-form"; const mocks = vi.hoisted(() => ({ fetchAccounts: vi.fn(), })); vi.mock("@/lib/api/domains/github-api", () => ({ fetchGitHubCLIAccounts: mocks.fetchAccounts, })); beforeEach(() => { mocks.fetchAccounts.mockReset(); }); afterEach(() => cleanup()); describe("GitHubCLIForm", () => { it("shows account discovery failures instead of an empty-account message", async () => { mocks.fetchAccounts.mockRejectedValue(new Error("Request failed (500): github_internal_error")); render( , ); expect((await screen.findByRole("alert")).textContent).toBe( "Could not load GitHub CLI accounts. Request failed (500): github_internal_error", ); expect(screen.queryByText(/Sign in with/)).toBeNull(); }); it("keeps sign-in guidance for a successful empty account list", async () => { mocks.fetchAccounts.mockResolvedValue([]); render( , ); expect(await screen.findByText(/Sign in with/)).toBeTruthy(); expect(screen.queryByRole("alert")).toBeNull(); }); it("reports the preferred account without requiring a second action", async () => { const account = { host: "github.com", login: "octocat", active: true, selected: true, state: "active", }; const onAccountChange = vi.fn(); mocks.fetchAccounts.mockResolvedValue([account]); render( , ); expect(await screen.findByText(/octocat/)).toBeTruthy(); expect(onAccountChange).toHaveBeenLastCalledWith(account); expect(screen.queryByRole("button", { name: "Use account" })).toBeNull(); }); });