import { describe, it, expect } from 'vitest';
import { briefOverview, dedupeByName } from './overview';

describe('briefOverview', () => {
  it('leaves short text untouched (5e feature names)', () => {
    expect(briefOverview('Rage')).toBe('Rage');
    expect(briefOverview('Fighting Style')).toBe('Fighting Style');
  });

  it('keeps the first clean sentence of a long run-on, no mid-word cut', () => {
    const fighter = 'Fighting for honor, greed, loyalty, or simply the thrill of battle, you are an undisputed master of weaponry and combat techniques. You combine your actions through clever combinations…';
    expect(briefOverview(fighter)).toBe('Fighting for honor, greed, loyalty, or simply the thrill of battle, you are an undisputed master of weaponry and combat techniques.');
  });

  it('strips the Foundry "Name Source … pg. N" citation prefix, then takes the first sentence', () => {
    const bg = ' Acolyte Source Core Rulebook pg. 60 You spent your early days in a religious monastery or cloister. You may have traveled out into the world to spread the message of your religion or because you cast away the teachings of your faith.';
    expect(briefOverview(bg)).toBe('You spent your early days in a religious monastery or cloister.');
  });

  it('drops a stray trailing ellipsis from a pre-truncated summary', () => {
    expect(briefOverview('Dwarves are a stoic and stern people…')).toBe('Dwarves are a stoic and stern people');
  });

  it('falls back to a word-boundary trim when the first sentence is very long', () => {
    const long = `${'word '.repeat(60)}end.`;
    const out = briefOverview(long);
    expect(out.endsWith('…')).toBe(true);
    expect(out.length).toBeLessThanOrEqual(141);
    expect(out).not.toMatch(/wor…$/); // never cuts mid-word
  });
});

describe('dedupeByName', () => {
  it('keeps the first of each name, case-insensitively', () => {
    const out = dedupeByName([{ name: 'Human' }, { name: 'Elf' }, { name: 'human' }, { name: 'Elf' }]);
    expect(out.map((x) => x.name)).toEqual(['Human', 'Elf']);
  });
});
