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.

11.3 KB
Raw Blame
/** Runtime ownership coverage for subscription-driven news feed loads. */

import { resolve } from 'node:path';

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

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

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

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

function compileFeedRuntime() {
    const clearSemanticState = vi.fn();
    const renderNewsItems = vi.fn();
    const extractTrendingTopics = vi.fn();
    const updateBulkActionsBar = vi.fn();
    const loadVotesForNewsItems = vi.fn().mockResolvedValue(undefined);
    const showNewsAlert = vi.fn();

    const runtime = compileTemplateHarness({
        templatePath: NEWS_SOURCE_PATH,
        functionNames: [
            'stopNewsResearchPoll',
            'beginNewsResearchPoll',
            'isCurrentNewsResearchPoll',
            'clearStoredNewsResearch',
            'findNewsResearchCard',
            'removeNewsResearchCards',
            'endNewsResearchPollWithFeed',
            'completeNewsResearchPoll',
            'selectSubscription',
            'loadNewsFeed',
        ],
        dependencies: {
            clearSemanticState,
            showNewsAlert,
            safeRenderHTML: (container, content) => {
                container.textContent = content;
            },
            updateFeedHeader: vi.fn(),
            loadSavedNewsFeed: vi.fn(),
            renderNewsItems,
            extractTrendingTopics,
            updateBulkActionsBar,
            loadVotesForNewsItems,
            SafeLogger: {
                log: vi.fn(),
                error: vi.fn(),
            },
        },
        preamble: `
            let activeSubscription = 'all';
            let newsItems = [];
            let newsSemanticMatches = null;
            let newsFeedRequestId = 0;
            let newsFeedRequestIntent = 'generic';
            let newsResearchPollId = 0;
            let activeNewsResearchPoll = null;
            let newsResearchReloadTimer = null;
            let newsResearchRestoreId = 0;
        `,
        returnExpression: `({
            selectSubscription,
            loadNewsFeed,
            beginNewsResearchPoll,
            endNewsResearchPollWithFeed,
            completeNewsResearchPoll,
            getActiveSubscription: () => activeSubscription,
            getNewsItems: () => newsItems,
        })`,
    });

    return {
        ...runtime,
        clearSemanticState,
        renderNewsItems,
        extractTrendingTopics,
        updateBulkActionsBar,
        loadVotesForNewsItems,
        showNewsAlert,
    };
}

beforeEach(() => {
    document.body.innerHTML = '<section id="news-feed-content"></section>';
});

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

it('keeps a slow older subscription feed from replacing the newer selection', async () => {
    const olderFetch = deferred();
    const olderResponse = response({
        news_items: [{ id: 'old-news', headline: 'Old subscription' }],
    });
    const newerResponse = response({
        news_items: [{ id: 'new-news', headline: 'New subscription' }],
    });
    const fetchMock = vi.fn()
        .mockImplementationOnce(() => olderFetch.promise)
        .mockResolvedValueOnce(newerResponse);
    vi.stubGlobal('fetch', fetchMock);
    const runtime = compileFeedRuntime();

    const olderLoad = runtime.selectSubscription('subscription-old');
    const newerLoad = runtime.selectSubscription('subscription-new');
    await newerLoad;

    olderFetch.resolve(olderResponse);
    await olderLoad;

    expect(fetchMock.mock.calls.map(([url]) => url)).toEqual([
        '/news/api/feed?limit=20&use_cache=true&subscription_id=subscription-old',
        '/news/api/feed?limit=20&use_cache=true&subscription_id=subscription-new',
    ]);
    expect(runtime.getActiveSubscription()).toBe('subscription-new');
    expect(runtime.getNewsItems()).toEqual([
        { id: 'new-news', headline: 'New subscription' },
    ]);
    expect(newerResponse.json).toHaveBeenCalledOnce();
    expect(olderResponse.json).not.toHaveBeenCalled();
    expect(runtime.renderNewsItems).toHaveBeenCalledOnce();
    expect(runtime.extractTrendingTopics).toHaveBeenCalledOnce();
    expect(runtime.updateBulkActionsBar).toHaveBeenCalledOnce();
    expect(runtime.loadVotesForNewsItems).toHaveBeenCalledOnce();
    expect(runtime.clearSemanticState).toHaveBeenCalledTimes(2);
});

it('drops an older payload that finishes JSON parsing after the newer feed', async () => {
    const olderJson = deferred();
    const olderResponse = {
        ok: true,
        status: 200,
        json: vi.fn(() => olderJson.promise),
    };
    const newerResponse = response({
        news_items: [{ id: 'newest-news', headline: 'Newest selection' }],
    });
    const fetchMock = vi.fn()
        .mockResolvedValueOnce(olderResponse)
        .mockResolvedValueOnce(newerResponse);
    vi.stubGlobal('fetch', fetchMock);
    const runtime = compileFeedRuntime();

    const olderLoad = runtime.selectSubscription('subscription-parsing');
    await vi.waitFor(() => expect(olderResponse.json).toHaveBeenCalledOnce());

    await runtime.selectSubscription('subscription-newest');
    olderJson.resolve({
        news_items: [{ id: 'parsed-too-late', headline: 'Stale payload' }],
    });
    await olderLoad;

    expect(runtime.getNewsItems()).toEqual([
        { id: 'newest-news', headline: 'Newest selection' },
    ]);
    expect(runtime.renderNewsItems).toHaveBeenCalledOnce();
    expect(runtime.loadVotesForNewsItems).toHaveBeenCalledOnce();
});

