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.7 KB
Raw Blame
/** FastAPI queued-state hydration and websocket reconnect contracts. */

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

const RESEARCH_ID = 'queued-progress-3299';

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

beforeEach(() => {
    vi.resetModules();
    vi.useFakeTimers();
    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',
    };
});

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

it('renders queue ownership once and reuses its progress consumer on reconnect', async () => {
    vi.stubGlobal('URLBuilder', window.URLBuilder);
    vi.stubGlobal('URLS', window.URLS);
    vi.stubGlobal('URLValidator', { safeAssign: vi.fn() });
    vi.stubGlobal('ResearchStates', window.ResearchStates);
    vi.stubGlobal('Notification', { permission: 'denied' });
    vi.spyOn(window.URLBuilder, 'extractResearchIdFromPattern')
        .mockReturnValue(RESEARCH_ID);
    const queued = {
        status: 'queued',
        progress: 0,
        queue_position: 4,
    };
    window.api = {
        getResearchStatus: vi.fn().mockResolvedValue(queued),
    };
    let reconnect;
    window.socket = {
        subscribeToResearch: vi.fn(),
        onReconnect: vi.fn(callback => {
            reconnect = callback;
        }),
        isUsingPolling: vi.fn(() => false),
    };
    const intervalSpy = vi.spyOn(globalThis, 'setInterval');

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

    expect(window.api.getResearchStatus).toHaveBeenCalledTimes(2);
    expect(document.getElementById('status-text').textContent).toBe('queued');
    expect(document.getElementById('current-task').textContent)
        .toBe('Waiting in queue (position 4)...');
    expect(document.getElementById('progress-percentage').textContent).toBe('0%');
    expect(intervalSpy.mock.calls.filter(([, delay]) => delay === 5000))
        .toHaveLength(1);

    const firstSubscription = window.socket.subscribeToResearch.mock.calls[0];
    expect(firstSubscription).toEqual([RESEARCH_ID, expect.any(Function)]);
    expect(reconnect).toBeTypeOf('function');
    reconnect();

    expect(window.socket.subscribeToResearch).toHaveBeenCalledTimes(2);
    expect(window.socket.subscribeToResearch.mock.calls[1][0]).toBe(RESEARCH_ID);
    expect(window.socket.subscribeToResearch.mock.calls[1][1])
        .toBe(firstSubscription[1]);
});