import { describe, it, expect, beforeEach, afterEach } from 'vitest';
import { renderHook, act } from '@testing-library/react';
import { useHashTab } from '@/hooks/useHashTab';

beforeEach(() => {
  window.location.hash = '';
});

afterEach(() => {
  window.location.hash = '';
});

describe('useHashTab', () => {
  it('returns the default id when hash is empty', () => {
    const { result } = renderHook(() => useHashTab('schedule'));
    expect(result.current[0]).toBe('schedule');
  });

  it('reads the hash on mount', () => {
    window.location.hash = 'simulations';
    const { result } = renderHook(() => useHashTab('schedule'));
    expect(result.current[0]).toBe('simulations');
  });

  it('navigate() updates activeId and sets hash via replaceState (no history entry)', () => {
    const { result } = renderHook(() => useHashTab('schedule'));
    act(() => {
      result.current[1]('description');
    });
    expect(result.current[0]).toBe('description');
    expect(window.location.hash).toBe('#description');
  });

  it('falls back to defaultId when hash becomes empty', () => {
    window.location.hash = 'simulations';
    const { result } = renderHook(() => useHashTab('schedule'));
    act(() => {
      window.location.hash = '';
      window.dispatchEvent(new HashChangeEvent('hashchange'));
    });
    expect(result.current[0]).toBe('schedule');
  });
});
