import { fireEvent, render, screen } from '@testing-library/react';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { resetStore } from '../test/storeTestUtils';
import { useStore } from '../store/useStore';
import { Sidebar } from './Sidebar';

vi.mock('./ModelStatusBadge', () => ({
  ModelStatusBadge: () => <div>模型状态</div>,
}));

describe('Sidebar', () => {
  beforeEach(() => {
    resetStore();
  });

  it('shows admin user management only for admin users', () => {
    const setActiveModule = vi.fn();
    useStore.setState({ currentUser: { id: 1, username: 'admin', role: 'admin' } });

    render(<Sidebar activeModule="dashboard" setActiveModule={setActiveModule} />);

    fireEvent.click(screen.getByTitle('用户管理'));
    expect(setActiveModule).toHaveBeenCalledWith('admin');
  });

  it('hides admin user management for non-admin users', () => {
    useStore.setState({ currentUser: { id: 2, username: 'doctor', role: 'annotator' } });

    render(<Sidebar activeModule="dashboard" setActiveModule={vi.fn()} />);

    expect(screen.queryByTitle('用户管理')).not.toBeInTheDocument();
  });

  it('uses an explicit AI-styled icon for AI segmentation', () => {
    useStore.setState({ currentUser: { id: 2, username: 'doctor', role: 'annotator' } });

    render(<Sidebar activeModule="dashboard" setActiveModule={vi.fn()} />);

    expect(screen.getByTitle('AI智能分割').querySelector('[data-testid="ai-segmentation-icon"]')).toBeInTheDocument();
  });
});
