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.

5.0 KB
Raw Blame
/** Terminal ownership after cancelling an active progress page. */

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

const RESEARCH_ID = 'progress-cancel-owner-3299';

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

it('keeps Cancelled owned against a stale poll, socket frame, and reconnect', async () => {
    vi.useFakeTimers();
    document.body.innerHTML = `
        <div role="progressbar" aria-valuenow="25">
            <div id="progress-bar"></div>
        </div>
        <div id="progress-percentage"></div>
        <div id="status-text" class="ldr-status-indicator">In progress</div>
        <div id="current-task"></div>
        <button id="cancel-research-btn">Cancel Research</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',
    };

    let progressCallback;
    let reconnectCallback;
    window.socket = {
        subscribeToResearch: vi.fn((_researchId, callback) => {
            progressCallback = callback;
        }),
        unsubscribeFromResearch: vi.fn(),
        onReconnect: vi.fn(callback => {
            reconnectCallback = callback;
        }),
        isUsingPolling: vi.fn(() => true),
    };
    const statusMock = vi.fn().mockResolvedValue({
        status: 'in_progress',
        progress: 25,
        current_task: 'Initial task',
    });
    window.api = {
        getResearchStatus: statusMock,
        terminateResearch: vi.fn().mockResolvedValue({ status: 'success' }),
    };
    window.ui = {
        showMessage: vi.fn(),
        showError: vi.fn(),
    };
    vi.spyOn(window.URLBuilder, 'extractResearchIdFromPattern')
        .mockReturnValue(RESEARCH_ID);
    vi.stubGlobal('confirm', vi.fn(() => true));
    vi.stubGlobal('Notification', { permission: 'denied' });
    const clearIntervalSpy = vi.spyOn(globalThis, 'clearInterval');

    try {
        await import('@js/components/progress.js');
        document.dispatchEvent(new Event('DOMContentLoaded'));
        await vi.waitFor(() => {
            expect(statusMock).toHaveBeenCalledTimes(2);
            expect(progressCallback).toBeTypeOf('function');
            expect(reconnectCallback).toBeTypeOf('function');
            expect(document.getElementById('progress-bar').style.width)
                .toBe('25%');
        });

        const stalePoll = deferred();
        statusMock.mockImplementationOnce(() => stalePoll.promise);
        const stalePollRequest = window.progressComponent.checkProgress();
        await vi.waitFor(() => expect(statusMock).toHaveBeenCalledTimes(3));

        const intervalClearsBeforeCancel = clearIntervalSpy.mock.calls.length;
        document.getElementById('cancel-research-btn').click();
        await vi.advanceTimersByTimeAsync(0);
        expect(window.api.terminateResearch).toHaveBeenCalledWith(RESEARCH_ID);
        expect(document.getElementById('status-text').textContent)
            .toBe('Cancelled');
        expect(window.socket.unsubscribeFromResearch)
            .toHaveBeenCalledWith(RESEARCH_ID);
        expect(clearIntervalSpy.mock.calls.length)
            .toBeGreaterThan(intervalClearsBeforeCancel);

        stalePoll.resolve({
            status: 'in_progress',
            progress: 70,
            current_task: 'Late HTTP task',
        });
        await stalePollRequest;
        progressCallback({
            status: 'in_progress',
            progress: 90,
            current_task: 'Late socket task',
        });
        reconnectCallback();

        expect(document.getElementById('status-text').textContent)
            .toBe('Cancelled');
        expect(document.getElementById('progress-bar').style.width).toBe('25%');
        expect(document.getElementById('current-task').textContent)
            .toBe('Initial task');
        expect(window.socket.subscribeToResearch).toHaveBeenCalledOnce();
        expect(statusMock).toHaveBeenCalledTimes(3);
    } finally {
        vi.clearAllTimers();
        vi.useRealTimers();
        vi.restoreAllMocks();
        vi.unstubAllGlobals();
        delete window.api;
        delete window.socket;
        delete window.ui;
        delete window.ResearchStates;
        delete window.RESEARCH_STATUS;
        delete window.progressComponent;
        delete window.showNotification;
        document.body.replaceChildren();
    }
});