Spaces:
Running
Running
Download src/lib/format.test.ts from evalstate/mcp-webcam: direct link, hf CLI and curl.
- Browser
- Download file 2.73 kB
-
https://huggingface.co/spaces/evalstate/mcp-webcam/resolve/main/src/lib/format.test.ts
- Command line
-
hf download hf://spaces/evalstate/mcp-webcam/src/lib/format.test.ts
-
curl -L -o format.test.ts https://huggingface.co/spaces/evalstate/mcp-webcam/resolve/main/src/lib/format.test.ts
2.73 kB
| import { describe, expect, it } from 'vitest'; | |
| import { | |
| formatBytes, | |
| formatClientName, | |
| formatMs, | |
| formatUptime, | |
| parseBrowser, | |
| prettyCursor, | |
| relativeTime, | |
| shortId, | |
| } from './format'; | |
| describe('format helpers', () => { | |
| it('formats relativeTime across ranges', () => { | |
| const now = Date.parse('2026-12-01T12:00:00.000Z'); | |
| expect(relativeTime('2026-12-01T12:00:00.000Z', now)).toBe('just now'); | |
| expect(relativeTime('2026-12-01T11:59:45.000Z', now)).toBe('15s ago'); | |
| expect(relativeTime('2026-12-01T11:55:00.000Z', now)).toBe('5m ago'); | |
| expect(relativeTime('2026-12-01T09:00:00.000Z', now)).toBe('3h ago'); | |
| expect(relativeTime('2026-11-29T12:00:00.000Z', now)).toBe('2d ago'); | |
| expect(relativeTime(null, now)).toBe('—'); | |
| }); | |
| it('formats uptime cleanly', () => { | |
| const now = Date.parse('2026-12-01T12:00:00.000Z'); | |
| expect(formatUptime('2026-12-01T11:59:20.000Z', now)).toBe('40s'); | |
| expect(formatUptime('2026-12-01T11:55:42.000Z', now)).toBe('4m 18s'); | |
| expect(formatUptime('2026-12-01T09:50:00.000Z', now)).toBe('2h 10m'); | |
| }); | |
| it('shortens IDs and pretty-prints cursors', () => { | |
| expect(shortId('evt_123', 14)).toBe('evt_123'); | |
| expect(shortId('evt_abcdefghij_999', 12)).toBe('evt_abc…_999'); | |
| expect(prettyCursor(null)).toBe('∅'); | |
| expect(prettyCursor('ep_k9m2:14')).toBe('ep_k9m2:14'); | |
| expect(prettyCursor('ep_very_long_epoch_token:4291')).toBe('ep_very…n:4291'); | |
| }); | |
| it('formats bytes and milliseconds', () => { | |
| expect(formatBytes(512)).toBe('512 B'); | |
| expect(formatBytes(4320)).toBe('4.2 KB'); | |
| expect(formatBytes(84210)).toBe('82 KB'); | |
| expect(formatBytes(1_572_864)).toBe('1.5 MB'); | |
| expect(formatMs(500)).toBe('500ms'); | |
| expect(formatMs(3000)).toBe('3s'); | |
| expect(formatMs(1500)).toBe('1.5s'); | |
| }); | |
| it('parses browser and OS from user agent', () => { | |
| expect( | |
| parseBrowser( | |
| 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 Chrome/134.0.0.0 Safari/537.36', | |
| ), | |
| ).toBe('Chrome · macOS'); | |
| expect(parseBrowser('Mozilla/5.0 (X11; Linux x86_64) Gecko/20100101 Firefox/136.0')).toBe( | |
| 'Firefox · Linux', | |
| ); | |
| expect(parseBrowser(undefined)).toBe('Unknown browser'); | |
| }); | |
| it('formats client identity labels', () => { | |
| expect( | |
| formatClientName({ | |
| key: 'k', | |
| name: 'fast-agent-mcp', | |
| version: '0.10.73', | |
| source: 'envelope', | |
| era: 'modern', | |
| transport: 'http', | |
| }), | |
| ).toBe('fast-agent-mcp 0.10.73'); | |
| expect( | |
| formatClientName({ | |
| key: 'stdio', | |
| source: 'unknown', | |
| era: 'legacy', | |
| transport: 'stdio', | |
| }), | |
| ).toBe('stdio client'); | |
| }); | |
| }); | |