format.test.ts 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /**
  2. * Pure-function tests for the Sources formatters.
  3. * No React, no network.
  4. */
  5. import { describe, expect, it } from 'vitest';
  6. import { formatDate, formatRateLimit, statusLabel, typeLabel } from '@/features/sources/format';
  7. import type { SourceStatus } from '@/features/sources/types';
  8. describe('formatRateLimit', () => {
  9. it('formats sub-1k as /s', () => {
  10. expect(formatRateLimit(0)).toBe('0/s');
  11. expect(formatRateLimit(1)).toBe('1/s');
  12. expect(formatRateLimit(999)).toBe('999/s');
  13. });
  14. it('formats 1k..999k as N.Nk/s', () => {
  15. expect(formatRateLimit(1_000)).toBe('1.0k/s');
  16. expect(formatRateLimit(2_500)).toBe('2.5k/s');
  17. expect(formatRateLimit(750_000)).toBe('750.0k/s');
  18. });
  19. it('formats >= 1M as N.NM/s', () => {
  20. expect(formatRateLimit(1_000_000)).toBe('1.0M/s');
  21. expect(formatRateLimit(2_500_000)).toBe('2.5M/s');
  22. });
  23. });
  24. describe('formatDate', () => {
  25. it('returns em-dash for null/undefined/empty', () => {
  26. expect(formatDate(null)).toBe('\u2014');
  27. expect(formatDate(undefined)).toBe('\u2014');
  28. expect(formatDate('')).toBe('\u2014');
  29. });
  30. it('returns the original string for unparseable input', () => {
  31. expect(formatDate('not-a-date')).toBe('not-a-date');
  32. });
  33. it('formats a valid ISO date', () => {
  34. const out = formatDate('2026-06-18T12:00:00Z');
  35. // Don't lock to a specific locale-dependent string; just
  36. // assert it does not contain 'Invalid'.
  37. expect(out).not.toMatch(/Invalid/);
  38. expect(out.length).toBeGreaterThan(0);
  39. });
  40. });
  41. describe('statusLabel', () => {
  42. it('humanizes the status values', () => {
  43. expect(statusLabel('active')).toBe('Active');
  44. expect(statusLabel('suspended')).toBe('Suspended');
  45. });
  46. it('returns the input when unknown', () => {
  47. expect(statusLabel('archived' as SourceStatus)).toBe('archived');
  48. });
  49. });
  50. describe('typeLabel', () => {
  51. it('humanizes the type values', () => {
  52. expect(typeLabel('http')).toBe('HTTP');
  53. expect(typeLabel('mqtt')).toBe('MQTT');
  54. expect(typeLabel('ws')).toBe('WebSocket');
  55. expect(typeLabel('grpc')).toBe('gRPC');
  56. });
  57. });