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.

12.8 KB
Raw Blame
import '../helpers/safe-fetch-with-auth-to-fetch.js';

/**
 * Tests for the log-panel header badge's ERROR severity signal
 * (`.ldr-log-indicator--has-error`), added by PR #5648 and corrected in
 * its follow-up.
 *
 * All three defects below were introduced and corrected inside this branch;
 * none of them reached main. There was no JS test for the log panel's badge,
 * which is why they survived several revisions of the branch. Each `it` below
 * names the defect it pins:
 *
 *   1. The badge selected on the RAW level (`[data-log-type="error"]`),
 *      but the repo's normalizer folds CRITICAL/FATAL into the `error`
 *      display category (utils/log-helpers.js). A CRITICAL row therefore
 *      left the badge purple and titleless while the "Error" FILTER badge
 *      counted it — the panel disagreeing with itself, on precisely the
 *      two severities the signal exists for.
 *   2. The badge counted only entries currently in the DOM. The header
 *      number is deliberately floored to the server-known `fetched` count
 *      because the DOM holds fewer rows, so the badge could report a full
 *      count with no severity for a run whose errors had been paged out
 *      or pruned. The count is now a per-research high-water mark, and
 *      the tooltip says "at least N" while the view is truncated.
 *   3. Two other writers of the indicator's textContent (`initializeLogPanel`
 *      and `recomputeCountersFromDom`) never cleared the error class or
 *      the title, so a re-init or a recompute could repaint a fresh count
 *      under a stale red badge. All writers now go through
 *      `paintLogIndicators`.
 *
 * Plus the accessibility contract: colour alone must not carry the state,
 * and the explanation must not be hover-only (role + aria-label).
 */

let logPanel;
let emptyCounts;

