import { describe, it, expect } from 'vitest';
import { Bm25Ranker } from '../bm25.js';

describe('Bm25Ranker', () => {
  it('scores documents by term frequency', () => {
    const ranker = new Bm25Ranker();
    ranker.fit(['the cat sat on the mat', 'the dog chased the cat', 'the bird flew over the mat']);
    const results = ranker.rank('cat mat');
    expect(results.length).toBeGreaterThan(0);
    expect(results[0]!.score).toBeGreaterThan(0);
  });

  it('returns empty for no matches', () => {
    const ranker = new Bm25Ranker();
    ranker.fit(['aaa bbb', 'ccc ddd']);
    const results = ranker.rank('zzz');
    expect(results).toHaveLength(0);
  });

  it('handles single document corpus', () => {
    const ranker = new Bm25Ranker();
    ranker.fit(['only document here']);
    const results = ranker.rank('document');
    expect(results).toHaveLength(1);
  });

  it('ranks relevant docs higher', () => {
    const ranker = new Bm25Ranker();
    ranker.fit([
      'javascript is a programming language',
      'python is also a programming language',
      'the weather is nice today',
    ]);
    const results = ranker.rank('javascript programming');
    expect(results[0]!.index).toBe(0);
  });
});
