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.

4.4 KB
Raw Blame
/**
 * A socket frame without a percentage must not touch the page title.
 *
 * Relayed step events (the topic-organization source-gathering delegate's
 * planning/search/observation steps, LangGraph sub-search relays) are
 * emitted with `progress: null`. The bar already ignores a null; the title
 * ran it through Math.floor, which turned null into 0 and showed
 * "Research (0%)" for each such event.
 */

import '@js/config/urls.js';
import '@js/services/api.js';

const RESEARCH_ID = 'progress-null-frame-5188';

let progressCallback;

beforeEach(() => {
    progressCallback = null;
    document.title = 'Research in progress';
    document.body.innerHTML = `
        <div role="progressbar" aria-valuenow="0">
            <div id="progress-bar"></div>
        </div>
        <div id="progress-percentage"></div>
        <div id="status-text" class="ldr-status-indicator"></div>
        <div id="current-task"></div>
        <button id="cancel-research-btn"></button>
        <a id="view-results-btn" style="display: none"></a>
    `;
    window.RESEARCH_STATUS = {
        QUEUED: 'queued',
        IN_PROGRESS: 'in_progress',
        COMPLETED: 'completed',
        FAILED: 'failed',
        ERROR: 'error',
        CANCELLED: 'cancelled',
    };
    window.ResearchStates = {
        isTerminal: status => [
            'completed',
            'failed',
            'error',
            'cancelled',
        ].includes(status),
        isCompleted: status => status === 'completed',
        isFailed: status => ['failed', 'error'].includes(status),
        isCancelled: status => status === 'cancelled',
        isInProgress: status => status === 'in_progress',
        formatStatus: status => status,
        logLevel: () => 'info',
    };
    window.socket = {
        subscribeToResearch: vi.fn((_researchId, callback) => {
            progressCallback = callback;
        }),
        onReconnect: vi.fn(),
        isUsingPolling: vi.fn(() => false),
    };
});

afterEach(() => {
    vi.restoreAllMocks();
    vi.unstubAllGlobals();
    delete window.socket;
    delete window.ResearchStates;
    delete window.RESEARCH_STATUS;
    delete window.progressComponent;
    delete window.showNotification;
    document.body.replaceChildren();
});

it.each([
    ['null', null],
    ['absent', undefined],
])('keeps the title and bar for a step frame whose percentage is %s', async (_label, missing) => {
    vi.stubGlobal('URLBuilder', window.URLBuilder);
    vi.stubGlobal('URLS', window.URLS);
    vi.stubGlobal('ResearchStates', window.ResearchStates);
    vi.stubGlobal('Notification', { permission: 'denied' });
    vi.stubGlobal('fetch', vi.fn().mockResolvedValue({ ok: false }));
    vi.spyOn(window.URLBuilder, 'extractResearchIdFromPattern')
        .mockReturnValue(RESEARCH_ID);
    vi.spyOn(window.api, 'getResearchStatus').mockResolvedValue({
        status: 'in_progress',
        progress: 40,
        current_task: 'Hydrated from HTTP',
    });

    await import('@js/components/progress.js');
    document.dispatchEvent(new Event('DOMContentLoaded'));
    await vi.waitFor(() => {
        expect(progressCallback).toBeTypeOf('function');
        expect(document.title).toBe('Research (40%) - Local Deep Research');
    });

    progressCallback({
        status: 'in_progress',
        progress: 55,
        message: 'Gathering sources using source-based strategy',
    });
    expect(document.title).toBe('Research (55%) - Local Deep Research');
    expect(document.getElementById('progress-bar').style.width).toBe('55%');

    const stepFrame = {
        phase: 'search',
        message: 'Searching with 3 questions (iteration 1)',
    };
    if (missing === null) {
        stepFrame.progress = null;
    }
    progressCallback(stepFrame);

    // The frame is still consumed ...
    expect(document.getElementById('current-task').textContent)
        .toBe('Searching with 3 questions (iteration 1)');
    // ... but it neither resets the title nor moves the bar.
    expect(document.title).toBe('Research (55%) - Local Deep Research');
    expect(document.getElementById('progress-bar').style.width).toBe('55%');

    // The next frame that carries a percentage updates both again.
    progressCallback({
        status: 'in_progress',
        progress: 60,
        message: 'Generating text synthesis from topics',
    });
    expect(document.title).toBe('Research (60%) - Local Deep Research');
    expect(document.getElementById('progress-bar').style.width).toBe('60%');
});