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.9 KB
Raw Blame
/**
 * Tests for pages/notes.js — "Ask your notes" (askNotes).
 *
 * askNotes pre-flights /ask-context: it warns (rather than launching a run
 * that can't find anything) when there are no notes or none are indexed. When
 * ready, it starts a research run pinned to the Notes collection and opens the
 * results page. An empty query is a no-op.
 *
 * 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.getCSRFToken = () => 'csrf';
    window.RESEARCH_STATUS = { QUEUED: 'queued' };
    await import('@js/pages/notes.js');
    hook = window.__notesTest;
});

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

beforeEach(() => {
    document.body.innerHTML =
        '<input id="ldr-notes-ask-input" /><button id="ldr-notes-ask-btn"></button>' +
        '<div id="ldr-notes-ask-notice" style="display:none"></div>';
    window.ui = { showMessage: vi.fn() };
    window.URLValidator = { safeAssign: vi.fn() };
    document.getElementById('ldr-notes-ask-input').value = 'what did I learn?';
});

const notice = () => document.getElementById('ldr-notes-ask-notice').textContent;

function ctx(over) {
    return { success: true, collection_id: 'c1', note_count: 5, indexed_count: 5, ...over };
}

describe('askNotes', () => {
    it('does nothing for an empty query', async () => {
        document.getElementById('ldr-notes-ask-input').value = '   ';
        const fetchMock = vi.fn();
        globalThis.safeFetch = fetchMock;

        await hook.askNotes();

        expect(fetchMock).not.toHaveBeenCalled();
    });

    it('warns and does not start research when no notes are indexed', async () => {
        globalThis.safeFetch = vi.fn((url) => {
            if (url.includes('/ask-context')) {
                return Promise.resolve({ json: () => Promise.resolve(ctx({ indexed_count: 0 })) });
            }
            return Promise.resolve({ json: () => Promise.resolve({}) });
        });

        await hook.askNotes();

        expect(notice().toLowerCase()).toContain("aren't indexed");
        const started = globalThis.safeFetch.mock.calls.some(([u]) => u.includes('/api/start_research'));
        expect(started).toBe(false);
    });

    it('warns when there are no notes yet', async () => {
        globalThis.safeFetch = vi.fn(() =>
            Promise.resolve({ json: () => Promise.resolve(ctx({ note_count: 0 })) })
        );

        await hook.askNotes();

        expect(notice().toLowerCase()).toContain('no notes yet');
    });

    it('starts a research run pinned to the Notes collection and navigates', async () => {
        let startBody = null;
        globalThis.safeFetch = vi.fn((url, opts) => {
            if (url.includes('/ask-context')) {
                return Promise.resolve({ json: () => Promise.resolve(ctx()) });
            }
            startBody = JSON.parse(opts.body);
            return Promise.resolve({ json: () => Promise.resolve({ status: 'success', research_id: 'r9' }) });
        });

        await hook.askNotes();

        expect(startBody.search_engine).toBe('collection_c1');
        expect(window.URLValidator.safeAssign).toHaveBeenCalledWith(window.location, 'href', '/results/r9');
    });

    it('warns when the research run cannot start', async () => {
        globalThis.safeFetch = vi.fn((url) => {
            if (url.includes('/ask-context')) {
                return Promise.resolve({ json: () => Promise.resolve(ctx()) });
            }
            return Promise.resolve({ json: () => Promise.resolve({ status: 'error', message: 'no LLM configured' }) });
        });

        await hook.askNotes();

        expect(notice()).toContain('no LLM configured');
        expect(window.URLValidator.safeAssign).not.toHaveBeenCalled();
    });
});