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.

15.6 KB
Raw Blame
/** Runtime ownership and recovery coverage for monitorResearch(). */

import { resolve } from 'node:path';

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

import { compileTemplateHarness } from '../helpers/template-harness.js';

const NEWS_SOURCE_PATH = resolve(
    __dirname,
    '../../../src/local_deep_research/web/static/js/pages/news.js',
);

function response(payload, ok = true, status = ok ? 200 : 500) {
    return {
        ok,
        status,
        json: vi.fn().mockResolvedValue(payload),
    };
}

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

function compileMonitorRuntime() {
    const showNewsAlert = vi.fn();
    const loadNewsFeed = vi.fn().mockResolvedValue(undefined);
    const ResearchStates = {
        isInProgress: status => status === 'in_progress',
        isActive: status => ['queued', 'pending', 'in_progress'].includes(status),
        isCompleted: status => status === 'completed',
        isTerminal: status => [
            'completed',
            'failed',
            'error',
            'cancelled',
        ].includes(status),
    };

    const runtime = compileTemplateHarness({
        templatePath: NEWS_SOURCE_PATH,
        functionNames: [
            'cleanupNewsPage',
            'stopNewsResearchPoll',
            'beginNewsResearchPoll',
            'isCurrentNewsResearchPoll',
            'clearStoredNewsResearch',
            'findNewsResearchCard',
            'removeNewsResearchCards',
            'endNewsResearchPollWithFeed',
            'completeNewsResearchPoll',
            'monitorResearch',
            'checkActiveNewsResearch',
            'pollForNewsResearchResults',
        ],
        dependencies: {
            showNewsAlert,
            loadNewsFeed,
            ResearchStates,
            URLBuilder: window.URLBuilder,
            escapeHtml: value => String(value),
            SafeLogger: {
                log: vi.fn(),
                error: vi.fn(),
                warn: vi.fn(),
            },
        },
        preamble: `
            let autoRefreshInterval = null;
            let priorityCheckInterval = null;
            let refreshIndicatorInterval = null;
            let newsSemanticTimer = null;
            let newsFeedRequestId = 0;
            let newsFeedRequestIntent = 'generic';
            let newsResearchPollId = 0;
            let activeNewsResearchPoll = null;
            let newsResearchReloadTimer = null;
            let newsResearchRestoreId = 0;
            let searchHistoryLoadRequestId = 0;
        `,
        returnExpression: `({
            cleanupNewsPage,
            monitorResearch,
            checkActiveNewsResearch,
            pollForNewsResearchResults,
        })`,
    });

    return { ...runtime, showNewsAlert, loadNewsFeed };
}

beforeEach(() => {
    vi.useFakeTimers();
    localStorage.clear();
    document.body.innerHTML = `
        <section id="news-feed-content">
            <article id="existing-news">Existing news</article>
        </section>
    `;
});

afterEach(() => {
    vi.clearAllTimers();
    vi.useRealTimers();
    vi.restoreAllMocks();
    vi.unstubAllGlobals();
    localStorage.clear();
    document.body.replaceChildren();
});

it('allows only one monitor status request in flight and settles once', async () => {
    const pendingStatus = deferred();
    const fetchMock = vi.fn()
        .mockResolvedValueOnce(response({
            status: 'in_progress',
            progress: 15,
            query: 'Migration monitor',
        }))
        .mockImplementationOnce(() => pendingStatus.promise);
    vi.stubGlobal('fetch', fetchMock);
    const runtime = compileMonitorRuntime();

    await runtime.monitorResearch('monitor-3299');
    expect(document.querySelector('[data-research-id="monitor-3299"]'))
        .not.toBeNull();

    vi.advanceTimersByTime(3000);
    await Promise.resolve();
    expect(fetchMock).toHaveBeenCalledTimes(2);

    vi.advanceTimersByTime(6000);
    await Promise.resolve();
    expect(fetchMock).toHaveBeenCalledTimes(2);

    pendingStatus.resolve(response({
        status: 'completed',
        progress: 100,
    }));
    await Promise.resolve();
    await Promise.resolve();

    expect(localStorage.getItem('active_news_research')).toBeNull();
    expect(document.querySelector('.ldr-active-research-card')).toBeNull();
    expect(runtime.showNewsAlert).toHaveBeenCalledWith(
        'Test run completed! Loading results...',
        'success',
    );

    await vi.advanceTimersByTimeAsync(1000);
    expect(runtime.loadNewsFeed).toHaveBeenCalledOnce();
    expect(fetchMock).toHaveBeenCalledTimes(2);
});

