import { render, screen } from '@testing-library/react';
import { describe, expect, it } from 'vitest';
import { OAuthButtons } from '../oauth-buttons';

describe('OAuthButtons', () => {
  it('renders Google button', () => {
    render(<OAuthButtons />);
    expect(screen.getByRole('button', { name: /Google/i })).toBeInTheDocument();
  });

  it('renders Zalo button', () => {
    render(<OAuthButtons />);
    expect(screen.getByRole('button', { name: /Zalo/i })).toBeInTheDocument();
  });

  it('has two OAuth buttons', () => {
    render(<OAuthButtons />);
    const buttons = screen.getAllByRole('button');
    expect(buttons).toHaveLength(2);
  });

  it('buttons are type="button" (not submit)', () => {
    render(<OAuthButtons />);
    const buttons = screen.getAllByRole('button');
    buttons.forEach((btn) => {
      expect(btn).toHaveAttribute('type', 'button');
    });
  });

  it('redirects to Google OAuth URL on click', () => {
    const originalHref = window.location.href;
    delete (window as unknown as Record<string, unknown>)['location'];
    Object.defineProperty(window, 'location', {
      value: { href: originalHref },
      writable: true,
      configurable: true,
    });

    render(<OAuthButtons />);
    screen.getByRole('button', { name: /Google/i }).click();
    expect(window.location.href).toContain('/auth/google');

    // Restore
    Object.defineProperty(window, 'location', {
      value: { href: originalHref },
      writable: true,
      configurable: true,
    });
  });

  it('redirects to Zalo OAuth URL on click', () => {
    const originalHref = window.location.href;
    delete (window as unknown as Record<string, unknown>)['location'];
    Object.defineProperty(window, 'location', {
      value: { href: originalHref },
      writable: true,
      configurable: true,
    });

    render(<OAuthButtons />);
    screen.getByRole('button', { name: /Zalo/i }).click();
    expect(window.location.href).toContain('/auth/zalo');

    Object.defineProperty(window, 'location', {
      value: { href: originalHref },
      writable: true,
      configurable: true,
    });
  });
});
