import { describe, it, expect, vi } from 'vitest';
import { renderHook } from '@testing-library/react';
import { useKeyboardShortcuts } from '../use-keyboard-shortcuts';

describe('useKeyboardShortcuts', () => {
  it('should call handler when keyboard shortcut is pressed', () => {
    const handler = vi.fn();
    const shortcuts = [
      {
        key: 's',
        meta: true,
        handler,
        description: 'Save',
      },
    ];

    renderHook(() => useKeyboardShortcuts(shortcuts));

    // Simulate Cmd+S
    const event = new KeyboardEvent('keydown', {
      key: 's',
      metaKey: true,
      bubbles: true,
    });

    window.dispatchEvent(event);

    expect(handler).toHaveBeenCalledTimes(1);
  });

  it('should not call handler if modifiers do not match', () => {
    const handler = vi.fn();
    const shortcuts = [
      {
        key: 's',
        meta: true,
        ctrl: false,
        handler,
      },
    ];

    renderHook(() => useKeyboardShortcuts(shortcuts));

    // Simulate just 's' without meta
    const event = new KeyboardEvent('keydown', {
      key: 's',
      bubbles: true,
    });

    window.dispatchEvent(event);

    expect(handler).not.toHaveBeenCalled();
  });

  it('should handle multiple shortcuts', () => {
    const handler1 = vi.fn();
    const handler2 = vi.fn();
    const shortcuts = [
      { key: 's', meta: true, handler: handler1 },
      { key: 'p', meta: true, handler: handler2 },
    ];

    renderHook(() => useKeyboardShortcuts(shortcuts));

    const event1 = new KeyboardEvent('keydown', {
      key: 's',
      metaKey: true,
      bubbles: true,
    });
    window.dispatchEvent(event1);

    const event2 = new KeyboardEvent('keydown', {
      key: 'p',
      metaKey: true,
      bubbles: true,
    });
    window.dispatchEvent(event2);

    expect(handler1).toHaveBeenCalledTimes(1);
    expect(handler2).toHaveBeenCalledTimes(1);
  });
});
