utils.test.ts
1,471 bytes
| 1 | import { describe, expect, it } from 'vitest'; |
|---|---|
| 2 | import { cn, formatDuration, formatLatency, formatTokens, formatUsd, truncate } from './utils'; |
| 3 | |
| 4 | describe('formatUsd', () => { |
| 5 | it('adapts precision to magnitude', () => { |
| 6 | expect(formatUsd(0)).toBe('$0.00'); |
| 7 | expect(formatUsd(0.0005)).toBe('$0.0005'); |
| 8 | expect(formatUsd(0.25)).toBe('$0.250'); |
| 9 | expect(formatUsd(12.5)).toBe('$12.50'); |
| 10 | }); |
| 11 | }); |
| 12 | |
| 13 | describe('formatTokens', () => { |
| 14 | it('abbreviates thousands and millions', () => { |
| 15 | expect(formatTokens(500)).toBe('500'); |
| 16 | expect(formatTokens(1500)).toBe('1.5k'); |
| 17 | expect(formatTokens(20000)).toBe('20k'); |
| 18 | expect(formatTokens(2_000_000)).toBe('2.0M'); |
| 19 | }); |
| 20 | }); |
| 21 | |
| 22 | describe('formatLatency / formatDuration', () => { |
| 23 | it('formats ms and seconds', () => { |
| 24 | expect(formatLatency(450)).toBe('450ms'); |
| 25 | expect(formatLatency(1500)).toBe('1.5s'); |
| 26 | expect(formatDuration(45_000)).toBe('45s'); |
| 27 | expect(formatDuration(125_000)).toBe('2m 5s'); |
| 28 | }); |
| 29 | }); |
| 30 | |
| 31 | describe('truncate', () => { |
| 32 | it('leaves short strings and ellipsizes long ones within max length', () => { |
| 33 | expect(truncate('hello', 10)).toBe('hello'); |
| 34 | expect(truncate('hello world', 5)).toBe('he...'); |
| 35 | expect(truncate('hello world', 5).length).toBe(5); |
| 36 | }); |
| 37 | }); |
| 38 | |
| 39 | describe('cn', () => { |
| 40 | it('merges and de-conflicts tailwind classes', () => { |
| 41 | expect(cn('p-2', 'p-4')).toBe('p-4'); |
| 42 | expect(cn('text-sm', false && 'hidden', 'font-bold')).toBe('text-sm font-bold'); |
| 43 | }); |
| 44 | }); |
| 45 | |