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.0 KB
Raw Blame
/**
 * Tests for pages/notes.js — handleModalKeydown (markdown keyboard shortcuts).
 *
 * In the create-note modal editor, Ctrl/Cmd + B / I / K apply bold / italic /
 * link formatting (preventing the browser default); other keys pass through,
 * and with no textarea it is a no-op.
 *
 * 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: [] }) })
    );
    await import('@js/pages/notes.js');
    hook = window.__notesTest;
});

afterAll(() => {
    delete window.__VITEST_TEST__;
});

let applyFormat;
beforeEach(() => {
    document.body.innerHTML = '<textarea id="note-content"></textarea>';
    applyFormat = vi.fn();
    window.formatting = { applyMarkdownFormat: applyFormat };
});

function key(k, { ctrl = true } = {}) {
    return { key: k, ctrlKey: ctrl, metaKey: false, preventDefault: vi.fn() };
}

describe('handleModalKeydown', () => {
    it.each([
        ['b', 'bold'],
        ['i', 'italic'],
        ['k', 'link'],
    ])('Ctrl+%s applies %s formatting and prevents default', (k, format) => {
        const e = key(k);
        hook.handleModalKeydown(e);

        expect(e.preventDefault).toHaveBeenCalled();
        expect(applyFormat).toHaveBeenCalledWith(document.getElementById('note-content'), format);
    });

    it('does nothing for a non-shortcut key', () => {
        hook.handleModalKeydown(key('x'));
        expect(applyFormat).not.toHaveBeenCalled();
    });

    it('does nothing without the ctrl/meta modifier', () => {
        hook.handleModalKeydown(key('b', { ctrl: false }));
        expect(applyFormat).not.toHaveBeenCalled();
    });

    it('is a no-op when the editor textarea is absent', () => {
        document.body.innerHTML = '';
        expect(() => hook.handleModalKeydown(key('b'))).not.toThrow();
        expect(applyFormat).not.toHaveBeenCalled();
    });
});