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.

7.3 KB
Raw Blame
/**
 * Tests for pages/note-detail.js — wiki-link autocomplete keyboard nav.
 *
 * While the [[ autocomplete dropdown is open, ArrowDown/ArrowUp cycle the
 * active suggestion and Tab/Enter accept it — inserting [[Title]] over the
 * typed query span (wikiLinkStartPos..wikiLinkEndPos) in the editor textarea.
 * (Tab acceptance is standard combobox UX; without it Tab would move focus out
 * while the dropdown lingered.)
 *
 * Driven via the production-inert window.__noteDetailTest hook.
 */

let hook;

beforeAll(async () => {
    window.__VITEST_TEST__ = true;
    globalThis.safeFetch = vi.fn(() =>
        Promise.resolve({ ok: true, json: () => Promise.resolve({ success: true }) })
    );
    await import('@js/pages/note-detail.js');
    hook = window.__noteDetailTest;
});

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

const RESULTS = [
    { id: 'f1', title: 'Foo Note' },
    { id: 'b1', title: 'Bar Note' },
];

function key(k) {
    return { key: k, preventDefault: () => {} };
}

function setupEditor() {
    // The user typed "see [[Fo" — the [[ query starts at index 4, ends at 8.
    document.body.innerHTML = '<textarea id="note-content">see [[Fo</textarea>';
    hook.setWikiLinkState({ active: true, results: RESULTS, index: 0, start: 4, end: 8 });
}

describe('wiki-link autocomplete keyboard nav', () => {
    it('ArrowDown cycles the active suggestion', () => {
        setupEditor();
        expect(hook.getWikiLinkSelectedIndex()).toBe(0);

        hook.handleWikiLinkKeydown(key('ArrowDown'));
        expect(hook.getWikiLinkSelectedIndex()).toBe(1);

        // Wraps around back to the first.
        hook.handleWikiLinkKeydown(key('ArrowDown'));
        expect(hook.getWikiLinkSelectedIndex()).toBe(0);
    });

    it('Tab accepts the active suggestion, inserting [[Title]] over the query', () => {
        setupEditor();

        hook.handleWikiLinkKeydown(key('Tab'));

        // "see [[Fo" → "see [[Foo Note]]" (the [[Fo span replaced).
        expect(document.getElementById('note-content').value).toBe('see [[Foo Note]]');
    });

    it('Enter accepts the currently-selected suggestion (after ArrowDown → second)', () => {
        setupEditor();

        hook.handleWikiLinkKeydown(key('ArrowDown')); // select "Bar Note"
        hook.handleWikiLinkKeydown(key('Enter'));

        expect(document.getElementById('note-content').value).toBe('see [[Bar Note]]');
    });

    it('does nothing when the dropdown is not active', () => {
        setupEditor();
        hook.setWikiLinkState({ active: false, results: RESULTS, index: 0, start: 4, end: 8 });

        hook.handleWikiLinkKeydown(key('Tab'));

        // Inactive → no insertion; the raw text is untouched.
        expect(document.getElementById('note-content').value).toBe('see [[Fo');
    });

    it('strips the "|" alias separator from the accepted title (matches backend)', () => {
        // A note titled with a literal "|" — nothing forbids it. Accepting it
        // from autocomplete must drop the pipe portion, mirroring the backend's
        // partition("|")[0]; otherwise the save-path parser resolves the link
        // against only the pre-pipe text and silently links the wrong note/none.
        document.body.innerHTML = '<textarea id="note-content">see [[Co</textarea>';
        const ta = document.getElementById('note-content');
        ta.setSelectionRange(8, 8); // caret at the end of the [[Co query
        hook.setWikiLinkState({
            active: true,
            results: [{ id: 'c1', title: 'Cost | Benefit Analysis' }],
            index: 0, start: 4, end: 8,
        });

        hook.handleWikiLinkKeydown(key('Enter'));

        expect(ta.value).toBe('see [[Cost]]');
    });

    it('treats Enter as a newline (no accept) when the caret moved off the query', () => {
        // Dropdown opened for the [[Fo span, but the user then repositioned the
        // caret (mouse click / Home / Arrow — none fire "input", so the dropdown
        // stays active with a stale span). Enter must NOT be hijacked into
        // splicing a link at the stale position and swallowing the newline.
        document.body.innerHTML = '<textarea id="note-content">see [[Fo and more</textarea>';
        const ta = document.getElementById('note-content');
        hook.setWikiLinkState({ active: true, results: RESULTS, index: 0, start: 4, end: 8 });
        ta.setSelectionRange(16, 16); // caret moved to the end, off the [[Fo span

        const ev = { key: 'Enter', preventDefault: vi.fn() };
        hook.handleWikiLinkKeydown(ev);

        // No link spliced in, and Enter was NOT preventDefault'd (newline stands).
        expect(ta.value).toBe('see [[Fo and more');
        expect(ev.preventDefault).not.toHaveBeenCalled();
    });

    it('still accepts via Enter after End moves the caret past trailing text on the same line', () => {
        // Regression test for the CI "does not delete trailing text" Playwright
        // failure: the query is typed with trailing text directly after it on
        // the same line (no separating space — e.g. the user positioned the
        // caret with Home before typing "[[query"), then the user presses End
        // (no 'input' event fires, so wikiLinkEndPos is NOT refreshed — it
        // stays where typing left it) followed by Enter to accept.
        //
        // Unlike the "moved off the query" case above (caret parked mid-way
        // into unrelated trailing text), End moves the caret to the end of the
        // *current line* — a deliberate "skip past the rest of this line, then
        // accept" gesture, not "go edit somewhere else". wikiLinkCaretStillAtQuery
        // must honor that, and selectWikiLinkItem must still splice the
        // replacement into the tracked [[query span (not up to the relocated
        // caret), leaving the trailing text intact.
        document.body.innerHTML = '<textarea id="note-content">[[Fotail stays</textarea>';
        const ta = document.getElementById('note-content');
        hook.setWikiLinkState({ active: true, results: RESULTS, index: 0, start: 0, end: 4 });
        ta.setSelectionRange(ta.value.length, ta.value.length); // End: caret at end of line

        const ev = { key: 'Enter', preventDefault: vi.fn() };
        hook.handleWikiLinkKeydown(ev);

        expect(ta.value).toBe('[[Foo Note]]tail stays');
        expect(ev.preventDefault).toHaveBeenCalled();
    });

    it('leaves Enter to the IME while a composition is active', () => {
        setupEditor();

        const ev = { key: 'Enter', isComposing: true, preventDefault: vi.fn() };
        hook.handleWikiLinkKeydown(ev);

        expect(document.getElementById('note-content').value).toBe('see [[Fo');
        expect(ev.preventDefault).not.toHaveBeenCalled();
    });

    it('leaves Enter to the IME when Safari reports keyCode 229 after compositionend', () => {
        setupEditor();

        const ev = { key: 'Enter', isComposing: false, keyCode: 229, preventDefault: vi.fn() };
        hook.handleWikiLinkKeydown(ev);

        expect(document.getElementById('note-content').value).toBe('see [[Fo');
        expect(ev.preventDefault).not.toHaveBeenCalled();
    });

    it('does not move the active suggestion on arrow keys during a composition', () => {
        setupEditor();

        hook.handleWikiLinkKeydown({ key: 'ArrowDown', isComposing: true, preventDefault: vi.fn() });

        expect(hook.getWikiLinkSelectedIndex()).toBe(0);
    });
});