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.

19.6 KB
Raw Blame
/**
 * Runtime contracts for the news page's create-and-run subscription modal.
 *
 * These tests execute the checked-in submit and polling functions so the two
 * migrated POSTs, research-id handoff, status polling, and terminal cleanup
 * remain one coherent browser workflow.
 */

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 escapeHtml(value) {
    return String(value)
        .replaceAll('&', '&')
        .replaceAll('<', '&lt;')
        .replaceAll('>', '&gt;')
        .replaceAll('"', '&quot;')
        .replaceAll("'", '&#39;');
}

function compileSubscriptionRuntime({
    showNewsAlert = vi.fn(),
    loadSubscriptions = vi.fn().mockResolvedValue(undefined),
    loadNewsFeed = vi.fn().mockResolvedValue(undefined),
} = {}) {
    const hideModal = vi.fn();
    const bootstrap = {
        Modal: {
            getInstance: vi.fn(() => ({ hide: hideModal })),
        },
    };
    const ResearchStates = {
        isCompleted: status => status === 'completed',
        isTerminal: status => [
            'completed',
            'failed',
            'cancelled',
        ].includes(status),
    };

    const runtime = compileTemplateHarness({
        templatePath: NEWS_SOURCE_PATH,
        functionNames: [
            'stopNewsResearchPoll',
            'beginNewsResearchPoll',
            'isCurrentNewsResearchPoll',
            'clearStoredNewsResearch',
            'findNewsResearchCard',
            'removeNewsResearchCards',
            'endNewsResearchPollWithFeed',
            'completeNewsResearchPoll',
            'pollForNewsResearchResults',
            'getNewsSubscriptionModelConfig',
            'handleNewsSubscriptionSubmit',
        ],
        dependencies: {
            showNewsAlert,
            loadSubscriptions,
            loadNewsFeed,
            escapeHtml,
            bootstrap,
            ResearchStates,
            URLBuilder: window.URLBuilder,
            getCSRFToken: () => 'csrf-news-subscription',
        },
        preamble: `
            let newsFeedRequestId = 0;
            let newsFeedRequestIntent = 'generic';
            let newsResearchPollId = 0;
            let activeNewsResearchPoll = null;
            let newsResearchReloadTimer = null;
            let newsResearchRestoreId = 0;
            let activeNewsSubscriptionSubmit = null;
        `,
        returnExpression: `({
            getNewsSubscriptionModelConfig,
            handleNewsSubscriptionSubmit,
            pollForNewsResearchResults,
        })`,
    });

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

function renderSubscriptionModal() {
    document.body.innerHTML = `
        <div id="newsSubscriptionModal"></div>
        <form id="news-subscription-form">
            <textarea id="news-subscription-query">FastAPI migration status</textarea>
            <input id="news-subscription-name" value="Migration watch">
            <select id="news-subscription-frequency">
                <option value="4" selected>Every 4 hours</option>
            </select>
            <select id="news-subscription-folder">
                <option value="folder-3299" selected>Migration</option>
            </select>
            <input id="news-subscription-active" type="checkbox" checked>
            <input id="news-subscription-run-now" type="checkbox" checked>
            <input id="news-subscription-model" value="OLLAMA:llama3">
            <select id="news-subscription-strategy">
                <option value="source-based" selected>Source based</option>
            </select>
            <button id="news-subscription-submit" type="submit">
                Create Subscription
            </button>
        </form>
        <section id="news-feed-content">
            <article id="existing-news">Existing news</article>
        </section>
    `;
}

beforeEach(() => {
    vi.useFakeTimers();
    renderSubscriptionModal();
    localStorage.clear();
});

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

it('creates, runs, polls, and completes a subscription using the returned research ID', async () => {
    const statuses = [
        { status: 'in_progress', progress: 42 },
        { status: 'completed', progress: 100 },
    ];
    const fetchMock = vi.fn((url) => {
        if (url === '/news/api/subscribe') {
            return Promise.resolve(response({
                status: 'success',
                subscription_id: 'subscription-3299',
            }));
        }
        if (url === '/news/api/subscriptions/subscription-3299/run') {
            return Promise.resolve(response({
                status: 'success',
                research_id: 'research-3299',
            }));
        }
        if (url === '/api/research/research-3299/status') {
            return Promise.resolve(response(statuses.shift()));
        }
        throw new Error(`Unexpected fetch: ${url}`);
    });
    vi.stubGlobal('fetch', fetchMock);
    const runtime = compileSubscriptionRuntime();
    const event = { preventDefault: vi.fn() };

    await runtime.handleNewsSubscriptionSubmit(event);

    expect(event.preventDefault).toHaveBeenCalledOnce();
    expect(fetchMock).toHaveBeenNthCalledWith(1, '/news/api/subscribe', {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
            'X-CSRFToken': 'csrf-news-subscription',
        },
        body: JSON.stringify({
            query: 'FastAPI migration status',
            name: 'Migration watch',
            subscription_type: 'search',
            refresh_minutes: 240,
            folder_id: 'folder-3299',
            is_active: true,
            model_provider: 'OLLAMA',
            model: 'llama3',
            search_strategy: 'source-based',
        }),
    });
    expect(fetchMock).toHaveBeenNthCalledWith(
        2,
        '/news/api/subscriptions/subscription-3299/run',
        {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
                'X-CSRFToken': 'csrf-news-subscription',
            },
        },
    );
    expect(runtime.hideModal).toHaveBeenCalledOnce();
    expect(runtime.loadSubscriptions).toHaveBeenCalledOnce();
    expect(document.querySelector('.ldr-active-research-card').textContent)
        .toContain('research-3299');
    expect(JSON.parse(localStorage.getItem('active_news_research')))
        .toMatchObject({
            researchId: 'research-3299',
            query: 'FastAPI migration status',
        });

    await vi.advanceTimersByTimeAsync(5000);

    expect(fetchMock).toHaveBeenNthCalledWith(
        3,
        '/api/research/research-3299/status',
    );
    const progress = document.querySelector('[role="progressbar"]');
    expect(progress.getAttribute('aria-valuenow')).toBe('42');
    expect(progress.firstElementChild.style.width).toBe('42%');

    await vi.advanceTimersByTimeAsync(5000);

    expect(fetchMock).toHaveBeenNthCalledWith(
        4,
        '/api/research/research-3299/status',
    );
    expect(localStorage.getItem('active_news_research')).toBeNull();
    expect(document.querySelector('.ldr-active-research-card')).toBeNull();
    expect(runtime.showNewsAlert).toHaveBeenCalledWith(
        'News analysis completed! Loading results...',
        'success',
    );

    await vi.advanceTimersByTimeAsync(1000);

    expect(runtime.loadNewsFeed).toHaveBeenCalledOnce();
});

