import { describe, it, expect, vi, beforeEach } from 'vitest';
import { render, fireEvent, screen, waitFor } from '@testing-library/svelte';

vi.mock('$lib/auth/store.svelte', () => ({
  forgotPassword: vi.fn(),
}));

import { forgotPassword } from '$lib/auth/store.svelte';
import ForgotPasswordPage from './+page.svelte';

describe('/forgot-password page', () => {
  beforeEach(() => vi.clearAllMocks());

  it('shows success message after submit regardless of whether email exists', async () => {
    (forgotPassword as unknown as ReturnType<typeof vi.fn>).mockResolvedValue(undefined);
    render(ForgotPasswordPage);
    await fireEvent.input(screen.getByLabelText(/Email/i), { target: { value: 'test@example.com' } });
    await fireEvent.click(screen.getByRole('button', { name: /Send reset link/i }));
    await waitFor(() => expect(screen.getByText(/If your email is on file/i)).toBeInTheDocument());
    expect(forgotPassword).toHaveBeenCalledWith('test@example.com');
  });

  it('still shows success message when the API call rejects (no enumeration)', async () => {
    (forgotPassword as unknown as ReturnType<typeof vi.fn>).mockRejectedValue({ code: 'server_error' });
    render(ForgotPasswordPage);
    await fireEvent.input(screen.getByLabelText(/Email/i), { target: { value: 'test@example.com' } });
    await fireEvent.click(screen.getByRole('button', { name: /Send reset link/i }));
    await waitFor(() => expect(screen.getByText(/If your email is on file/i)).toBeInTheDocument());
  });
});
