import { describe, expect, it } from 'vitest';

import { findSdkPage, renderSdkTarget } from '../src/sdk/findSdkPage.js';
import type { SdkReference } from '../src/sdk/types.js';

// Pages without target metadata exercise the slug/title heuristic fallback.
const reference: SdkReference = {
  pages: [
    { slug: 'classes/Client', title: 'Client', content: 'class page' },
    { slug: 'sdk/interfaces/Options', title: 'Options', content: 'interface page' },
  ],
  groups: [],
};

describe('findSdkPage heuristic fallback', () => {
  it('matches by kind directory in the slug', () => {
    expect(findSdkPage(reference, { kind: 'class', name: 'Client' })?.slug).toBe('classes/Client');
    expect(findSdkPage(reference, { kind: 'interface', name: 'Options' })?.slug).toBe(
      'sdk/interfaces/Options'
    );
  });

  it('does not match a same-named symbol of a different kind', () => {
    expect(findSdkPage(reference, { kind: 'interface', name: 'Client' })).toBeUndefined();
    expect(() => renderSdkTarget(reference, { kind: 'interface', name: 'Client' })).toThrow(
      'SDK symbol not found: interface Client'
    );
  });

  it('prefers exact target metadata over the heuristic', () => {
    const withTargets: SdkReference = {
      pages: [
        {
          slug: 'other/location',
          title: 'Client',
          content: 'tagged page',
          target: { kind: 'class', name: 'Client' },
        },
        ...reference.pages,
      ],
      groups: [],
    };
    expect(findSdkPage(withTargets, { kind: 'class', name: 'Client' })?.slug).toBe(
      'other/location'
    );
  });
});
