78 lines
1.9 KiB
TypeScript
78 lines
1.9 KiB
TypeScript
import { describe, it, expect } from 'vitest';
|
||
import { formatBytes, estimateTokens, getModelContextWindow, formatUptime } from './format.js';
|
||
|
||
describe('formatBytes', () => {
|
||
it('formats 0 bytes', () => {
|
||
expect(formatBytes(0)).toBe('0 B');
|
||
});
|
||
|
||
it('formats bytes', () => {
|
||
expect(formatBytes(512)).toBe('512 B');
|
||
});
|
||
|
||
it('formats kilobytes', () => {
|
||
expect(formatBytes(1536)).toBe('1.5 KB');
|
||
});
|
||
|
||
it('formats megabytes', () => {
|
||
expect(formatBytes(5242880)).toBe('5 MB');
|
||
});
|
||
|
||
it('formats gigabytes', () => {
|
||
expect(formatBytes(4294967296)).toBe('4 GB');
|
||
});
|
||
|
||
it('returns 0 B for negative input', () => {
|
||
expect(formatBytes(-100)).toBe('0 B');
|
||
});
|
||
});
|
||
|
||
describe('estimateTokens', () => {
|
||
it('returns 0 for empty string', () => {
|
||
expect(estimateTokens('')).toBe(0);
|
||
});
|
||
|
||
it('returns 0 for whitespace-only string', () => {
|
||
expect(estimateTokens(' ')).toBe(0);
|
||
});
|
||
|
||
it('estimates tokens for a sentence', () => {
|
||
const result = estimateTokens('Hello world this is a test');
|
||
expect(result).toBeGreaterThan(0);
|
||
// 6 words × 1.3 = 7.8, ceil = 8
|
||
expect(result).toBe(8);
|
||
});
|
||
});
|
||
|
||
describe('getModelContextWindow', () => {
|
||
it('returns 128k for models with 128k in name', () => {
|
||
expect(getModelContextWindow('llama3-128k')).toBe(128_000);
|
||
});
|
||
|
||
it('returns 32k for models with 32k in name', () => {
|
||
expect(getModelContextWindow('gpt4-32k')).toBe(32_000);
|
||
});
|
||
|
||
it('returns 4096 for models without size marker', () => {
|
||
expect(getModelContextWindow('llama3:latest')).toBe(4_096);
|
||
});
|
||
});
|
||
|
||
describe('formatUptime', () => {
|
||
it('formats minutes only', () => {
|
||
expect(formatUptime(120)).toBe('2m');
|
||
});
|
||
|
||
it('formats hours and minutes', () => {
|
||
expect(formatUptime(3661)).toBe('1h 1m');
|
||
});
|
||
|
||
it('formats days, hours, and minutes', () => {
|
||
expect(formatUptime(90061)).toBe('1d 1h 1m');
|
||
});
|
||
|
||
it('formats zero', () => {
|
||
expect(formatUptime(0)).toBe('0m');
|
||
});
|
||
});
|