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.

18.1 KB
Raw Blame
/**
 * Runtime coverage for the library page search controller.
 *
 * These tests drive the real mode menu and debounce path.  The lower-level
 * LibrarySearch module remains real as well, so assertions cover both the
 * migrated collection-search route and what the user sees after it returns.
 */

import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';

const originalWindowState = new Map([
    'LibrarySearch',
    'SemanticSearch',
    'URLBuilder',
    'URLS',
    'api',
    'safeUpdateButton',
].map(key => [key, {
    owned: Object.hasOwn(window, key),
    value: window[key],
}]));

function jsonResponse(body, status = 200) {
    return {
        ok: status >= 200 && status < 300,
        status,
        json: vi.fn().mockResolvedValue(body),
    };
}

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

function installFixture() {
    document.body.innerHTML = `
        <div id="library-search-notice" style="display: none"></div>
        <button id="search-mode-btn"></button>
        <div id="search-mode-menu">
            <button class="dropdown-item active" data-mode="hybrid">Hybrid</button>
            <button class="dropdown-item" data-mode="text">Text</button>
            <button class="dropdown-item" data-mode="semantic">Semantic</button>
        </div>
        <input id="search-documents">
        <select id="filter-collection">
            <option value="">All collections</option>
            <option value="collection-a">Collection A</option>
        </select>
        <select id="filter-domain">
            <option value="">All domains</option>
            <option value="example.com">example.com</option>
        </select>
        <select id="filter-research">
            <option value="">All research</option>
        </select>
        <select id="filter-date">
            <option value="">Any date</option>
        </select>
        <div id="documents-container">
            <article class="ldr-document-card" data-doc-id="existing-doc"
                     data-domain="example.com" data-research="research-a">
                <div class="card-header"></div>
                <div class="card-body">Existing climate document</div>
            </article>
        </div>
        <div id="semantic-results-container" style="display: none"></div>
    `;
}

function semanticCardFactory() {
    return vi.fn(result => {
        const card = document.createElement('article');
        card.className = 'ldr-semantic-result';
        card.dataset.docId = result.document_id;
        card.textContent = result.title || result.document_id;
        return card;
    });
}

async function installController({ collections, fetchImpl }) {
    const createSemanticResultCard = semanticCardFactory();
    window.SemanticSearch = {
        createSemanticResultCard,
        buildTieredResults: vi.fn(),
        renderSnippet: vi.fn(),
    };
    window.URLBuilder = {
        build: (template, id) => template.replace('{id}', id),
        documentPage: id => `/library/document/${id}`,
    };
    window.URLS = {
        LIBRARY_API: {
            COLLECTION_SEARCH: '/library/api/collections/{id}/search',
        },
    };
    window.api = { getCsrfToken: vi.fn(() => 'csrf-library-test') };
    window.safeUpdateButton = vi.fn();
    vi.stubGlobal('safeFetch', vi.fn(fetchImpl));

    await import('@js/components/library_search.js');
    window.LibrarySearch.initLibrarySearch(null, collections);
    await import('@js/components/library_search_ui.js');

    return {
        createSemanticResultCard,
        safeFetch: globalThis.safeFetch,
    };
}

function selectMode(mode) {
    document.querySelector(`[data-mode="${mode}"]`).click();
}

beforeEach(() => {
    vi.resetModules();
    vi.useFakeTimers();
    installFixture();
});

afterEach(() => {
    vi.useRealTimers();
    vi.restoreAllMocks();
    vi.unstubAllGlobals();
    document.body.replaceChildren();
    originalWindowState.forEach((state, key) => {
        if (state.owned) {
            window[key] = state.value;
        } else {
            delete window[key];
        }
    });
});

