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

import {
  DEFAULT_PUNCTUATION_STRIP_SET_EN,
  compareBreaksLexicographically,
  joinTokens,
  matchENKeyword,
  normalizeENKeyword,
  normalizeWhitespace,
  tokenizeEN,
} from '../index';

describe('textWrap core-contract', () => {
  it('normalizeWhitespace(NORMALIZE): 折叠空白 + 去首尾', () => {
    const out = normalizeWhitespace('  a   b \n c  ', 'NORMALIZE');
    expect(out.normalizedText).toBe('a b c');
    expect(out.hadMultiWhitespace).toBe(true);
  });

  it('tokenizeEN: 只按空白分词（极简派，不拆标点）', () => {
    const { normalizedText } = normalizeWhitespace('I am so tired.', 'NORMALIZE');
    const tokens = tokenizeEN(normalizedText);
    expect(tokens.map((t) => t.text)).toEqual(['I', 'am', 'so', 'tired.']);
  });

  it('joinTokens: 默认用单空格重组', () => {
    const { normalizedText } = normalizeWhitespace('I am so tired', 'NORMALIZE');
    const tokens = tokenizeEN(normalizedText);
    expect(joinTokens(tokens, 0, 2)).toBe('I am');
    expect(joinTokens(tokens, 2, 4)).toBe('so tired');
  });

  it('normalizeENKeyword/matchENKeyword: 全词等值匹配，不做 substring', () => {
    const config = { punctuationStripSetEN: DEFAULT_PUNCTUATION_STRIP_SET_EN };

    expect(normalizeENKeyword('BUT,', config)).toBe('but');
    expect(matchENKeyword('but,', 'but', config)).toBe(true);
    expect(matchENKeyword('rebuttal', 'but', config)).toBe(false);
  });

  it('compareBreaksLexicographically: 字典序 + 短数组优先', () => {
    expect(compareBreaksLexicographically([2], [3])).toBeLessThan(0);
    expect(compareBreaksLexicographically([2], [2, 5])).toBeLessThan(0);
    expect(compareBreaksLexicographically([2, 3], [2])).toBeGreaterThan(0);
    expect(compareBreaksLexicographically([2, 3], [2, 3])).toBe(0);
  });
});

