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.6 KB
Raw Blame
/** Direct contracts for progress-page bootstrap degradation paths. */

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

const RESEARCH_ID = 'fallback/research-3299';
let registeredWindowListeners = [];

async function importAndInitializeProgress() {
    let initializeProgress;
    const originalDocumentAdd = document.addEventListener.bind(document);
    const originalWindowAdd = window.addEventListener.bind(window);
    const documentAddSpy = vi.spyOn(document, 'addEventListener')
        .mockImplementation((type, listener, options) => {
            if (type === 'DOMContentLoaded') {
                initializeProgress = listener;
                return;
            }
            originalDocumentAdd(type, listener, options);
        });
    const windowAddSpy = vi.spyOn(window, 'addEventListener')
        .mockImplementation((type, listener, options) => {
            registeredWindowListeners.push([type, listener, options]);
            originalWindowAdd(type, listener, options);
        });

    try {
        await import('@js/components/progress.js');
    } finally {
        documentAddSpy.mockRestore();
        windowAddSpy.mockRestore();
    }

    expect(initializeProgress).toEqual(expect.any(Function));
    initializeProgress();
}

beforeEach(() => {
    vi.resetModules();
    vi.useFakeTimers();
    registeredWindowListeners = [];
    document.body.innerHTML = `
        <main>
            <div class="ldr-progress-container"></div>
            <div class="ldr-status-container"></div>
            <div class="ldr-task-container"></div>
            <a id="owned-navigation" href="/history">History</a>
        </main>
    `;

    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(() => {
    registeredWindowListeners.forEach(([type, listener, options]) => {
        window.removeEventListener(type, listener, options);
    });
    vi.restoreAllMocks();
    vi.unstubAllGlobals();
    vi.clearAllTimers();
    vi.useRealTimers();
    document.body.replaceChildren();
    delete window.api;
    delete window.socket;
    delete window.pollIntervals;
    delete window.addConsoleLog;
    delete window.progressComponent;
    delete window.showNotification;
    delete window.ui;
    delete window.ResearchStates;
    delete window.RESEARCH_STATUS;
});

it('creates required UI and starts one owned poll when socket setup throws', 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 getResearchStatus = vi.fn().mockResolvedValue({
        status: 'in_progress',
        progress: 17,
        current_task: 'Migrating realtime state',
    });
    window.api = { getResearchStatus };
    window.addConsoleLog = vi.fn();
    window.socket = {
        subscribeToResearch: vi.fn(() => {
            throw new Error('socket bootstrap failed');
        }),
        onReconnect: vi.fn(),
        isUsingPolling: vi.fn(() => true),
    };
    const intervalSpy = vi.spyOn(globalThis, 'setInterval');

    await importAndInitializeProgress();
    await vi.waitFor(() => expect(getResearchStatus).toHaveBeenCalledTimes(2));

    const progress = document.getElementById('progress-bar');
    expect(progress).not.toBeNull();
    expect(progress.parentElement.getAttribute('role')).toBe('progressbar');
    expect(progress.parentElement.getAttribute('aria-valuenow')).toBe('17');
    expect(document.getElementById('progress-percentage').textContent).toBe('17%');
    // The page intentionally keeps the specific task instead of replacing it
    // with the generic "In Progress" status label.
    expect(document.getElementById('status-text').textContent).toBe('Initializing');
    expect(document.getElementById('current-task').textContent)
        .toBe('Migrating realtime state');
    expect(intervalSpy.mock.calls.filter(([, delay]) => delay === 3000))
        .toHaveLength(1);
    expect(window.addConsoleLog).toHaveBeenCalledWith(
        'Using polling for research updates instead of WebSockets',
        'info',
    );

    const websocketError = new window.ErrorEvent('error', {
        message: 'Invalid WebSocket frame header',
        cancelable: true,
    });
    window.dispatchEvent(websocketError);
    expect(websocketError.defaultPrevented).toBe(true);
});

it('cleans service-owned polling before preserving a navigation callback', 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);
    window.api = {
        getResearchStatus: vi.fn().mockResolvedValue({
            status: 'in_progress',
            progress: 1,
        }),
    };
    window.socket = {
        subscribeToResearch: vi.fn(),
        onReconnect: vi.fn(),
        isUsingPolling: vi.fn(() => true),
    };
    const originalNavigation = vi.fn(() => false);
    document.getElementById('owned-navigation').onclick = originalNavigation;
    const pollingTimer = setInterval(() => {}, 60000);
    window.pollIntervals = { [RESEARCH_ID]: pollingTimer };
    const clearIntervalSpy = vi.spyOn(globalThis, 'clearInterval');

    await importAndInitializeProgress();
    await vi.waitFor(() => expect(window.api.getResearchStatus).toHaveBeenCalled());

    const navigation = document.getElementById('owned-navigation');
    const click = new MouseEvent('click', { bubbles: true, cancelable: true });
    navigation.dispatchEvent(click);

    expect(clearIntervalSpy).toHaveBeenCalledWith(pollingTimer);
    expect(originalNavigation).toHaveBeenCalledOnce();
});

it('explains the missing-research-id redirect with an error toast', async () => {
    vi.stubGlobal('URLBuilder', window.URLBuilder);
    vi.stubGlobal('URLValidator', { safeAssign: vi.fn() });
    vi.stubGlobal('ResearchStates', window.ResearchStates);
    vi.stubGlobal('Notification', { permission: 'denied' });
    vi.spyOn(window.URLBuilder, 'extractResearchIdFromPattern')
        .mockReturnValue(null);
    const showMessage = vi.fn();
    const showError = vi.fn();
    window.ui = { showMessage, showError };

    await importAndInitializeProgress();

    // window.ui.showError takes a CONTAINER id as its first argument, so
    // passing the message there resolves no element and renders nothing —
    // the redirect landed with no explanation for it.
    expect(showError).not.toHaveBeenCalled();
    expect(showMessage).toHaveBeenCalledWith(
        'No active research found. Please start a new research.',
        'error',
    );

    await vi.advanceTimersByTimeAsync(3000);
    expect(window.URLValidator.safeAssign).toHaveBeenCalledWith(
        window.location,
        'href',
        '/',
    );
});