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.1 KB
Raw Blame
/**
 * Tests for pages/notes.js — collection filter (load / render / clear).
 *
 * loadCollections fetches the collections and renders the filter dropdown
 * (with an "All Collections" default); a failure surfaces a degraded-feature
 * warning rather than silently showing an empty filter. clearCollectionFilter
 * resets the active collection and reloads the full list.
 *
 * 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__;
});

let select;
beforeEach(() => {
    select = document.createElement('select');
    document.body.appendChild(select);
    hook.setCollectionFilter(select);
    window.ui = { showMessage: vi.fn() };
});

describe('loadCollections', () => {
    it('renders the dropdown with an "All Collections" default plus each collection', async () => {
        globalThis.safeFetch = vi.fn((url) => {
            expect(url).toContain('/library/api/collections');
            return Promise.resolve({ json: () => Promise.resolve({
                success: true,
                collections: [{ id: 'c1', name: 'Research' }, { id: 'c2', name: 'Personal' }],
            }) });
        });

        await hook.loadCollections();

        const opts = Array.from(select.querySelectorAll('option')).map((o) => o.textContent);
        expect(opts[0]).toBe('All Collections');
        expect(opts).toContain('Research');
        expect(opts).toContain('Personal');
    });

    it('warns (degraded feature) instead of silently emptying on failure', async () => {
        // Pre-populate the dropdown so we actually exercise the "not silently
        // emptying" claim: renderCollectionFilter is skipped on failure, so a
        // previously-populated select must survive untouched. With the default
        // empty select there is nothing to empty and a regression that cleared
        // it on failure would pass unnoticed.
        select.innerHTML =
            '<option value="">All Collections</option>' +
            '<option value="c1">Research</option>';
        globalThis.safeFetch = vi.fn(() =>
            Promise.resolve({ json: () => Promise.resolve({ success: false }) })
        );

        await hook.loadCollections();

        expect(window.ui.showMessage).toHaveBeenCalledWith(
            expect.stringContaining('Could not load collections'), 'warning'
        );
        const opts = Array.from(select.querySelectorAll('option')).map((o) => o.textContent);
        expect(opts).toContain('Research'); // pre-existing options preserved
    });

    it('warns on a thrown request', async () => {
        select.innerHTML =
            '<option value="">All Collections</option>' +
            '<option value="c1">Research</option>';
        globalThis.safeFetch = vi.fn(() => Promise.reject(new Error('net down')));

        await hook.loadCollections();

        expect(window.ui.showMessage).toHaveBeenCalledWith(
            expect.stringContaining('Could not load collections'), 'warning'
        );
        const opts = Array.from(select.querySelectorAll('option')).map((o) => o.textContent);
        expect(opts).toContain('Research'); // pre-existing options preserved
    });
});

describe('clearCollectionFilter', () => {
    it('resets the active collection and reloads', async () => {
        hook.setSearchState({ search: '', collectionId: 'c1' });
        select.innerHTML = '<option value="c1" selected>Research</option>';

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

        hook.clearCollectionFilter();
        await Promise.resolve(); // let loadNotes' fetch kick off

        expect(hook.getCurrentFilter().collection_id).toBe('');
        expect(select.value).toBe('');
        expect(reloaded).toBe(true);
    });
});