import { test } from 'node:test';
import assert from 'node:assert/strict';
import { normalize, matchQuery } from './AsyncSearch';

test('normalize lowercases and strips whitespace by default', () => {
  assert.equal(normalize('Hello World'), 'helloworld');
  assert.equal(normalize('  A  B  '), 'ab');
  assert.equal(normalize(''), '');
});

test('normalize strips all whitespace kinds by default', () => {
  assert.equal(normalize('a\tb\nc\r d'), 'abcd');
});

test('normalize caseSensitive keeps original case', () => {
  assert.equal(normalize('Hello World', { caseSensitive: true }), 'HelloWorld');
});

test('normalize ignoreWhitespace=false keeps whitespace', () => {
  assert.equal(normalize('Hello World', { ignoreWhitespace: false }), 'hello world');
});

test('normalize default uses NFKC (compatibility) unicode normalization', () => {
  // U+FB01 (ﬁ ligature) decomposes to "fi" under NFKC but not NFC.
  assert.equal(normalize('ﬁle'), 'file');
  // With normalizeUnicode disabled (NFC), the ligature is preserved.
  assert.equal(normalize('ﬁle', { normalizeUnicode: false }), 'ﬁle');
});

test('normalize does not strip diacritics', () => {
  // normalize only does NFKC/NFC + lowercase; it does not remove accents.
  assert.equal(normalize('Café'), 'café');
});

test('matchQuery default matches by prefix', () => {
  assert.equal(matchQuery('hello world', 'hello'), true);
  assert.equal(matchQuery('hello world', 'world'), false);
  assert.equal(matchQuery('hello', ''), true);
  assert.equal(matchQuery('', 'a'), false);
});

test('matchQuery contain option matches anywhere', () => {
  assert.equal(matchQuery('hello world', 'world', { contain: true }), true);
  assert.equal(matchQuery('hello world', 'lo wo', { contain: true }), true);
  assert.equal(matchQuery('hello world', 'xyz', { contain: true }), false);
  assert.equal(matchQuery('hello', '', { contain: true }), true);
});

test('matchQuery is case sensitive (does not normalize internally)', () => {
  assert.equal(matchQuery('Hello', 'hello'), false);
  assert.equal(matchQuery('Hello', 'Hello'), true);
});
