steam-backlog-enforcer/web/src/format.test.ts
Krzysztof kuhy Rudnicki 0d93539944
Some checks are pending
dependency freshness / dependencies on newest stable (push) Waiting to run
md-naming / markdown naming convention (push) Waiting to run
pre-commit / pre-commit (${{ matrix.group }}) (mypy, mypy, ) (push) Waiting to run
pre-commit / pre-commit (${{ matrix.group }}) (pylint, pylint, ) (push) Waiting to run
pre-commit / pre-commit (${{ matrix.group }}) (rest, , mypy,pylint) (push) Waiting to run
Tests / test (push) Waiting to run
Tests / web-coverage (push) Waiting to run
repo-contract / repo contract (push) Waiting to run
total block: two-unit countdown (2w 2d … 45s), ticking every second
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-23 20:59:41 +02:00

115 lines
3.1 KiB
TypeScript

import { afterEach, describe, expect, it, vi } from 'vitest'
import {
daysUntil,
fmtAgo,
fmtDuration,
fmtEta,
fmtHours,
fmtHoursPrecise,
fmtPlaytime,
fmtRemaining,
isoDate,
} from './format'
afterEach(() => {
vi.useRealTimers()
})
describe('fmtHours', () => {
it('returns a dash for non-positive values', () => {
expect(fmtHours(0)).toBe('—')
expect(fmtHours(-3)).toBe('—')
})
it('rounds with thousands separators', () => {
expect(fmtHours(67031.1)).toBe('67,031 h')
})
})
describe('fmtHoursPrecise', () => {
it('returns a dash for non-positive values', () => {
expect(fmtHoursPrecise(-1)).toBe('—')
})
it('keeps one decimal place', () => {
expect(fmtHoursPrecise(44.28)).toBe('44.3')
})
})
describe('fmtPlaytime', () => {
it('reports untouched at zero', () => {
expect(fmtPlaytime(0)).toBe('untouched')
})
it('converts minutes to hours', () => {
expect(fmtPlaytime(90)).toBe('1.5 h')
})
})
describe('fmtEta', () => {
it('returns N/A for null', () => {
expect(fmtEta(null)).toBe('N/A')
})
it('returns days and the target date', () => {
vi.useFakeTimers()
vi.setSystemTime(new Date('2026-05-29T12:00:00'))
const out = fmtEta(10)
expect(out).toContain('10 days')
expect(out).toContain('2026-06-08')
})
})
describe('isoDate', () => {
it('formats a date as YYYY-MM-DD', () => {
expect(isoDate(new Date(2026, 0, 5))).toBe('2026-01-05')
})
})
describe('daysUntil', () => {
it('counts whole days to a future date', () => {
vi.useFakeTimers()
vi.setSystemTime(new Date('2026-05-29T12:00:00'))
expect(daysUntil('2026-06-08')).toBe(10)
})
})
describe('fmtDuration', () => {
it('formats hours, minutes and seconds', () => {
expect(fmtDuration(26846)) .toBe('7h 27m')
expect(fmtDuration(2640)).toBe('44m')
expect(fmtDuration(38)).toBe('38s')
expect(fmtDuration(-5)).toBe('0s')
})
})
describe('fmtAgo', () => {
const now = new Date('2026-08-28T21:00:00Z')
it('describes recent and older readings', () => {
expect(fmtAgo('2026-08-28T20:56:00Z', now)).toBe('4m ago')
expect(fmtAgo('2026-08-28T20:59:55Z', now)).toBe('just now')
})
it('handles a missing or unparsable timestamp', () => {
expect(fmtAgo('', now)).toBe('unknown')
expect(fmtAgo('not a date', now)).toBe('unknown')
})
})
describe('fmtRemaining', () => {
const H = 3600
const D = 24 * H
it('shows the two largest units, weeks down to seconds', () => {
expect(fmtRemaining(16 * D)).toBe('2w 2d')
expect(fmtRemaining(2 * D + 14 * H)).toBe('2d 14h')
expect(fmtRemaining(2 * H + 20 * 60)).toBe('2h 20m')
expect(fmtRemaining(5 * 60 + 7)).toBe('5m 7s')
expect(fmtRemaining(45)).toBe('45s')
})
it('keeps a zero second unit and caps at weeks', () => {
expect(fmtRemaining(2 * D)).toBe('2d 0h')
expect(fmtRemaining(60 * D)).toBe('8w 4d')
})
it('rounds up so it reaches 0s only when the time is up', () => {
expect(fmtRemaining(H + 8 * 60 + 0.3)).toBe('1h 8m')
expect(fmtRemaining(59.2)).toBe('1m 0s')
expect(fmtRemaining(0)).toBe('0s')
expect(fmtRemaining(-5)).toBe('0s')
})
})