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.

2.1 KB
Raw Blame
/**
 * Tests for pages/notes.js — handleNoteCardAction (card click delegation).
 *
 * In select mode, clicking a note card toggles its selection and prevents the
 * native <a href> navigation. In normal mode the handler does nothing — the
 * anchor navigates natively.
 *
 * 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 setup() {
    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,
    });
    window.ui = { showMessage: vi.fn() };
    hook.setNotes([{ id: 'n0', title: 'N0', content_preview: 'p', tags: [] }]);
}

function cardEl(noteId) {
    const el = document.createElement('a');
    el.dataset.noteId = noteId;
    return el;
}

describe('handleNoteCardAction', () => {
    it('in select mode: toggles selection and prevents native navigation', () => {
        setup();
        hook.setSelectState({ mode: true, ids: [] });
        const event = { preventDefault: vi.fn() };

        hook.handleNoteCardAction(cardEl('n0'), event);

        expect(event.preventDefault).toHaveBeenCalled();
        expect(hook.getSelectedIds()).toEqual(['n0']);
    });

    it('in normal mode: does nothing (lets the anchor navigate)', () => {
        setup();
        hook.setSelectState({ mode: false, ids: [] });
        const event = { preventDefault: vi.fn() };

        hook.handleNoteCardAction(cardEl('n0'), event);

        expect(event.preventDefault).not.toHaveBeenCalled();
        expect(hook.getSelectedIds()).toEqual([]);
    });
});