// Feature: snowui-dashboard-redesign, Property 2: Active navigation item matches pathname
// **Validates: Requirements 1.5**

import { describe, it, expect } from 'vitest';
import * as fc from 'fast-check';
import { navItems, type NavItem } from '@/lib/constants/navigation';

/**
 * Active item determination logic: finds the nav item whose href matches the pathname.
 * This is the same logic used in the Sidebar component.
 */
function getActiveNavItem(pathname: string, items: readonly NavItem[]): NavItem | undefined {
  return items.find((item) => item.href === pathname);
}

describe('Property 2: Active navigation item matches pathname', () => {
  it('for any pathname matching a nav item href, the active item logic returns that item', () => {
    fc.assert(
      fc.property(
        fc.constantFrom(...navItems),
        (navItem) => {
          const active = getActiveNavItem(navItem.href, navItems);
          expect(active).toBeDefined();
          expect(active!.href).toBe(navItem.href);
          expect(active!.label).toBe(navItem.label);
        },
      ),
      { numRuns: 100 },
    );
  });

  it('for non-matching pathnames, no item is active', () => {
    // Generate random pathnames that don't match any nav item href
    const existingHrefs = new Set(navItems.map((item) => item.href));

    fc.assert(
      fc.property(
        fc.string({ minLength: 1, maxLength: 30 }).map((s) => `/${s.replace(/\//g, '-')}-nonexistent`),
        (pathname) => {
          // Skip if by chance it matches an existing href
          fc.pre(!existingHrefs.has(pathname));

          const active = getActiveNavItem(pathname, navItems);
          expect(active).toBeUndefined();
        },
      ),
      { numRuns: 100 },
    );
  });

  it('active item is unique — only one item matches any given pathname', () => {
    fc.assert(
      fc.property(
        fc.constantFrom(...navItems),
        (navItem) => {
          const matches = navItems.filter((item) => item.href === navItem.href);
          expect(matches).toHaveLength(1);
        },
      ),
      { numRuns: 100 },
    );
  });
});
