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.1 KB
Raw Blame
/**
 * Tests for pages/notes.js — select-mode selection pruning.
 *
 * In multi-select mode, renderNotes prunes selectedNoteIds down to the
 * currently-visible result set, so the synthesis preview and the submitted
 * note_ids can't diverge from what the user sees (and a now-hidden selection
 * can't get stuck un-deselectable). A search that filters out a selected note
 * must drop it from the selection; a note that is still visible must stay
 * selected. 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;
});

function setupDom() {
    document.body.innerHTML =
        '<div id="grid"></div><div id="empty"></div>' +
        '<span id="ldr-select-count"></span>' +
        '<button id="ldr-synthesize-btn"></button>';
    hook.setDomRefs({
        grid: document.getElementById('grid'),
        empty: document.getElementById('empty'),
        notice: null,
    });
}

const A = { id: 'a', title: 'A', content_preview: 'pa', tags: [] };
const B = { id: 'b', title: 'B', content_preview: 'pb', tags: [] };
const C = { id: 'c', title: 'C', content_preview: 'pc', tags: [] };

describe('select-mode selection pruning', () => {
    it('drops selected notes a superseding search filtered out, keeps the visible one', () => {
        setupDom();
        hook.setNotes([A, B, C]);
        hook.setSelectState({ mode: true, ids: ['a', 'b', 'c'] });

        // A search now matches only C — A and B are no longer visible.
        hook.setNotes([C]);
        hook.renderNotes(false);

        expect(hook.getSelectedIds().sort()).toEqual(['c']);
        // 1 selected < SYNTH_MIN (2) → synthesize disabled.
        expect(document.getElementById('ldr-synthesize-btn').disabled).toBe(true);
        expect(document.getElementById('ldr-select-count').textContent).toBe('1 selected');
    });

    it('prunes the whole selection when the search matches nothing', () => {
        setupDom();
        hook.setNotes([A, B]);
        hook.setSelectState({ mode: true, ids: ['a', 'b'] });

        hook.setNotes([]);
        hook.renderNotes(false);

        expect(hook.getSelectedIds()).toEqual([]);
        expect(document.getElementById('ldr-synthesize-btn').disabled).toBe(true);
    });

    it('leaves the selection intact when all selected notes are still visible', () => {
        setupDom();
        hook.setNotes([A, B, C]);
        hook.setSelectState({ mode: true, ids: ['a', 'b'] });

        hook.setNotes([A, B, C]); // same visible set — nothing to prune
        hook.renderNotes(false);

        expect(hook.getSelectedIds().sort()).toEqual(['a', 'b']);
    });
});