Files
glchat/web/tests/format.test.ts
T

29 lines
870 B
TypeScript
Raw Normal View History

import { describe, expect, it } from 'vitest';
import { errorCode, formatBytes } from '@/lib/format';
import { ApiError } from '@/api/client';
describe('formatBytes', () => {
it('renders human readable sizes', () => {
expect(formatBytes(512)).toBe('512 B');
expect(formatBytes(1024)).toBe('1 KB');
expect(formatBytes(26214400)).toBe('25 MB');
expect(formatBytes(1073741824)).toBe('1 GB');
});
it('handles invalid input', () => {
expect(formatBytes(-1)).toBe('—');
expect(formatBytes(Number.NaN)).toBe('—');
});
});
describe('errorCode', () => {
it('extracts the server error code', () => {
expect(errorCode(new ApiError(403, 'perm.denied', 'nope'))).toBe('perm.denied');
});
it('maps unknown failures to a network code', () => {
expect(errorCode(new Error('socket hang up'))).toBe('network.unreachable');
});
});