import { fireEvent, render, screen } from '@testing-library/react';
import { describe, expect, it, vi } from 'vitest';
import { ToolsPalette } from './ToolsPalette';

describe('ToolsPalette', () => {
  it('switches tools and dispatches undo/redo actions when available', () => {
    const setActiveTool = vi.fn();
    const onUndo = vi.fn();
    const onRedo = vi.fn();

    render(
      <ToolsPalette
        activeTool="move"
        setActiveTool={setActiveTool}
        onUndo={onUndo}
        onRedo={onRedo}
        canUndo
        canRedo
      />,
    );

    fireEvent.click(screen.getByTitle('创建多边形 (P)'));
    fireEvent.click(screen.getByTitle('调整多边形 (E)'));
    fireEvent.click(screen.getByTitle('正向选点 (SAM)'));
    fireEvent.click(screen.getByTitle('撤销操作 (Ctrl+Z)'));
    fireEvent.click(screen.getByTitle('重做操作 (Ctrl+Shift+Z)'));

    expect(setActiveTool).toHaveBeenNthCalledWith(1, 'create_polygon');
    expect(setActiveTool).toHaveBeenNthCalledWith(2, 'edit_polygon');
    expect(setActiveTool).toHaveBeenNthCalledWith(3, 'point_pos');
    expect(onUndo).toHaveBeenCalled();
    expect(onRedo).toHaveBeenCalled();
  });

  it('switches to SAM trigger and calls the AI navigation hook', () => {
    const setActiveTool = vi.fn();
    const onTriggerAI = vi.fn();

    render(<ToolsPalette activeTool="move" setActiveTool={setActiveTool} onTriggerAI={onTriggerAI} />);
    fireEvent.click(screen.getByTitle('打开 AI 智能分割'));

    expect(setActiveTool).toHaveBeenCalledWith('sam_trigger');
    expect(onTriggerAI).toHaveBeenCalled();
  });
});
