// REQ-USR-003: UserToolbar 工具栏单测（BR8/BR9/BR13/D7/D10）
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { renderShell } from './renderShell';
import UserToolbar from '../../src/pages/usr/UserList/UserToolbar';

function setup(over?: { exporting?: boolean; loading?: boolean }) {
  const onRefresh = vi.fn();
  const onAdd = vi.fn();
  const onExport = vi.fn();
  renderShell(
    <UserToolbar
      onRefresh={onRefresh}
      onAdd={onAdd}
      onExport={onExport}
      exporting={over?.exporting ?? false}
      loading={over?.loading ?? false}
    />,
    { preloadedAuth: { token: 't', user: { id: 1, sUserName: 'a', sUserType: 'x', sLanguage: '中文' } } },
  );
  return { onRefresh, onAdd, onExport };
}

describe('UserToolbar', () => {
  beforeEach(() => {
    vi.clearAllMocks();
  });

  it('renders 刷新/新增/导出Excel/设置 buttons', () => {
    setup();
    expect(screen.getByTestId('btn-refresh')).toBeInTheDocument();
    expect(screen.getByText('刷新')).toBeInTheDocument();
    expect(screen.getByText('新增')).toBeInTheDocument();
    expect(screen.getByText('导出Excel')).toBeInTheDocument();
    expect(screen.getByTestId('btn-gear')).toBeInTheDocument();
  });

  it('click 刷新 calls onRefresh / click 新增 calls onAdd (BR8/BR13)', async () => {
    const user = userEvent.setup();
    const { onRefresh, onAdd } = setup();
    await user.click(screen.getByTestId('btn-refresh'));
    expect(onRefresh).toHaveBeenCalledTimes(1);
    await user.click(screen.getByTestId('btn-add'));
    expect(onAdd).toHaveBeenCalledTimes(1);
  });

  it('click 导出Excel calls onExport (BR9)', async () => {
    const user = userEvent.setup();
    const { onExport } = setup();
    await user.click(screen.getByTestId('btn-export'));
    expect(onExport).toHaveBeenCalledTimes(1);
  });

  it('exporting disables 导出Excel and shows loading (BR9)', async () => {
    const user = userEvent.setup();
    const { onExport } = setup({ exporting: true });
    const btn = screen.getByTestId('btn-export');
    expect(btn).toBeDisabled();
    await user.click(btn);
    expect(onExport).not.toHaveBeenCalled();
  });

  it('gear setting is placeholder (no callback) (D7)', async () => {
    const user = userEvent.setup();
    const { onRefresh, onAdd, onExport } = setup();
    await user.click(screen.getByTestId('btn-gear'));
    expect(onRefresh).not.toHaveBeenCalled();
    expect(onAdd).not.toHaveBeenCalled();
    expect(onExport).not.toHaveBeenCalled();
  });
});
