Skip to content

A cross-platform desktop All-in-One assistant for Claude Code, Codex, OpenCode,DeepSeek Harness and more. Only official website: ccswitch.io

41.1 KB
Raw Blame
import type { ReactNode } from "react";
import { act, renderHook, waitFor } from "@testing-library/react";
import {
  focusManager,
  QueryClient,
  QueryClientProvider,
} from "@tanstack/react-query";
import { beforeEach, describe, expect, it, vi } from "vitest";
import { useManagedAuth } from "@/components/providers/forms/hooks/useManagedAuth";
import { CODEX_OAUTH_DUPLICATE_ACCOUNT_ERROR } from "@/lib/api/auth";

const apiMocks = vi.hoisted(() => ({
  authGetStatus: vi.fn(),
  authStartLogin: vi.fn(),
  authPollForAccount: vi.fn(),
  authCancelLogin: vi.fn(),
  authRemoveAccount: vi.fn(),
  authLogout: vi.fn(),
  authSetDefaultAccount: vi.fn(),
}));
const toastMocks = vi.hoisted(() => ({
  success: vi.fn(),
  warning: vi.fn(),
  error: vi.fn(),
}));

vi.mock("@/lib/api", () => ({
  authApi: {
    authGetStatus: (...args: unknown[]) => apiMocks.authGetStatus(...args),
    authStartLogin: (...args: unknown[]) => apiMocks.authStartLogin(...args),
    authPollForAccount: (...args: unknown[]) =>
      apiMocks.authPollForAccount(...args),
    authCancelLogin: (...args: unknown[]) => apiMocks.authCancelLogin(...args),
    authLogout: (...args: unknown[]) => apiMocks.authLogout(...args),
    authRemoveAccount: (...args: unknown[]) =>
      apiMocks.authRemoveAccount(...args),
    authSetDefaultAccount: (...args: unknown[]) =>
      apiMocks.authSetDefaultAccount(...args),
  },
  settingsApi: {
    openExternal: vi.fn().mockResolvedValue(undefined),
  },
}));

vi.mock("@/lib/clipboard", () => ({
  copyText: vi.fn().mockResolvedValue(undefined),
}));

vi.mock("sonner", () => ({
  toast: {
    success: toastMocks.success,
    warning: toastMocks.warning,
    error: toastMocks.error,
  },
}));

function createWrapper(
  queryClient = new QueryClient({
    defaultOptions: {
      queries: { retry: false },
      mutations: { retry: false },
    },
  }),
) {
  return function Wrapper({ children }: { children: ReactNode }) {
    return (
      <QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
    );
  };
}

