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.

5.1 KB
Raw Blame
/**
 * Tests for pages/notes.js — hybrid-search AI-unavailable notice.
 *
 * When the semantic (AI) leg of a hybrid search fails, the user must be told
 * the AI search was unavailable — EVEN when the keyword leg also returned no
 * matches. Pre-fix the notice was gated on `textResults.length > 0`
 * (notes.js loadNotesHybridSearch), so a failed AI search with zero keyword
 * matches looked identical to "you have no matching notes", silently hiding
 * the failure. Reverting the fix fails the first test (notice stays hidden).
 *
 * Driven via the production-inert window.__notesTest hook; the page bootstrap
 * (DOMContentLoaded) never runs in jsdom, 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, notes: [] }) })
    );
    // Shared escaping helper (formatting.js provides it in production);
    // needed by the load-error state renderer.
    globalThis.escapeHtml = (s) => String(s ?? '');
    // The shared tiering component — return empty tiers; we only care about
    // the notice, not the merge.
    await import('@js/components/semantic_search.js');
    window.SemanticSearch = {
        ...window.SemanticSearch,
        buildTieredResults: () => ({ tier1: [], tier2: [], tier3: [] }),
    };

    await import('@js/pages/notes.js');
    hook = window.__notesTest;
});

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

function makeDom() {
    const grid = document.createElement('div');
    const empty = document.createElement('div');
    const notice = document.createElement('div');
    notice.style.display = 'none';
    document.body.append(grid, empty, notice);
    return { grid, empty, notice };
}

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

describe('hybrid search — AI-unavailable notice', () => {
    it('shows the notice when the AI leg fails even with zero keyword matches', async () => {
        const dom = makeDom();
        hook.setDomRefs(dom);
        hook.setSearchState({ search: 'nomatch' });

        // Keyword leg → empty; semantic leg → success:false (AI unavailable).
        globalThis.safeFetch = vi.fn((url) => {
            if (url.includes('semantic-search')) {
                return Promise.resolve({ json: () => Promise.resolve({ success: false }) });
            }
            return Promise.resolve({ json: () => Promise.resolve({ success: true, notes: [] }) });
        });

        await hook.loadNotesHybridSearch();
        await flush();

        // Pre-fix this stayed 'none' because textResults.length === 0.
        expect(dom.notice.style.display).toBe('block');
        expect(dom.notice.textContent.toLowerCase()).toContain('ai search unavailable');
        expect(hook.getNotes()).toEqual([]);
    });

    it('does not show the notice when the AI leg succeeds', async () => {
        const dom = makeDom();
        hook.setDomRefs(dom);
        hook.setSearchState({ search: 'anything' });

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

        await hook.loadNotesHybridSearch();
        await flush();

        expect(dom.notice.style.display).toBe('none');
    });
});

describe('hybrid search — keyword-leg failure surfaced', () => {
    it('shows a text-search-unavailable notice when only the keyword leg fails', async () => {
        const dom = makeDom();
        hook.setDomRefs(dom);
        hook.setSearchState({ search: 'anything' });

        // Keyword leg → success:false; semantic leg → success.
        globalThis.safeFetch = vi.fn((url) => {
            if (url.includes('semantic-search')) {
                return Promise.resolve({ json: () => Promise.resolve({ success: true, results: [] }) });
            }
            return Promise.resolve({ json: () => Promise.resolve({ success: false, error: 'db down' }) });
        });

        await hook.loadNotesHybridSearch();
        await flush();

        // Pre-fix the failed keyword leg mapped to [] with zero indication —
        // indistinguishable from "your keywords matched nothing".
        expect(dom.notice.style.display).toBe('block');
        expect(dom.notice.textContent.toLowerCase()).toContain('text search unavailable');
    });

    it('shows the error state (with retry) when BOTH legs fail', async () => {
        const dom = makeDom();
        hook.setDomRefs(dom);
        hook.setSearchState({ search: 'anything' });

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

        await hook.loadNotesHybridSearch();
        await flush();

        expect(dom.empty.innerHTML).toContain('retry-load-notes');
        expect(dom.empty.textContent).toContain("Couldn't load notes");
        expect(hook.getNotes()).toEqual([]);
    });
});