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.

6.1 KB
Raw Blame
/**
 * Tests for components/annotation_surface.js — the annotation anchor engine
 * (shared by research_notes.js and document_notes.js).
 *
 * Quotes are captured from selection.toString(), which inserts newlines at
 * <br> and block boundaries; the haystack is built from text nodes, where
 * those boundaries contribute NO characters (textContent of "but<br>shared"
 * is "butshared"). buildNormalizedText bridges the two with virtual spaces,
 * so anchors match the passage the user actually selected. Reports are
 * immutable, so a matching anchor can never drift.
 */

let hook;

beforeAll(async () => {
    window.__VITEST_TEST__ = true;
    globalThis.safeFetch = vi.fn();
    globalThis.fetch = vi.fn(() =>
        Promise.resolve({ ok: true, json: () => Promise.resolve({ success: true, notes: [], annotations: [] }) })
    );
    await import('@js/components/annotation_surface.js');
    hook = window.__annotationSurfaceTest;
});

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

function container(html) {
    const el = document.createElement('div');
    el.id = 'results-content';
    // eslint-disable-next-line no-unsanitized/property -- test fixture, hardcoded HTML
    el.innerHTML = html;
    document.body.replaceChildren(el);
    return el;
}

describe('buildNormalizedText', () => {
    it('emits a virtual space at <br> boundaries (renderer newline → <br>)', () => {
        const el = container('<p>cost substantially, but<br>shared ownership</p>');
        const { text } = hook.buildNormalizedText(el);
        expect(text).toBe('cost substantially, but shared ownership');
    });

    it('emits a virtual space between block elements', () => {
        const el = container('<h2>Findings</h2><p>Dedup reduces cost.</p>');
        const { text } = hook.buildNormalizedText(el);
        expect(text).toBe('Findings Dedup reduces cost.');
    });

    it('collapses whitespace runs like a selection does', () => {
        const el = container('<p>a    b\n\n c</p>');
        const { text } = hook.buildNormalizedText(el);
        expect(text).toBe('a b c');
    });

    it('emits a virtual space when EXITING a block (bare text after a close tag)', () => {
        // Regression: only emitting on block ENTER glued the caption to the
        // quote ("insightas noted"), so a quote spanning the boundary (where a
        // selection inserts a newline) never anchored.
        const el = container('<div><blockquote>Key insight</blockquote>as noted below.</div>');
        const { text } = hook.buildNormalizedText(el);
        expect(text).toBe('Key insight as noted below.');
    });
});

describe('findQuoteIndex', () => {
    it('finds a unique quote', () => {
        expect(hook.findQuoteIndex('alpha beta gamma', 'beta', '', '')).toBe(6);
    });

    it('returns -1 for a missing quote', () => {
        expect(hook.findQuoteIndex('alpha beta', 'delta', '', '')).toBe(-1);
    });

    it('disambiguates repeated phrases via prefix/suffix context', () => {
        const hay = 'the cost is high. later the cost is low.';
        // Selecting the SECOND "the cost" (prefix "later ").
        const idx = hook.findQuoteIndex(hay, 'the cost', 'later ', ' is low');
        expect(idx).toBe(hay.indexOf('later ') + 'later '.length);
    });

    it('returns -1 for an empty quote instead of looping forever', () => {
        // indexOf('', n) never returns -1; without the guard the hit loop hangs.
        expect(hook.findQuoteIndex('alpha beta', '', '', '')).toBe(-1);
    });
});

describe('applyAnnotation', () => {
    it('wraps a quote spanning a <br> in per-segment marks', () => {
        const el = container('<p>reduces cost substantially, but<br>shared ownership must be tracked</p>');
        const ok = hook.applyAnnotation(el, {
            note_id: 'n1',
            quote: 'substantially, but shared ownership',
            prefix: '',
            suffix: ''
        });
        expect(ok).toBe(true);
        const marks = el.querySelectorAll('mark.ldr-research-annotation');
        expect(marks.length).toBe(2); // one segment per side of the <br>
        const highlighted = [...marks].map((m) => m.textContent).join(' ');
        expect(highlighted).toContain('substantially, but');
        expect(highlighted).toContain('shared ownership');
        expect(marks[0].dataset.noteId).toBe('n1');
    });

    it('reports false (and leaves the DOM untouched) for an unmatchable quote', () => {
        const el = container('<p>completely different content</p>');
        const ok = hook.applyAnnotation(el, {
            note_id: 'n2', quote: 'not in the report', prefix: '', suffix: ''
        });
        expect(ok).toBe(false);
        expect(el.querySelectorAll('mark').length).toBe(0);
    });

    it('anchors a quote spanning a collapsed double-space without throwing', () => {
        // Regression: the quote maps to two non-contiguous segments on ONE text
        // node (the double space collapses to one); wrapping front-to-first
        // threw IndexSizeError and aborted every annotation on the page.
        const el = container('<p>The result is  significant for this study.</p>');
        const ok = hook.applyAnnotation(el, {
            note_id: 'n3', quote: 'result is significant', prefix: '', suffix: ''
        });
        expect(ok).toBe(true);
        const marks = el.querySelectorAll('mark.ldr-research-annotation');
        expect(marks.length).toBeGreaterThan(0);
        const highlighted = [...marks].map((m) => m.textContent).join('');
        expect(highlighted.replace(/\s+/g, ' ')).toBe('result is significant');
    });

    it('does not abort a second annotation when the first is unwrappable', () => {
        // With per-annotation application, one bad/overlapping wrap must not
        // prevent a later, valid annotation from anchoring.
        const el = container('<p>The result is  significant for this study today.</p>');
        hook.applyAnnotation(el, { note_id: 'a', quote: 'result is significant', prefix: '', suffix: '' });
        const ok2 = hook.applyAnnotation(el, { note_id: 'b', quote: 'this study today', prefix: '', suffix: '' });
        expect(ok2).toBe(true);
        expect(el.querySelector('mark[data-note-id="b"]')).not.toBeNull();
    });
});