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.

13.7 KB
Raw Blame
/**
 * Tests for utils/log-helpers.js
 *
 * The log panel filters and dedups thousands of log entries during a
 * research run. These helpers power that — an off-by-one in
 * checkLogVisibility means warnings disappear; a collision in
 * hashString means dedupe drops real entries.
 */

import '@js/utils/log-helpers.js';

const {
    bumpCount,
    checkLogVisibility,
    emptyCounts,
    getDisplayLogCategory,
    hashString,
    normalizeMessage,
    normalizeTimestamps,
} = window.LdrLogHelpers;

describe('checkLogVisibility', () => {
    it("filter 'all' shows every type", () => {
        for (const type of ['info', 'milestone', 'warning', 'error']) {
            expect(checkLogVisibility(type, 'all')).toBe(true);
        }
    });

    it("filter 'info' only shows info entries (regression: was a no-op)", () => {
        // The pre-fix implementation returned true for every type under
        // the 'info' filter, which made clicking the Info filter button
        // effectively a no-op. This test pins down the correct, narrow
        // behaviour: only entries whose type is 'info' are visible.
        expect(checkLogVisibility('info', 'info')).toBe(true);
        expect(checkLogVisibility('milestone', 'info')).toBe(false);
        expect(checkLogVisibility('warning', 'info')).toBe(false);
        expect(checkLogVisibility('error', 'info')).toBe(false);
    });

    it("filter 'milestone' only shows milestones", () => {
        expect(checkLogVisibility('milestone', 'milestone')).toBe(true);
        expect(checkLogVisibility('info', 'milestone')).toBe(false);
        expect(checkLogVisibility('warning', 'milestone')).toBe(false);
        expect(checkLogVisibility('error', 'milestone')).toBe(false);
    });

    it("accepts plural 'milestones' as an alias", () => {
        expect(checkLogVisibility('milestone', 'milestones')).toBe(true);
        expect(checkLogVisibility('info', 'milestones')).toBe(false);
    });

    it("filter 'warning' shows warnings and errors", () => {
        expect(checkLogVisibility('warning', 'warning')).toBe(true);
        expect(checkLogVisibility('error', 'warning')).toBe(true);
        expect(checkLogVisibility('info', 'warning')).toBe(false);
        expect(checkLogVisibility('milestone', 'warning')).toBe(false);
    });

    it("accepts plural 'warnings' as an alias", () => {
        expect(checkLogVisibility('warning', 'warnings')).toBe(true);
        expect(checkLogVisibility('error', 'warnings')).toBe(true);
        expect(checkLogVisibility('info', 'warnings')).toBe(false);
    });

    it("filter 'error' only shows errors", () => {
        expect(checkLogVisibility('error', 'error')).toBe(true);
        expect(checkLogVisibility('warning', 'error')).toBe(false);
        expect(checkLogVisibility('info', 'error')).toBe(false);
        expect(checkLogVisibility('milestone', 'error')).toBe(false);
    });

    it("accepts plural 'errors' as an alias", () => {
        expect(checkLogVisibility('error', 'errors')).toBe(true);
        expect(checkLogVisibility('warning', 'errors')).toBe(false);
    });

    it('groups Loguru aliases under the matching display filters', () => {
        for (const type of ['critical', 'fatal', 'CRITICAL', 'FATAL']) {
            expect(checkLogVisibility(type, 'error')).toBe(true);
            expect(checkLogVisibility(type, 'errors')).toBe(true);
            expect(checkLogVisibility(type, 'warning')).toBe(true);
            expect(checkLogVisibility(type, 'milestone')).toBe(false);
        }
        expect(checkLogVisibility('success', 'milestone')).toBe(true);
        expect(checkLogVisibility('SUCCESS', 'milestones')).toBe(true);
        expect(checkLogVisibility('success', 'error')).toBe(false);
    });

    it('unknown filter defaults to showing everything', () => {
        expect(checkLogVisibility('info', 'bogus')).toBe(true);
        expect(checkLogVisibility('error', '')).toBe(true);
        expect(checkLogVisibility('milestone', undefined)).toBe(true);
    });
});

describe('getDisplayLogCategory', () => {
    it('maps diagnostic aliases into existing UI categories', () => {
        expect(getDisplayLogCategory('CRITICAL')).toBe('error');
        expect(getDisplayLogCategory('fatal')).toBe('error');
        expect(getDisplayLogCategory('SUCCESS')).toBe('milestone');
    });

    it('normalizes but otherwise preserves primary and custom types', () => {
        expect(getDisplayLogCategory('WARNING')).toBe('warning');
        expect(getDisplayLogCategory('DEBUG')).toBe('debug');
        expect(getDisplayLogCategory('NOTICE')).toBe('notice');
        expect(getDisplayLogCategory('constructor')).toBe('constructor');
        expect(getDisplayLogCategory('__proto__')).toBe('__proto__');
    });
});

