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.

8.8 KB
Raw Blame
/**
 * Runtime contracts for the news page's migrated collection-search consumers.
 *
 * The checked-in functions are executed with the production URL builder and
 * semantic merge helper. This pins the FastAPI request, result filtering,
 * tier ordering, and in-flight request ownership without copying the browser
 * implementation into the fixture.
 */

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) {
    return {
        ok,
        json: vi.fn().mockResolvedValue(payload),
    };
}

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

function compileNewsSearchRuntime({
    items,
    collectionId = 'news-collection-3299',
    csrfToken = 'csrf-news-search',
    renderNewsItems = vi.fn(),
    showNewsAlert = vi.fn(),
} = {}) {
    const newsCardConfig = {
        getId: result => result.research_id || '',
        getTitle: result => result.research_title || 'Untitled',
        getUrl: () => '#',
        getBadges: () => [{ icon: 'newspaper', label: 'News' }],
        getDate: result => result.research_created_at,
        getSubtitle: () => null,
    };

    return compileTemplateHarness({
        templatePath: NEWS_SOURCE_PATH,
        functionNames: ['runNewsSemanticSearch', 'runNewsHybridSearch'],
        dependencies: {
            initialItems: items || [],
            initialCollectionId: collectionId,
            csrfToken,
            renderNewsItems,
            showNewsAlert,
            NEWS_CARD_CONFIG: newsCardConfig,
            URLBuilder: window.URLBuilder,
            URLS: window.URLS,
        },
        preamble: `
            const NEWS_SM = {
                HYBRID: 'hybrid',
                TEXT: 'text',
                SEMANTIC: 'semantic',
            };
            let newsSearchMode = NEWS_SM.HYBRID;
            let newsCollectionId = initialCollectionId;
            let newsSearchId = 0;
            let newsItems = initialItems;
            let newsSemanticMatches = null;
            const getCSRFToken = () => csrfToken;
        `,
        returnExpression: `({
            runNewsSemanticSearch,
            runNewsHybridSearch,
            setMode: value => { newsSearchMode = value; },
            getItems: () => newsItems,
            getMatches: () => newsSemanticMatches,
        })`,
    });
}

function renderSearchSurface() {
    document.body.innerHTML = `
        <section id="news-feed-content"></section>
        <section id="news-semantic-results" style="display: none"></section>
    `;
}

beforeEach(() => {
    renderSearchSurface();
});

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

it('posts semantic searches with CSRF and renders only current news results', async () => {
    const fetchMock = vi.fn().mockResolvedValue(response({
        success: true,
        results: [
            {
                research_id: 'news-1',
                research_title: 'Migration match',
                similarity: 94,
            },
            {
                research_id: 3299,
                research_title: 'Numeric news ID',
                similarity: 87,
            },
            {
                research_id: 'unrelated-research',
                research_title: 'Not in the news feed',
                similarity: 99,
            },
            { research_title: 'Missing research ID', similarity: 100 },
        ],
    }));
    vi.stubGlobal('fetch', fetchMock);
    const runtime = compileNewsSearchRuntime({
        items: [
            { id: 'card-1', research_id: 'news-1' },
            { id: 'card-2', research_id: 3299 },
        ],
    });
    runtime.setMode('semantic');

    await runtime.runNewsSemanticSearch('FastAPI migration');

    expect(fetchMock).toHaveBeenCalledOnce();
    expect(fetchMock).toHaveBeenCalledWith(
        '/library/api/collections/news-collection-3299/search',
        {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
                'X-CSRFToken': 'csrf-news-search',
            },
            body: JSON.stringify({
                query: 'FastAPI migration',
                limit: 50,
            }),
        },
    );
    expect(document.getElementById('news-feed-content').style.display)
        .toBe('none');
    const cards = document.querySelectorAll(
        '#news-semantic-results .ldr-semantic-result',
    );
    expect(Array.from(cards, card => card.dataset.id))
        .toEqual(['news-1', '3299']);
    expect(document.body.textContent).not.toContain('Not in the news feed');
    expect(document.body.textContent).not.toContain('Missing research ID');
});