beforeAll(async () => {
    await import('@js/utils/log-helpers.js');
    emptyCounts = window.LdrLogHelpers.emptyCounts;

    window.escapeHtml = (s) => String(s ?? '').replace(/[&<>"']/g, '');
    window.URLBuilder = {
        researchLogs: (id, limit) =>
            `/api/research/${id}/logs${limit ? `?limit=${limit}` : ''}`,
        historyLogCount: (id) => `/api/research/${id}/log_count`,
        researchLogsExport: (id) => `/api/research/${id}/logs/export`,
    };

    Object.defineProperty(window, 'location', {
        configurable: true,
        value: { ...window.location, pathname: '/', search: '', hash: '' },
    });

    await import('@js/components/logpanel.js');
    logPanel = window.logPanel;
});

beforeEach(() => {
    // The #research-progress wrapper makes initializeLogPanel treat this
    // as a research page — without it the init path bails before it
    // paints the indicator, and the two re-init tests below would pass
    // vacuously.
    document.body.innerHTML = `
        <div id="research-progress">
            <div class="ldr-collapsible-log-panel">
                <div class="ldr-log-panel-header" id="log-panel-toggle">
                    <i class="ldr-toggle-icon"></i>
                    <span class="ldr-log-indicator" id="log-indicator">0</span>
                </div>
                <div class="ldr-log-panel-content collapsed" id="log-panel-content">
                    <div class="ldr-console-log" id="console-log-container"></div>
                </div>
            </div>
        </div>
    `;
    window._logPanelState.counts = emptyCounts();
    window._logPanelState.renderedIds = new Set();
    window._logPanelState.currentFilter = 'all';
    window._logPanelState.totalLogs = null;
    window._logPanelState.fetchedLogs = null;
    window._logPanelState.renderedLimit = null;
    window._logPanelState.errorHighWater = 0;
    window._logPanelState.initialized = false;
    window._logPanelState.connectedResearchId = null;

    // initializeLogPanel pre-fetches logs (and the row count) for a
    // non-null research id; stub both so the tests never touch the
    // network and never leave an unhandled rejection behind.
    globalThis.fetch = vi.fn(() =>
        Promise.resolve({ ok: true, json: () => Promise.resolve([]) })
    );
});

/** Append one rendered row of the given RAW severity to the container. */
function seed(rawLevel, message = `msg-${rawLevel}`) {
    const container = document.getElementById('console-log-container');
    const entry = document.createElement('div');
    entry.className = `ldr-console-log-entry ldr-log-${rawLevel}`;
    entry.dataset.logType = rawLevel;
    entry.dataset.logId = `${rawLevel}-${message}`;
    const span = document.createElement('span');
    span.className = 'ldr-log-message';
    span.textContent = message;
    entry.appendChild(span);
    container.appendChild(entry);
    return entry;
}

function badge() {
    return document.querySelector('.ldr-log-indicator');
}

describe('header badge severity — display-category normalization', () => {
    // Defect 1. Before the fix the selector was
    //   '[data-log-type="error"], .ldr-log-error'
    // which matches neither of these rows, so both assertions failed.
    it.each(['critical', 'fatal'])(
        'turns red for a %s entry (folded into the error category)',
        (level) => {
            seed(level);
            logPanel._updateLogCountIndicator();

            expect(
                badge().classList.contains('ldr-log-indicator--has-error')
            ).toBe(true);
            expect(badge().title).toContain('1 error');
        }
    );

    it('turns red for a plain error entry', () => {
        seed('error');
        logPanel._updateLogCountIndicator();
        expect(
            badge().classList.contains('ldr-log-indicator--has-error')
        ).toBe(true);
    });

    it('stays neutral — no class, no title, no ARIA — with no errors', () => {
        seed('info');
        seed('warning');
        seed('milestone');
        logPanel._updateLogCountIndicator();

        expect(
            badge().classList.contains('ldr-log-indicator--has-error')
        ).toBe(false);
        expect(badge().title).toBe('');
        expect(badge().hasAttribute('role')).toBe(false);
        expect(badge().hasAttribute('aria-label')).toBe(false);
    });

    it('counts critical and error together, matching the Error filter badge', () => {
        seed('error', 'a');
        seed('critical', 'b');
        seed('fatal', 'c');
        seed('info', 'd');
        logPanel._updateLogCountIndicator();
        expect(badge().title).toContain('3 errors');
    });
});

describe('header badge severity — entries outside the rendered window', () => {
    // Defect 2. The engine-self-disable ERROR this feature exists to
    // surface is emitted at init, so it is the row most likely to be
    // dropped from the fetched window or evicted by the ordered prune.
    it('keeps the signal after the error row leaves the DOM', () => {
        const errorRow = seed('error');
        seed('info');
        logPanel._updateLogCountIndicator();
        expect(
            badge().classList.contains('ldr-log-indicator--has-error')
        ).toBe(true);

        // The row is paged out / pruned; only routine rows remain.
        errorRow.remove();
        logPanel._updateLogCountIndicator();

        expect(
            badge().classList.contains('ldr-log-indicator--has-error')
        ).toBe(true);
        expect(window._logPanelState.errorHighWater).toBe(1);
    });

    it('words the tooltip as a lower bound while the view is truncated', () => {
        seed('error');
        // The server holds far more rows than the DOM renders.
        window._logPanelState.totalLogs = 9002;
        window._logPanelState.fetchedLogs = 500;
        logPanel._updateLogCountIndicator();

        expect(badge().title).toContain('at least 1 error');
        // The number itself is still the floored server count, unchanged
        // by the severity work.
        expect(badge().textContent).toBe('500');
    });

    it('does not claim a lower bound when the DOM holds everything', () => {
        seed('error');
        window._logPanelState.totalLogs = 1;
        window._logPanelState.fetchedLogs = 1;
        logPanel._updateLogCountIndicator();

        expect(badge().title).toContain('1 error');
        expect(badge().title).not.toContain('at least');
    });

    it('does not carry the previous research’s errors across a switch', () => {
        seed('error');
        logPanel._updateLogCountIndicator();
        expect(window._logPanelState.errorHighWater).toBe(1);

        // Research switch: initializeLogPanel resets the per-research state.
        document.getElementById('console-log-container').innerHTML = '';
        logPanel.initialize('research-b');

        expect(window._logPanelState.errorHighWater).toBe(0);
        expect(
            badge().classList.contains('ldr-log-indicator--has-error')
        ).toBe(false);
    });
});

describe('header badge severity — every writer clears the state', () => {
    // Defect 3. initializeLogPanel and recomputeCountersFromDom wrote
    // textContent directly and left the class/title untouched, so a badge
    // that had legitimately gone red for research A kept its colour and
    // tooltip under B's count.
    it('recomputeCountersFromDom clears a stale red badge', () => {
        badge().classList.add('ldr-log-indicator--has-error');
        badge().title = 'stale tooltip from a previous paint';
        badge().setAttribute('role', 'img');
        badge().setAttribute('aria-label', 'stale');

        seed('info');
        logPanel._recomputeCountersFromDom();

        expect(
            badge().classList.contains('ldr-log-indicator--has-error')
        ).toBe(false);
        expect(badge().title).toBe('');
        expect(badge().hasAttribute('aria-label')).toBe(false);
    });

    it('initializeLogPanel clears a stale red badge', () => {
        badge().classList.add('ldr-log-indicator--has-error');
        badge().title = 'stale tooltip from a previous paint';

        // No researchId → no count fetch, just the synchronous "0" paint.
        logPanel.initialize(null);

        expect(
            badge().classList.contains('ldr-log-indicator--has-error')
        ).toBe(false);
        expect(badge().title).toBe('');
    });
});

describe('header badge severity — accessibility', () => {
    // The blocked state was conveyed by colour alone (--error-color vs
    // --accent-primary: two similar-luminance mid-tones, both with white
    // text) and the explanation lived only in a hover `title` on a bare
    // span. The glyph lives in CSS (::after) so textContent stays a bare
    // number for updateFilterCounters; the text alternative lives here.
    it('exposes the severity to assistive tech, not just on hover', () => {
        seed('critical');
        logPanel._updateLogCountIndicator();

        expect(badge().getAttribute('role')).toBe('img');
        const label = badge().getAttribute('aria-label');
        expect(label).toContain('log entries');
        expect(label).toContain('1 error');
    });

    it('keeps textContent a bare number so the All badge can parse it', () => {
        seed('error');
        seed('info');
        logPanel._updateLogCountIndicator();
        expect(badge().textContent).toBe('2');
        expect(badge().textContent).not.toContain('!');
    });
});

describe('header badge severity — a header without a console container', () => {
    // The success paths all repaint through recomputeCountersFromDom on the
    // emptied container, so only a DOM that has the indicator but not the
    // container isolates initializeLogPanel's own paint. Reverting that
    // call leaves the previous research's red badge, tooltip and ARIA label
    // sitting under a stale count.
    it('repaints the badge on init when the container is absent', () => {
        document.body.innerHTML = `
            <div id="research-progress">
                <div class="ldr-collapsible-log-panel">
                    <div class="ldr-log-panel-header" id="log-panel-toggle">
                        <i class="ldr-toggle-icon"></i>
                        <span class="ldr-log-indicator" id="log-indicator">7</span>
                    </div>
                    <div class="ldr-log-panel-content collapsed" id="log-panel-content"></div>
                </div>
            </div>
        `;
        expect(document.getElementById('console-log-container')).toBeNull();

        badge().classList.add('ldr-log-indicator--has-error');
        badge().title = 'at least 7 errors in this research';
        badge().setAttribute('role', 'img');
        badge().setAttribute('aria-label', '7 log entries, at least 7 errors');

        // No researchId → no count fetch, just the synchronous "0" paint.
        logPanel.initialize(null);

        expect(badge().textContent).toBe('0');
        expect(
            badge().classList.contains('ldr-log-indicator--has-error')
        ).toBe(false);
        expect(badge().title).toBe('');
        expect(badge().hasAttribute('aria-label')).toBe(false);
    });
});