describe('library search UI runtime contracts', () => {
    it('posts a selected-collection semantic search to the migrated route and renders its result', async () => {
        const { createSemanticResultCard, safeFetch } = await installController({
            collections: [{ id: 'collection-a', indexed_document_count: 1 }],
            fetchImpl: () => Promise.resolve(jsonResponse({
                success: true,
                results: [{
                    document_id: 'semantic-doc',
                    title: 'Climate evidence',
                    similarity: 91,
                }],
            })),
        });

        document.getElementById('filter-collection').value = 'collection-a';
        document.getElementById('search-documents').value = 'climate';
        selectMode('semantic');
        await vi.advanceTimersByTimeAsync(500);

        expect(safeFetch).toHaveBeenCalledTimes(1);
        expect(safeFetch).toHaveBeenCalledWith(
            '/library/api/collections/collection-a/search',
            expect.objectContaining({
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                    'X-CSRFToken': 'csrf-library-test',
                },
                body: JSON.stringify({ query: 'climate', limit: 20 }),
            }),
        );
        expect(createSemanticResultCard).toHaveBeenCalledWith(
            expect.objectContaining({ document_id: 'semantic-doc' }),
            expect.any(Object),
            'climate',
        );
        expect(document.getElementById('semantic-results-container').textContent)
            .toContain('Climate evidence');
        expect(document.getElementById('documents-container').style.display).toBe('none');
    });

    it('fans out across indexed collections, deduplicates, and applies the active domain filter', async () => {
        const responseByCollection = {
            'collection-a': {
                success: true,
                results: [
                    { document_id: 'shared', title: 'Older match', similarity: 70, domain: 'example.com' },
                    { document_id: 'other-domain', title: 'Wrong domain', similarity: 99, domain: 'other.test' },
                ],
            },
            'collection-b': {
                success: true,
                results: [
                    { document_id: 'shared', title: 'Best match', similarity: 94, domain: 'example.com' },
                    { document_id: 'second', title: 'Second match', similarity: 80, domain: 'example.com' },
                ],
            },
        };
        const { createSemanticResultCard, safeFetch } = await installController({
            collections: [
                { id: 'collection-a', indexed_document_count: 2 },
                { id: 'collection-b', indexed_document_count: 2 },
                { id: 'not-indexed', indexed_document_count: 0 },
            ],
            fetchImpl: url => {
                const collectionId = String(url).split('/').at(-2);
                return Promise.resolve(jsonResponse(responseByCollection[collectionId]));
            },
        });

        document.getElementById('filter-domain').value = 'example.com';
        document.getElementById('search-documents').value = 'evidence';
        selectMode('semantic');
        await vi.advanceTimersByTimeAsync(500);

        expect(safeFetch.mock.calls.map(([url]) => url)).toEqual([
            '/library/api/collections/collection-a/search',
            '/library/api/collections/collection-b/search',
        ]);
        expect(createSemanticResultCard).toHaveBeenCalledTimes(2);
        expect(createSemanticResultCard.mock.calls.map(([result]) => [
            result.document_id,
            result.title,
        ])).toEqual([
            ['shared', 'Best match'],
            ['second', 'Second match'],
        ]);
        expect(document.getElementById('library-search-notice').textContent)
            .toContain('Searching across 2 indexed collections');
    });

    it('does not let a late semantic response overwrite the UI after switching back to text mode', async () => {
        let resolveSearch;
        const pendingSearch = new Promise(resolve => {
            resolveSearch = resolve;
        });
        const { createSemanticResultCard } = await installController({
            collections: [{ id: 'collection-a', indexed_document_count: 1 }],
            fetchImpl: () => pendingSearch,
        });

        document.getElementById('filter-collection').value = 'collection-a';
        document.getElementById('search-documents').value = 'climate';
        selectMode('semantic');
        await vi.advanceTimersByTimeAsync(500);

        selectMode('text');
        resolveSearch(jsonResponse({
            success: true,
            results: [{ document_id: 'late-result', title: 'Late result', similarity: 99 }],
        }));
        await vi.advanceTimersByTimeAsync(0);

        expect(createSemanticResultCard).not.toHaveBeenCalled();
        expect(document.getElementById('semantic-results-container').style.display).toBe('none');
        expect(document.getElementById('documents-container').style.display).toBe('grid');
        expect(document.getElementById('semantic-results-container').textContent)
            .not.toContain('Late result');
    });

    it('renders and then clears hybrid badges, snippets, and content-only results', async () => {
        const { createSemanticResultCard, safeFetch } = await installController({
            collections: [{ id: 'collection-a', indexed_document_count: 2 }],
            fetchImpl: () => Promise.resolve(jsonResponse({
                success: true,
                results: [
                    {
                        document_id: 'existing-doc',
                        title: 'Existing climate document',
                        similarity: 93,
                        snippet: 'Matched climate evidence',
                    },
                    {
                        document_id: 'content-only',
                        title: 'Content-only evidence',
                        similarity: 81,
                    },
                ],
            })),
        });
        window.SemanticSearch.renderSnippet.mockReturnValue(
            '<mark>Matched climate evidence</mark>',
        );
        window.SemanticSearch.buildTieredResults.mockImplementation(
            (textResults, semanticResults) => ({
                tier1: [{
                    historyItem: textResults[0],
                    semanticMatch: semanticResults[0],
                }],
                tier2: [],
                tier3: [{ semanticResult: semanticResults[1] }],
            }),
        );
        document.getElementById('filter-collection').value = 'collection-a';
        const input = document.getElementById('search-documents');
        input.value = 'climate';

        input.dispatchEvent(new Event('input'));
        await vi.advanceTimersByTimeAsync(750);

        expect(safeFetch).toHaveBeenCalledWith(
            '/library/api/collections/collection-a/search',
            expect.any(Object),
        );
        expect(window.SemanticSearch.buildTieredResults).toHaveBeenCalledOnce();
        const existingCard = document.querySelector(
            '[data-doc-id="existing-doc"]',
        );
        expect(existingCard.querySelector('[data-similarity]').textContent)
            .toContain('93% match');
        expect(existingCard.querySelector('.ldr-library-snippet').textContent)
            .toContain('Matched climate evidence');
        expect(createSemanticResultCard).toHaveBeenCalledWith(
            expect.objectContaining({ document_id: 'content-only' }),
            expect.any(Object),
            'climate',
        );
        expect(document.getElementById('documents-container').textContent)
            .toContain('Content-only evidence');

        input.value = '';
        input.dispatchEvent(new Event('input'));
        await vi.advanceTimersByTimeAsync(250);

        expect(existingCard.querySelector('[data-similarity]')).toBeNull();
        expect(existingCard.querySelector('.ldr-library-snippet')).toBeNull();
        expect(document.querySelector('.ldr-semantic-result')).toBeNull();
        expect(document.querySelector('.ldr-hybrid-divider')).toBeNull();
        expect(document.getElementById('documents-container').firstElementChild)
            .toBe(existingCard);
    });

    it('keeps the newest hybrid query when overlapping searches resolve out of order', async () => {
        const older = deferred();
        const newer = deferred();
        const { createSemanticResultCard, safeFetch } = await installController({
            collections: [{ id: 'collection-a', indexed_document_count: 2 }],
            fetchImpl: vi.fn()
                .mockReturnValueOnce(older.promise)
                .mockReturnValueOnce(newer.promise),
        });
        window.SemanticSearch.buildTieredResults.mockImplementation(
            (_textResults, semanticResults) => ({
                tier1: [],
                tier2: [],
                tier3: semanticResults.map(semanticResult => ({
                    semanticResult,
                })),
            }),
        );
        document.getElementById('filter-collection').value = 'collection-a';
        const input = document.getElementById('search-documents');

        input.value = 'older query';
        input.dispatchEvent(new Event('input'));
        await vi.advanceTimersByTimeAsync(750);
        input.value = 'newer query';
        input.dispatchEvent(new Event('input'));
        await vi.advanceTimersByTimeAsync(750);
        expect(safeFetch).toHaveBeenCalledTimes(2);

        newer.resolve(jsonResponse({
            success: true,
            results: [{
                document_id: 'newer-result',
                title: 'Newest result',
                similarity: 95,
            }],
        }));
        await vi.advanceTimersByTimeAsync(0);
        expect(document.getElementById('documents-container').textContent)
            .toContain('Newest result');

        older.resolve(jsonResponse({
            success: true,
            results: [{
                document_id: 'older-result',
                title: 'Stale result',
                similarity: 99,
            }],
        }));
        await vi.advanceTimersByTimeAsync(0);

        expect(createSemanticResultCard.mock.calls.map(([result]) => (
            result.document_id
        ))).toEqual(['newer-result']);
        expect(document.getElementById('documents-container').textContent)
            .not.toContain('Stale result');
    });

    it('explains why semantic search is unavailable without indexed collections', async () => {
        const { safeFetch } = await installController({
            collections: [{ id: 'not-indexed', indexed_document_count: 0 }],
            fetchImpl: vi.fn(),
        });
        document.getElementById('search-documents').value = 'evidence';

        selectMode('semantic');
        await vi.advanceTimersByTimeAsync(500);

        expect(safeFetch).not.toHaveBeenCalled();
        const notice = document.getElementById('library-search-notice');
        expect(notice.style.display).toBe('block');
        expect(notice.textContent).toContain(
            'No collections have been indexed yet',
        );
        expect(notice.querySelector('a').getAttribute('href'))
            .toBe('/library/collections');
        expect(document.getElementById('semantic-results-container').textContent)
            .toBe('');
    });

    it('recovers the semantic results surface after a request failure', async () => {
        const fetchImpl = vi.fn()
            .mockRejectedValueOnce(new Error('search backend unavailable'))
            .mockResolvedValueOnce(jsonResponse({
                success: true,
                results: [{
                    document_id: 'recovered-result',
                    title: 'Recovered evidence',
                    similarity: 88,
                }],
            }));
        const { createSemanticResultCard } = await installController({
            collections: [{ id: 'collection-a', indexed_document_count: 1 }],
            fetchImpl,
        });
        document.getElementById('filter-collection').value = 'collection-a';
        const input = document.getElementById('search-documents');
        input.value = 'first attempt';

        selectMode('semantic');
        await vi.advanceTimersByTimeAsync(500);
        expect(document.getElementById('semantic-results-container').textContent)
            .toContain('Search failed. Please try again.');

        input.value = 'retry query';
        input.dispatchEvent(new Event('input'));
        await vi.advanceTimersByTimeAsync(750);

        expect(fetchImpl).toHaveBeenCalledTimes(2);
        expect(createSemanticResultCard).toHaveBeenCalledWith(
            expect.objectContaining({ document_id: 'recovered-result' }),
            expect.any(Object),
            'retry query',
        );
        expect(document.getElementById('semantic-results-container').textContent)
            .toContain('Recovered evidence');
    });

    it('filters visible cards locally in text mode', async () => {
        const secondCard = document.createElement('article');
        secondCard.className = 'ldr-document-card';
        secondCard.dataset.docId = 'other-doc';
        secondCard.dataset.domain = 'other.test';
        secondCard.dataset.research = 'research-b';
        secondCard.textContent = 'Unrelated economics paper';
        document.getElementById('documents-container').appendChild(secondCard);
        await installController({
            collections: [],
            fetchImpl: vi.fn(),
        });
        selectMode('text');
        const input = document.getElementById('search-documents');
        input.value = 'climate';

        input.dispatchEvent(new Event('input'));
        await vi.advanceTimersByTimeAsync(250);

        expect(document.querySelector('[data-doc-id="existing-doc"]').style.display)
            .toBe('');
        expect(secondCard.style.display).toBe('none');
        expect(document.getElementById('semantic-results-container').style.display)
            .toBe('none');
    });
});