import { describe, it, expect } from 'vitest';
import { cardsFromGlossary } from './flashcards';
import type { GlossaryEntry } from './glossary';

describe('cardsFromGlossary', () => {
  it('maps one card per entry and carries provenance', () => {
    const entries: GlossaryEntry[] = [
      {
        term: 'Photosynthesis',
        definition: 'A process used by plants to make food.',
        start: 7,
        segmentId: 'segX',
      },
    ];
    const cards = cardsFromGlossary(entries);
    expect(cards.length).toBe(1);
    expect(cards[0]!.back).toBe('A process used by plants to make food.');
    expect(cards[0]!.start).toBe(7);
    expect(cards[0]!.segmentId).toBe('segX');
  });

  it('uses a "What is X?" front when the term is absent from the definition', () => {
    const entries: GlossaryEntry[] = [
      { term: 'Photosynthesis', definition: 'A process used by plants.', start: 0 },
    ];
    const [card] = cardsFromGlossary(entries);
    expect(card!.front).toBe('What is Photosynthesis?');
  });

  it('makes a cloze front when the term appears in the definition', () => {
    const entries: GlossaryEntry[] = [
      {
        term: 'Recursion',
        definition: 'Recursion is when a function calls itself.',
        start: 0,
      },
    ];
    const [card] = cardsFromGlossary(entries);
    expect(card!.front).toBe('_____ is when a function calls itself.');
    expect(card!.front).not.toContain('Recursion');
  });

  it('returns [] for no entries', () => {
    expect(cardsFromGlossary([])).toEqual([]);
  });
});
