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.

7.3 KB
Raw Blame
/** Direct browser-runtime contracts for progress-page research cancellation. */

import '@js/config/urls.js';
import '@js/security/url-validator.js';

const RESEARCH_ID = 'progress-cancel-3299';

beforeEach(async () => {
    vi.resetModules();
    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">
            <i class="fas fa-stop-circle"></i> 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',
    };
    window.socket = {
        subscribeToResearch: vi.fn(),
        onReconnect: vi.fn(),
        isUsingPolling: vi.fn(() => false),
    };
    window.api = {
        getResearchStatus: vi.fn().mockResolvedValue({
            status: 'in_progress',
            progress: 25,
        }),
        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' });
    vi.stubGlobal('URLBuilder', window.URLBuilder);
    vi.stubGlobal('URLValidator', window.URLValidator);
    vi.stubGlobal('ResearchStates', window.ResearchStates);
    vi.stubGlobal('fetch', vi.fn().mockResolvedValue({
        ok: true,
        json: async () => ({ status: 'success', data: { overview: { truncation_occurred: false } } }),
    }));

    // Fresh page state for each case; do not retain prior terminal flags or
    // accumulate DOMContentLoaded handlers across shuffled cases.
    let initialize;
    const addListener = document.addEventListener.bind(document);
    vi.spyOn(document, 'addEventListener').mockImplementation((type, callback, options) => {
        if (type === 'DOMContentLoaded') initialize = callback;
        else addListener(type, callback, options);
    });
    await import('@js/components/progress.js');
    initialize();
    await vi.advanceTimersByTimeAsync(0);
});

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

it('renders the cancelled state after the authenticated API call succeeds', async () => {
    document.getElementById('cancel-research-btn').click();
    await vi.advanceTimersByTimeAsync(0);

    expect(confirm).toHaveBeenCalledWith(
        'Are you sure you want to cancel this research?',
    );
    expect(window.api.terminateResearch).toHaveBeenCalledWith(RESEARCH_ID);
    expect(document.getElementById('status-text').textContent).toBe('Cancelled');
    expect(document.getElementById('status-text').classList)
        .toContain('ldr-status-cancelled');
    expect(document.getElementById('cancel-research-btn').style.display)
        .toBe('none');
    const homeLink = document.getElementById('view-results-btn');
    expect(homeLink.textContent).toBe('Start New Research');
    expect(homeLink.getAttribute('href')).toBe('/');
    expect(homeLink.style.display).toBe('inline-block');
    expect(window.ui.showMessage).toHaveBeenCalledWith(
        'Research has been cancelled.',
    );
    expect(window.ui.showError).not.toHaveBeenCalled();
});

it('restores the cancel control and reports an API failure', async () => {
    window.api.terminateResearch.mockRejectedValueOnce(
        new Error('FastAPI cancellation unavailable'),
    );

    const cancelButton = document.getElementById('cancel-research-btn');
    cancelButton.click();
    await vi.advanceTimersByTimeAsync(0);

    expect(window.api.terminateResearch).toHaveBeenCalledWith(RESEARCH_ID);
    expect(cancelButton.disabled).toBe(false);
    expect(cancelButton.textContent).toContain('Cancel Research');
    expect(cancelButton.style.display).not.toBe('none');
    expect(document.getElementById('status-text').textContent)
        .toBe('In progress');
    // showMessage(message, 'error'), NOT window.ui.showError(message): the
    // shared helper's first argument is a container id, so a bare message
    // resolves no element and renders nothing. Mocking showError let that
    // pass while production dropped the message on the floor.
    expect(window.ui.showError).not.toHaveBeenCalled();
    expect(window.ui.showMessage).toHaveBeenCalledWith(
        'Failed to cancel research. Please try again.',
        'error',
    );
});

it.each(['success', 'failure'])('keeps completion when the pending Cancel ends in %s', async outcome => {
    let settle;
    window.api.terminateResearch.mockReturnValueOnce(new Promise((resolve, reject) => {
        settle = outcome === 'success' ? resolve : reject;
    }));
    document.getElementById('cancel-research-btn').click();
    const progressCallback = window.socket.subscribeToResearch.mock.calls[0][1];
    progressCallback({ status: 'completed', progress: 100 });
    const resultLink = document.getElementById('view-results-btn').getAttribute('href');
    expect(resultLink).toBe(`/results/${RESEARCH_ID}`);
    settle(outcome === 'success'
        ? { status: 'success', research_status: 'completed' }
        : new Error('late cancellation response failed'));
    await vi.advanceTimersByTimeAsync(0);
    expect(document.getElementById('status-text').textContent).not.toBe('Cancelled');
    expect(document.getElementById('view-results-btn').getAttribute('href')).toBe(resultLink);
    expect(document.getElementById('cancel-research-btn').style.display).toBe('none');
    expect(window.ui.showError).not.toHaveBeenCalled();
    expect(window.ui.showMessage).not.toHaveBeenCalledWith('Research has been cancelled.');
});

it('honors already-completed termination responses without a final socket frame', async () => {
    window.api.terminateResearch.mockResolvedValueOnce({
        status: 'success', research_status: 'completed', message: 'Research already completed',
    });
    document.getElementById('cancel-research-btn').click();
    await vi.advanceTimersByTimeAsync(0);
    expect(document.getElementById('view-results-btn').getAttribute('href')).toBe(`/results/${RESEARCH_ID}`);
    expect(document.getElementById('status-text').textContent).not.toBe('Cancelled');
    expect(window.ui.showMessage).not.toHaveBeenCalledWith('Research has been cancelled.');
});