it('cleans monitor state and restores the feed after a status network error', async () => {
    const fetchMock = vi.fn()
        .mockResolvedValueOnce(response({
            status: 'in_progress',
            progress: 25,
            query: 'Network recovery',
        }))
        .mockRejectedValueOnce(new Error('offline'));
    vi.stubGlobal('fetch', fetchMock);
    const runtime = compileMonitorRuntime();

    await runtime.monitorResearch('monitor-network');
    expect(localStorage.getItem('active_news_research')).not.toBeNull();
    expect(document.querySelector('[data-research-id="monitor-network"]'))
        .not.toBeNull();

    await vi.advanceTimersByTimeAsync(3000);

    expect(localStorage.getItem('active_news_research')).toBeNull();
    expect(document.querySelector('.ldr-active-research-card')).toBeNull();
    expect(runtime.showNewsAlert).toHaveBeenCalledWith(
        'Error checking research status. Please try again.',
        'error',
    );
    expect(runtime.loadNewsFeed).toHaveBeenCalledOnce();

    await vi.advanceTimersByTimeAsync(9000);
    expect(fetchMock).toHaveBeenCalledTimes(2);
});

it('retires an in-flight monitor on page cleanup without repainting after it settles', async () => {
    const pendingStatus = deferred();
    const fetchMock = vi.fn(() => pendingStatus.promise);
    vi.stubGlobal('fetch', fetchMock);
    const runtime = compileMonitorRuntime();

    const monitoring = runtime.monitorResearch('cleanup-live', 'Leave the page');
    await Promise.resolve();

    expect(fetchMock).toHaveBeenCalledOnce();
    expect(JSON.parse(localStorage.getItem('active_news_research')))
        .toMatchObject({ researchId: 'cleanup-live' });
    expect(vi.getTimerCount()).toBe(1);

    runtime.cleanupNewsPage();

    expect(vi.getTimerCount()).toBe(0);
    expect(JSON.parse(localStorage.getItem('active_news_research')))
        .toMatchObject({ researchId: 'cleanup-live' });

    pendingStatus.resolve(response({
        status: 'completed',
        progress: 100,
    }));
    await monitoring;

    expect(runtime.showNewsAlert).not.toHaveBeenCalled();
    expect(runtime.loadNewsFeed).not.toHaveBeenCalled();
    expect(document.querySelector('.ldr-active-research-card')).toBeNull();
    expect(JSON.parse(localStorage.getItem('active_news_research')))
        .toMatchObject({ researchId: 'cleanup-live' });
});

it.each(['completed', 'in_progress'])(
    'does not let stale restored %s run A replace a directly started run B',
    async staleStatus => {
        localStorage.setItem('active_news_research', JSON.stringify({
            researchId: 'restore-a',
            query: 'Old restored run',
            startTime: new Date().toISOString(),
        }));
        const staleRestore = deferred();
        const fetchMock = vi.fn()
            .mockImplementationOnce(() => staleRestore.promise)
            .mockResolvedValueOnce(response({
                status: 'in_progress',
                progress: 30,
                query: 'Current direct run',
            }));
        vi.stubGlobal('fetch', fetchMock);
        const runtime = compileMonitorRuntime();

        const restoring = runtime.checkActiveNewsResearch();
        await runtime.monitorResearch('direct-b', 'Current direct run');
        staleRestore.resolve(response({
            status: staleStatus,
            progress: staleStatus === 'completed' ? 100 : 20,
            query: 'Old restored run',
        }));
        await restoring;

        expect(fetchMock).toHaveBeenCalledTimes(2);
        expect(JSON.parse(localStorage.getItem('active_news_research')))
            .toMatchObject({ researchId: 'direct-b' });
        expect(document.querySelector('[data-research-id="direct-b"]'))
            .not.toBeNull();
        expect(document.querySelector('[data-research-id="restore-a"]'))
            .toBeNull();
        expect(runtime.showNewsAlert).not.toHaveBeenCalledWith(
            'Your news analysis has completed! Loading results...',
            'success',
        );
    },
);