it('does not let a rejected stale semantic request replace newer results', async () => {
    const olderRequest = deferred();
    const fetchMock = vi.fn()
        .mockImplementationOnce(() => olderRequest.promise)
        .mockResolvedValueOnce(response({
            success: true,
            results: [{
                research_id: 'news-new',
                research_title: 'Newest result',
                similarity: 91,
            }],
        }));
    vi.stubGlobal('fetch', fetchMock);
    const runtime = compileNewsSearchRuntime({
        items: [{ id: 'new-card', research_id: 'news-new' }],
    });
    runtime.setMode('semantic');

    const olderSearch = runtime.runNewsSemanticSearch('older query');
    await runtime.runNewsSemanticSearch('newer query');
    olderRequest.reject(new Error('older request lost its connection'));
    await olderSearch;

    const container = document.getElementById('news-semantic-results');
    expect(container.textContent).toContain('Newest result');
    expect(container.textContent).not.toContain('Error performing semantic search');
});

it('filters hybrid matches and applies the real tier-one/tier-two ordering', async () => {
    const firstItem = { id: 'card-a', research_id: 'news-a' };
    const secondItem = { id: 'card-b', research_id: 'news-b' };
    const renderNewsItems = vi.fn();
    const fetchMock = vi.fn().mockResolvedValue(response({
        success: true,
        results: [
            {
                research_id: 'news-b',
                similarity: 92,
                snippet: 'Semantic match',
            },
            {
                research_id: 'not-a-current-news-item',
                similarity: 99,
                snippet: 'Must be filtered',
            },
        ],
    }));
    vi.stubGlobal('fetch', fetchMock);
    const runtime = compileNewsSearchRuntime({
        items: [firstItem, secondItem],
        renderNewsItems,
    });

    await runtime.runNewsHybridSearch('ownership races');

    expect(fetchMock).toHaveBeenCalledWith(
        '/library/api/collections/news-collection-3299/search',
        expect.objectContaining({
            method: 'POST',
            body: JSON.stringify({ query: 'ownership races', limit: 50 }),
        }),
    );
    expect(runtime.getItems()).toEqual([secondItem, firstItem]);
    expect(Array.from(runtime.getMatches().entries())).toEqual([
        ['news-b', { similarity: 92, snippet: 'Semantic match' }],
    ]);
    expect(renderNewsItems).toHaveBeenCalledOnce();
    expect(renderNewsItems).toHaveBeenCalledWith('ownership races');
    expect(document.getElementById('news-hybrid-loading')).toBeNull();
    expect(document.querySelector('.ldr-hybrid-divider')).toBeNull();
});

it('keeps the newer hybrid loading state when an older request rejects', async () => {
    const olderRequest = deferred();
    const newerRequest = deferred();
    const fetchMock = vi.fn()
        .mockImplementationOnce(() => olderRequest.promise)
        .mockImplementationOnce(() => newerRequest.promise);
    vi.stubGlobal('fetch', fetchMock);
    const runtime = compileNewsSearchRuntime({
        items: [{ id: 'card-a', research_id: 'news-a' }],
    });

    const olderSearch = runtime.runNewsHybridSearch('older query');
    const newerSearch = runtime.runNewsHybridSearch('newer query');
    olderRequest.reject(new Error('stale request failed'));
    await olderSearch;

    expect(document.getElementById('news-hybrid-loading')).not.toBeNull();

    newerRequest.resolve(response({
        success: true,
        results: [{ research_id: 'news-a', similarity: 88 }],
    }));
    await newerSearch;

    expect(runtime.getMatches().get('news-a')).toEqual({
        similarity: 88,
        snippet: '',
    });
    expect(document.getElementById('news-hybrid-loading')).toBeNull();
});