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.9 KB
Raw Blame
/**
 * Regression coverage for the progress page's completion-triggered
 * per-research context-overflow request.
 */

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

const RESEARCH_ID = 'progress-overflow-17';
const CONTEXT_URL = `/api/research/${RESEARCH_ID}/context-overflow`;
let progressCallback;

beforeEach(() => {
    progressCallback = null;
    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>
    `;

    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((_researchId, callback) => {
            progressCallback = callback;
        }),
        onReconnect: vi.fn(),
        isUsingPolling: vi.fn(() => false),
    };
});

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('consumes live progress before checking overflow on completion', async () => {
    vi.stubGlobal('URLBuilder', window.URLBuilder);
    vi.stubGlobal('URLS', window.URLS);
    vi.stubGlobal('ResearchStates', window.ResearchStates);
    vi.spyOn(window.URLBuilder, 'extractResearchIdFromPattern').mockReturnValue(RESEARCH_ID);
    const statusMock = vi.spyOn(window.api, 'getResearchStatus').mockResolvedValue({
        status: 'in_progress',
        progress: 40,
        current_task: 'Gathering sources',
    });
    const fetchMock = vi.fn().mockResolvedValue({
        ok: true,
        json: vi.fn().mockResolvedValue({
            status: 'success',
            data: {
                overview: {
                    truncation_occurred: true,
                    truncated_count: 3,
                    tokens_lost: 2500,
                },
            },
        }),
    });
    vi.stubGlobal('fetch', fetchMock);

    // Keep browser-notification permission out of the behavior under test;
    // the in-page alert is the observable fallback available to every user.
    vi.stubGlobal('Notification', { permission: 'denied' });

    await import('@js/components/progress.js');
    document.dispatchEvent(new Event('DOMContentLoaded'));
    await vi.waitFor(() => expect(statusMock.mock.calls.length).toBeGreaterThanOrEqual(2));

    expect(window.socket.subscribeToResearch).toHaveBeenCalledWith(
        RESEARCH_ID,
        expect.any(Function),
    );
    expect(progressCallback).toBeTypeOf('function');

    progressCallback({
        research_id: RESEARCH_ID,
        status: 'in_progress',
        progress: 73,
        current_task: 'Synthesizing migrated results',
    });

    expect(document.getElementById('progress-bar').style.width).toBe('73%');
    expect(document.getElementById('progress-percentage').textContent)
        .toBe('73%');
    expect(document.getElementById('current-task').textContent)
        .toBe('Synthesizing migrated results');
    expect(document.title).toBe('Research (73%) - Local Deep Research');
    expect(fetchMock).not.toHaveBeenCalled();

    statusMock.mockResolvedValue({ status: 'completed', progress: 100 });
    await window.progressComponent.checkProgress();

    await vi.waitFor(() => {
        expect(document.getElementById('notification-container')?.textContent)
            .toContain('Context Overflow Warning');
    });

    expect(fetchMock).toHaveBeenCalledWith(CONTEXT_URL);
    expect(fetchMock).toHaveBeenCalledTimes(1);
    const toast = document.getElementById('notification-container');
    expect(toast.textContent).toContain('Context truncated 3 time(s) during research.');
    expect(toast.textContent).toContain('~2,500 tokens lost.');
    expect(toast.querySelector('button.btn-primary').textContent)
        .toBe('View overflow details');

    // Poll and socket terminal events may race. The completion guard must keep
    // that duplicate signal from issuing another overflow request/toast.
    await window.progressComponent.checkProgress();
    expect(fetchMock).toHaveBeenCalledTimes(1);
    expect(toast.querySelectorAll('.ldr-alert-warning')).toHaveLength(1);
});