it.each(['queued', 'pending'])(
    'resumes a stored %s run using the production active-state semantics',
    async activeStatus => {
        localStorage.setItem('active_news_research', JSON.stringify({
            researchId: `restore-${activeStatus}`,
            query: `${activeStatus} migration news`,
            startTime: new Date().toISOString(),
        }));
        const fetchMock = vi.fn()
            .mockResolvedValueOnce(response({ status: activeStatus, progress: 0 }))
            .mockResolvedValueOnce(response({
                status: activeStatus,
                progress: 0,
                query: `${activeStatus} migration news`,
            }));
        vi.stubGlobal('fetch', fetchMock);
        const runtime = compileMonitorRuntime();

        await runtime.checkActiveNewsResearch();

        expect(fetchMock).toHaveBeenCalledTimes(2);
        expect(JSON.parse(localStorage.getItem('active_news_research')))
            .toMatchObject({ researchId: `restore-${activeStatus}` });
        await vi.waitFor(() => {
            expect(document.querySelector(
                `[data-research-id="restore-${activeStatus}"]`,
            )).not.toBeNull();
        });
    },
);

it('does not duplicate a run that is already monitoring during page restore', async () => {
    const initialStatus = deferred();
    const fetchMock = vi.fn(() => initialStatus.promise);
    vi.stubGlobal('fetch', fetchMock);
    const runtime = compileMonitorRuntime();

    const monitoring = runtime.monitorResearch('already-live', 'Live run');
    await runtime.checkActiveNewsResearch();

    expect(fetchMock).toHaveBeenCalledOnce();
    initialStatus.resolve(response({
        status: 'in_progress',
        progress: 5,
        query: 'Live run',
    }));
    await monitoring;
    expect(document.querySelector('[data-research-id="already-live"]'))
        .not.toBeNull();
});

it('times out a monitor whose initial status request never settles', async () => {
    const initialStatus = deferred();
    vi.stubGlobal('fetch', vi.fn(() => initialStatus.promise));
    const runtime = compileMonitorRuntime();

    const monitoring = runtime.monitorResearch('hung-initial', 'Hung run');
    await vi.advanceTimersByTimeAsync(5 * 60 * 1000);

    expect(localStorage.getItem('active_news_research')).toBeNull();
    expect(runtime.showNewsAlert).toHaveBeenCalledWith(
        'Research monitoring timed out. Please try again.',
        'warning',
    );
    expect(runtime.loadNewsFeed).toHaveBeenCalledOnce();

    initialStatus.resolve(response({ status: 'in_progress', progress: 10 }));
    await monitoring;
    expect(document.querySelector('[data-research-id="hung-initial"]'))
        .toBeNull();
});

it('bounds a hung page-restore probe without discarding resumable state', async () => {
    localStorage.setItem('active_news_research', JSON.stringify({
        researchId: 'hung-restore',
        query: 'Retry this restore later',
        startTime: new Date().toISOString(),
    }));
    let restoreSignal;
    const fetchMock = vi.fn((url, options) => {
        restoreSignal = options.signal;
        return new Promise(() => {});
    });
    vi.stubGlobal('fetch', fetchMock);
    const runtime = compileMonitorRuntime();

    const restoring = runtime.checkActiveNewsResearch();
    await vi.advanceTimersByTimeAsync(10000);
    await restoring;

    expect(fetchMock).toHaveBeenCalledOnce();
    expect(restoreSignal.aborted).toBe(true);
    expect(JSON.parse(localStorage.getItem('active_news_research')))
        .toMatchObject({ researchId: 'hung-restore' });
    expect(runtime.showNewsAlert).not.toHaveBeenCalled();
    expect(runtime.loadNewsFeed).not.toHaveBeenCalled();
    expect(vi.getTimerCount()).toBe(0);
});

