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.9 KB
Raw Blame
/**
 * Tests for pages/note-detail.js — _selectionWithinNote scoping.
 *
 * findSimilarPassages searches the user's current text selection. A page-wide
 * window.getSelection() would treat text selected ANYWHERE (e.g. in a previous
 * AI results panel) as the "passage" to search. _selectionWithinNote() guards
 * against that: it returns the selection only when both endpoints lie inside
 * #note-content-rendered, otherwise ''.
 *
 * 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__;
});

function selectWithin(elId) {
    const node = document.getElementById(elId).firstChild;
    const range = document.createRange();
    range.setStart(node, 0);
    range.setEnd(node, node.textContent.length);
    const sel = window.getSelection();
    sel.removeAllRanges();
    sel.addRange(range);
    return sel;
}

describe('_selectionWithinNote — passage-selection scoping', () => {
    beforeEach(() => {
        document.body.innerHTML =
            '<div id="note-content-rendered">inside the note body</div>' +
            '<div id="ai-panel">text from a previous AI panel</div>';
    });

    it('returns empty when nothing is selected (collapsed/no range)', () => {
        const sel = window.getSelection();
        if (sel) sel.removeAllRanges();
        expect(hook._selectionWithinNote()).toBe('');
    });

    it('returns empty for a selection OUTSIDE the note content', () => {
        selectWithin('ai-panel'); // selection lives outside #note-content-rendered
        expect(hook._selectionWithinNote()).toBe('');
    });

    it('returns empty when the note-content container is absent', () => {
        document.body.innerHTML = '<div id="ai-panel">elsewhere</div>';
        selectWithin('ai-panel');
        expect(hook._selectionWithinNote()).toBe('');
    });

    it('returns the selected text when it IS within the note content', () => {
        // Stub getSelection with a deterministic in-note selection: the DOM
        // Selection.toString() is unreliable under the test DOM, and the old
        // accept-'' escape hatch let an always-empty regression pass the
        // whole suite. The stub mirrors exactly what _selectionWithinNote
        // reads (rangeCount, isCollapsed, anchor/focus nodes, String()).
        const noteNode = document.getElementById(
            'note-content-rendered'
        ).firstChild;
        const originalGetSelection = window.getSelection;
        window.getSelection = () => ({
            rangeCount: 1,
            isCollapsed: false,
            anchorNode: noteNode,
            focusNode: noteNode,
            toString: () => '  inside the note body  ',
        });
        try {
            expect(hook._selectionWithinNote()).toBe('inside the note body');
        } finally {
            window.getSelection = originalGetSelection;
        }
    });

    it('rejects a selection with only ONE endpoint inside the note', () => {
        const noteNode = document.getElementById(
            'note-content-rendered'
        ).firstChild;
        const panelNode = document.getElementById('ai-panel').firstChild;
        const originalGetSelection = window.getSelection;
        window.getSelection = () => ({
            rangeCount: 1,
            isCollapsed: false,
            anchorNode: noteNode,
            focusNode: panelNode, // drag ended in the AI panel
            toString: () => 'inside the note bodytext from a previous AI panel',
        });
        try {
            expect(hook._selectionWithinNote()).toBe('');
        } finally {
            window.getSelection = originalGetSelection;
        }
    });
});