// ── Formatting ──────────────────────────────────────────────────────────── // // Small functions, and the tests are small too — but three of them guard claims // that would otherwise be made by a page that looks fine: an unknown duration // rendered as zero, a timestamp in the wrong unit, and "in 0 seconds". // // Locale-dependent output is asserted loosely on purpose. `Intl` formats to the // RUNNER's locale, and a test pinned to "3 minutes ago" would be a test that // fails on a machine set to French while the page it describes is correct. import test from 'node:test' import assert from 'node:assert/strict' import { ago, clock, count, day, duration, prefab, shortId } from '../src/lib/format.js' const NOW = Date.parse('2026-09-16T12:00:00Z') test('a relative time picks the unit that fits', () => { assert.match(ago(NOW - 3 * 60_000, NOW), /3/) assert.match(ago(NOW - 5 * 3600_000, NOW), /5/) assert.match(ago(NOW - 3 * 86400_000, NOW), /3/) }) test('"just now" rather than "in 0 seconds"', () => { // What `numeric: 'auto'` produces under a minute is not what anybody means, // and a feed row a few seconds old is the commonest row on the page. assert.equal(ago(NOW, NOW), 'just now') assert.equal(ago(NOW - 10_000, NOW), 'just now') }) test('both time shapes this module serves are accepted', () => { // `updatedAt` is an ISO string the model produced; an event's `t` is the // millisecond stamp the plugin put on the frame. A helper that took only one // would be a helper every caller has to remember the type for. assert.equal(ago('2026-09-16T11:57:00.000Z', NOW), ago(NOW - 3 * 60_000, NOW)) }) test('a missing time is "never", not the epoch', () => { assert.equal(ago(null), 'never') assert.equal(ago(undefined), 'never') assert.equal(ago(''), 'never') assert.equal(day(null), 'unknown') }) test('an unknown duration is a dash, and a short one keeps its seconds', () => { // The distinction the plugin makes and this must not lose: `sessionSec` is // ABSENT for a player who was already on when it loaded, so zero and unknown // arrive at the same function and must not render the same way. assert.equal(duration(null), '—') assert.equal(duration(0), '—') assert.equal(duration(40), '40s') assert.equal(duration(90), '2m') assert.equal(duration(3720), '1h 2m') assert.equal(duration(7200), '2h') }) test('a prefab reads as words, without a lookup table', () => { assert.equal(prefab('rifle.ak'), 'rifle ak') assert.equal(prefab('scientistnpc_full_any'), 'scientistnpc full any') assert.equal(prefab(null), '') }) test('a steam id is shortened without pretending to be a name', () => { assert.equal(shortId('76561198000000001'), '…000001') assert.equal(shortId(''), '') }) test('a count that is not a number is zero, never NaN on the page', () => { assert.equal(count(undefined), '0') assert.equal(count(null), '0') }) test("a feed row from another day carries its date, not just a time", () => { // Found by the page walk: with the feed filtered to the previous wipe, three // events from six weeks ago rendered as `02:03 PM` and read as this afternoon. // Today's rows stay bare, because a killfeed of today's fights does not want // the date on every line. // Asserted against `Intl` rather than against a literal: a 12-hour locale puts // letters in a bare time ("05:30 AM"), so "has letters in it" is not the test — // "is exactly the time, and nothing else" is. const time = (at) => new Date(at).toLocaleTimeString(undefined, { hour: '2-digit', minute: '2-digit' }) const todayAt = NOW - 90 * 60_000 assert.equal(clock(todayAt, NOW), time(todayAt)) const olderAt = NOW - 46 * 86400_000 assert.ok(clock(olderAt, NOW).endsWith(time(olderAt))) assert.ok(clock(olderAt, NOW).length > time(olderAt).length, 'an older row carries no date') // Yesterday counts as another day even when it is only a few hours back — the // boundary is the calendar, not a duration, because that is what a reader // means by "what time was that". const lateLastNight = Date.parse('2026-09-15T23:50:00') const earlyToday = Date.parse('2026-09-16T00:20:00') assert.ok(clock(lateLastNight, earlyToday).length > time(lateLastNight).length) })