describe('emptyCounts', () => {
    it('returns a fresh object on each call (mutations do not leak)', () => {
        const a = emptyCounts();
        const b = emptyCounts();
        expect(a).not.toBe(b);
        a.info = 99;
        expect(b.info).toBe(0);
    });

    it('initialises every category to zero with the expected keys', () => {
        const counts = emptyCounts();
        expect(counts).toEqual({
            info: 0,
            milestone: 0,
            warning: 0,
            error: 0,
        });
    });
});

describe('bumpCount', () => {
    it('adjusts tracked categories in place by the given delta', () => {
        const counts = emptyCounts();
        bumpCount(counts, 'error', 1);
        bumpCount(counts, 'error', 1);
        bumpCount(counts, 'milestone', 1);
        expect(counts.error).toBe(2);
        expect(counts.milestone).toBe(1);
        bumpCount(counts, 'error', -1);
        expect(counts.error).toBe(1);
    });

    it('floors at zero so a decrement cannot drive a bucket negative', () => {
        const counts = emptyCounts();
        bumpCount(counts, 'warning', -1);
        expect(counts.warning).toBe(0);
    });

    it('ignores untracked categories, including inherited keys', () => {
        const counts = emptyCounts();
        bumpCount(counts, 'debug', 1);
        bumpCount(counts, '__proto__', 1);
        bumpCount(counts, 'constructor', 1);
        expect(counts).toEqual({
            info: 0,
            milestone: 0,
            warning: 0,
            error: 0,
        });
        expect(Object.prototype.constructor).toBe(Object);
    });
});

describe('hashString', () => {
    it('returns "0" for empty/null/undefined', () => {
        expect(hashString('')).toBe('0');
        expect(hashString(null)).toBe('0');
        expect(hashString(undefined)).toBe('0');
    });

    it('is deterministic: same input → same output', () => {
        const a = hashString('log entry one');
        const b = hashString('log entry one');
        expect(a).toBe(b);
    });

    it('produces different hashes for different inputs', () => {
        expect(hashString('log entry one')).not.toBe(hashString('log entry two'));
        expect(hashString('a')).not.toBe(hashString('b'));
    });

    it('handles long strings', () => {
        const long = 'x'.repeat(10000);
        expect(hashString(long)).toMatch(/^-?\d+$/);
    });

    it('handles unicode characters', () => {
        // Should not throw and should differ from its ASCII analogue
        expect(hashString('café')).toMatch(/^-?\d+$/);
        expect(hashString('café')).not.toBe(hashString('cafe'));
    });
});

describe('normalizeMessage', () => {
    it('returns empty string for falsy input', () => {
        expect(normalizeMessage('')).toBe('');
        expect(normalizeMessage(null)).toBe('');
        expect(normalizeMessage(undefined)).toBe('');
    });

    it('trims whitespace', () => {
        expect(normalizeMessage('  hello  ')).toBe('hello');
        expect(normalizeMessage('\t\nhello\r\n')).toBe('hello');
    });

    it('lowercases', () => {
        expect(normalizeMessage('HELLO World')).toBe('hello world');
    });

    it('trims and lowercases in combination', () => {
        expect(normalizeMessage('  HELLO  ')).toBe('hello');
    });
});

