// @vitest-environment jsdom
import { describe, it, expect, vi, beforeEach } from 'vitest';
import '@testing-library/jest-dom/vitest';
import { render, screen, fireEvent, cleanup } from '@testing-library/react';
import React from 'react';

const { mockSearchNotes, mockClearSearch } = vi.hoisted(() => ({
  mockSearchNotes: vi.fn(),
  mockClearSearch: vi.fn()
}));

vi.mock('../../src/renderer/inbox/store.js', () => ({
  useInbox: Object.assign(
    (selector?: (s: { searchQuery: string }) => unknown) => {
      const state = { searchQuery: '' };
      return selector ? selector(state) : state;
    },
    { getState: () => ({ searchNotes: mockSearchNotes, clearSearch: mockClearSearch }) }
  )
}));

import { SearchBox } from '../../src/renderer/inbox/components/SearchBox';

describe('SearchBox', () => {
  beforeEach(() => {
    vi.clearAllMocks();
    cleanup();
    vi.useFakeTimers();
  });

  it('타이핑 → 200ms debounce 후 searchNotes 호출', () => {
    render(<SearchBox />);
    const input = screen.getByRole('searchbox');
    fireEvent.change(input, { target: { value: '회의' } });
    expect(mockSearchNotes).not.toHaveBeenCalled();
    vi.advanceTimersByTime(200);
    expect(mockSearchNotes).toHaveBeenCalledWith('회의');
  });

  it('빈 값 → clearSearch 호출', () => {
    render(<SearchBox />);
    const input = screen.getByRole('searchbox');
    fireEvent.change(input, { target: { value: '' } });
    vi.advanceTimersByTime(200);
    expect(mockClearSearch).toHaveBeenCalled();
  });
});