it('keeps the restore deadline active while the status body is hung', async () => {
    localStorage.setItem('active_news_research', JSON.stringify({
        researchId: 'hung-restore-body',
        query: 'Retry the body later',
        startTime: new Date().toISOString(),
    }));
    const statusBody = deferred();
    let restoreSignal;
    const fetchMock = vi.fn((url, options) => {
        restoreSignal = options.signal;
        return Promise.resolve({
            ok: true,
            status: 200,
            json: vi.fn(() => statusBody.promise),
        });
    });
    vi.stubGlobal('fetch', fetchMock);
    const runtime = compileMonitorRuntime();

    const restoring = runtime.checkActiveNewsResearch();
    await vi.advanceTimersByTimeAsync(10000);
    await restoring;

    expect(fetchMock).toHaveBeenCalledOnce();
    expect(restoreSignal.aborted).toBe(true);
    expect(JSON.parse(localStorage.getItem('active_news_research')))
        .toMatchObject({ researchId: 'hung-restore-body' });
    expect(runtime.showNewsAlert).not.toHaveBeenCalled();
    expect(runtime.loadNewsFeed).not.toHaveBeenCalled();
    expect(vi.getTimerCount()).toBe(0);
});

it('hands an active restore to monitoring without blocking page bootstrap', async () => {
    localStorage.setItem('active_news_research', JSON.stringify({
        researchId: 'active-restore-handoff',
        query: 'Continue in the background',
        startTime: new Date().toISOString(),
    }));
    const monitorStatus = deferred();
    const fetchMock = vi.fn()
        .mockResolvedValueOnce(response({ status: 'queued', progress: 0 }))
        .mockImplementationOnce(() => monitorStatus.promise);
    vi.stubGlobal('fetch', fetchMock);
    const runtime = compileMonitorRuntime();

    await expect(runtime.checkActiveNewsResearch()).resolves.toBeUndefined();

    expect(fetchMock).toHaveBeenCalledTimes(2);
    expect(JSON.parse(localStorage.getItem('active_news_research')))
        .toMatchObject({ researchId: 'active-restore-handoff' });
    expect(runtime.showNewsAlert).not.toHaveBeenCalled();
    expect(runtime.loadNewsFeed).not.toHaveBeenCalled();
});

it('uses a wall-clock timeout while a subscription status request is hung', async () => {
    const pendingStatus = deferred();
    const fetchMock = vi.fn(() => pendingStatus.promise);
    vi.stubGlobal('fetch', fetchMock);
    const runtime = compileMonitorRuntime();
    const activeCard = document.createElement('article');
    activeCard.className = 'ldr-active-research-card';
    activeCard.dataset.researchId = 'hung-subscription';
    document.getElementById('news-feed-content').prepend(activeCard);

    await runtime.pollForNewsResearchResults(
        'hung-subscription',
        'Hung subscription',
    );
    await vi.advanceTimersByTimeAsync(5 * 60 * 1000);

    expect(fetchMock).toHaveBeenCalledOnce();
    expect(localStorage.getItem('active_news_research')).toBeNull();
    expect(document.querySelector('[data-research-id="hung-subscription"]'))
        .toBeNull();
    expect(runtime.showNewsAlert).toHaveBeenCalledWith(
        'Research taking too long. Check the progress page.',
        'warning',
    );
    expect(runtime.loadNewsFeed).toHaveBeenCalledOnce();

    pendingStatus.resolve(response({ status: 'completed', progress: 100 }));
    await Promise.resolve();
    await Promise.resolve();
    expect(runtime.showNewsAlert).toHaveBeenCalledOnce();
});