import { test } from 'node:test' import assert from 'node:assert/strict' import { longDate, shortDate, dateTime, monthTile, ago, categoryLabel } from '../src/lib/format.js' // Unit-test the shared date/label formatters. Date strings are given with an // explicit local time (no trailing Z) so getMonth/getDate read the same value in // any timezone the test runs in — otherwise a date-only UTC string could shift a // day. The point is to lock the human-facing formats the whole site renders. test('longDate renders "Month D, YYYY"', () => { assert.equal(longDate('2026-06-24T12:00:00'), 'June 24, 2026') }) test('shortDate renders "Mon D"', () => { assert.equal(shortDate('2026-06-24T12:00:00'), 'Jun 24') }) test('dateTime renders "Mon D HH:MM" with zero-padded time', () => { assert.equal(dateTime('2026-06-24T08:05:00'), 'Jun 24 08:05') }) test('monthTile returns the uppercased 3-letter month and 2-digit year', () => { assert.deepEqual(monthTile('2026-06-24T12:00:00'), { mon: 'JUN', num: "'26" }) }) test('every formatter returns an empty/placeholder value for a missing or invalid date', () => { for (const bad of [null, undefined, '', 'not-a-date']) { assert.equal(longDate(bad), '') assert.equal(shortDate(bad), '') assert.equal(dateTime(bad), '') assert.equal(ago(bad), '') assert.deepEqual(monthTile(bad), { mon: '—', num: '' }) } }) // ── ago(): relative-time buckets ──────────────────────────────────────── test('ago picks the right unit as the gap widens', () => { const now = Date.now() assert.equal(ago(new Date(now - 5 * 1000)), '5s ago') assert.equal(ago(new Date(now - 5 * 60 * 1000)), '5m ago') assert.equal(ago(new Date(now - 3 * 60 * 60 * 1000)), '3h ago') assert.equal(ago(new Date(now - 2 * 24 * 60 * 60 * 1000)), '2d ago') }) test('ago floors to at least 1s (never "0s ago" or a negative)', () => { assert.equal(ago(new Date(Date.now())), '1s ago') assert.equal(ago(new Date(Date.now() + 5000)), '1s ago') // a slightly-future timestamp }) // ── categoryLabel(): known map + passthrough ──────────────────────────── test('categoryLabel maps known db categories and passes unknown ones through', () => { assert.equal(categoryLabel('five_on_friday'), 'Five on Friday') assert.equal(categoryLabel('newsletter'), 'Newsletter') assert.equal(categoryLabel('mystery_category'), 'mystery_category') // unknown → itself })