SEV-378: add tolerant formatDate/formatDateTime to shared (v0.3.0)

Our Go/Turso layer emits SQLite datetime('now') as "YYYY-MM-DD HH:MM:SS"
(UTC, no zone), which new Date() can't parse → "Invalid Date" in admin.
formatDate/formatDateTime normalize that shape (space→T + Z) and RFC3339,
returning '' for falsy/invalid. Tests cover both formats + empties.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
Bastian de Byl
2026-06-10 16:36:24 -04:00
parent 2b54028495
commit e405985c34
4 changed files with 47 additions and 2 deletions
+19 -1
View File
@@ -2,7 +2,7 @@ import { test } from 'node:test';
import assert from 'node:assert/strict';
import {
formatLabel, formatStatus, formatTimeline, formatRange,
formatBudget, formatBudgetBucket, vehicleName,
formatBudget, formatBudgetBucket, vehicleName, formatDate, formatDateTime,
} from './format.js';
test('formatLabel replaces _ and - and title-cases', () => {
@@ -68,3 +68,21 @@ test('vehicleName handles commissioned and sparse vehicles', () => {
assert.equal(vehicleName({ make: 'BMW' }, 'Lead'), 'BMW');
assert.equal(vehicleName(undefined), 'Project');
});
test('formatDate handles SQLite and RFC3339 timestamps (SEV-378)', () => {
// SQLite "YYYY-MM-DD HH:MM:SS" (UTC)
assert.equal(formatDate('2026-06-10 14:42:27'), 'Jun 10, 2026');
// RFC3339
assert.equal(formatDate('2026-06-10T14:42:27Z'), 'Jun 10, 2026');
// falsy / invalid → ''
assert.equal(formatDate(''), '');
assert.equal(formatDate(null), '');
assert.equal(formatDate('not a date'), '');
});
test('formatDateTime handles both formats + empties (SEV-378)', () => {
assert.match(formatDateTime('2026-06-10 14:42:27'), /Jun 10, 2026/);
assert.match(formatDateTime('2026-06-10T14:42:27Z'), /Jun 10, 2026/);
assert.equal(formatDateTime(''), '');
assert.equal(formatDateTime('garbage'), '');
});