format.test.ts 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /**
  2. * Pure-function tests for the Telegram bots formatters.
  3. * No React, no network.
  4. */
  5. import { describe, expect, it } from 'vitest';
  6. import {
  7. formatDate,
  8. formatDateTime,
  9. statusLabel,
  10. statusVariant,
  11. tokenSetLabel,
  12. tokenSetVariant,
  13. truncate,
  14. } from '@/features/telegram/format';
  15. import type { TelegramBotStatus } from '@/features/telegram/types';
  16. describe('statusLabel', () => {
  17. it('humanizes the status values', () => {
  18. expect(statusLabel('active')).toBe('Active');
  19. expect(statusLabel('paused')).toBe('Paused');
  20. });
  21. it('returns the input when unknown', () => {
  22. expect(statusLabel('weird' as TelegramBotStatus)).toBe('weird');
  23. });
  24. });
  25. describe('statusVariant', () => {
  26. it('maps active to success and paused to warning', () => {
  27. expect(statusVariant('active')).toBe('success');
  28. expect(statusVariant('paused')).toBe('warning');
  29. });
  30. });
  31. describe('tokenSetLabel', () => {
  32. it('renders Configured when set and Not set when unset', () => {
  33. expect(tokenSetLabel(true)).toBe('Configured');
  34. expect(tokenSetLabel(false)).toBe('Not set');
  35. });
  36. });
  37. describe('tokenSetVariant', () => {
  38. it('maps set to success and unset to warning', () => {
  39. expect(tokenSetVariant(true)).toBe('success');
  40. expect(tokenSetVariant(false)).toBe('warning');
  41. });
  42. });
  43. describe('formatDate', () => {
  44. it('returns em-dash for null/undefined/empty', () => {
  45. expect(formatDate(null)).toBe('\u2014');
  46. expect(formatDate(undefined)).toBe('\u2014');
  47. expect(formatDate('')).toBe('\u2014');
  48. });
  49. it('returns the original string for unparseable input', () => {
  50. expect(formatDate('not-a-date')).toBe('not-a-date');
  51. });
  52. it('formats a valid ISO date', () => {
  53. const out = formatDate('2026-06-18T12:00:00Z');
  54. expect(out).not.toMatch(/Invalid/);
  55. expect(out.length).toBeGreaterThan(0);
  56. });
  57. });
  58. describe('formatDateTime', () => {
  59. it('returns em-dash for null/undefined/empty', () => {
  60. expect(formatDateTime(null)).toBe('\u2014');
  61. expect(formatDateTime(undefined)).toBe('\u2014');
  62. expect(formatDateTime('')).toBe('\u2014');
  63. });
  64. it('formats a valid ISO date with a time component', () => {
  65. const out = formatDateTime('2026-06-18T12:34:00Z');
  66. expect(out).not.toMatch(/Invalid/);
  67. expect(out.length).toBeGreaterThan(0);
  68. });
  69. });
  70. describe('truncate', () => {
  71. it('returns em-dash for null/undefined/empty', () => {
  72. expect(truncate(null)).toBe('\u2014');
  73. expect(truncate(undefined)).toBe('\u2014');
  74. expect(truncate('')).toBe('\u2014');
  75. });
  76. it('returns the input when shorter than the cap', () => {
  77. expect(truncate('hello', 10)).toBe('hello');
  78. });
  79. it('truncates and adds an ellipsis when over the cap', () => {
  80. const out = truncate('this is a long string that will be cut off', 10);
  81. expect(out.endsWith('\u2026')).toBe(true);
  82. expect(out.length).toBe(10);
  83. });
  84. it('uses a default cap of 60', () => {
  85. const out = truncate('a'.repeat(80));
  86. expect(out.length).toBe(60);
  87. expect(out.endsWith('\u2026')).toBe(true);
  88. });
  89. });