import { describe, expect, it } from 'vitest';
import { resolveVisualCandidates } from '../../lib/card-visual-match.js';

describe('resolveVisualCandidates', () => {
  const baseCard = {
    id: 1,
    name: 'Lightning Bolt',
    set_name: 'Alpha',
    set_code: 'lea',
    card_number: '161',
    game: 'mtg',
    rarity: 'common',
    image_url: 'https://example.com/bolt.jpg',
    card_type: 'Instant',
    mana_cost: '{R}',
    power: null,
  };

  it('escalates when no candidates remain', () => {
    expect(resolveVisualCandidates([]).type).toBe('escalate');
  });

  it('auto-matches a clear visual winner', () => {
    const result = resolveVisualCandidates([
      { ...baseCard, sim: 0.9 },
      { ...baseCard, id: 2, sim: 0.7 },
    ]);
    expect(result.type).toBe('matched');
    expect(result.card.id).toBe(1);
  });

  it('opens disambiguation when top matches are close', () => {
    const result = resolveVisualCandidates([
      { ...baseCard, sim: 0.8 },
      { ...baseCard, id: 2, set_name: 'Beta', sim: 0.77 },
    ]);
    expect(result.type).toBe('disambiguation');
    expect(result.matches).toHaveLength(2);
  });
});