describe("useManagedAuth", () => {
  it("opens browser login without copying a device code and keeps the reauth target", async () => {
    const { copyText } = await import("@/lib/clipboard");
    const { settingsApi } = await import("@/lib/api");
    vi.mocked(copyText).mockClear();
    vi.mocked(settingsApi.openExternal).mockClear();
    apiMocks.authStartLogin.mockResolvedValue({
      provider: "codex_oauth",
      device_code: "browser:flow",
      user_code: "",
      verification_uri: "https://auth.openai.com/oauth/authorize?state=random",
      expires_in: 900,
      interval: 1,
    });
    const { result, unmount } = renderHook(
      () => useManagedAuth("codex_oauth", undefined, "browser"),
      { wrapper: createWrapper() },
    );
    await waitFor(() => expect(result.current.isStatusSuccess).toBe(true));
    act(() => result.current.reauthAccount("acct-1"));
    await waitFor(() => expect(result.current.isPolling).toBe(true));
    expect(apiMocks.authStartLogin).toHaveBeenCalledWith(
      "codex_oauth",
      undefined,
      "acct-1",
      "browser",
    );
    await waitFor(() =>
      expect(settingsApi.openExternal).toHaveBeenCalledWith(
        "https://auth.openai.com/oauth/authorize?state=random",
      ),
    );
    expect(copyText).not.toHaveBeenCalled();
    act(() => result.current.cancelAuth());
    await waitFor(() =>
      expect(apiMocks.authCancelLogin).toHaveBeenCalledWith(
        "codex_oauth",
        "browser:flow",
      ),
    );
    unmount();
  });

  it("copies the Claude sign-in link when the browser is not to be opened", async () => {
    const { copyText } = await import("@/lib/clipboard");
    const { settingsApi } = await import("@/lib/api");
    vi.mocked(copyText).mockClear();
    vi.mocked(settingsApi.openExternal).mockClear();
    window.localStorage.setItem("cc-switch.authCenter.openBrowser", "0");
    apiMocks.authPollForAccount.mockResolvedValue(null);
    apiMocks.authStartLogin.mockResolvedValue({
      provider: "claude_official",
      device_code: "flow-1",
      user_code: "",
      verification_uri: "https://claude.com/cai/oauth/authorize?state=s",
      expires_in: 900,
      interval: 1,
    });
    const { result, unmount } = renderHook(
      () => useManagedAuth("claude_official"),
      { wrapper: createWrapper() },
    );
    await waitFor(() => expect(result.current.isStatusSuccess).toBe(true));
    act(() => result.current.addAccount());
    await waitFor(() => expect(result.current.isPolling).toBe(true));
    // Windows 上 Claude Code 自己开浏览器,要告诉后端别开
    expect(apiMocks.authStartLogin).toHaveBeenCalledWith(
      "claude_official",
      undefined,
      undefined,
      "copy_link",
    );
    await waitFor(() =>
      expect(copyText).toHaveBeenCalledWith(
        "https://claude.com/cai/oauth/authorize?state=s",
      ),
    );
    expect(settingsApi.openExternal).not.toHaveBeenCalled();
    act(() => result.current.cancelAuth());
    unmount();
    window.localStorage.removeItem("cc-switch.authCenter.openBrowser");
  });

  it("copies only the link, not the device code, when the browser is not to be opened", async () => {
    const { copyText } = await import("@/lib/clipboard");
    const { settingsApi } = await import("@/lib/api");
    vi.mocked(copyText).mockClear();
    vi.mocked(settingsApi.openExternal).mockClear();
    window.localStorage.setItem("cc-switch.authCenter.openBrowser", "0");
    apiMocks.authPollForAccount.mockRejectedValue(new Error("pending"));
    apiMocks.authStartLogin.mockResolvedValue({
      provider: "codex_oauth",
      device_code: "device-1",
      user_code: "ABCD-EFGH",
      verification_uri: "https://auth.openai.com/codex/device",
      expires_in: 900,
      interval: 5,
    });
    const { result, unmount } = renderHook(
      () => useManagedAuth("codex_oauth", undefined, "device_code"),
      { wrapper: createWrapper() },
    );
    await waitFor(() => expect(result.current.isStatusSuccess).toBe(true));
    act(() => result.current.addAccount());
    await waitFor(() => expect(result.current.isPolling).toBe(true));
    expect(apiMocks.authStartLogin).toHaveBeenCalledWith(
      "codex_oauth",
      undefined,
      undefined,
      "device_code",
    );
    await waitFor(() =>
      expect(copyText).toHaveBeenCalledWith(
        "https://auth.openai.com/codex/device",
      ),
    );
    expect(copyText).not.toHaveBeenCalledWith("ABCD-EFGH");
    expect(settingsApi.openExternal).not.toHaveBeenCalled();
    act(() => result.current.cancelAuth());
    unmount();
    window.localStorage.removeItem("cc-switch.authCenter.openBrowser");
  });

  beforeEach(() => {
    apiMocks.authGetStatus.mockReset().mockResolvedValue({
      provider: "codex_oauth",
      authenticated: true,
      default_account_id: "acct-1",
      accounts: [
        {
          id: "acct-1",
          provider: "codex_oauth",
          login: "[email protected]",
          avatar_url: null,
          authenticated_at: 1,
          is_default: true,
          github_domain: "",
          reauth_required: false,
          requires_reauth: false,
        },
      ],
    });
    apiMocks.authStartLogin
      .mockReset()
      .mockImplementation(() => new Promise(() => {}));
    apiMocks.authPollForAccount.mockReset().mockResolvedValue(null);
    apiMocks.authCancelLogin.mockReset().mockResolvedValue(true);
    apiMocks.authRemoveAccount.mockReset().mockResolvedValue(undefined);
    apiMocks.authLogout.mockReset().mockResolvedValue(undefined);
    apiMocks.authSetDefaultAccount.mockReset().mockResolvedValue(undefined);
  });

  describe.each(["codex_oauth", "agy_oauth"] as const)(
    "%s browser login",
    (provider) => {
      const browserResponse = {
        provider,
        device_code: "browser:flow",
        user_code: "",
        verification_uri: "https://example.com/authorize",
        expires_in: 60,
        interval: 1,
      };
      const renderBrowserAuth = () =>
        renderHook(
          () =>
            useManagedAuth(
              provider,
              undefined,
              provider === "codex_oauth" ? "browser" : undefined,
            ),
          { wrapper: createWrapper() },
        );

      it("polls every 1500 ms without overlapping and resumes after pending errors", async () => {
        const { copyText } = await import("@/lib/clipboard");
        const { settingsApi } = await import("@/lib/api");
        let resolvePoll!: (account: null) => void;
        apiMocks.authStartLogin.mockResolvedValue(browserResponse);
        apiMocks.authPollForAccount
          .mockImplementationOnce(
            () => new Promise((resolve) => (resolvePoll = resolve)),
          )
          .mockRejectedValueOnce(new Error("authorization_pending"))
          .mockRejectedValueOnce(new Error("slow_down"))
          .mockResolvedValueOnce({ id: "acct-1" });
        const { result, unmount } = renderBrowserAuth();
        await waitFor(() => expect(result.current.isStatusSuccess).toBe(true));
        vi.useFakeTimers();
        try {
          await act(async () => result.current.startAuth());
          expect(apiMocks.authPollForAccount).toHaveBeenCalledTimes(1);
          expect(settingsApi.openExternal).toHaveBeenCalledWith(
            browserResponse.verification_uri,
          );
          expect(copyText).not.toHaveBeenCalled();
          await act(() => vi.advanceTimersByTimeAsync(4500));
          expect(apiMocks.authPollForAccount).toHaveBeenCalledTimes(1);

          await act(async () => resolvePoll(null));
          await act(() => vi.advanceTimersByTimeAsync(1499));
          expect(apiMocks.authPollForAccount).toHaveBeenCalledTimes(1);
          for (const elapsed of [1, 1500, 1500]) {
            await act(() => vi.advanceTimersByTimeAsync(elapsed));
          }
          expect(apiMocks.authPollForAccount).toHaveBeenCalledTimes(4);
          expect(result.current.pollingState).toBe("idle");
          expect(result.current.deviceCode).toBeNull();
          await act(() => vi.advanceTimersByTimeAsync(60000));
          expect(apiMocks.authPollForAccount).toHaveBeenCalledTimes(4);
          expect(apiMocks.authCancelLogin).not.toHaveBeenCalled();
        } finally {
          unmount();
          vi.useRealTimers();
        }
      });

      it.each(["cancel", "expire", "replace", "unmount"] as const)(
        "ignores an in-flight result after %s and cancels the old flow",
        async (ending) => {
          let resolvePoll!: (account: object) => void;
          apiMocks.authStartLogin
            .mockResolvedValueOnce(browserResponse)
            .mockResolvedValueOnce({
              ...browserResponse,
              device_code: "browser:replacement",
            });
          apiMocks.authPollForAccount
            .mockImplementationOnce(
              () => new Promise((resolve) => (resolvePoll = resolve)),
            )
            .mockImplementation(() => new Promise(() => {}));
          const { result, unmount } = renderBrowserAuth();
          await waitFor(() =>
            expect(result.current.isStatusSuccess).toBe(true),
          );
          vi.useFakeTimers();
          try {
            await act(async () => result.current.startAuth());
            expect(apiMocks.authPollForAccount).toHaveBeenCalledTimes(1);
            const statusCalls = apiMocks.authGetStatus.mock.calls.length;
            if (ending === "expire") {
              await act(() => vi.advanceTimersByTimeAsync(60000));
              expect(result.current.pollingState).toBe("error");
              expect(result.current.error).toBe(
                "Login expired. Please try again.",
              );
            } else if (ending === "replace") {
              await act(async () => result.current.startAuth());
              expect(result.current.deviceCode?.device_code).toBe(
                "browser:replacement",
              );
            } else if (ending === "unmount") {
              unmount();
            } else {
              await act(async () => result.current.cancelAuth());
              expect(result.current.pollingState).toBe("idle");
              expect(result.current.deviceCode).toBeNull();
            }
            expect(apiMocks.authCancelLogin).toHaveBeenCalledWith(
              provider,
              "browser:flow",
            );
            const expectedStatusAfterCancel =
              provider === "agy_oauth" && ending !== "unmount"
                ? statusCalls + 1
                : statusCalls;
            expect(apiMocks.authGetStatus).toHaveBeenCalledTimes(
              expectedStatusAfterCancel,
            );
            const statusCallsBeforeStalePoll =
              apiMocks.authGetStatus.mock.calls.length;

            await act(async () => resolvePoll({ id: "stale-account" }));
            await act(() => vi.advanceTimersByTimeAsync(4500));
            expect(apiMocks.authGetStatus).toHaveBeenCalledTimes(
              statusCallsBeforeStalePoll,
            );
            expect(apiMocks.authPollForAccount).toHaveBeenCalledTimes(
              ending === "replace" ? 2 : 1,
            );
            if (ending === "replace") {
              expect(result.current.isPolling).toBe(true);
              expect(result.current.deviceCode?.device_code).toBe(
                "browser:replacement",
              );
            }
          } finally {
            unmount();
            vi.useRealTimers();
          }
        },
      );
    },
  );

  it.each([
    [1, 8000],
    [10, 13000],
  ])("keeps device-code backoff for interval %s", async (interval, delay) => {
    apiMocks.authStartLogin.mockResolvedValue({
      provider: "codex_oauth",
      device_code: "device-1",
      user_code: "ABCD-EFGH",
      verification_uri: "https://example.com/device",
      expires_in: 600,
      interval,
    });
    const { result, unmount } = renderHook(
      () => useManagedAuth("codex_oauth"),
      {
        wrapper: createWrapper(),
      },
    );
    await waitFor(() => expect(result.current.isStatusSuccess).toBe(true));
    vi.useFakeTimers();
    try {
      await act(async () => result.current.startAuth());
      expect(apiMocks.authPollForAccount).toHaveBeenCalledTimes(1);
      await act(() => vi.advanceTimersByTimeAsync(delay - 1));
      expect(apiMocks.authPollForAccount).toHaveBeenCalledTimes(1);
      await act(() => vi.advanceTimersByTimeAsync(1));
      expect(apiMocks.authPollForAccount).toHaveBeenCalledTimes(2);
    } finally {
      unmount();
      vi.useRealTimers();
    }
  });

  it("starts reauthentication for the selected account", async () => {
    const { result } = renderHook(() => useManagedAuth("codex_oauth"), {
      wrapper: createWrapper(),
    });
    await waitFor(() => expect(result.current.isStatusSuccess).toBe(true));

    act(() => result.current.reauthAccount("acct-1"));

    await waitFor(() =>
      expect(apiMocks.authStartLogin).toHaveBeenCalledWith(
        "codex_oauth",
        undefined,
        "acct-1",
      ),
    );
  });

  it("retries reauthentication for the same target account", async () => {
    apiMocks.authStartLogin.mockRejectedValue(new Error("start failed"));
    const { result } = renderHook(() => useManagedAuth("codex_oauth"), {
      wrapper: createWrapper(),
    });
    await waitFor(() => expect(result.current.isStatusSuccess).toBe(true));

    act(() => result.current.reauthAccount("acct-1"));
    await waitFor(() => expect(result.current.pollingState).toBe("error"));
    act(() => result.current.retryAuth());

    await waitFor(() =>
      expect(apiMocks.authStartLogin).toHaveBeenCalledTimes(2),
    );
    expect(apiMocks.authStartLogin).toHaveBeenNthCalledWith(
      2,
      "codex_oauth",
      undefined,
      "acct-1",
    );
  });

  it("localizes a duplicate Codex account error", async () => {
    apiMocks.authStartLogin.mockResolvedValue({
      provider: "codex_oauth",
      device_code: "device-1",
      user_code: "ABCD-EFGH",
      verification_uri: "https://example.com/device",
      expires_in: 600,
      interval: 5,
    });
    apiMocks.authPollForAccount.mockRejectedValue(
      new Error(CODEX_OAUTH_DUPLICATE_ACCOUNT_ERROR),
    );
    const { result } = renderHook(() => useManagedAuth("codex_oauth"), {
      wrapper: createWrapper(),
    });

    act(() => result.current.addAccount());

    await waitFor(() => expect(result.current.pollingState).toBe("error"));
    expect(result.current.error).toBe(
      "该 ChatGPT 账号已添加,请直接使用现有账号。",
    );
  });

  it("cancels the active Codex device flow in the backend", async () => {
    apiMocks.authStartLogin.mockResolvedValue({
      provider: "codex_oauth",
      device_code: "device-1",
      user_code: "ABCD-EFGH",
      verification_uri: "https://example.com/device",
      expires_in: 600,
      interval: 5,
    });
    apiMocks.authPollForAccount.mockImplementation(() => new Promise(() => {}));
    const { result } = renderHook(() => useManagedAuth("codex_oauth"), {
      wrapper: createWrapper(),
    });
    act(() => result.current.reauthAccount("acct-1"));
    await waitFor(() => expect(result.current.deviceCode).not.toBeNull());

    act(() => result.current.cancelAuth());

    await waitFor(() =>
      expect(apiMocks.authCancelLogin).toHaveBeenCalledWith(
        "codex_oauth",
        "device-1",
      ),
    );
    expect(result.current.pollingState).toBe("idle");
  });

  it("stops polling and cancels the backend flow when an account is removed mid-login", async () => {
    apiMocks.authStartLogin.mockResolvedValue({
      provider: "agy_oauth",
      device_code: "flow-remove",
      user_code: "",
      verification_uri: "",
      expires_in: 600,
      interval: 2,
    });
    apiMocks.authPollForAccount.mockImplementation(() => new Promise(() => {}));
    apiMocks.authRemoveAccount.mockResolvedValue(undefined);
    apiMocks.authCancelLogin.mockResolvedValue(true);
    const { result } = renderHook(() => useManagedAuth("agy_oauth"), {
      wrapper: createWrapper(),
    });
    act(() => result.current.addAccount());
    await waitFor(() => expect(result.current.isPolling).toBe(true));

    act(() => result.current.removeAccount("a"));

    await waitFor(() =>
      expect(apiMocks.authCancelLogin).toHaveBeenCalledWith(
        "agy_oauth",
        "flow-remove",
      ),
    );
    await waitFor(() => expect(result.current.isPolling).toBe(false));
  });

  it("refreshes status when login committed before cancellation", async () => {
    let resolvePoll!: (account: object) => void;
    let resolveCancel!: (cancelled: boolean) => void;
    apiMocks.authStartLogin.mockResolvedValue({
      provider: "codex_oauth",
      device_code: "device-1",
      user_code: "ABCD-EFGH",
      verification_uri: "https://example.com/device",
      expires_in: 600,
      interval: 5,
    });
    apiMocks.authPollForAccount.mockImplementation(
      () => new Promise((resolve) => (resolvePoll = resolve)),
    );
    apiMocks.authCancelLogin.mockImplementation(
      () => new Promise((resolve) => (resolveCancel = resolve)),
    );
    const { result } = renderHook(() => useManagedAuth("codex_oauth"), {
      wrapper: createWrapper(),
    });
    await waitFor(() => expect(result.current.isStatusSuccess).toBe(true));

    act(() => result.current.reauthAccount("acct-1"));
    await waitFor(() => expect(apiMocks.authPollForAccount).toHaveBeenCalled());
    apiMocks.authGetStatus.mockResolvedValue({
      provider: "codex_oauth",
      authenticated: true,
      default_account_id: "acct-1",
      accounts: [
        ...result.current.accounts,
        {
          id: "acct-2",
          provider: "codex_oauth",
          login: "[email protected]",
          avatar_url: null,
          authenticated_at: 2,
          is_default: false,
          github_domain: "",
          reauth_required: false,
          requires_reauth: false,
        },
      ],
    });

    act(() => result.current.cancelAuth());
    await waitFor(() => expect(apiMocks.authCancelLogin).toHaveBeenCalled());
    act(() => {
      resolvePoll({ id: "acct-2" });
      resolveCancel(false);
    });

    await waitFor(() => expect(result.current.accounts).toHaveLength(2));
  });

  it("waits for active-flow cancellation before starting another login", async () => {
    let resolveCancel!: (cancelled: boolean) => void;
    apiMocks.authStartLogin.mockResolvedValue({
      provider: "codex_oauth",
      device_code: "device-1",
      user_code: "ABCD-EFGH",
      verification_uri: "https://example.com/device",
      expires_in: 600,
      interval: 5,
    });
    apiMocks.authPollForAccount.mockImplementation(() => new Promise(() => {}));
    apiMocks.authCancelLogin.mockImplementation(
      () => new Promise((resolve) => (resolveCancel = resolve)),
    );
    const { result } = renderHook(() => useManagedAuth("codex_oauth"), {
      wrapper: createWrapper(),
    });

    act(() => result.current.reauthAccount("acct-1"));
    await waitFor(() => expect(result.current.deviceCode).not.toBeNull());
    act(() => result.current.reauthAccount("acct-1"));
    await waitFor(() => expect(apiMocks.authCancelLogin).toHaveBeenCalled());
    expect(apiMocks.authStartLogin).toHaveBeenCalledTimes(1);

    act(() => resolveCancel(true));

    await waitFor(() =>
      expect(apiMocks.authStartLogin).toHaveBeenCalledTimes(2),
    );
  });

  it.each([
    ["remove", false],
    ["remove", true],
    ["logout", false],
    ["logout", true],
  ] as const)(
    "refreshes providers and accounts after %s (partial failure=%s)",
    async (operation, fails) => {
      const queryClient = new QueryClient({
        defaultOptions: {
          queries: { retry: false },
          mutations: { retry: false },
        },
      });
      queryClient.setQueryData(["providers", "codex"], {
        current: "old-binding",
      });
      queryClient.setQueryData(["providers", "claude"], {
        current: "unrelated",
      });
      const { result } = renderHook(() => useManagedAuth("codex_oauth"), {
        wrapper: createWrapper(queryClient),
      });
      await waitFor(() => expect(result.current.accounts).toHaveLength(1));
      apiMocks.authGetStatus.mockResolvedValue({
        provider: "codex_oauth",
        authenticated: false,
        accounts: [],
      });
      if (fails) {
        const mutation =
          operation === "remove"
            ? apiMocks.authRemoveAccount
            : apiMocks.authLogout;
        mutation.mockRejectedValue(
          new Error("账号已删除,但 Codex 供应商解绑失败"),
        );
      }
      act(() =>
        operation === "remove"
          ? result.current.removeAccount("acct-1")
          : result.current.logout(),
      );
      await waitFor(() =>
        expect(
          queryClient.getQueryState(["providers", "codex"])?.isInvalidated,
        ).toBe(true),
      );
      await waitFor(() => expect(result.current.accounts).toHaveLength(0));
      expect(
        queryClient.getQueryState(["providers", "claude"])?.isInvalidated,
      ).toBe(false);
      if (fails) expect(result.current.error).toContain("解绑失败");
    },
  );

  it("shows a success toast after removing an account", async () => {
    const { result } = renderHook(() => useManagedAuth("codex_oauth"), {
      wrapper: createWrapper(),
    });
    await waitFor(() => expect(result.current.isStatusSuccess).toBe(true));

    act(() => result.current.removeAccount("acct-1"));

    await waitFor(() =>
      expect(apiMocks.authRemoveAccount).toHaveBeenCalledWith(
        "codex_oauth",
        "acct-1",
      ),
    );
    await waitFor(() =>
      expect(toastMocks.success).toHaveBeenCalledWith("账号已移除"),
    );
  });

  // 移除/登出的 xAI 号若正是 Grok CLI 当前登录,后端会让出 CLI 登录;
  // 没绑号的 Grok 官方卡查的就是 CLI 当前登录,成功失败都要重查
  it.each([
    ["remove", false],
    ["remove", true],
    ["logout", false],
    ["logout", true],
  ] as const)(
    "invalidates the Grok CLI quota after xAI %s (failed=%s)",
    async (operation, fails) => {
      const queryClient = new QueryClient({
        defaultOptions: {
          queries: { retry: false },
          mutations: { retry: false },
        },
      });
      const quotaKey = ["subscription", "quota", "grokbuild"];
      queryClient.setQueryData(quotaKey, { success: true, tiers: [] });
      const { result } = renderHook(() => useManagedAuth("xai_oauth"), {
        wrapper: createWrapper(queryClient),
      });
      await waitFor(() => expect(result.current.isStatusSuccess).toBe(true));
      if (fails) {
        (operation === "remove"
          ? apiMocks.authRemoveAccount
          : apiMocks.authLogout
        ).mockRejectedValue(new Error("boom"));
      }
      act(() =>
        operation === "remove"
          ? result.current.removeAccount("acct-1")
          : result.current.logout(),
      );
      await waitFor(() =>
        expect(queryClient.getQueryState(quotaKey)?.isInvalidated).toBe(true),
      );
    },
  );

  it("leaves the Grok CLI quota alone for other providers", async () => {
    const queryClient = new QueryClient({
      defaultOptions: {
        queries: { retry: false },
        mutations: { retry: false },
      },
    });
    const quotaKey = ["subscription", "quota", "grokbuild"];
    queryClient.setQueryData(quotaKey, { success: true, tiers: [] });
    queryClient.setQueryData(["providers", "codex"], { current: "x" });
    const { result } = renderHook(() => useManagedAuth("codex_oauth"), {
      wrapper: createWrapper(queryClient),
    });
    await waitFor(() => expect(result.current.isStatusSuccess).toBe(true));
    act(() => result.current.removeAccount("acct-1"));
    await waitFor(() => expect(toastMocks.success).toHaveBeenCalled());
    await waitFor(() =>
      expect(
        queryClient.getQueryState(["providers", "codex"])?.isInvalidated,
      ).toBe(true),
    );
    expect(queryClient.getQueryState(quotaKey)?.isInvalidated).toBe(false);
  });

  it("refreshes agy_oauth login status on window focus even within staleTime", async () => {
    apiMocks.authGetStatus
      .mockResolvedValueOnce({
        provider: "agy_oauth",
        authenticated: true,
        default_account_id: "a",
        accounts: [
          {
            id: "a",
            provider: "agy_oauth",
            login: "[email protected]",
            avatar_url: null,
            authenticated_at: 1,
            is_default: true,
            github_domain: "google.com",
            requires_reauth: false,
          },
          {
            id: "b",
            provider: "agy_oauth",
            login: "[email protected]",
            avatar_url: null,
            authenticated_at: 2,
            is_default: false,
            github_domain: "google.com",
            requires_reauth: false,
          },
        ],
      })
      .mockResolvedValueOnce({
        provider: "agy_oauth",
        authenticated: true,
        default_account_id: "b",
        accounts: [
          {
            id: "a",
            provider: "agy_oauth",
            login: "[email protected]",
            avatar_url: null,
            authenticated_at: 1,
            is_default: false,
            github_domain: "google.com",
            requires_reauth: false,
          },
          {
            id: "b",
            provider: "agy_oauth",
            login: "[email protected]",
            avatar_url: null,
            authenticated_at: 2,
            is_default: true,
            github_domain: "google.com",
            requires_reauth: false,
          },
        ],
      });

    focusManager.setFocused(true);
    try {
      const { result } = renderHook(() => useManagedAuth("agy_oauth"), {
        wrapper: createWrapper(),
      });
      await waitFor(() => expect(result.current.defaultAccountId).toBe("a"));
      expect(apiMocks.authGetStatus).toHaveBeenCalledTimes(1);

      act(() => focusManager.setFocused(false));
      act(() => focusManager.setFocused(true));

      await waitFor(() => expect(result.current.defaultAccountId).toBe("b"));
      expect(apiMocks.authGetStatus).toHaveBeenCalledTimes(2);
    } finally {
      focusManager.setFocused(undefined);
    }
  });

  it("refreshes agy_oauth login status and invalidates quota caches on visible window focus even when focusManager stays focused", async () => {
    const queryClient = new QueryClient({
      defaultOptions: {
        queries: { retry: false },
        mutations: { retry: false },
      },
    });
    const wrapper = ({ children }: { children: ReactNode }) => (
      <QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
    );
    const quotaA = {
      tool: "agy",
      credentialStatus: "valid",
      credentialMessage: null,
      success: true,
      tiers: [],
      extraUsage: null,
      error: null,
      queriedAt: 1,
    };
    const notFound = {
      ...quotaA,
      credentialStatus: "not_found",
      success: false,
      queriedAt: null,
    };
    queryClient.setQueryData(["agy_oauth", "quota", "a"], quotaA);
    queryClient.setQueryData(["agy_oauth", "quota", "b"], notFound);
    queryClient.setQueryData(["agy_oauth", "last_quota", "a"], null);

    apiMocks.authGetStatus
      .mockResolvedValueOnce({
        provider: "agy_oauth",
        authenticated: true,
        default_account_id: "a",
        accounts: [],
      })
      .mockResolvedValueOnce({
        provider: "agy_oauth",
        authenticated: true,
        default_account_id: "b",
        accounts: [],
      });

    // 模拟真实 Tauri 窗口(windowActivity.ts):切去外部终端时窗口仍可见,focusManager 始终为 true
    focusManager.setFocused(true);
    try {
      const { result } = renderHook(() => useManagedAuth("agy_oauth"), {
        wrapper,
      });
      await waitFor(() => expect(result.current.defaultAccountId).toBe("a"));
      expect(apiMocks.authGetStatus).toHaveBeenCalledTimes(1);

      act(() => {
        window.dispatchEvent(new Event("blur"));
        window.dispatchEvent(new Event("focus"));
      });

      await waitFor(() => expect(result.current.defaultAccountId).toBe("b"));
      expect(apiMocks.authGetStatus).toHaveBeenCalledTimes(2);
      // 外部终端换号后同步重置 not_found 并失效额度缓存
      expect(
        queryClient.getQueryData(["agy_oauth", "quota", "b"]),
      ).toBeUndefined();
      expect(
        queryClient
          .getQueryCache()
          .find({ queryKey: ["agy_oauth", "quota", "b"] }),
      ).toBeDefined();
      expect(
        queryClient.getQueryState(["agy_oauth", "quota", "a"])?.isInvalidated,
      ).toBe(true);
      expect(
        queryClient.getQueryState(["agy_oauth", "last_quota", "a"])
          ?.isInvalidated,
      ).toBe(true);
    } finally {
      focusManager.setFocused(undefined);
    }
  });

  it("does not invalidate agy quota caches on window focus when the current account did not change", async () => {
    const queryClient = new QueryClient({
      defaultOptions: {
        queries: { retry: false },
        mutations: { retry: false },
      },
    });
    const wrapper = ({ children }: { children: ReactNode }) => (
      <QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
    );
    queryClient.setQueryData(["agy_oauth", "quota", "a"], {
      tool: "agy",
      credentialStatus: "valid",
      credentialMessage: null,
      success: true,
      tiers: [],
      extraUsage: null,
      error: null,
      queriedAt: 1,
    });
    apiMocks.authGetStatus.mockResolvedValue({
      provider: "agy_oauth",
      authenticated: true,
      default_account_id: "a",
      accounts: [],
    });

    const { result } = renderHook(() => useManagedAuth("agy_oauth"), {
      wrapper,
    });
    await waitFor(() => expect(result.current.defaultAccountId).toBe("a"));
    expect(apiMocks.authGetStatus).toHaveBeenCalledTimes(1);

    act(() => {
      window.dispatchEvent(new Event("focus"));
    });

    await waitFor(() =>
      expect(apiMocks.authGetStatus).toHaveBeenCalledTimes(2),
    );
    expect(
      queryClient.getQueryState(["agy_oauth", "quota", "a"])?.isInvalidated,
    ).toBe(false);
  });

  it("resets cached not_found and invalidates agy quota caches when agy_oauth login polling completes", async () => {
    const queryClient = new QueryClient({
      defaultOptions: {
        queries: { retry: false },
        mutations: { retry: false },
      },
    });
    const quotaA = {
      tool: "agy",
      credentialStatus: "valid",
      credentialMessage: null,
      success: true,
      tiers: [],
      extraUsage: null,
      error: null,
      queriedAt: 1,
    };
    const notFound = {
      ...quotaA,
      credentialStatus: "not_found",
      success: false,
      queriedAt: null,
    };
    queryClient.setQueryData(["agy_oauth", "quota", "a"], quotaA);
    queryClient.setQueryData(["agy_oauth", "quota", "b"], notFound);
    queryClient.setQueryData(["agy_oauth", "last_quota", "a"], null);

    apiMocks.authGetStatus
      .mockResolvedValueOnce({
        provider: "agy_oauth",
        authenticated: true,
        default_account_id: "a",
        accounts: [],
      })
      .mockResolvedValue({
        provider: "agy_oauth",
        authenticated: true,
        default_account_id: "b",
        accounts: [],
      });
    apiMocks.authStartLogin.mockResolvedValue({
      provider: "agy_oauth",
      device_code: "browser:flow",
      user_code: "",
      verification_uri: "",
      browser_opened: true,
      expires_in: 60,
      interval: 1,
    });
    apiMocks.authPollForAccount.mockResolvedValueOnce({ id: "b" });

    const { result, unmount } = renderHook(() => useManagedAuth("agy_oauth"), {
      wrapper: createWrapper(queryClient),
    });
    await waitFor(() => expect(result.current.defaultAccountId).toBe("a"));

    act(() => result.current.addAccount());

    await waitFor(() => {
      expect(result.current.defaultAccountId).toBe("b");
      expect(result.current.pollingState).toBe("idle");
    });
    expect(
      queryClient.getQueryData(["agy_oauth", "quota", "b"]),
    ).toBeUndefined();
    expect(
      queryClient
        .getQueryCache()
        .find({ queryKey: ["agy_oauth", "quota", "b"] }),
    ).toBeDefined();
    expect(
      queryClient.getQueryState(["agy_oauth", "quota", "a"])?.isInvalidated,
    ).toBe(true);
    expect(
      queryClient.getQueryState(["agy_oauth", "last_quota", "a"])
        ?.isInvalidated,
    ).toBe(true);
    unmount();
  });

  it("tracks lastCompletedLogin (created vs existing) and invalidates agy caches on agy_oauth completion", async () => {
    const queryClient = new QueryClient({
      defaultOptions: {
        queries: { retry: false },
        mutations: { retry: false },
      },
    });
    queryClient.setQueryData(["agy_oauth", "quota", "acct-1"], {
      success: true,
    });
    queryClient.setQueryData(["subscription", "quota", "agy"], {
      success: true,
    });
    apiMocks.authStartLogin.mockResolvedValue({
      provider: "agy_oauth",
      device_code: "flow-agy",
      user_code: "",
      verification_uri: "",
      expires_in: 600,
      interval: 2,
    });
    apiMocks.authPollForAccount
      .mockResolvedValueOnce({ id: "acct-1", login: "[email protected]" })
      .mockResolvedValueOnce({ id: "acct-2", login: "[email protected]" });

    const { result } = renderHook(() => useManagedAuth("agy_oauth"), {
      wrapper: createWrapper(queryClient),
    });
    await waitFor(() => expect(result.current.isStatusSuccess).toBe(true));

    // Existing account reauth -> created: false
    act(() => result.current.reauthAccount("acct-1"));
    await waitFor(() =>
      expect(result.current.lastCompletedLogin).toEqual({
        account: { id: "acct-1", login: "[email protected]" },
        created: false,
      }),
    );
    expect(
      queryClient.getQueryState(["agy_oauth", "quota", "acct-1"])
        ?.isInvalidated,
    ).toBe(true);
    expect(
      queryClient.getQueryState(["subscription", "quota", "agy"])
        ?.isInvalidated,
    ).toBe(true);

    // New account -> created: true
    act(() => result.current.reauthAccount("acct-1"));
    await waitFor(() =>
      expect(result.current.lastCompletedLogin).toEqual({
        account: { id: "acct-2", login: "[email protected]" },
        created: true,
      }),
    );
  });

  it("invalidates agy_oauth status when cancelling an in-progress login to surface restored account", async () => {
    const queryClient = new QueryClient({
      defaultOptions: {
        queries: { retry: false },
        mutations: { retry: false },
      },
    });
    apiMocks.authStartLogin.mockResolvedValue({
      provider: "agy_oauth",
      device_code: "flow-agy-cancel",
      user_code: "",
      verification_uri: "",
      expires_in: 600,
      interval: 2,
    });
    apiMocks.authCancelLogin.mockResolvedValue(true);

    const { result } = renderHook(() => useManagedAuth("agy_oauth"), {
      wrapper: createWrapper(queryClient),
    });
    await waitFor(() => expect(result.current.isStatusSuccess).toBe(true));

    await act(async () => result.current.addAccount());
    await waitFor(() => expect(result.current.isPolling).toBe(true));

    const statusCallsBeforeCancel = apiMocks.authGetStatus.mock.calls.length;
    await act(async () => {
      await result.current.cancelAuthAndWait();
    });

    await waitFor(() =>
      expect(apiMocks.authGetStatus.mock.calls.length).toBeGreaterThan(
        statusCallsBeforeCancel,
      ),
    );
  });

  it("resets shared default and account quota caches on removeAccount and logout", async () => {
    const queryClient = new QueryClient({
      defaultOptions: {
        queries: { retry: false },
        mutations: { retry: false },
      },
    });
    apiMocks.authRemoveAccount.mockResolvedValue(undefined);
    apiMocks.authLogout.mockResolvedValue(undefined);
    queryClient.setQueryData(["codex_oauth", "quota", "default"], { ok: 1 });
    queryClient.setQueryData(["codex_oauth", "quota", "acct-1"], { ok: 1 });
    queryClient.setQueryData(["copilot", "quota", "default"], { ok: 1 });
    queryClient.setQueryData(["copilot", "quota", "acct-gh"], { ok: 1 });

    const codex = renderHook(() => useManagedAuth("codex_oauth"), {
      wrapper: createWrapper(queryClient),
    });
    await waitFor(() =>
      expect(codex.result.current.isStatusSuccess).toBe(true),
    );
    act(() => codex.result.current.removeAccount("acct-1"));
    await waitFor(() =>
      expect(
        queryClient.getQueryData(["codex_oauth", "quota", "acct-1"]),
      ).toBeUndefined(),
    );
    expect(
      queryClient.getQueryData(["codex_oauth", "quota", "default"]),
    ).toBeUndefined();

    const copilot = renderHook(() => useManagedAuth("github_copilot"), {
      wrapper: createWrapper(queryClient),
    });
    await waitFor(() =>
      expect(copilot.result.current.isStatusSuccess).toBe(true),
    );
    act(() => copilot.result.current.logout());
    await waitFor(() =>
      expect(
        queryClient.getQueryData(["copilot", "quota", "acct-gh"]),
      ).toBeUndefined(),
    );
    expect(
      queryClient.getQueryData(["copilot", "quota", "default"]),
    ).toBeUndefined();
  });

  it("resets default quota and invalidates plan caches on setDefaultAccount across providers", async () => {
    const queryClient = new QueryClient({
      defaultOptions: {
        queries: { retry: false },
        mutations: { retry: false },
      },
    });
    apiMocks.authSetDefaultAccount.mockResolvedValue(undefined);
    queryClient.setQueryData(["claude_official", "default_plan"], {
      plan: "max",
    });
    queryClient.setQueryData(["subscription", "quota", "claude"], { ok: 1 });
    queryClient.setQueryData(["xai_oauth", "quota", "default"], { ok: 1 });
    queryClient.setQueryData(["subscription", "quota", "agy"], { ok: 1 });
    queryClient.setQueryData(["agy_oauth", "last_quota", "acct-1"], { ok: 1 });

    const claude = renderHook(() => useManagedAuth("claude_official"), {
      wrapper: createWrapper(queryClient),
    });
    await waitFor(() =>
      expect(claude.result.current.isStatusSuccess).toBe(true),
    );
    act(() => claude.result.current.setDefaultAccount("acct-1"));
    await waitFor(() => {
      expect(
        queryClient.getQueryState(["claude_official", "default_plan"])
          ?.isInvalidated,
      ).toBe(true);
      expect(
        queryClient.getQueryData(["subscription", "quota", "claude"]),
      ).toBeUndefined();
    });

    const xai = renderHook(() => useManagedAuth("xai_oauth"), {
      wrapper: createWrapper(queryClient),
    });
    await waitFor(() => expect(xai.result.current.isStatusSuccess).toBe(true));
    act(() => xai.result.current.setDefaultAccount("acct-1"));
    await waitFor(() =>
      expect(
        queryClient.getQueryData(["xai_oauth", "quota", "default"]),
      ).toBeUndefined(),
    );

    const agy = renderHook(() => useManagedAuth("agy_oauth"), {
      wrapper: createWrapper(queryClient),
    });
    await waitFor(() => expect(agy.result.current.isStatusSuccess).toBe(true));
    const agyStatusCallsBefore = apiMocks.authGetStatus.mock.calls.filter(
      ([provider]) => provider === "agy_oauth",
    ).length;
    act(() => agy.result.current.setDefaultAccount("acct-1"));
    await waitFor(() => {
      expect(
        apiMocks.authGetStatus.mock.calls.filter(
          ([provider]) => provider === "agy_oauth",
        ).length,
      ).toBeGreaterThan(agyStatusCallsBefore);
      expect(
        queryClient.getQueryData(["subscription", "quota", "agy"]),
      ).toBeUndefined();
      expect(
        queryClient.getQueryState(["agy_oauth", "last_quota", "acct-1"])
          ?.isInvalidated,
      ).toBe(true);
    });
  });
});