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.

3.2 KB
Raw Blame
/** Malformed HTTP snapshots must not steal progress ownership. */

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

const RESEARCH_ID = 'progress-malformed-3299';

function deferred() {
    let resolvePromise;
    const promise = new Promise(resolve => {
        resolvePromise = resolve;
    });
    return { promise, resolve: resolvePromise };
}

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('lets an older valid status recover after a newer malformed snapshot', async () => {
    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(),
        onReconnect: vi.fn(),
        isUsingPolling: vi.fn(() => false),
    };
    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);
    const olderValidStatus = deferred();
    const statusMock = vi.spyOn(window.api, 'getResearchStatus')
        .mockImplementationOnce(() => olderValidStatus.promise)
        .mockResolvedValueOnce({});

    await import('@js/components/progress.js');
    document.dispatchEvent(new Event('DOMContentLoaded'));

    await vi.waitFor(() => {
        expect(statusMock).toHaveBeenCalledTimes(2);
        expect(document.getElementById('status-text').textContent)
            .toMatch(/Error/i);
    });

    olderValidStatus.resolve({
        status: 'in_progress',
        progress: 65,
        current_task: 'Recovered valid status',
    });

    await vi.waitFor(() => {
        expect(document.getElementById('progress-bar').style.width).toBe('65%');
        expect(document.getElementById('current-task').textContent)
            .toBe('Recovered valid status');
    });
});