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.

9.4 KB
Raw Blame
/** Runtime contracts for the news page's advanced research entry point. */

import { resolve } from 'node:path';

import '@js/security/xss-protection.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 = 200, statusText = '' } = {}) {
    return {
        ok,
        status,
        statusText,
        json: vi.fn().mockResolvedValue(payload),
    };
}

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

function compileAdvancedSearchRuntime() {
    const showNewsAlert = vi.fn();
    const pollForNewsResearchResults = vi.fn();
    const createSubscriptionFromSearch = vi.fn().mockResolvedValue(undefined);
    const safeLogger = {
        log: vi.fn(),
        error: vi.fn(),
    };
    const { performAdvancedNewsSearch } = compileTemplateHarness({
        templatePath: NEWS_SOURCE_PATH,
        functionNames: [
            'advancedNewsSearchKey',
            'performAdvancedNewsSearch',
            'executeAdvancedNewsSearch',
        ],
        dependencies: {
            URLS: { API: { START_RESEARCH: '/api/start_research' } },
            getCSRFToken: () => window.api.getCsrfToken(),
            showNewsAlert,
            SafeLogger: safeLogger,
            escapeHtml: window.escapeHtml,
            pollForNewsResearchResults,
            createSubscriptionFromSearch,
        },
        preamble: 'const activeAdvancedNewsSearches = new Map();',
        returnExpression: '({ performAdvancedNewsSearch })',
    });
    return {
        performAdvancedNewsSearch,
        showNewsAlert,
        pollForNewsResearchResults,
        createSubscriptionFromSearch,
        safeLogger,
    };
}

beforeEach(() => {
    document.body.innerHTML = '<section id="news-feed-content">Old feed</section>';
    window.RESEARCH_STATUS = { QUEUED: 'queued' };
    window.api = {
        getCsrfToken: vi.fn(() => 'csrf-advanced-news'),
        redirectToLogin: vi.fn(),
    };
    delete window.__newsXss;
});

afterEach(() => {
    vi.clearAllTimers();
    vi.useRealTimers();
    vi.restoreAllMocks();
    vi.unstubAllGlobals();
    document.body.replaceChildren();
    delete window.RESEARCH_STATUS;
    delete window.api;
    delete window.__newsXss;
});

it('starts advanced research with CSRF and hands an inert owned card to polling', async () => {
    const runtime = compileAdvancedSearchRuntime();
    const researchId = 'research-1" onmouseover="window.__newsXss=true';
    const query = '<img src=x onerror="window.__newsXss=true"> migration news';
    const fetchMock = vi.fn().mockResolvedValue(response({
        status: 'queued',
        research_id: researchId,
    }));
    vi.stubGlobal('fetch', fetchMock);

    await runtime.performAdvancedNewsSearch(query, 'source-based', {
        provider: 'openrouter',
        model: 'model/x',
        customEndpoint: 'https://models.example/v1',
        searchEngine: 'serper',
        iterations: 2,
        questions: 3,
    });

    expect(fetchMock).toHaveBeenCalledOnce();
    const [url, options] = fetchMock.mock.calls[0];
    expect(url).toBe('/api/start_research');
    expect(options).toMatchObject({
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
            'X-CSRFToken': 'csrf-advanced-news',
        },
    });
    expect(JSON.parse(options.body)).toEqual({
        query,
        mode: 'quick',
        strategy: 'source-based',
        metadata: {
            is_news_search: true,
            search_type: 'news_analysis',
            display_in: 'news_feed',
            triggered_by: 'test_run',
        },
        model_provider: 'openrouter',
        model: 'model/x',
        custom_endpoint: 'https://models.example/v1',
        search_engine: 'serper',
        iterations: 2,
        questions_per_iteration: 3,
    });

    const card = document.querySelector('.ldr-active-research-card');
    expect(card).not.toBeNull();
    expect(card.dataset.researchId).toBe(researchId);
    expect(card.hasAttribute('onmouseover')).toBe(false);
    expect(card.querySelector('img')).toBeNull();
    expect(window.__newsXss).toBeUndefined();
    expect(runtime.pollForNewsResearchResults)
        .toHaveBeenCalledWith(researchId, query);
    expect(runtime.createSubscriptionFromSearch)
        .toHaveBeenCalledWith(query, researchId);
    expect(runtime.showNewsAlert).toHaveBeenLastCalledWith(
        'Analyzing news... Results will appear below when ready.',
        'info',
    );
});

