import { DASHBOARD_MODULES, KNOWN_COMMAND_NAMES } from '@nexumi/shared';
import { describe, expect, it } from 'vitest';
import {
  SETTINGS_SEARCH_FIELDS,
  buildGuildSearchHref,
  buildGuildSearchIndex
} from './dashboard-search-index';

describe('dashboard-search-index', () => {
  it('includes core pages, all modules, all commands, and settings fields', () => {
    const index = buildGuildSearchIndex();
    const pages = index.filter((entry) => entry.kind === 'page');
    const commands = index.filter((entry) => entry.kind === 'command');
    const settings = index.filter((entry) => entry.kind === 'setting');

    expect(pages.length).toBe(5 + DASHBOARD_MODULES.length);
    expect(commands.length).toBe(KNOWN_COMMAND_NAMES.length);
    expect(settings.length).toBe(SETTINGS_SEARCH_FIELDS.length);
    expect(settings.length).toBeGreaterThan(40);
  });

  it('builds correct hrefs for pages, commands, and settings', () => {
    const guildId = '123';
    expect(buildGuildSearchHref(guildId, { id: 'p', kind: 'page', href: '' })).toBe('/dashboard/123');
    expect(buildGuildSearchHref(guildId, { id: 'p', kind: 'page', href: 'moderation' })).toBe(
      '/dashboard/123/moderation'
    );
    expect(
      buildGuildSearchHref(guildId, {
        id: 'c',
        kind: 'command',
        href: 'commands?q=ban'
      })
    ).toBe('/dashboard/123/commands?q=ban');
    expect(
      buildGuildSearchHref(guildId, {
        id: 's',
        kind: 'setting',
        href: 'logging',
        hash: 'field-logging-retention'
      })
    ).toBe('/dashboard/123/logging#field-logging-retention');
  });

  it('assigns unique ids and hashes for settings entries', () => {
    const ids = new Set(SETTINGS_SEARCH_FIELDS.map((entry) => entry.id));
    expect(ids.size).toBe(SETTINGS_SEARCH_FIELDS.length);
    for (const entry of SETTINGS_SEARCH_FIELDS) {
      expect(entry.hash).toMatch(/^field-/);
      expect(entry.href.length).toBeGreaterThan(0);
    }
  });
});
