import { afterEach, describe, expect, test, vi } from 'vitest';
import { render, screen, fireEvent } from '@testing-library/svelte';
import HorizontalScrollRow from './HorizontalScrollRow.svelte';

afterEach(() => vi.clearAllMocks());

describe('HorizontalScrollRow', () => {
  test('renders left and right scroll buttons', () => {
    render(HorizontalScrollRow, { props: { rows: [['a', 'b', 'c']] } });
    expect(screen.getByRole('button', { name: /scroll left/i })).toBeInTheDocument();
    expect(screen.getByRole('button', { name: /scroll right/i })).toBeInTheDocument();
  });

  test('left button starts disabled (scrollLeft is 0)', () => {
    render(HorizontalScrollRow, { props: { rows: [['a']] } });
    expect(screen.getByRole('button', { name: /scroll left/i })).toBeDisabled();
  });

  test('clicking right button calls scrollBy on the container', async () => {
    const { container } = render(HorizontalScrollRow, { props: { rows: [['a', 'b']] } });
    const scroller = container.querySelector('[data-testid="scroll-container"]') as HTMLElement;
    const spy = vi.fn();
    scroller.scrollBy = spy as unknown as HTMLElement['scrollBy'];
    Object.defineProperty(scroller, 'clientWidth', { value: 800, configurable: true });
    Object.defineProperty(scroller, 'scrollWidth', { value: 2000, configurable: true });
    Object.defineProperty(scroller, 'scrollLeft', { value: 0, configurable: true, writable: true });

    await fireEvent.click(screen.getByRole('button', { name: /scroll right/i }));
    expect(spy).toHaveBeenCalledWith({ left: expect.any(Number), behavior: 'smooth' });
    expect(spy.mock.calls[0][0].left).toBeGreaterThan(0);
  });

  test('renders title as a heading when prop is provided', () => {
    render(HorizontalScrollRow, { props: { rows: [['a']], title: 'My section' } });
    expect(screen.getByRole('heading', { name: 'My section' })).toBeInTheDocument();
  });

  test('omits heading when title prop is absent', () => {
    render(HorizontalScrollRow, { props: { rows: [['a']] } });
    expect(screen.queryByRole('heading')).not.toBeInTheDocument();
  });
});
