Files
shared/src/format.test.js
T
Bastian de Byl e405985c34 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>
2026-06-10 16:36:24 -04:00

89 lines
3.4 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { test } from 'node:test';
import assert from 'node:assert/strict';
import {
formatLabel, formatStatus, formatTimeline, formatRange,
formatBudget, formatBudgetBucket, vehicleName, formatDate, formatDateTime,
} from './format.js';
test('formatLabel replaces _ and - and title-cases', () => {
assert.equal(formatLabel('contract_pending'), 'Contract Pending');
assert.equal(formatLabel('in-progress'), 'In Progress');
assert.equal(formatLabel(''), '');
assert.equal(formatLabel(null), '');
});
test('formatLabel spaces letter→digit boundaries (SEV-362)', () => {
assert.equal(formatLabel('level2'), 'Level 2');
assert.equal(formatLabel('level1'), 'Level 1');
});
test('formatLabel applies acronym casing (SEV-362)', () => {
assert.equal(formatLabel('dcfc'), 'DCFC');
assert.equal(formatLabel('dc_fast'), 'DC Fast');
assert.equal(formatLabel('keep_original_ac'), 'Keep Original A/C');
});
test('formatLabel leaves bare numeric ranges intact (SEV-362)', () => {
assert.equal(formatLabel('100-150'), '100-150');
assert.equal(formatLabel('200+'), '200+');
});
test('formatStatus is formatLabel', () => {
assert.equal(formatStatus('matched'), 'Matched');
});
test('formatTimeline maps known slugs, falls back otherwise', () => {
assert.equal(formatTimeline('6_to_12_months'), '612 months');
assert.equal(formatTimeline('flexible'), 'Flexible');
assert.equal(formatTimeline('something_else'), 'Something Else');
});
test('formatRange formats wizard range slugs (SEV-362)', () => {
assert.equal(formatRange('150-200'), '150200 mi');
assert.equal(formatRange('100-150'), '100150 mi');
assert.equal(formatRange('200+'), '200+ mi');
assert.equal(formatRange('200-plus'), '200+ mi');
assert.equal(formatRange('250'), '250 mi');
assert.equal(formatRange(''), '');
assert.equal(formatRange(null), '');
});
test('formatBudget formats numeric dollar ranges (SEV-362)', () => {
assert.equal(formatBudget(30000, 60000), '$30,000 $60,000');
assert.equal(formatBudget(30000, 0), '$30,000+');
assert.equal(formatBudget(0, 60000), 'Up to $60,000');
assert.equal(formatBudget(0, 0), 'Flexible');
assert.equal(formatBudget(undefined, undefined), 'Flexible');
});
test('formatBudgetBucket keeps the wizard slug labels', () => {
assert.equal(formatBudgetBucket('25k-50k'), '$25,000 $50,000');
assert.equal(formatBudgetBucket(''), '');
});
test('vehicleName handles commissioned and sparse vehicles', () => {
assert.equal(vehicleName({ year: 1980, make: 'BMW', model: 'E30' }), '1980 BMW E30');
assert.equal(vehicleName({ commissioned: true }), 'Commissioned build');
assert.equal(vehicleName({}), 'Project');
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'), '');
});