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.1 KB
Raw Blame
/** Initial failure ownership for the progress page's HTTP fallback. */

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

const RESEARCH_ID = 'progress-initial-error-3299';

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

async function flushPromises(turns = 8) {
    for (let turn = 0; turn < turns; turn += 1) {
        await Promise.resolve();
    }
}

beforeEach(() => {
    vi.useFakeTimers();
    document.body.innerHTML = `
        <div role="progressbar" aria-valuenow="0">
            <div id="progress-bar" class="bg-primary"></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: () => 'error',
    };
});

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

it('retires fallback polling and ignores an older status after initial failure', async () => {
    vi.stubGlobal('URLBuilder', window.URLBuilder);
    vi.stubGlobal('URLS', window.URLS);
    vi.stubGlobal('ResearchStates', window.ResearchStates);
    vi.stubGlobal('Notification', { permission: 'denied' });
    vi.spyOn(window.URLBuilder, 'extractResearchIdFromPattern')
        .mockReturnValue(RESEARCH_ID);
    const olderPoll = deferred();
    const getResearchStatus = vi.fn()
        .mockImplementationOnce(() => olderPoll.promise)
        .mockResolvedValueOnce({
            status: 'failed',
            message: 'Provider credentials rejected',
        });
    window.api = { getResearchStatus };
    const intervalSpy = vi.spyOn(globalThis, 'setInterval');
    const clearIntervalSpy = vi.spyOn(globalThis, 'clearInterval');

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

    expect(getResearchStatus).toHaveBeenCalledTimes(2);
    expect(intervalSpy).toHaveBeenCalledWith(expect.any(Function), 3000);
    const fallbackInterval = intervalSpy.mock.results[0].value;
    expect(clearIntervalSpy).toHaveBeenCalledWith(fallbackInterval);
    expect(document.getElementById('progress-bar').style.width).toBe('100%');
    expect(document.getElementById('progress-bar').classList)
        .toContain('bg-danger');
    expect(document.getElementById('current-task').textContent)
        .toBe('Error: Provider credentials rejected');
    expect(document.getElementById('cancel-research-btn').style.display)
        .toBe('none');
    expect(document.getElementById('view-results-btn').textContent)
        .toBe('View Error Report');

    olderPoll.resolve({
        status: 'in_progress',
        progress: 12,
        current_task: 'Stale provider setup',
    });
    await olderPoll.promise;
    await flushPromises();

    expect(document.getElementById('progress-bar').style.width).toBe('100%');
    expect(document.getElementById('current-task').textContent)
        .toBe('Error: Provider credentials rejected');
    expect(document.getElementById('view-results-btn').style.display)
        .toBe('inline-block');
});