describe('normalizeTimestamps', () => {
    it('is a no-op when all logs share the same date', () => {
        const logs = [
            {time: '2026-04-01T10:00:00Z', message: 'a'},
            {time: '2026-04-01T11:00:00Z', message: 'b'},
            {time: '2026-04-01T12:00:00Z', message: 'c'},
        ];
        const before = JSON.parse(JSON.stringify(logs));
        normalizeTimestamps(logs);
        expect(logs).toEqual(before);
    });

    it('re-stamps an outlier date to the majority date', () => {
        const logs = [
            {time: '2026-04-01T10:00:00.000Z', message: 'a'},
            {time: '2026-04-01T11:00:00.000Z', message: 'b'},
            {time: '2026-04-01T12:00:00.000Z', message: 'c'},
            {time: '2026-04-02T15:30:00.000Z', message: 'outlier'},
        ];
        const originalTime = logs[3].time;
        normalizeTimestamps(logs);
        const outlier = logs[3];
        // The outlier's date portion should now be 2026-04-01
        expect(outlier.time.startsWith('2026-04-01')).toBe(true);
        // The time should have changed from the original
        expect(outlier.time).not.toBe(originalTime);
        // ID should be regenerated from the new timestamp + hash
        expect(outlier.id).toMatch(/^2026-04-01.*-/);
    });

    it('preserves an existing id when re-stamping an outlier date', () => {
        // The standard-array loader preserves the server-provided database
        // id for distinct persisted rows. Re-deriving from `time + hash`
        // here would collide two rows that share (timestamp, message) but
        // differ only by id, so normalizeTimestamps must leave the id
        // alone when one is already present.
        const logs = [
            {time: '2026-04-01T10:00:00.000Z', message: 'a', id: 'row-1'},
            {time: '2026-04-01T11:00:00.000Z', message: 'b', id: 'row-2'},
            {time: '2026-04-01T12:00:00.000Z', message: 'c', id: 'row-3'},
            {time: '2026-04-02T15:30:00.000Z', message: 'outlier', id: 'row-4'},
        ];
        normalizeTimestamps(logs);
        const outlier = logs[3];
        expect(outlier.time.startsWith('2026-04-01')).toBe(true);
        expect(outlier.id).toBe('row-4');
    });

    it('preserves distinct existing ids across multiple outliers that share a message', () => {
        // Two outlier rows on the same minority date, with identical
        // (timestamp, message) tuples but distinct server ids. Before the
        // fix both rows were re-keyed to the same `time+hash` value and
        // the downstream uniqueLogsMap collapsed one of them. The fix
        // must keep both ids intact so the dedup layer sees them as two
        // distinct rows.
        const logs = [
            {time: '2026-04-01T10:00:00.000Z', message: 'shared', id: 'row-a'},
            {time: '2026-04-01T11:00:00.000Z', message: 'shared', id: 'row-b'},
            {time: '2026-04-01T12:00:00.000Z', message: 'shared', id: 'row-c'},
            {time: '2026-04-02T10:00:00.000Z', message: 'shared', id: 'row-d'},
            {time: '2026-04-02T10:00:00.000Z', message: 'shared', id: 'row-e'},
        ];
        normalizeTimestamps(logs);
        const ids = logs.map((log) => log.id);
        expect(ids).toEqual(['row-a', 'row-b', 'row-c', 'row-d', 'row-e']);
    });

    it('treats numeric ids (including 0) as present', () => {
        // The parser accepts `log.id != null && log.id !== ''` as "has
        // id". normalizeTimestamps must agree on that boundary so a
        // server that hands out `0` as a valid id never gets it clobbered
        // by the time+hash fallback.
        const logs = [
            {time: '2026-04-01T10:00:00.000Z', message: 'a', id: 0},
            {time: '2026-04-01T11:00:00.000Z', message: 'b', id: 1},
            {time: '2026-04-01T12:00:00.000Z', message: 'c', id: 2},
            {time: '2026-04-02T15:30:00.000Z', message: 'outlier', id: 7},
        ];
        normalizeTimestamps(logs);
        expect(logs.map((log) => log.id)).toEqual([0, 1, 2, 7]);
    });

    it('still derives a fallback id when an outlier has no id at all', () => {
        // The progress_log envelope payload doesn't ship server ids, so
        // for that path normalizeTimestamps is the first place a derived
        // id ever appears. Regression guard: the new "preserve existing
        // id" branch must not accidentally swallow the fallback too.
        const logs = [
            {time: '2026-04-01T10:00:00.000Z', message: 'a'},
            {time: '2026-04-01T11:00:00.000Z', message: 'b'},
            {time: '2026-04-02T15:30:00.000Z', message: 'outlier'},
        ];
        normalizeTimestamps(logs);
        const outlier = logs[2];
        expect(outlier.time.startsWith('2026-04-01')).toBe(true);
        expect(outlier.id).toMatch(/^2026-04-01.*-/);
    });

    it('does nothing when logs is empty', () => {
        const logs = [];
        expect(() => normalizeTimestamps(logs)).not.toThrow();
        expect(logs).toEqual([]);
    });

    it('does nothing when every log has an unparseable time', () => {
        const logs = [
            {time: 'not-a-date', message: 'a'},
            {time: 'also-not-a-date', message: 'b'},
        ];
        // Should not throw; mostCommonDate would remain null
        expect(() => normalizeTimestamps(logs)).not.toThrow();
    });

    it('handles a tie by picking one date deterministically (first seen wins via iteration order)', () => {
        // Two days with equal count — either is acceptable, just shouldn't crash
        const logs = [
            {time: '2026-04-01T10:00:00Z', message: 'a'},
            {time: '2026-04-02T10:00:00Z', message: 'b'},
        ];
        expect(() => normalizeTimestamps(logs)).not.toThrow();
        // Both should end up on the same day
        const d1 = logs[0].time.split('T')[0];
        const d2 = logs[1].time.split('T')[0];
        expect(d1).toBe(d2);
    });
});