import test from 'node:test';
import assert from 'node:assert/strict';
import { geoTagText, stableGeoJitter } from '../dashboard/inject.mjs';

test('geoTagText matches headlines case-insensitively', () => {
  assert.deepEqual(geoTagText('ukraine reports new air defense activity'), {
    lat: 49,
    lon: 32,
    region: 'Ukraine',
  });

  assert.deepEqual(geoTagText('flooding disrupts são paulo transport'), {
    lat: -23.5,
    lon: -46.6,
    region: 'São Paulo',
  });
});

test('geoTagText prefers longer place names before broad countries', () => {
  assert.deepEqual(geoTagText('New York markets react before wider US session'), {
    lat: 40.7,
    lon: -74,
    region: 'New York',
  });
});

test('geoTagText uses word boundaries to reduce false positives', () => {
  assert.equal(geoTagText('A music festival announces its lineup'), null);
  assert.equal(geoTagText('Officials discuss a new focus for aid'), null);
  assert.deepEqual(geoTagText('US officials discuss a new aid package'), {
    lat: 39,
    lon: -98,
    region: 'US',
  });
});

test('stableGeoJitter is deterministic and bounded', () => {
  const key = 'BBC|lower-case ukraine headline|Sun, 17 May 2026 12:00:00 GMT|https://example.test/a';
  const latA = stableGeoJitter(key, 'lat');
  const latB = stableGeoJitter(key, 'lat');
  const lon = stableGeoJitter(key, 'lon');

  assert.equal(latA, latB);
  assert.notEqual(latA, lon);
  assert.ok(latA >= -1 && latA <= 1);
  assert.ok(lon >= -1 && lon <= 1);
});
