/**
 * useAuth Hook 单元测试
 */
import { renderHook, waitFor } from "@testing-library/react";
import { describe, it, expect, vi, beforeEach } from "vitest";
import { useLogin, useRegister, useLogout } from "@/hooks/useAuth";
import * as authApi from "@/api/auth";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { BrowserRouter } from "react-router-dom";
import React from "react";

// Mock API
vi.mock("@/api/auth");
vi.mock("react-router-dom", async () => {
  const actual = await vi.importActual("react-router-dom");
  return {
    ...actual,
    useNavigate: () => vi.fn(),
  };
});

// Test wrapper
const createWrapper = () => {
  const queryClient = new QueryClient({
    defaultOptions: {
      queries: { retry: false },
      mutations: { retry: false },
    },
  });
  return ({ children }: { children: React.ReactNode }) => (
    <BrowserRouter>
      <QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
    </BrowserRouter>
  );
};

describe.skip("useAuth", () => {
  beforeEach(() => {
    vi.clearAllMocks();
    localStorage.clear();
  });

  it("should login successfully", async () => {
    const mockResponse = {
      access_token: "mock-token",
      refresh_token: "refresh-token",
      token_type: "bearer",
      user_id: "1",
      email: "test@example.com",
      username: "testuser",
      display_name: "Test User",
    };

    vi.mocked(authApi.login).mockResolvedValue(mockResponse);
    vi.mocked(authApi.getCurrentUser).mockResolvedValue({
      id: "1",
      email: "test@example.com",
      username: "testuser",
      display_name: "Test User",
    });

    const { result } = renderHook(() => useLogin(), {
      wrapper: createWrapper(),
    });

    await waitFor(() => {
      expect(result.current).toBeDefined();
    });

    expect(authApi.login).toBeDefined();
  });

  it("should register successfully", async () => {
    const mockResponse = {
      user_id: "1",
      email: "test@example.com",
      username: "testuser",
      display_name: "Test User",
      message: "注册成功",
    };

    vi.mocked(authApi.register).mockResolvedValue(mockResponse);

    const { result } = renderHook(() => useRegister(), {
      wrapper: createWrapper(),
    });

    await waitFor(() => {
      expect(result.current).toBeDefined();
    });

    expect(authApi.register).toBeDefined();
  });

  it("should logout successfully", async () => {
    localStorage.setItem("access_token", "mock-token");

    vi.mocked(authApi.logout).mockResolvedValue(undefined);

    const { result } = renderHook(() => useLogout(), {
      wrapper: createWrapper(),
    });

    await waitFor(() => {
      expect(result.current).toBeDefined();
    });

    expect(authApi.logout).toBeDefined();
  });
});
