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.0 KB
Raw Blame
/**
 * Tests for pages/notes.js — setupEventListeners (search debounce + collection change).
 *
 * Typing in the search box debounces (300ms in Text mode, longer for AI modes)
 * then updates the filter and reloads. Changing the collection filter reloads
 * immediately with the new collection.
 *
 * 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/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;
});

let searchInput, collectionFilter;
beforeEach(() => {
    document.body.innerHTML = '';
    searchInput = document.createElement('input');
    collectionFilter = document.createElement('select');
    const grid = document.createElement('div');
    const empty = document.createElement('div');
    document.body.append(searchInput, collectionFilter, grid, empty);
    hook.setSearchInput(searchInput);
    hook.setCollectionFilter(collectionFilter);
    hook.setDomRefs({ grid, empty, notice: null });
    hook.setMode('text'); // 300ms debounce
    window.ui = { showMessage: vi.fn() };
    hook.setupEventListeners();
});

describe('setupEventListeners', () => {
    it('debounces the search input, then updates the filter and reloads', async () => {
        vi.useFakeTimers();
        try {
            const fetchMock = vi.fn(() =>
                Promise.resolve({ json: () => Promise.resolve({ success: true, notes: [] }) })
            );
            globalThis.safeFetch = fetchMock;

            searchInput.value = 'quantum';
            searchInput.dispatchEvent(new Event('input'));

            // Still debouncing.
            expect(fetchMock).not.toHaveBeenCalled();

            await vi.advanceTimersByTimeAsync(300);

            expect(hook.getCurrentFilter().search).toBe('quantum');
            expect(fetchMock).toHaveBeenCalled(); // loadNotes ran
        } finally {
            vi.useRealTimers();
        }
    });

    it('reloads with the new collection when the collection filter changes', async () => {
        const fetchMock = vi.fn(() =>
            Promise.resolve({ json: () => Promise.resolve({ success: true, notes: [] }) })
        );
        globalThis.safeFetch = fetchMock;

        collectionFilter.value = '';
        collectionFilter.innerHTML = '<option value="c9">C9</option>';
        collectionFilter.value = 'c9';
        collectionFilter.dispatchEvent(new Event('change'));
        await Promise.resolve();

        expect(hook.getCurrentFilter().collection_id).toBe('c9');
        expect(fetchMock).toHaveBeenCalled();
    });
});