import { describe, it, expect, beforeEach } from 'vitest';
import { render, screen, fireEvent } from '@testing-library/react';
import { Login } from '../src/components/Login';
import { getToken, clearToken } from '../src/api';

describe('Login', () => {
  beforeEach(() => {
    clearToken();
  });

  it('stores the pasted token and calls onLogin', () => {
    let logged = false;
    render(<Login onLogin={() => { logged = true; }} />);

    const input = screen.getByPlaceholderText(/mcpctl_pat_/i) as HTMLInputElement;
    fireEvent.change(input, { target: { value: 'mcpctl_pat_test_abc' } });
    fireEvent.click(screen.getByText(/Continue/));

    expect(getToken()).toBe('mcpctl_pat_test_abc');
    expect(logged).toBe(true);
  });

  it('does nothing on empty submit', () => {
    let logged = false;
    render(<Login onLogin={() => { logged = true; }} />);
    fireEvent.click(screen.getByText(/Continue/));
    expect(getToken()).toBeNull();
    expect(logged).toBe(false);
  });

  it('toggles the help panel', () => {
    render(<Login onLogin={() => {}} />);
    expect(screen.queryByText(/mcpctl auth login/)).toBeNull();
    fireEvent.click(screen.getByText(/Where do I get a token/));
    expect(screen.getByText(/mcpctl auth login/)).toBeInTheDocument();
  });
});