it('renders a FastAPI detail error without starting polling or a subscription', async () => {
    const runtime = compileAdvancedSearchRuntime();
    vi.stubGlobal('fetch', vi.fn().mockResolvedValue(response(
        { detail: 'Search strategy is invalid' },
        { ok: false, status: 422, statusText: 'Unprocessable Entity' },
    )));

    await runtime.performAdvancedNewsSearch('migration news');

    expect(runtime.showNewsAlert).toHaveBeenLastCalledWith(
        'Search strategy is invalid',
        'error',
    );
    expect(runtime.pollForNewsResearchResults).not.toHaveBeenCalled();
    expect(runtime.createSubscriptionFromSearch).not.toHaveBeenCalled();
    expect(document.getElementById('news-feed-content').textContent)
        .toBe('Old feed');
});

it('uses the shared login redirect after an unauthorized advanced search', async () => {
    vi.useFakeTimers();
    const runtime = compileAdvancedSearchRuntime();
    vi.stubGlobal('fetch', vi.fn().mockResolvedValue(response(
        { detail: 'Not authenticated' },
        { ok: false, status: 401, statusText: 'Unauthorized' },
    )));

    await runtime.performAdvancedNewsSearch('private migration news');

    expect(runtime.showNewsAlert).toHaveBeenLastCalledWith(
        'Authentication required. Please log in to perform research.',
        'error',
    );
    expect(window.api.redirectToLogin).not.toHaveBeenCalled();
    await vi.advanceTimersByTimeAsync(2000);
    expect(window.api.redirectToLogin).toHaveBeenCalledOnce();
    expect(runtime.pollForNewsResearchResults).not.toHaveBeenCalled();
    expect(runtime.createSubscriptionFromSearch).not.toHaveBeenCalled();
});

it('keeps duplicate callers on the active request and releases after an unreadable error body', async () => {
    const errorBody = deferred();
    const runtime = compileAdvancedSearchRuntime();
    const fetchMock = vi.fn()
        .mockResolvedValueOnce({
            ok: false,
            status: 422,
            statusText: 'Unprocessable Entity',
            json: vi.fn(() => errorBody.promise),
        })
        .mockResolvedValueOnce(response({
            status: 'queued',
            research_id: 'retry-news-3299',
        }));
    vi.stubGlobal('fetch', fetchMock);

    const firstSearch = runtime.performAdvancedNewsSearch('first migration search');
    const duplicateSearch = runtime.performAdvancedNewsSearch('first migration search');
    let duplicateSettled = false;
    duplicateSearch.then(() => {
        duplicateSettled = true;
    });

    await vi.waitFor(() => expect(fetchMock).toHaveBeenCalledOnce());
    await Promise.resolve();
    expect(duplicateSettled).toBe(false);

    errorBody.reject(new Error('response body was not JSON'));
    await Promise.all([firstSearch, duplicateSearch]);
    expect(runtime.showNewsAlert).toHaveBeenLastCalledWith(
        'Error starting research: Unprocessable Entity',
        'error',
    );

    await runtime.performAdvancedNewsSearch('retry migration search');

    expect(fetchMock).toHaveBeenCalledTimes(2);
    expect(runtime.pollForNewsResearchResults).toHaveBeenCalledOnce();
    expect(runtime.pollForNewsResearchResults).toHaveBeenCalledWith(
        'retry-news-3299',
        'retry migration search',
    );
});

it('keys active searches canonically while preserving distinct invocations', async () => {
    const requests = [];
    const runtime = compileAdvancedSearchRuntime();
    const fetchMock = vi.fn(() => {
        const request = deferred();
        requests.push(request);
        return request.promise;
    });
    vi.stubGlobal('fetch', fetchMock);

    const sharedConfig = {
        provider: 'openai',
        model: 'embedding-news-v1',
        iterations: 2,
    };
    const sameConfigDifferentOrder = {
        iterations: 2,
        model: 'embedding-news-v1',
        provider: 'openai',
    };
    const searches = [
        runtime.performAdvancedNewsSearch('shared query', 'source-based', sharedConfig),
        runtime.performAdvancedNewsSearch(
            'shared query',
            'source-based',
            sameConfigDifferentOrder,
        ),
        runtime.performAdvancedNewsSearch('different query', 'source-based', sharedConfig),
        runtime.performAdvancedNewsSearch('shared query', 'focused', sharedConfig),
        runtime.performAdvancedNewsSearch('shared query', 'source-based', {
            ...sharedConfig,
            model: 'embedding-news-v2',
        }),
    ];

    expect(fetchMock).toHaveBeenCalledTimes(4);
    for (const request of requests) {
        request.resolve(response(
            { detail: 'End the concurrency fixture' },
            { ok: false, status: 422, statusText: 'Unprocessable Entity' },
        ));
    }
    await Promise.all(searches);
});