Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

2.8 KB
Raw Blame
/**
 * Tests for utils/format-bytes.js — window.formatBytes.
 *
 * Single source of truth for human-readable byte sizes, shared by
 * delete_manager.js and pdf_upload_handler.js. The unit-boundary
 * rounding (1023 -> Bytes, 1024 -> KB) is the interesting part.
 */

import '@js/utils/format-bytes.js';

const formatBytes = window.formatBytes;

describe('formatBytes', () => {
    it('returns "0 Bytes" for 0', () => {
        expect(formatBytes(0)).toBe('0 Bytes');
    });

    it('formats sub-kilobyte values in Bytes', () => {
        expect(formatBytes(500)).toBe('500 Bytes');
        expect(formatBytes(1023)).toContain('Bytes');
    });

    it('formats kilobytes, including fractional', () => {
        expect(formatBytes(1024)).toBe('1 KB');
        expect(formatBytes(1536)).toBe('1.5 KB');
    });

    it('formats megabytes and gigabytes', () => {
        expect(formatBytes(1048576)).toBe('1 MB');
        expect(formatBytes(1073741824)).toBe('1 GB');
    });

    it('rounds to 2 decimal places', () => {
        // 1234567 bytes ≈ 1.18 MB
        expect(formatBytes(1234567)).toBe('1.18 MB');
    });

    it('picks the unit by power-of-1024 boundary (1023->Bytes, 1024->KB)', () => {
        expect(formatBytes(1023)).toContain('Bytes');
        expect(formatBytes(1024)).toContain('KB');
        expect(formatBytes(1024 * 1024 - 1)).toContain('KB');
        expect(formatBytes(1024 * 1024)).toContain('MB');
    });

    it('formats terabyte-and-larger sizes without "undefined"', () => {
        // Regression: sizes only went up to GB, so i>=4 yielded "N undefined".
        expect(formatBytes(1024 ** 4)).toBe('1 TB');
        expect(formatBytes(5 * 1024 ** 4)).toBe('5 TB');
        expect(formatBytes(1024 ** 5)).toBe('1 PB');
        expect(formatBytes(1024 ** 6)).toBe('1 EB');
        // Beyond the largest unit, clamp to EB rather than emit undefined.
        expect(formatBytes(1024 ** 7)).toBe('1024 EB');
    });

    it('handles sub-1-byte values without a negative index (lower-bound clamp)', () => {
        // log(bytes) < 0 for bytes < 1 would give a negative index -> undefined.
        expect(formatBytes(0.5)).toBe('0.5 Bytes');
    });

    it('returns "0 Bytes" for non-positive / non-finite inputs', () => {
        // Math.log(-1) is NaN, which propagates through the clamp and would
        // otherwise yield "NaN undefined". The guard short-circuits this.
        // Infinity passes the relational check but would clamp to "Infinity EB".
        expect(formatBytes(-1)).toBe('0 Bytes');
        expect(formatBytes(-1024)).toBe('0 Bytes');
        expect(formatBytes(NaN)).toBe('0 Bytes');
        expect(formatBytes(Number.POSITIVE_INFINITY)).toBe('0 Bytes');
        expect(formatBytes(Number.NEGATIVE_INFINITY)).toBe('0 Bytes');
        expect(formatBytes(undefined)).toBe('0 Bytes');
        expect(formatBytes(null)).toBe('0 Bytes');
    });
});