// Create Subscription used to split on the first colon for the provider but
// save the whole selection as the model, so OLLAMA:llama3.1:8b was saved as
// model "OLLAMA:llama3.1:8b" and an unprefixed llama3.1:8b got provider
// "llama3.1" (#7860). It now goes through the parser Run Once uses.
it.each([
    ['OLLAMA:llama3.1:8b', 'OLLAMA', 'llama3.1:8b'],
    // A typed model tag is not split, even when its first segment is a
    // provider key in another case (qwen:7b is an Ollama tag, not QWEN).
    ['ollama:llama3.1:8b', '', 'ollama:llama3.1:8b'],
    ['qwen:7b', '', 'qwen:7b'],
    ['OPENAI:gpt-4.1-mini', 'OPENAI', 'gpt-4.1-mini'],
    ['OLLAMA:OPENAI:model:tag', 'OLLAMA', 'OPENAI:model:tag'],
    ['llama3.1:8b', '', 'llama3.1:8b'],
    ['custom:model:tag', '', 'custom:model:tag'],
    ['gpt-4.1-mini', '', 'gpt-4.1-mini'],
    ['', '', ''],
])('saves selection %j with the provider split Run Once uses', async (
    selected,
    provider,
    model,
) => {
    document.getElementById('news-subscription-model').value = selected;
    document.getElementById('news-subscription-run-now').checked = false;
    const fetchMock = vi.fn().mockResolvedValue(response({
        status: 'success',
        subscription_id: 'subscription-model-split',
    }));
    vi.stubGlobal('fetch', fetchMock);
    const runtime = compileSubscriptionRuntime();

    // The shared parser: only an uppercase provider key (the form the
    // dropdown's options use) is split off the free-text model field.
    expect(runtime.getNewsSubscriptionModelConfig())
        .toEqual(selected ? { provider, model } : null);

    await runtime.handleNewsSubscriptionSubmit({ preventDefault: vi.fn() });

    expect(fetchMock).toHaveBeenCalledOnce();
    const [url, options] = fetchMock.mock.calls[0];
    expect(url).toBe('/news/api/subscribe');
    const payload = JSON.parse(options.body);
    expect(payload.model_provider).toBe(provider);
    expect(payload.model).toBe(model);
    expect(runtime.loadSubscriptions).toHaveBeenCalledOnce();
});

