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/note-detail.js — wiki-link autocomplete search.
 *
 * searchNotesForLinking fetches /search-for-linking, sets wikiLinkResults, and
 * renders the dropdown (items, "no matches" placeholder, or an error
 * placeholder). handleWikiLinkInput debounces it so rapid typing coalesces
 * into a single search.
 *
 * Driven via the production-inert window.__noteDetailTest hook.
 */

let hook;

beforeAll(async () => {
    window.__VITEST_TEST__ = true;
    globalThis.safeFetch = vi.fn(() =>
        Promise.resolve({ ok: true, json: () => Promise.resolve({ success: true }) })
    );
    globalThis.escapeHtml = (s) => String(s ?? '');
    await import('@js/pages/note-detail.js');
    hook = window.__noteDetailTest;
});

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

describe('searchNotesForLinking', () => {
    let dropdown;
    beforeEach(() => {
        dropdown = document.createElement('div');
        hook.setWikiLinkDropdown(dropdown);
        hook.setNote({ id: 'note-1', title: 'T', content: 'c', tags: [] });
    });

    it('populates wikiLinkResults and renders items on success', async () => {
        globalThis.safeFetch = vi.fn((url) => {
            expect(url).toContain('/notes/api/notes/search-for-linking');
            return Promise.resolve({ json: () => Promise.resolve({
                success: true,
                notes: [{ id: 'a', title: 'Alpha' }, { id: 'b', title: 'Beta' }],
            }) });
        });

        await hook.searchNotesForLinking('Al');

        expect(hook.getWikiLinkResults().map((n) => n.title)).toEqual(['Alpha', 'Beta']);
        // Two option rows rendered; the matched query is highlighted, so the
        // unhighlighted "Beta" appears verbatim.
        expect(dropdown.querySelectorAll('.ldr-wiki-link-item').length).toBe(2);
        expect(dropdown.innerHTML).toContain('Beta');
    });

    it('shows a placeholder when no notes match', async () => {
        globalThis.safeFetch = vi.fn(() =>
            Promise.resolve({ json: () => Promise.resolve({ success: true, notes: [] }) })
        );

        await hook.searchNotesForLinking('zzz');

        expect(hook.getWikiLinkResults()).toEqual([]);
        expect(dropdown.innerHTML.toLowerCase()).toContain('no matching notes');
    });

    it('clears results and shows an error placeholder on a non-success response', async () => {
        globalThis.safeFetch = vi.fn(() =>
            Promise.resolve({ json: () => Promise.resolve({ success: false }) })
        );

        await hook.searchNotesForLinking('x');

        expect(hook.getWikiLinkResults()).toEqual([]);
        expect(dropdown.innerHTML.toLowerCase()).toContain('could not load');
    });
});

describe('handleWikiLinkInput — debounce', () => {
    it('coalesces rapid typing into a single search for the latest query', async () => {
        vi.useFakeTimers();
        try {
            const ta = document.createElement('textarea');
            document.body.appendChild(ta);
            hook.setNote({ id: 'note-1', title: 'T', content: 'c', tags: [] });

            const fetchMock = vi.fn(() =>
                Promise.resolve({ json: () => Promise.resolve({ success: true, notes: [] }) })
            );
            globalThis.safeFetch = fetchMock;

            // Type "[[Fo" then "[[Foo" before the 300ms debounce elapses.
            ta.value = 'see [[Fo';
            ta.selectionStart = ta.value.length;
            hook.handleWikiLinkInput(ta);

            ta.value = 'see [[Foo';
            ta.selectionStart = ta.value.length;
            hook.handleWikiLinkInput(ta); // resets the debounce timer

            expect(fetchMock).not.toHaveBeenCalled(); // still within the debounce

            await vi.advanceTimersByTimeAsync(300);

            // Exactly one search ran, for the latest query.
            expect(fetchMock).toHaveBeenCalledTimes(1);
            expect(fetchMock.mock.calls[0][0]).toContain('Foo');
        } finally {
            vi.useRealTimers();
        }
    });
});