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.

4.8 KB
Raw Blame
/**
 * Tests for pages/notes.js — keyword / semantic search loaders.
 *
 * loadNotesKeywordSearch GETs /notes (honoring the search + collection filter)
 * and renders the results; loadNotesSemanticSearch GETs /semantic-search and
 * renders with similarity. Semantic is AI-only, so a failure can't degrade
 * silently — it surfaces an error and clears the list (no stale results).
 *
 * Driven via the production-inert window.__notesTest hook.
 */

let hook;

beforeAll(async () => {
    window.__VITEST_TEST__ = true;
    globalThis.safeFetch = vi.fn(() =>
        Promise.resolve({ json: () => Promise.resolve({ success: true, notes: [] }) })
    );
    globalThis.escapeHtml = (s) => String(s ?? '');
    await import('@js/pages/notes.js');
    hook = window.__notesTest;
});

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

function dom() {
    const grid = document.createElement('div');
    const empty = document.createElement('div');
    const notice = document.createElement('div');
    document.body.append(grid, empty, notice);
    hook.setDomRefs({ grid, empty, notice });
    window.ui = { showMessage: vi.fn() };
}

const NOTE = { id: 'a', title: 'Alpha', content_preview: 'p', tags: [] };

describe('loadNotesKeywordSearch', () => {
    it('loads and renders keyword results, honoring the search + collection filter', async () => {
        dom();
        hook.setSearchState({ search: 'foo', collectionId: 'coll-1' });

        let calledUrl = '';
        globalThis.safeFetch = vi.fn((url) => {
            calledUrl = url;
            return Promise.resolve({ json: () => Promise.resolve({ success: true, notes: [NOTE] }) });
        });

        await hook.loadNotesKeywordSearch();

        expect(calledUrl).toContain('search=foo');
        expect(calledUrl).toContain('collection_id=coll-1');
        expect(hook.getNotes()).toEqual([NOTE]);
    });

    it('surfaces an error on a non-success response', async () => {
        dom();
        hook.setSearchState({ search: '' });
        globalThis.safeFetch = vi.fn(() =>
            Promise.resolve({ json: () => Promise.resolve({ success: false, error: 'boom' }) })
        );

        await hook.loadNotesKeywordSearch();

        expect(window.ui.showMessage).toHaveBeenCalledWith('Failed to load notes', 'error');
    });
});

describe('loadNotesSemanticSearch', () => {
    it('renders semantic results (with similarity) on success', async () => {
        dom();
        hook.setSearchState({ search: 'meaning' });
        globalThis.safeFetch = vi.fn((url) => {
            expect(url).toContain('/notes/api/notes/semantic-search');
            return Promise.resolve({ json: () => Promise.resolve({ success: true, results: [NOTE] }) });
        });

        await hook.loadNotesSemanticSearch();

        expect(hook.getNotes()).toEqual([NOTE]);
    });

    it('AI-only failure surfaces an error and clears the list (no stale results)', async () => {
        dom();
        hook.setSearchState({ search: 'meaning' });
        hook.setNotes([NOTE]); // stale prior results
        globalThis.safeFetch = vi.fn(() =>
            Promise.resolve({ json: () => Promise.resolve({ success: false, error: 'ai down' }) })
        );

        await hook.loadNotesSemanticSearch();

        expect(hook.getNotes()).toEqual([]); // cleared, not silently kept
        expect(window.ui.showMessage).toHaveBeenCalledWith(
            'AI search failed — try Text or AI Hybrid mode', 'error'
        );
    });
});

describe('load-failure error state (spinner can never spin forever)', () => {
    it('keyword failure replaces the grid with an error state and retry', async () => {
        dom();
        hook.setSearchState({ search: '' });
        globalThis.safeFetch = vi.fn(() =>
            Promise.resolve({ json: () => Promise.resolve({ success: false, error: 'boom' }) })
        );

        await hook.loadNotesKeywordSearch();

        // Pre-fix only a transient toast fired and the template's initial
        // loading spinner stayed in the grid forever with no retry.
        const empty = document.body.lastElementChild.previousElementSibling
            || document.body.children[1];
        expect(document.body.innerHTML).toContain('retry-load-notes');
        expect(document.body.textContent).toContain("Couldn't load notes");
    });

    it('semantic failure shows the error state, not "No matching notes found"', async () => {
        dom();
        hook.setSearchState({ search: 'meaning' });
        globalThis.safeFetch = vi.fn(() =>
            Promise.resolve({ json: () => Promise.resolve({ success: false, error: 'ai down' }) })
        );

        await hook.loadNotesSemanticSearch();

        expect(document.body.innerHTML).toContain('retry-load-notes');
        expect(document.body.textContent).not.toContain('No matching notes found');
    });
});