← back to Govarbitrage

apps/mobile/tests/format.test.mjs

100 lines

/**
 * Regression tests for lib/format.ts — the guarded financial/score formatters
 * hardened across TK-10279 Cycles 1-3. Zero-dependency: runs on Node's built-in
 * test runner with TS type-stripping, no jest/babel needed (format.ts is a pure
 * module with no imports).
 *
 * Run: npm run test:unit   (node --test --experimental-strip-types tests/*.test.mjs)
 */
import { test } from "node:test";
import assert from "node:assert/strict";
import {
  fmtUSD, fmtPct, fmtScore,
  closingCountdown, isClosingSoon, fmtDateTime,
  conditionLabel, sourceLabel, dropShipLabel,
} from "../lib/format.ts";

const inMs = (ms) => new Date(Date.now() + ms).toISOString();
const HOUR = 3_600_000;

test("fmtPct clamps implausible + guards null/non-finite (Cycle 1 fix)", () => {
  assert.equal(fmtPct(23002764), "—", "the 2.3-billion-% backend value renders as a dash");
  assert.equal(fmtPct(6.83), "683.0%", "legit aggressive return survives");
  assert.equal(fmtPct(0.42), "42.0%");
  assert.equal(fmtPct(-0.15), "-15.0%");
  assert.equal(fmtPct(null), "—");
  assert.equal(fmtPct(undefined), "—");
  assert.equal(fmtPct(NaN), "—");
  assert.equal(fmtPct(Infinity), "—");
  assert.equal(fmtPct(1000), "100000.0%", "ceiling is exclusive: exactly 1000 ratio still renders");
  assert.equal(fmtPct(1000.01), "—", "just OVER the ceiling (>1000 ratio) → dash");
  assert.equal(fmtPct(999), "99900.0%", "under the ceiling renders");
});

test("fmtPct decimals option (Cycle 2 probabilityOfSale fix)", () => {
  assert.equal(fmtPct(0.85, { decimals: 0 }), "85%");
  assert.equal(fmtPct(0.333, { decimals: 0 }), "33%");
  assert.equal(fmtPct(1, { decimals: 0 }), "100%");
});

test("fmtUSD guards non-finite (Cycle 1 fix)", () => {
  assert.equal(fmtUSD(1234), "$1,234");
  assert.equal(fmtUSD(1234.5, 2), "$1,234.50");
  assert.equal(fmtUSD(null), "—");
  assert.equal(fmtUSD(undefined), "—");
  assert.equal(fmtUSD(NaN), "—");
  assert.equal(fmtUSD(Infinity), "—");
  assert.equal(fmtUSD(0), "$0");
  assert.equal(fmtUSD(-500), "-$500");
});

test("fmtScore rounds + guards non-finite (Cycle 3 fix)", () => {
  assert.equal(fmtScore(87.6), "88");
  assert.equal(fmtScore(40), "40");
  assert.equal(fmtScore(null), "—");
  assert.equal(fmtScore(NaN), "—");
  assert.equal(fmtScore(Infinity), "—");
});

test("fmtUSD decimals + negative edges", () => {
  assert.equal(fmtUSD(1234.5, 2), "$1,234.50");
  assert.equal(fmtUSD(1234.5), "$1,235", "default 0 decimals rounds");
  assert.equal(fmtUSD(-1234.5, 2), "-$1,234.50");
});

test("closingCountdown — auction-urgency logic (Cody Cycle-7 gap)", () => {
  assert.equal(closingCountdown(null), "—");
  assert.equal(closingCountdown(undefined), "—");
  assert.equal(closingCountdown(inMs(-HOUR)), "CLOSED", "past close");
  assert.equal(closingCountdown(inMs(0)), "CLOSED", "exactly now (ms<=0)");
  assert.match(closingCountdown(inMs(50 * HOUR)), /^\d+d \d+h$/, "multi-day → Nd Nh");
  assert.match(closingCountdown(inMs(3 * HOUR + 5 * 60_000)), /^\d+h \d+m$/, "hours → Nh Nm");
  assert.match(closingCountdown(inMs(30 * 60_000)), /^\d+m$/, "under an hour → Nm");
});

test("isClosingSoon — 24h boundary (Cody Cycle-7 gap)", () => {
  assert.equal(isClosingSoon(null), false);
  assert.equal(isClosingSoon(inMs(2 * HOUR)), true, "within 24h");
  assert.equal(isClosingSoon(inMs(48 * HOUR)), false, "beyond 24h");
  assert.equal(isClosingSoon(inMs(-HOUR)), false, "already closed is not 'soon'");
});

test("fmtDateTime — admin timestamp (Steve's hard rule)", () => {
  assert.equal(fmtDateTime(null), "—");
  assert.equal(fmtDateTime(undefined), "—");
  const s = fmtDateTime("2026-05-20T15:17:00Z");
  assert.equal(typeof s, "string");
  assert.notEqual(s, "—");
  assert.match(s, /2026/, "renders the year (locale-format, so assert the year not exact string)");
});

test("label lookups fall back to the raw code for unknown values", () => {
  assert.equal(conditionLabel("NEW"), "New");
  assert.equal(conditionLabel(null), "Unknown");
  assert.equal(conditionLabel("MYSTERY"), "MYSTERY", "unknown code passes through");
  assert.equal(sourceLabel("GOVDEALS"), "GovDeals");
  assert.equal(sourceLabel("WEIRD_SRC"), "WEIRD_SRC");
  assert.equal(dropShipLabel("EASY"), "Drop Ship: Easy");
  assert.equal(dropShipLabel("???"), "???");
});