Spaces:
Running
Running
File size: 2,727 Bytes
6f5f15e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 | 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');
});
});
|