it('gates a second submit until the owned create-and-run workflow settles', async () => {
    const pendingCreate = deferred();
    const fetchMock = vi.fn()
        .mockImplementationOnce(() => pendingCreate.promise)
        .mockResolvedValueOnce(response({
            status: 'success',
            research_id: 'research-single-owner',
        }));
    vi.stubGlobal('fetch', fetchMock);
    const runtime = compileSubscriptionRuntime();
    const firstEvent = { preventDefault: vi.fn() };
    const secondEvent = { preventDefault: vi.fn() };

    const firstSubmit = runtime.handleNewsSubscriptionSubmit(firstEvent);
    const secondSubmit = runtime.handleNewsSubscriptionSubmit(secondEvent);

    expect(firstEvent.preventDefault).toHaveBeenCalledOnce();
    expect(secondEvent.preventDefault).toHaveBeenCalledOnce();
    expect(fetchMock).toHaveBeenCalledOnce();
    expect(fetchMock).toHaveBeenCalledWith(
        '/news/api/subscribe',
        expect.objectContaining({ method: 'POST' }),
    );
    expect(document.getElementById('news-subscription-submit').disabled)
        .toBe(true);

    await secondSubmit;
    expect(document.getElementById('news-subscription-submit').disabled)
        .toBe(true);

    pendingCreate.resolve(response({
        status: 'success',
        subscription_id: 'subscription-single-owner',
    }));
    await firstSubmit;

    expect(fetchMock).toHaveBeenCalledTimes(2);
    expect(fetchMock).toHaveBeenNthCalledWith(
        2,
        '/news/api/subscriptions/subscription-single-owner/run',
        expect.objectContaining({ method: 'POST' }),
    );
    expect(document.querySelectorAll('.ldr-active-research-card'))
        .toHaveLength(1);
    expect(runtime.hideModal).toHaveBeenCalledOnce();
    expect(runtime.loadSubscriptions).toHaveBeenCalledOnce();
    expect(document.getElementById('news-subscription-submit').disabled)
        .toBe(false);
});

it.each([
    ['missing', undefined],
    ['blank', '   '],
])('warns and does not monitor when a successful run has a %s research ID', async (
    _label,
    researchId,
) => {
    const fetchMock = vi.fn()
        .mockResolvedValueOnce(response({
            status: 'success',
            subscription_id: 'subscription-invalid-run',
        }))
        .mockResolvedValueOnce(response({
            status: 'success',
            research_id: researchId,
        }));
    vi.stubGlobal('fetch', fetchMock);
    const runtime = compileSubscriptionRuntime();

    await runtime.handleNewsSubscriptionSubmit({ preventDefault: vi.fn() });
    await vi.advanceTimersByTimeAsync(15000);

    expect(fetchMock).toHaveBeenCalledTimes(2);
    expect(runtime.showNewsAlert).toHaveBeenCalledWith(
        'Subscription created, but its research run could not be started.',
        'warning',
    );
    expect(localStorage.getItem('active_news_research')).toBeNull();
    expect(document.querySelector('.ldr-active-research-card')).toBeNull();
    expect(runtime.loadNewsFeed).not.toHaveBeenCalled();
    expect(document.getElementById('news-subscription-submit').disabled)
        .toBe(false);
});

it('stops polling and restores the feed when research reaches a failed state', async () => {
    const fetchMock = vi.fn()
        .mockResolvedValueOnce(response({
            status: 'success',
            subscription_id: 'subscription-failed',
        }))
        .mockResolvedValueOnce(response({
            status: 'success',
            research_id: 'research-failed',
        }))
        .mockResolvedValueOnce(response({
            status: 'failed',
            progress: 57,
            metadata: { error: 'provider unavailable' },
        }));
    vi.stubGlobal('fetch', fetchMock);
    const runtime = compileSubscriptionRuntime();

    await runtime.handleNewsSubscriptionSubmit({ preventDefault: vi.fn() });
    await vi.advanceTimersByTimeAsync(5000);

    expect(fetchMock).toHaveBeenNthCalledWith(
        3,
        '/api/research/research-failed/status',
    );
    expect(localStorage.getItem('active_news_research')).toBeNull();
    expect(runtime.showNewsAlert).toHaveBeenCalledWith(
        'Research failed: provider unavailable',
        'error',
    );
    expect(runtime.loadNewsFeed).toHaveBeenCalledOnce();

    await vi.advanceTimersByTimeAsync(15000);
    expect(fetchMock).toHaveBeenCalledTimes(3);
});

it('reports an immediate-run failure without starting a status poll', async () => {
    const fetchMock = vi.fn()
        .mockResolvedValueOnce(response({
            status: 'success',
            subscription_id: 'subscription-no-run',
        }))
        .mockResolvedValueOnce(response(
            { error: 'research queue full' },
            false,
            409,
        ));
    vi.stubGlobal('fetch', fetchMock);
    const runtime = compileSubscriptionRuntime();

    await runtime.handleNewsSubscriptionSubmit({ preventDefault: vi.fn() });
    await vi.advanceTimersByTimeAsync(15000);

    expect(fetchMock).toHaveBeenCalledTimes(2);
    expect(localStorage.getItem('active_news_research')).toBeNull();
    expect(document.querySelector('.ldr-active-research-card')).toBeNull();
    expect(runtime.showNewsAlert).toHaveBeenCalledWith(
        'Subscription created, but its research run could not be started.',
        'warning',
    );
    expect(runtime.loadSubscriptions).toHaveBeenCalledOnce();
});