it('does not let a delayed completion reload supersede a newer focused feed', async () => {
    vi.useFakeTimers();
    const fetchMock = vi.fn().mockResolvedValue(response({
        news_items: [{ id: 'focused', headline: 'Migration focused result' }],
    }));
    vi.stubGlobal('fetch', fetchMock);
    const runtime = compileFeedRuntime();

    const poll = runtime.beginNewsResearchPoll('completed-run');
    runtime.completeNewsResearchPoll(poll, 'Completed');
    await runtime.loadNewsFeed('migration');
    await vi.advanceTimersByTimeAsync(1000);

    expect(fetchMock).toHaveBeenCalledOnce();
    expect(fetchMock).toHaveBeenCalledWith(
        '/news/api/feed?limit=20&use_cache=true&focus=migration',
    );
    expect(runtime.getNewsItems()).toEqual([
        { id: 'focused', headline: 'Migration focused result' },
    ]);
    expect(runtime.showNewsAlert).toHaveBeenCalledWith('Completed', 'success');
    vi.useRealTimers();
});

it.each(['completion', 'failure'])(
    'preserves a focused feed started before poll %s cleanup',
    async mode => {
        vi.useFakeTimers();
        const focusedResponse = deferred();
        const fetchMock = vi.fn(() => focusedResponse.promise);
        vi.stubGlobal('fetch', fetchMock);
        const runtime = compileFeedRuntime();

        const poll = runtime.beginNewsResearchPoll(`terminal-${mode}`);
        const focusedLoad = runtime.loadNewsFeed('migration');
        if (mode === 'completion') {
            runtime.completeNewsResearchPoll(poll, 'Completed');
            await vi.advanceTimersByTimeAsync(1000);
        } else {
            runtime.endNewsResearchPollWithFeed(poll, 'Failed');
        }

        expect(fetchMock).toHaveBeenCalledOnce();
        focusedResponse.resolve(response({
            news_items: [{
                id: `focused-${mode}`,
                headline: 'Migration focus survives',
            }],
        }));
        await focusedLoad;

        expect(runtime.getNewsItems()).toEqual([{
            id: `focused-${mode}`,
            headline: 'Migration focus survives',
        }]);
        expect(runtime.renderNewsItems).toHaveBeenCalledOnce();
    },
);

it.each(['completion', 'failure'])(
    'preserves a subscription feed started before poll %s cleanup',
    async mode => {
        vi.useFakeTimers();
        const subscriptionResponse = deferred();
        const fetchMock = vi.fn(() => subscriptionResponse.promise);
        vi.stubGlobal('fetch', fetchMock);
        const runtime = compileFeedRuntime();

        const poll = runtime.beginNewsResearchPoll(`subscription-${mode}`);
        const subscriptionLoad = runtime.selectSubscription('migration-news');
        if (mode === 'completion') {
            runtime.completeNewsResearchPoll(poll, 'Completed');
            await vi.advanceTimersByTimeAsync(1000);
        } else {
            runtime.endNewsResearchPollWithFeed(poll, 'Failed');
        }

        expect(fetchMock).toHaveBeenCalledOnce();
        subscriptionResponse.resolve(response({
            news_items: [{
                id: `subscription-${mode}`,
                headline: 'Selected subscription survives',
            }],
        }));
        await subscriptionLoad;

        expect(fetchMock).toHaveBeenCalledWith(
            '/news/api/feed?limit=20&use_cache=true&subscription_id=migration-news',
        );
        expect(runtime.getNewsItems()).toEqual([{
            id: `subscription-${mode}`,
            headline: 'Selected subscription survives',
        }]);
        expect(runtime.renderNewsItems).toHaveBeenCalledOnce();
    },
);

it.each(['completion', 'failure'])(
    'refreshes a generic feed that started before poll %s cleanup',
    async mode => {
        vi.useFakeTimers();
        const preTerminalResponse = deferred();
        const fetchMock = vi.fn()
            .mockImplementationOnce(() => preTerminalResponse.promise)
            .mockResolvedValueOnce(response({
                news_items: [{
                    id: `post-terminal-${mode}`,
                    headline: 'Post-terminal result',
                }],
            }));
        vi.stubGlobal('fetch', fetchMock);
        const runtime = compileFeedRuntime();

        const poll = runtime.beginNewsResearchPoll(`generic-${mode}`);
        const preTerminalLoad = runtime.loadNewsFeed();
        if (mode === 'completion') {
            runtime.completeNewsResearchPoll(poll, 'Completed');
            await vi.advanceTimersByTimeAsync(1000);
        } else {
            runtime.endNewsResearchPollWithFeed(poll, 'Failed');
        }

        await vi.waitFor(() => expect(fetchMock).toHaveBeenCalledTimes(2));
        await vi.waitFor(() => {
            expect(runtime.getNewsItems()).toEqual([{
                id: `post-terminal-${mode}`,
                headline: 'Post-terminal result',
            }]);
        });

        const staleResponse = response({
            news_items: [{ id: 'pre-terminal', headline: 'Stale result' }],
        });
        preTerminalResponse.resolve(staleResponse);
        await preTerminalLoad;

        expect(fetchMock.mock.calls.map(([url]) => url)).toEqual([
            '/news/api/feed?limit=20&use_cache=true',
            '/news/api/feed?limit=20&use_cache=true',
        ]);
        expect(staleResponse.json).not.toHaveBeenCalled();
        expect(runtime.getNewsItems()).toEqual([{
            id: `post-terminal-${mode}`,
            headline: 'Post-terminal result',
        }]);
        expect(runtime.renderNewsItems).toHaveBeenCalledOnce();
    },
);