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.

3.5 KB
Raw Blame
/** Direct bootstrap coverage for the migrated unified-search page. */

let documentListeners;

function installDom() {
    document.body.innerHTML = `
        <input id="ldr-unified-search-input">
        <section id="ldr-unified-search-results"></section>
        <section id="ldr-unified-search-empty"></section>
        <p id="ldr-unified-search-notice"></p>
        <button id="unified-search-mode-btn"><i></i></button>
        <span id="unified-search-mode-label"></span>
        <ul id="unified-search-mode-menu"></ul>
    `;
}

beforeEach(() => {
    vi.resetModules();
    vi.useFakeTimers();
    installDom();
    documentListeners = [];
    const addDocumentListener = document.addEventListener.bind(document);
    vi.spyOn(document, 'addEventListener').mockImplementation(
        (type, listener, options) => {
            documentListeners.push([type, listener, options]);
            addDocumentListener(type, listener, options);
        },
    );
    delete window.__VITEST_TEST__;
    window.SemanticSearch = {
        MIN_QUERY_LENGTH: 2,
        MIN_SIMILARITY: 0.35,
        buildTieredResults: vi.fn(),
        flattenTieredResults: vi.fn(),
    };
    window.formatting = { similarityBadge: vi.fn(() => '') };
    vi.stubGlobal('escapeHtml', value => String(value ?? '')
        .replaceAll('&', '&amp;')
        .replaceAll('<', '&lt;')
        .replaceAll('>', '&gt;')
        .replaceAll('"', '&quot;'));
});

afterEach(() => {
    for (const [type, listener, options] of documentListeners) {
        document.removeEventListener(type, listener, options);
    }
    documentListeners = [];
    vi.clearAllTimers();
    vi.useRealTimers();
    vi.restoreAllMocks();
    vi.unstubAllGlobals();
    delete window.SemanticSearch;
    delete window.formatting;
    document.body.replaceChildren();
});

it('wires the real page controls and renders a debounced text result', async () => {
    const safeFetchWithAuth = vi.fn().mockResolvedValue({
        json: vi.fn().mockResolvedValue({
            success: true,
            results: [{
                id: 'bootstrap-result',
                title: 'Unified migration result',
                content_preview: 'Found through the wired page input',
                url: '/results/bootstrap-result',
                source_type: 'research_report',
            }],
        }),
    });
    vi.stubGlobal('safeFetchWithAuth', safeFetchWithAuth);

    await import('@js/pages/unified_search.js');
    document.dispatchEvent(new Event('DOMContentLoaded'));

    expect(document.querySelectorAll('#unified-search-mode-menu [data-mode]'))
        .toHaveLength(3);
    expect(document.getElementById('unified-search-mode-label').textContent)
        .toBe('AI Hybrid');
    expect(document.getElementById('ldr-unified-search-input').placeholder)
        .toContain('AI Hybrid');

    document.querySelector('[data-mode="text"]').click();
    const input = document.getElementById('ldr-unified-search-input');
    input.value = 'migration coverage';
    input.dispatchEvent(new Event('input', { bubbles: true }));
    await vi.advanceTimersByTimeAsync(300);

    await vi.waitFor(() => expect(safeFetchWithAuth).toHaveBeenCalledOnce());
    expect(safeFetchWithAuth.mock.calls[0][0])
        .toBe('/library/search/api/keyword?q=migration+coverage&limit=20');
    await vi.waitFor(() => {
        expect(document.getElementById('ldr-unified-search-results').textContent)
            .toContain('Unified migration result');
    });
    expect(document.querySelector('.ldr-unified-result-card').getAttribute('href'))
        .toBe('/results/bootstrap-result');
});