it('percent-encodes the returned subscription ID into one segment of the run path', async () => {
    const fetchMock = vi.fn()
        .mockResolvedValueOnce(response({
            status: 'success',
            subscription_id: '../../api/v1/settings?reset=1#top',
        }))
        .mockResolvedValueOnce(response(
            { error: 'research queue full' },
            false,
            409,
        ));
    vi.stubGlobal('fetch', fetchMock);
    const runtime = compileSubscriptionRuntime();

    await runtime.handleNewsSubscriptionSubmit({ preventDefault: vi.fn() });

    expect(fetchMock).toHaveBeenCalledTimes(2);
    const [runUrl, runOptions] = fetchMock.mock.calls[1];
    expect(runUrl).toBe(
        '/news/api/subscriptions/..%2F..%2Fapi%2Fv1%2Fsettings%3Freset%3D1%23top/run',
    );
    expect(runOptions.method).toBe('POST');
    // The ID can neither climb out of /news/api/subscriptions/ nor add a
    // query string or fragment.
    const parsed = new URL(runUrl, 'http://localhost');
    expect(parsed.pathname).toBe(runUrl);
    expect(parsed.search).toBe('');
    expect(parsed.hash).toBe('');
});

it('retires a superseded status request before it can contradict the newer run', async () => {
    const staleStatus = deferred();
    const fetchMock = vi.fn()
        .mockImplementationOnce(() => staleStatus.promise)
        .mockResolvedValueOnce(response({
            status: 'completed',
            progress: 100,
        }));
    vi.stubGlobal('fetch', fetchMock);
    const runtime = compileSubscriptionRuntime();
    const feed = document.getElementById('news-feed-content');
    const olderCard = document.createElement('article');
    olderCard.className = 'ldr-active-research-card';
    olderCard.dataset.researchId = 'research-old';
    feed.prepend(olderCard);

    runtime.pollForNewsResearchResults('research-old', 'Old run');
    vi.advanceTimersByTime(5000);
    await Promise.resolve();
    expect(fetchMock).toHaveBeenCalledOnce();

    const newerCard = document.createElement('article');
    newerCard.className = 'ldr-active-research-card';
    newerCard.dataset.researchId = 'research-new';
    feed.prepend(newerCard);
    runtime.pollForNewsResearchResults('research-new', 'New run');
    expect(document.querySelector('[data-research-id="research-old"]'))
        .toBeNull();
    expect(document.querySelector('[data-research-id="research-new"]'))
        .not.toBeNull();
    await vi.advanceTimersByTimeAsync(5000);

    expect(runtime.showNewsAlert).toHaveBeenCalledWith(
        'News analysis completed! Loading results...',
        'success',
    );
    expect(JSON.parse(localStorage.getItem('active_news_research') || 'null'))
        .toBeNull();
    expect(document.querySelector('.ldr-active-research-card')).toBeNull();

    staleStatus.resolve(response({
        status: 'failed',
        metadata: { error: 'stale provider failure' },
    }));
    await Promise.resolve();
    await Promise.resolve();
    await vi.advanceTimersByTimeAsync(1000);

    expect(runtime.showNewsAlert).toHaveBeenCalledTimes(1);
    expect(runtime.showNewsAlert).not.toHaveBeenCalledWith(
        'Research failed: stale provider failure',
        'error',
    );
    expect(runtime.loadNewsFeed).toHaveBeenCalledOnce();

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

it('cleans persisted and visible polling state after a status HTTP failure', async () => {
    const activeCard = document.createElement('article');
    activeCard.className = 'ldr-active-research-card';
    activeCard.dataset.researchId = 'research-http-failure';
    document.getElementById('news-feed-content').prepend(activeCard);

    const fetchMock = vi.fn().mockResolvedValue(response(
        { detail: 'status unavailable' },
        false,
        503,
    ));
    vi.stubGlobal('fetch', fetchMock);
    const runtime = compileSubscriptionRuntime();

    runtime.pollForNewsResearchResults(
        'research-http-failure',
        'Unavailable status',
    );
    await vi.advanceTimersByTimeAsync(5000);

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

    await vi.advanceTimersByTimeAsync(15000);
    expect(fetchMock).toHaveBeenCalledOnce();
});