import test from 'node:test'; import assert from 'node:assert/strict'; import { REGIONAL_NEWS_WINDOW_HOURS, googleNewsWindowOperator, normalizeRegionalArticles, recentRegionalArticles, regionalNewsWindowHours, normalizeRegionalPlace, normalizeRegionalWeather, regionalDistanceM, weatherCodeLabel, } from './regionalBrief.js'; test('normalizes a regional place with stable locality fallback', () => { assert.deepEqual(normalizeRegionalPlace({ address: { town: 'Davis', state: 'California', country: 'United States', country_code: 'us', } }), { label: 'Davis, California', locality: 'Davis', region: 'California', country: 'United States', countryCode: 'US', }); assert.equal(normalizeRegionalPlace({ address: {} }), null); }); test('normalizes, deduplicates, and rejects unsafe regional-news rows', () => { const articles = normalizeRegionalArticles({ articles: [ { title: 'Regional update', url: 'https://news.example/one', domain: 'news.example', seendate: '20260722T081500Z' }, { title: 'Regional update', url: 'https://news.example/two' }, { title: 'Unsafe', url: 'javascript:alert(1)' }, { title: 'Second story', url: 'https://other.example/story', seendate: '2026-07-22T07:00:00Z' }, ] }); assert.equal(articles.length, 2); assert.equal(articles[0].publishedAt, '2026-07-22T08:15:00Z'); assert.equal(articles[1].domain, 'other.example'); }); test('normalizes weather values and labels WMO conditions', () => { const weather = normalizeRegionalWeather({ current: { time: '2026-07-22T08:15:00Z', temperature_2m: 21.4, apparent_temperature: 20.8, precipitation: 0, cloud_cover: 42, wind_speed_10m: 18.2, wind_direction_10m: 270, visibility: 18000, weather_code: 2, } }); assert.equal(weather.temperatureC, 21.4); assert.equal(weather.visibilityM, 18000); assert.equal(weatherCodeLabel(weather.weatherCode), 'PARTLY CLOUDY'); assert.equal(normalizeRegionalWeather({ current: {} }), null); }); test('zone-naive Open-Meteo timestamps are pinned to UTC, zoned ones pass through', () => { // Open-Meteo's default payload carries no zone designator; JS would parse it // as host-local time, skewing observedAt by the UTC offset. const naive = normalizeRegionalWeather({ current: { time: '2026-08-17T00:15', temperature_2m: 20 } }); assert.equal(naive.observedAt, '2026-08-17T00:15:00.000Z'); const zoned = normalizeRegionalWeather({ current: { time: '2026-08-17T00:15:00+02:00', temperature_2m: 20 } }); assert.equal(zoned.observedAt, '2026-08-16T22:15:00.000Z'); const invalid = normalizeRegionalWeather({ current: { time: 'not-a-time', temperature_2m: 20 } }); assert.equal(invalid.observedAt, null); }); test('regional distance handles nearby movement and missing positions', () => { const distance = regionalDistanceM( { latitude: 38.5, longitude: -121.7 }, { latitude: 38.6, longitude: -121.7 }, ); assert.ok(distance > 11000 && distance < 11200); assert.equal(regionalDistanceM(null, null), Infinity); }); test('regional news is held to one 24-hour window, newest first, undated dropped', () => { assert.equal(REGIONAL_NEWS_WINDOW_HOURS, 24); assert.equal(googleNewsWindowOperator(), 'when:1d'); assert.equal(googleNewsWindowOperator(6), 'when:6h'); assert.equal(regionalNewsWindowHours('12'), 12); assert.equal(regionalNewsWindowHours(0), 24); assert.equal(regionalNewsWindowHours(undefined), 24); const now = Date.parse('2026-11-04T12:00:00Z'); const at = (hours) => new Date(now - hours * 3_600_000).toISOString(); const rows = [ { title: 'a', publishedAt: at(23) }, { title: 'stale', publishedAt: at(25) }, { title: 'undated', publishedAt: null }, { title: 'garbled', publishedAt: 'not a date' }, { title: 'b', publishedAt: at(1) }, { title: 'future', publishedAt: at(-2) }, { title: 'c', publishedAt: at(5) }, ]; assert.deepEqual( recentRegionalArticles(rows, { now }).map((row) => row.title), ['b', 'c', 'a'], ); assert.deepEqual( recentRegionalArticles(rows, { now, limit: 2 }).map((row) => row.title), ['b', 'c'], ); assert.deepEqual( recentRegionalArticles(rows, { now, windowHours: 4 }).map((row) => row.title), ['b'], ); assert.deepEqual(recentRegionalArticles([], { now }), []); assert.deepEqual(recentRegionalArticles(undefined, { now }), []); });