/** * Pure-function tests for the Sources formatters. * No React, no network. */ import { describe, expect, it } from 'vitest'; import { formatDate, formatRateLimit, statusLabel, typeLabel } from '@/features/sources/format'; import type { SourceStatus } from '@/features/sources/types'; describe('formatRateLimit', () => { it('formats sub-1k as /s', () => { expect(formatRateLimit(0)).toBe('0/s'); expect(formatRateLimit(1)).toBe('1/s'); expect(formatRateLimit(999)).toBe('999/s'); }); it('formats 1k..999k as N.Nk/s', () => { expect(formatRateLimit(1_000)).toBe('1.0k/s'); expect(formatRateLimit(2_500)).toBe('2.5k/s'); expect(formatRateLimit(750_000)).toBe('750.0k/s'); }); it('formats >= 1M as N.NM/s', () => { expect(formatRateLimit(1_000_000)).toBe('1.0M/s'); expect(formatRateLimit(2_500_000)).toBe('2.5M/s'); }); }); describe('formatDate', () => { it('returns em-dash for null/undefined/empty', () => { expect(formatDate(null)).toBe('\u2014'); expect(formatDate(undefined)).toBe('\u2014'); expect(formatDate('')).toBe('\u2014'); }); it('returns the original string for unparseable input', () => { expect(formatDate('not-a-date')).toBe('not-a-date'); }); it('formats a valid ISO date', () => { const out = formatDate('2026-06-18T12:00:00Z'); // Don't lock to a specific locale-dependent string; just // assert it does not contain 'Invalid'. expect(out).not.toMatch(/Invalid/); expect(out.length).toBeGreaterThan(0); }); }); describe('statusLabel', () => { it('humanizes the status values', () => { expect(statusLabel('active')).toBe('Active'); expect(statusLabel('suspended')).toBe('Suspended'); }); it('returns the input when unknown', () => { expect(statusLabel('archived' as SourceStatus)).toBe('archived'); }); }); describe('typeLabel', () => { it('humanizes the type values', () => { expect(typeLabel('http')).toBe('HTTP'); expect(typeLabel('mqtt')).toBe('MQTT'); expect(typeLabel('ws')).toBe('WebSocket'); expect(typeLabel('grpc')).toBe('gRPC'); }); });