import { describe, it, expect, vi, beforeEach } from "vitest";
import { render, screen } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { LoginButton } from "./LoginButton";

// Mock window.location
const mockLocation = {
  href: "",
  assign: vi.fn(),
};
Object.defineProperty(window, "location", {
  value: mockLocation,
  writable: true,
});

describe("LoginButton", (): void => {
  beforeEach((): void => {
    mockLocation.href = "";
    mockLocation.assign.mockClear();
  });

  it("should render sign in button", (): void => {
    render(<LoginButton />);
    const button = screen.getByRole("button", { name: /sign in/i });
    expect(button).toBeInTheDocument();
  });

  it("should redirect to OIDC endpoint on click", async (): Promise<void> => {
    const user = userEvent.setup();
    render(<LoginButton />);

    const button = screen.getByRole("button", { name: /sign in/i });
    await user.click(button);

    expect(mockLocation.assign).toHaveBeenCalledWith("http://localhost:3001/auth/signin/authentik");
  });

  it("should have proper styling", (): void => {
    render(<LoginButton />);
    const button = screen.getByRole("button", { name: /sign in/i });
    expect(button).toHaveClass("w-full");
  });
});
