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.7 KB
Raw Blame
/**
 * Tests for components/annotation_surface.js — applyWhenReady re-applies
 * highlights across the results page's asynchronous render.
 *
 * The results page (results.js) first swaps results.html's
 * `.ldr-loading-spinner` for its OWN placeholder
 * (`<i class="fas fa-spinner fa-pulse"> Loading research results…`) and
 * only THEN, after the report fetch, replaces the container's innerHTML
 * with the real report. A one-shot readiness check keyed on
 * `.ldr-loading-spinner` would see the placeholder as "ready", anchor
 * nothing, and never re-run — so highlights would silently never appear
 * on the first view of a research report. This drives the real
 * LDRAnnotationSurface.init() through that exact sequence.
 */

let SafeLoggerErrors;

beforeAll(() => {
    window.__VITEST_TEST__ = true;
    globalThis.SafeLogger = {
        error: (...a) => SafeLoggerErrors.push(a),
        warn: () => {},
        info: () => {},
        debug: () => {},
    };
    // The surface now calls safeFetchWithAuth (auth-aware wrapper added on
    // main); delegate to the fetch mock each test installs.
    globalThis.safeFetchWithAuth = (...args) =>
        (globalThis.safeFetch || globalThis.fetch)(...args);
});

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

beforeEach(async () => {
    SafeLoggerErrors = [];
    document.body.innerHTML = '';
    // Fresh module each test so init()'s document-level listeners don't stack.
    vi.resetModules();
    await import('@js/components/annotation_surface.js');
});

function mockAnnotationsFetch(annotations) {
    globalThis.fetch = vi.fn(() =>
        Promise.resolve({
            ok: true,
            json: () => Promise.resolve({ success: true, annotations }),
        })
    );
}

function flush() {
    return new Promise((r) => setTimeout(r, 0));
}

describe('applyWhenReady across the placeholder → content swap', () => {
    it('anchors the highlight only after the real report replaces the placeholder', async () => {
        // Container starts with results.js's loading placeholder (NOT the
        // .ldr-loading-spinner the naive heuristic looked for).
        const container = document.createElement('div');
        container.id = 'results-content';
        // eslint-disable-next-line no-unsanitized/property -- test fixture
        container.innerHTML =
            '<div class="text-center my-5"><i class="fas fa-spinner fa-pulse"></i>' +
            '<p class="mt-3">Loading research results...</p></div>';
        document.body.appendChild(container);

        mockAnnotationsFetch([
            { note_id: 'n1', quote: 'attention mechanism', prefix: '', suffix: '' },
        ]);

        const base = '/notes/api/research/res-1';
        window.LDRAnnotationSurface.init({
            containerId: 'results-content',
            endpoints: {
                list: `${base}/annotations`,
                create: `${base}/annotations`,
                deleteFor: (id) => `${base}/annotations/${id}`,
            },
        });

        await flush(); // annotations fetch resolves

        // Placeholder is still showing → nothing anchored yet, and we must
        // NOT have given up (no "could not be anchored" error logged).
        expect(container.querySelectorAll('mark.ldr-research-annotation').length).toBe(0);
        expect(
            SafeLoggerErrors.some((e) => String(e[0]).includes('could not be anchored'))
        ).toBe(false);

        // results.js now renders the real report (wholesale innerHTML swap).
        // eslint-disable-next-line no-unsanitized/property -- test fixture
        container.innerHTML =
            '<h2>Findings</h2><p>We propose a new attention mechanism for this.</p>';
        await flush(); // MutationObserver fires → re-apply

        const marks = container.querySelectorAll('mark.ldr-research-annotation');
        expect(marks.length).toBe(1);
        expect(marks[0].textContent).toBe('attention mechanism');
    });

    it('applies highlights to a real report that merely begins with the word "Loading"', async () => {
        const container = document.createElement('div');
        container.id = 'results-content';
        // A genuine (long) report whose text happens to START with "Loading".
        // Pre-fix, looksLikeLoading() treated ANY leading "Loading"/"Searching"
        // as a placeholder and suppressed every highlight on the page forever;
        // the fix only treats a SHORT (<120 char) such body as a placeholder.
        // eslint-disable-next-line no-unsanitized/property -- test fixture
        container.innerHTML =
            '<p>Loading historical context first: the paper introduces a novel ' +
            'attention mechanism that substantially reduces the compute cost of ' +
            'long-context inference across a wide range of downstream tasks.</p>';
        document.body.appendChild(container);

        mockAnnotationsFetch([
            { note_id: 'n1', quote: 'attention mechanism', prefix: '', suffix: '' },
        ]);

        const base = '/notes/api/research/res-1';
        window.LDRAnnotationSurface.init({
            containerId: 'results-content',
            endpoints: {
                list: `${base}/annotations`,
                create: `${base}/annotations`,
                deleteFor: (id) => `${base}/annotations/${id}`,
            },
        });

        await flush();

        const marks = container.querySelectorAll('mark.ldr-research-annotation');
        expect(marks.length).toBe(1);
        expect(marks[0].textContent).toBe('attention mechanism');
    });

    it('keeps re-applying until every DISTINCT annotation anchors, not just until the mark count is reached', async () => {
        const container = document.createElement('div');
        container.id = 'results-content';
        // Annotation A's quote spans a <br> → TWO marks for ONE annotation.
        // Annotation B's quote is absent from this first render.
        // eslint-disable-next-line no-unsanitized/property -- test fixture
        container.innerHTML =
            '<p>reduces cost substantially, but<br>shared ownership must be tracked</p>';
        document.body.appendChild(container);

        mockAnnotationsFetch([
            { note_id: 'A', quote: 'substantially, but shared ownership', prefix: '', suffix: '' },
            { note_id: 'B', quote: 'governance model', prefix: '', suffix: '' },
        ]);

        const base = '/notes/api/research/res-1';
        window.LDRAnnotationSurface.init({
            containerId: 'results-content',
            endpoints: {
                list: `${base}/annotations`,
                create: `${base}/annotations`,
                deleteFor: (id) => `${base}/annotations/${id}`,
            },
        });
        await flush();
        // A anchored as 2 marks. Pre-fix, a mark COUNT of 2 >= total 2 declared
        // the set complete and tore down the observer — even though B had not
        // anchored — so B's later content would never be picked up.

        // B's content now arrives (wholesale re-render, as results.js does).
        // eslint-disable-next-line no-unsanitized/property -- test fixture
        container.innerHTML =
            '<p>reduces cost substantially, but<br>shared ownership must be tracked</p>' +
            '<p>The governance model is shared.</p>';
        await flush();

        // With the distinct-annotation completion metric the observer was still
        // active and re-anchored BOTH. Pre-fix it had already stopped, so
        // neither the swapped-away A nor the newly-present B is anchored.
        const ids = new Set(
            [...container.querySelectorAll('mark.ldr-research-annotation')].map(
                (m) => m.dataset.noteId
            )
        );
        expect(ids.has('B')).toBe(true);
        expect(ids.has('A')).toBe(true);
    });
});