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.

22.1 KB
Raw Blame
/**
 * Tests for pages/unified_search.js — the "Search everything" page.
 *
 * The page is built around SEARCH_MODE_REGISTRY: the controller consumes
 * only the registry interface (label/icon/placeholder + run(query,
 * {signal}) → {results, notice?}), so these tests pin that contract:
 * registry-driven mode switching, hybrid leg-degradation notices, the
 * both-legs-fail error state, and result-card rendering (badge + link).
 *
 * Driven via the production-inert window.__unifiedSearchTest hook; the
 * page bootstrap (DOMContentLoaded) never runs here, so DOM refs are
 * injected by the test.
 */

let hook;

beforeAll(async () => {
    window.__VITEST_TEST__ = true;

    globalThis.safeFetch = vi.fn(() =>
        Promise.resolve({ json: () => Promise.resolve({ success: true, results: [] }) })
    );
    // The page now calls safeFetchWithAuth (auth-aware wrapper added on
    // main). In tests, delegate to whichever fetch mock a test installs so
    // the existing per-test globalThis.safeFetch reassignments keep working.
    globalThis.safeFetchWithAuth = (...args) =>
        (globalThis.safeFetch || globalThis.fetch)(...args);
    // Shared escaping helper (xss-protection.js provides it in production).
    // Real escaping so the badge/link rendering assertions are meaningful.
    globalThis.escapeHtml = (s) => String(s ?? '')
        .replace(/&/g, '&')
        .replace(/</g, '&lt;')
        .replace(/>/g, '&gt;')
        .replace(/"/g, '&quot;');
    // The shared tiering component used by hybrid mode.
    await import('@js/components/semantic_search.js');
    window.SemanticSearch = {
        ...window.SemanticSearch,
        buildTieredResults: (textResults, semanticResults) => ({
            tier1: [],
            tier2: textResults.map((t) => ({ historyItem: t })),
            tier3: semanticResults.map((s) => ({ semanticResult: s })),
        }),
    };

    await import('@js/pages/unified_search.js');
    hook = window.__unifiedSearchTest;
});

afterAll(() => {
    delete window.__VITEST_TEST__;
    delete window.SemanticSearch;
});

function makeDom() {
    const input = document.createElement('input');
    const results = document.createElement('div');
    const empty = document.createElement('div');
    const notice = document.createElement('div');
    notice.style.display = 'none';
    const modeBtn = document.createElement('button');
    modeBtn.appendChild(document.createElement('i'));
    const modeMenu = document.createElement('ul');
    const modeLabel = document.createElement('span');
    document.body.append(input, results, empty, notice, modeBtn, modeMenu, modeLabel);
    return { input, results, empty, notice, modeBtn, modeMenu, modeLabel };
}

function flush() {
    return new Promise((r) => setTimeout(r, 0));
}

beforeEach(() => {
    document.body.innerHTML = '';
});

describe('mode registry drives the selector UI', () => {
    it('switching modes applies the registry placeholder, label and icon', () => {
        const dom = makeDom();
        hook.setDomRefs(dom);
        hook.renderUnifiedSearchModeMenu();

        hook.setUnifiedSearchMode('text');
        expect(hook.getMode()).toBe('text');
        expect(dom.input.placeholder).toBe(hook.SEARCH_MODE_REGISTRY.text.placeholder);
        expect(dom.modeLabel.textContent).toBe('Text Only');
        expect(dom.modeBtn.querySelector('i').className).toContain('fa-font');

        hook.setUnifiedSearchMode('semantic');
        expect(dom.input.placeholder).toBe(hook.SEARCH_MODE_REGISTRY.semantic.placeholder);
        expect(dom.modeLabel.textContent).toBe('AI Only');
        // The dropdown item rendered from the registry is marked active.
        const active = dom.modeMenu.querySelector('.dropdown-item.active');
        expect(active.dataset.mode).toBe('semantic');
    });

    it('renders one dropdown item per registry entry', () => {
        const dom = makeDom();
        hook.setDomRefs(dom);
        hook.renderUnifiedSearchModeMenu();
        const items = dom.modeMenu.querySelectorAll('[data-mode]');
        expect(items.length).toBe(Object.keys(hook.SEARCH_MODE_REGISTRY).length);
    });
});

describe('input invalidation', () => {
    it('skips the redundant spinner rebuild on consecutive keystrokes', async () => {
        // Typing fires the input handler per keystroke and each one used
        // to rebuild the spinner DOM (replaceChildren + createElement) even
        // though the previous keystroke's spinner was still on screen —
        // pure per-keystroke churn. Node IDENTITY is the assertion:
        // childElementCount stays 1 across a replacement too.
        vi.useFakeTimers();

        try {
            const dom = makeDom();
            hook.setDomRefs(dom);
            hook.setUnifiedSearchMode('text');
            hook.setupUnifiedSearchListeners();

            globalThis.safeFetch = vi.fn(() =>
                Promise.resolve({
                    json: () => Promise.resolve({
                        success: true,
                        results: [{
                            id: 'needle',
                            title: 'Needle result',
                            content_preview: 'haystack',
                            url: '/notes/needle',
                            source_type: 'note',
                        }],
                    }),
                })
            );

            dom.input.value = 'needle';
            dom.input.dispatchEvent(new Event('input'));
            expect(dom.results.childElementCount).toBe(1);
            const spinnerAfterFirst = dom.results.firstElementChild;
            expect(spinnerAfterFirst.className).toContain(
                'ldr-semantic-search-loading'
            );

            dom.input.value = 'needles';
            dom.input.dispatchEvent(new Event('input'));
            // Same node instance — no teardown/rebuild between keystrokes.
            expect(dom.results.firstElementChild).toBe(spinnerAfterFirst);
            expect(dom.results.childElementCount).toBe(1);

            // ...and the debounce still lands: results replace the spinner.
            await vi.advanceTimersByTimeAsync(300);
            expect(dom.results.textContent).toContain('Needle result');
            expect(dom.results.firstElementChild).not.toBe(spinnerAfterFirst);
        } finally {
            vi.clearAllTimers();
            vi.useRealTimers();
        }
    });

    it('aborts an in-flight search before the replacement debounce fires', async () => {
        vi.useFakeTimers();
        let resolveOld;
        const oldResponse = new Promise((resolve) => {
            resolveOld = resolve;
        });

        try {
            const dom = makeDom();
            hook.setDomRefs(dom);
            hook.setUnifiedSearchMode('text');
            hook.setupUnifiedSearchListeners();

            globalThis.safeFetch = vi.fn((_url, options) =>
                Promise.resolve({
                    json: () => oldResponse,
                    signal: options.signal,
                })
            );

            dom.input.value = 'old query';
            const oldRun = hook.runUnifiedSearch();
            // Poll on the observable effect rather than flushing a
            // hard-coded number of microtask ticks: the old shape
            // (`await Promise.resolve(); await Promise.resolve();`)
            // encoded runKeywordMode's internal await depth, so one
            // benign extra await on the keyword path would flake this
            // suite (#5578).
            await vi.waitFor(() => expect(globalThis.safeFetch).toHaveBeenCalledOnce());

            const oldSignal = globalThis.safeFetch.mock.calls[0][1].signal;
            dom.input.value = 'new query';
            dom.input.dispatchEvent(new Event('input'));
            const abortedImmediately = oldSignal.aborted;

            resolveOld({
                success: true,
                results: [{
                    id: 'old',
                    title: 'Old result',
                    content_preview: 'stale',
                    url: '/notes/old',
                    source_type: 'note',
                }],
            });
            await oldRun;

            expect(abortedImmediately).toBe(true);
            expect(dom.results.textContent).not.toContain('Old result');
            // The replacement request remains debounced.
            expect(globalThis.safeFetch).toHaveBeenCalledTimes(1);
            await vi.advanceTimersByTimeAsync(300);
            expect(globalThis.safeFetch).toHaveBeenCalledTimes(2);
            expect(globalThis.safeFetch.mock.calls[1][0]).toContain(
                'q=new+query'
            );
        } finally {
            vi.clearAllTimers();
            vi.useRealTimers();
        }
    });

    it('clears results when the query shrinks below the minimum length', async () => {
        const dom = makeDom();
        hook.setDomRefs(dom);
        hook.setUnifiedSearchMode('text');
        hook.setupUnifiedSearchListeners();

        globalThis.safeFetch = vi.fn(() =>
            Promise.resolve({
                json: () => Promise.resolve({
                    success: true,
                    results: [{
                        id: 'rendered',
                        title: 'Rendered result',
                        content_preview: 'current',
                        url: '/notes/rendered',
                        source_type: 'note',
                    }],
                }),
            })
        );

        dom.input.value = 'current query';
        await hook.runUnifiedSearch();
        expect(dom.results.textContent).toContain('Rendered result');

        dom.input.value = '';
        dom.input.dispatchEvent(new Event('input'));

        expect(dom.results.childElementCount).toBe(0);
        expect(dom.results.style.display).toBe('none');
        expect(dom.empty.style.display).toBe('block');
        expect(dom.empty.textContent).toContain('Search everything');
        expect(globalThis.safeFetch).toHaveBeenCalledTimes(1);
    });

    it('does not let an older rejected request replace newer results', async () => {
        const dom = makeDom();
        hook.setDomRefs(dom);
        hook.setUnifiedSearchMode('text');

        let rejectOld;
        const oldRequest = new Promise((_resolve, reject) => {
            rejectOld = reject;
        });
        globalThis.safeFetch = vi.fn()
            .mockImplementationOnce(() => oldRequest)
            .mockResolvedValueOnce({
                json: () => Promise.resolve({
                    success: true,
                    results: [{
                        id: 'new',
                        title: 'Current result',
                        content_preview: 'fresh',
                        url: '/notes/new',
                        source_type: 'note',
                    }],
                }),
            });

        dom.input.value = 'older query';
        const olderRun = hook.runUnifiedSearch();
        await Promise.resolve();
        dom.input.value = 'newer query';
        await hook.runUnifiedSearch();
        expect(dom.results.textContent).toContain('Current result');

        rejectOld(new Error('late network failure'));
        await olderRun;

        expect(dom.results.textContent).toContain('Current result');
        expect(dom.empty.textContent).not.toContain("Couldn't search");
    });

    it('cancels the pending debounce when a mode switch runs immediately', async () => {
        vi.useFakeTimers();
        try {
            const dom = makeDom();
            hook.setDomRefs(dom);
            hook.setUnifiedSearchMode('hybrid');
            hook.setupUnifiedSearchListeners();
            globalThis.safeFetch = vi.fn().mockResolvedValue({
                json: () => Promise.resolve({ success: true, results: [] }),
            });

            dom.input.value = 'mode switch query';
            dom.input.dispatchEvent(new Event('input'));
            hook.setUnifiedSearchMode('text');
            await vi.waitFor(() => expect(globalThis.safeFetch).toHaveBeenCalledOnce());

            await vi.advanceTimersByTimeAsync(500);
            expect(globalThis.safeFetch).toHaveBeenCalledTimes(1);
            expect(globalThis.safeFetch.mock.calls[0][0]).toContain('/api/keyword');
        } finally {
            vi.clearAllTimers();
            vi.useRealTimers();
        }
    });

    it('leaves no dangling controller when a short query skips the try/finally', async () => {
        // A mode switch calls runUnifiedSearch unconditionally. With a
        // query below MIN_QUERY_LENGTH that call installs a fresh
        // AbortController and then returns BEFORE the try block, so the
        // finally that clears the module-level reference never runs
        // (#5580). Detected indirectly: a stale controller would be
        // aborted as a no-op by the NEXT search's guard, so abort must
        // never fire here.
        const dom = makeDom();
        hook.setDomRefs(dom);
        hook.setUnifiedSearchMode('text');
        hook.setupUnifiedSearchListeners();
        globalThis.safeFetch = vi.fn().mockResolvedValue({
            json: () => Promise.resolve({ success: true, results: [] }),
        });

        const abortSpy = vi.spyOn(AbortController.prototype, 'abort');

        try {
            dom.input.value = 'a'; // below MIN_QUERY_LENGTH (2)
            hook.setUnifiedSearchMode('semantic'); // runs runUnifiedSearch

            dom.input.value = 'a real query now';
            await hook.runUnifiedSearch();

            expect(globalThis.safeFetch).toHaveBeenCalledOnce();
            // No in-flight request was ever superseded: nothing to abort.
            expect(abortSpy).not.toHaveBeenCalled();
        } finally {
            abortSpy.mockRestore();
        }
    });
});

describe('failure recovery', () => {
    it('shows a server-reported mode error instead of a misleading network error', async () => {
        const dom = makeDom();
        hook.setDomRefs(dom);
        hook.setUnifiedSearchMode('text');
        dom.input.value = 'bad request';
        globalThis.safeFetch = vi.fn().mockResolvedValue({
            json: () => Promise.resolve({
                success: false,
                error: 'Search filter is invalid',
            }),
        });

        await hook.runUnifiedSearch();

        expect(dom.empty.textContent).toContain('Search filter is invalid');
        expect(dom.empty.textContent).not.toContain('check your connection');
        expect(dom.empty.querySelector('[data-action="retry-unified-search"]'))
            .not.toBeNull();
    });

    it('retries the current query through the registered recovery action', async () => {
        const dom = makeDom();
        hook.setDomRefs(dom);
        hook.setUnifiedSearchMode('text');
        dom.input.value = 'retry query';
        globalThis.safeFetch = vi.fn()
            .mockResolvedValueOnce({
                json: () => Promise.resolve({ success: false, error: 'Temporary failure' }),
            })
            .mockResolvedValueOnce({
                json: () => Promise.resolve({
                    success: true,
                    results: [{
                        id: 'recovered',
                        title: 'Recovered result',
                        content_preview: 'available again',
                        url: '/notes/recovered',
                        source_type: 'note',
                    }],
                }),
            });

        await hook.runUnifiedSearch();
        expect(dom.empty.querySelector('[data-action="retry-unified-search"]'))
            .not.toBeNull();
        hook.UNIFIED_SEARCH_ACTIONS['retry-unified-search']();

        await vi.waitFor(() => {
            expect(dom.results.textContent).toContain('Recovered result');
        });
        expect(globalThis.safeFetch).toHaveBeenCalledTimes(2);
    });
});

describe('hybrid mode — leg degradation', () => {
    it('shows the error state (with retry) when BOTH legs fail', async () => {
        const dom = makeDom();
        hook.setDomRefs(dom);
        hook.setUnifiedSearchMode('hybrid');
        dom.input.value = 'anything';

        globalThis.safeFetch = vi.fn(() =>
            Promise.resolve({ json: () => Promise.resolve({ success: false }) })
        );

        await hook.runUnifiedSearch();
        await flush();

        expect(dom.empty.innerHTML).toContain('retry-unified-search');
        expect(dom.empty.textContent).toContain("Couldn't search");
        expect(dom.results.style.display).toBe('none');
    });

    it("shows the 'Text search unavailable' notice when only the keyword leg fails", async () => {
        const dom = makeDom();
        hook.setDomRefs(dom);
        hook.setUnifiedSearchMode('hybrid');
        dom.input.value = 'anything';

        globalThis.safeFetch = vi.fn((url) => {
            if (url.includes('/api/semantic')) {
                return Promise.resolve({
                    json: () => Promise.resolve({
                        success: true,
                        results: [{ id: 'n1', title: 'Note', content_preview: 'p', url: '/notes/n1', source_type: 'note', similarity: 0.8 }],
                    }),
                });
            }
            return Promise.resolve({ json: () => Promise.resolve({ success: false, error: 'db down' }) });
        });

        await hook.runUnifiedSearch();
        await flush();

        expect(dom.notice.style.display).toBe('block');
        expect(dom.notice.textContent.toLowerCase()).toContain('text search unavailable');
        // The AI leg's results still render.
        expect(dom.results.innerHTML).toContain('/notes/n1');
    });

    it("shows the 'AI search unavailable' notice when only the semantic leg fails", async () => {
        const dom = makeDom();
        hook.setDomRefs(dom);
        hook.setUnifiedSearchMode('hybrid');
        dom.input.value = 'anything';

        globalThis.safeFetch = vi.fn((url) => {
            if (url.includes('/api/semantic')) {
                return Promise.resolve({ json: () => Promise.resolve({ success: false }) });
            }
            return Promise.resolve({ json: () => Promise.resolve({ success: true, results: [] }) });
        });

        await hook.runUnifiedSearch();
        await flush();

        expect(dom.notice.style.display).toBe('block');
        expect(dom.notice.textContent.toLowerCase()).toContain('ai search unavailable');
    });
});

describe('result rendering — badge + link', () => {
    it('renders each result as a link card with its source-type badge', () => {
        const dom = makeDom();
        hook.setDomRefs(dom);

        hook.renderUnifiedSearchResults([
            { id: 'n1', title: 'My note', content_preview: 'note text', url: '/notes/n1', source_type: 'note', similarity: 0.87 },
            { id: 'r1', title: 'Run report', content_preview: 'report text', url: '/results/res-9', source_type: 'research_report' },
            { id: 'u1', title: 'Uploaded PDF', content_preview: 'pdf text', url: '/library/document/u1', source_type: 'user_upload' },
        ]);

        const cards = dom.results.querySelectorAll('a.ldr-unified-result-card');
        expect(cards.length).toBe(3);
        expect(cards[0].getAttribute('href')).toBe('/notes/n1');
        expect(cards[0].textContent).toContain('Note');
        expect(cards[0].textContent).toContain('My note');
        // Similarity badge only when the result carries a similarity.
        expect(cards[0].querySelector('.ldr-similarity-badge').textContent).toContain('87% match');
        expect(cards[1].getAttribute('href')).toBe('/results/res-9');
        expect(cards[1].textContent).toContain('Report');
        expect(cards[1].querySelector('.ldr-similarity-badge')).toBeNull();
        // Unknown source types fall back to the generic Document badge.
        expect(cards[2].getAttribute('href')).toBe('/library/document/u1');
        expect(cards[2].textContent).toContain('Document');
        expect(dom.empty.style.display).toBe('none');
    });

    it('escapes user data and refuses non-relative hrefs', () => {
        const dom = makeDom();
        hook.setDomRefs(dom);

        hook.renderUnifiedSearchResults([
            { id: 'x', title: '<img src=x>', content_preview: '<script>', url: 'javascript:alert(1)', source_type: 'note' },
        ]);

        const card = dom.results.querySelector('a.ldr-unified-result-card');
        expect(card.getAttribute('href')).toBe('#');
        expect(dom.results.querySelector('img')).toBeNull();
        expect(dom.results.querySelector('script')).toBeNull();
        expect(card.textContent).toContain('<img src=x>');
    });

    it('rejects protocol-relative and backslash-normalized cross-origin hrefs', () => {
        // Browsers normalize a backslash to '/', so '/\evil.com' navigates
        // to '//evil.com' cross-origin — both must resolve to '#'. Only the
        // server-computed relative paths (/notes/…, /results/…) are allowed.
        expect(hook.unifiedSearchSafeHref('//evil.com')).toBe('#');
        expect(hook.unifiedSearchSafeHref('/\\evil.com')).toBe('#');
        expect(hook.unifiedSearchSafeHref('https://evil.com')).toBe('#');
        // Legitimate relative paths pass through unchanged.
        expect(hook.unifiedSearchSafeHref('/notes/abc')).toBe('/notes/abc');
        expect(hook.unifiedSearchSafeHref('/results/xyz')).toBe('/results/xyz');
    });

    it('rejects embedded control characters the URL parser would strip', () => {
        // A tab/newline/CR after the first slash slips past the '//' check,
        // but the URL parser strips it, yielding a protocol-relative
        // cross-origin URL once parsed ('/<TAB>/evil.com' -> '//evil.com').
        // Built with fromCharCode so the source stays free of raw control
        // bytes. All must resolve to '#'.
        const TAB = String.fromCharCode(9);
        const LF = String.fromCharCode(10);
        const CR = String.fromCharCode(13);
        expect(hook.unifiedSearchSafeHref('/' + TAB + '/evil.com')).toBe('#');
        expect(hook.unifiedSearchSafeHref('/' + LF + '/evil.com')).toBe('#');
        expect(hook.unifiedSearchSafeHref('/' + CR + '/evil.com')).toBe('#');
    });

    it('shows the empty state for zero results', () => {
        const dom = makeDom();
        hook.setDomRefs(dom);

        hook.renderUnifiedSearchResults([]);

        expect(dom.results.style.display).toBe('none');
        expect(dom.empty.style.display).toBe('block');
        expect(dom.empty.textContent).toContain('No matches found');
    });
});