import { render, screen, cleanup } from "@testing-library/react";
import { expect, test, vi, beforeEach, afterEach } from "vitest";
import LoginPage from "./page";

const mockPush = vi.fn();
vi.mock("next/navigation", () => ({
  useRouter: () => ({ push: mockPush }),
}));

vi.mock("../auth-context", () => ({
  useAuth: () => ({ login: vi.fn() }),
}));

beforeEach(() => vi.clearAllMocks());
afterEach(() => cleanup());

test("renders login form with title", () => {
  render(<LoginPage />);
  expect(screen.getByText("Welcome back")).toBeDefined();
});

test("renders email and password inputs", () => {
  render(<LoginPage />);
  expect(screen.getByLabelText("Email")).toBeDefined();
  expect(screen.getByLabelText("Password")).toBeDefined();
});

test("renders sign in button", () => {
  render(<LoginPage />);
  expect(screen.getByRole("button", { name: "Sign in" })).toBeDefined();
});

test("renders link to signup", () => {
  render(<LoginPage />);
  expect(screen.getByText("Sign up")).toBeDefined();
});
