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.3 KB
Raw Blame
/**
 * Tests for pages/notes.js — select -> synthesize gating (SYNTH_MIN/MAX = 2..5).
 *
 * toggleNoteSelection adds/removes a note, but caps the selection at SYNTH_MAX
 * (a further add warns and is dropped). openSynthesizeModal refuses to open
 * unless the selection is within [SYNTH_MIN, SYNTH_MAX], otherwise it renders
 * the selected titles and shows the modal.
 *
 * 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__;
});

function noteList(n) {
    return Array.from({ length: n }, (_, i) => ({ id: `n${i}`, title: `Note ${i}`, content_preview: 'p', tags: [] }));
}

function setup(count) {
    document.body.innerHTML =
        '<div id="grid"></div><div id="empty"></div>' +
        '<span id="ldr-select-count"></span><button id="ldr-synthesize-btn"></button>' +
        '<div id="synthesis-selected-list"></div><div id="synthesis-preview"></div>';
    hook.setDomRefs({
        grid: document.getElementById('grid'),
        empty: document.getElementById('empty'),
        notice: null,
    });
    window.ui = { showMessage: vi.fn() };
    hook.setNotes(noteList(count));
    hook.setSelectState({ mode: true, ids: [] });
}

describe('toggleNoteSelection', () => {
    it('adds then removes a note', () => {
        setup(3);
        hook.toggleNoteSelection('n0');
        expect(hook.getSelectedIds()).toEqual(['n0']);
        hook.toggleNoteSelection('n0');
        expect(hook.getSelectedIds()).toEqual([]);
    });

    it('caps the selection at SYNTH_MAX (5) and warns on the 6th', () => {
        setup(6);
        ['n0', 'n1', 'n2', 'n3', 'n4'].forEach((id) => hook.toggleNoteSelection(id));
        expect(hook.getSelectedIds().length).toBe(5);

        hook.toggleNoteSelection('n5'); // 6th — over the cap
        expect(hook.getSelectedIds().length).toBe(5); // not added
        expect(window.ui.showMessage).toHaveBeenCalledWith(
            expect.stringContaining('at most 5'), 'warning'
        );
    });
});

describe('openSynthesizeModal', () => {
    it('refuses to open with fewer than SYNTH_MIN selected', () => {
        setup(3);
        const modal = { show: vi.fn() };
        hook.setSynthesizeModal(modal);
        hook.setSelectState({ mode: true, ids: ['n0'] }); // only 1

        hook.openSynthesizeModal();

        expect(modal.show).not.toHaveBeenCalled();
        expect(window.ui.showMessage).toHaveBeenCalledWith(
            expect.stringContaining('2-5 notes'), 'warning'
        );
    });

    it('opens and lists the selected titles for a valid selection', () => {
        setup(3);
        const modal = { show: vi.fn() };
        hook.setSynthesizeModal(modal);
        hook.setSelectState({ mode: true, ids: ['n0', 'n2'] });

        hook.openSynthesizeModal();

        expect(modal.show).toHaveBeenCalled();
        const list = document.getElementById('synthesis-selected-list').innerHTML;
        expect(list).toContain('Note 0');
        expect(list).toContain('Note 2');
    });
});