import { test } from 'node:test';
import assert from 'node:assert/strict';
import { testBadWords, BAD_WORDS_REGEX } from './bad-words';

test('testBadWords returns false for clean text', () => {
  assert.equal(testBadWords('hello world'), false);
  assert.equal(testBadWords('this is clean text'), false);
  assert.equal(testBadWords(''), false);
});

test('testBadWords matches custom additions', () => {
  // 'torture' and 't0rture' are appended in additionalBadWords
  assert.equal(testBadWords('torture'), true);
  assert.equal(testBadWords('t0rture'), true);
});

test('testBadWords is case-insensitive', () => {
  assert.equal(testBadWords('Torture'), true);
  assert.equal(testBadWords('TORTURE'), true);
  assert.equal(testBadWords('DamN'), true);
});

test('testBadWords matches words from the base list', () => {
  assert.equal(testBadWords('damn'), true);
  assert.equal(testBadWords('hell'), true);
  assert.equal(testBadWords('crap'), true);
});

test('testBadWords respects word boundaries (no match inside a larger word)', () => {
  // alphanumeric extension on either side prevents a match
  assert.equal(testBadWords('tortured'), false);
  assert.equal(testBadWords('tortures'), false);
  assert.equal(testBadWords('damning'), false);
  assert.equal(testBadWords('hello'), false);
  assert.equal(testBadWords('shell'), false);
  assert.equal(testBadWords('crappy'), false);
});

test('testBadWords does not match a bad word as a substring of an unrelated word', () => {
  assert.equal(testBadWords('class'), false);
  assert.equal(testBadWords('pass'), false);
  assert.equal(testBadWords('grass'), false);
});

test('underscore acts as a word boundary', () => {
  // surrounding underscores still allow a match, since `_` is a boundary char
  assert.equal(testBadWords('word_torture_word'), true);
  // but an alphanumeric char glued directly to the word still blocks it
  assert.equal(testBadWords('tor_ture'), false);
});

test('testBadWords matches a bad word embedded in a sentence with spaces', () => {
  assert.equal(testBadWords('what the hell is this'), true);
  assert.equal(testBadWords('I think this is fine'), false);
});

test('BAD_WORDS_REGEX is a global regex; reuse it via String.match', () => {
  // testBadWords lowercases the input before matching
  assert.ok('torture'.toLowerCase().match(BAD_WORDS_REGEX));
  assert.equal('innocent'.toLowerCase().match(BAD_WORDS_REGEX), null);
});
