import { describe, expect, test, vi } from 'vitest';
import { render, screen } from '@testing-library/svelte';
import { pageUrlModule } from '$test-utils/mocks/appState';

const state = vi.hoisted(() => ({
  pageUrl: new URL('http://localhost/admin')
}));

vi.mock('$app/state', () => pageUrlModule(state));

import AdminTabs from './AdminTabs.svelte';

describe('AdminTabs', () => {
  test('Overview tab is current when on /admin', () => {
    state.pageUrl = new URL('http://localhost/admin');
    render(AdminTabs);
    const overview = screen.getByRole('link', { name: /overview/i });
    expect(overview).toHaveAttribute('aria-current', 'page');
  });

  test('Integrations tab is current when on /admin/integrations', () => {
    state.pageUrl = new URL('http://localhost/admin/integrations');
    render(AdminTabs);
    expect(screen.getByRole('link', { name: /integrations/i })).toHaveAttribute(
      'aria-current',
      'page'
    );
    expect(screen.getByRole('link', { name: /overview/i })).not.toHaveAttribute('aria-current');
  });

  test('Requests tab is current when on /admin/requests', () => {
    state.pageUrl = new URL('http://localhost/admin/requests');
    render(AdminTabs);
    expect(screen.getByRole('link', { name: /requests/i })).toHaveAttribute('aria-current', 'page');
  });

  test('Quarantine tab is current when on /admin/quarantine', () => {
    state.pageUrl = new URL('http://localhost/admin/quarantine');
    render(AdminTabs);
    expect(screen.getByRole('link', { name: /quarantine/i })).toHaveAttribute(
      'aria-current',
      'page'
    );
  });

  test('Users tab is current when on /admin/users', () => {
    state.pageUrl = new URL('http://localhost/admin/users');
    render(AdminTabs);
    expect(screen.getByRole('link', { name: /users/i })).toHaveAttribute(
      'aria-current',
      'page'
    );
  });

  test('renders all eight tabs in order', () => {
    state.pageUrl = new URL('http://localhost/admin');
    render(AdminTabs);
    const links = screen.getAllByRole('link');
    expect(links.map((l) => l.textContent?.trim())).toEqual([
      'Overview',
      'Integrations',
      'Requests',
      'Quarantine',
      'Playback errors',
      'Diagnostics',
      'Tuning',
      'Users'
    ]);
  });
});
