import { test } from 'node:test';
import assert from 'node:assert/strict';
import { sanitizeForRegex, HTTP_URL_PATTERN, URL_REG, EMAIL_REGEX, JUMBO_EMOJI_REG } from './regex';

test('sanitizeForRegex escapes regex metacharacters', () => {
  assert.equal(sanitizeForRegex('a.b'), 'a\\.b');
  assert.equal(sanitizeForRegex('a+b'), 'a\\+b');
  assert.equal(sanitizeForRegex('(grp)'), '\\(grp\\)');
  // hyphen is escaped as a hex code (safe inside character classes)
  assert.equal(sanitizeForRegex('a-b'), 'a\\x2db');
  // round-trip: the escaped form matches the literal, not as a metacharacter
  assert.equal(new RegExp(`^${sanitizeForRegex('a.b')}$`).test('a.b'), true);
  assert.equal(new RegExp(`^${sanitizeForRegex('a.b')}$`).test('axb'), false);
});

test('URL pattern matches http(s) urls', () => {
  const urlRe = new RegExp(HTTP_URL_PATTERN); // fresh, non-global for stateless .test
  assert.equal(urlRe.test('https://example.com'), true);
  assert.equal(urlRe.test('http://www.foo.com/path?q=1'), true);
  assert.equal(urlRe.test('just some text'), false);
  assert.ok(URL_REG instanceof RegExp);
  assert.equal(URL_REG.global, true); // used with matchAll elsewhere
});

test('EMAIL_REGEX', () => {
  assert.equal(EMAIL_REGEX.test('a@b.com'), true);
  assert.equal(EMAIL_REGEX.test('john.doe@example.co'), true);
  assert.equal(EMAIL_REGEX.test('nope'), false);
  assert.equal(EMAIL_REGEX.test('a@b'), false); // no TLD
  assert.equal(EMAIL_REGEX.test('@b.com'), false); // empty local part
});

test('JUMBO_EMOJI_REG matches 1-10 emoji / custom emoji only', () => {
  assert.equal(JUMBO_EMOJI_REG.test('😀'), true);
  assert.equal(JUMBO_EMOJI_REG.test('😀😀😀'), true);
  assert.equal(JUMBO_EMOJI_REG.test(':custom_emoji:'), true);
  assert.equal(JUMBO_EMOJI_REG.test('hello'), false);
  assert.equal(JUMBO_EMOJI_REG.test('hi 😀'), false); // leading text disqualifies
});
