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.

9.1 KB
Raw Blame
/**
 * Direct mutation contracts for the shared research/document annotation UI.
 * Existing coverage pins quote anchoring; these cases exercise the browser
 * selection toolbar and the FastAPI create/delete lifecycles themselves.
 */

let postJson;
let toast;
let onChanged;

const apiResponse = (data, { ok = true, status = 200 } = {}) => ({
    ok,
    status,
    json: () => Promise.resolve(data),
});

const flush = () => new Promise(resolve => setTimeout(resolve, 0));

function buildSurface(html = '<p>A sufficiently long selected passage lives here.</p>') {
    const container = document.createElement('article');
    container.id = 'annotation-content';
    // eslint-disable-next-line no-unsanitized/property -- static test fixture
    container.innerHTML = html;
    document.body.replaceChildren(container);
    return container;
}

function initSurface() {
    onChanged = vi.fn();
    return window.LDRAnnotationSurface.init({
        containerId: 'annotation-content',
        endpoints: {
            list: '/notes/api/research/run-1/annotations',
            create: '/notes/api/research/run-1/annotations',
            deleteFor: noteId =>
                `/notes/api/research/run-1/annotations/${encodeURIComponent(noteId)}`,
        },
        onChanged,
    });
}

function installSelection(container, selectedText) {
    const textNode = container.querySelector('p').firstChild;
    const range = {
        commonAncestorContainer: textNode,
        getBoundingClientRect: () => ({
            top: 80,
            bottom: 100,
            left: 40,
            width: 180,
        }),
        comparePoint: () => 0,
    };
    vi.spyOn(window, 'getSelection').mockReturnValue({
        isCollapsed: false,
        rangeCount: 1,
        getRangeAt: () => range,
        toString: () => selectedText,
    });
    return range;
}

beforeEach(async () => {
    vi.resetModules();
    document.body.replaceChildren();
    window.__VITEST_TEST__ = true;
    postJson = vi.fn();
    toast = vi.fn();
    window.NotesShared = {
        csrfToken: vi.fn(() => 'csrf-annotation'),
        postJson,
        toast,
    };
    globalThis.safeFetchWithAuth = vi.fn().mockResolvedValue(apiResponse({
        success: true,
        annotations: [],
    }));
    window.confirm = vi.fn(() => true);
    vi.spyOn(SafeLogger, 'error').mockImplementation(() => {});
    await import('@js/components/annotation_surface.js');
});

afterEach(() => {
    vi.restoreAllMocks();
    document.body.replaceChildren();
    delete window.__annotationSurfaceTest;
    delete window.LDRAnnotationSurface;
    delete window.confirm;
});

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

async function openCreatePopover(selectedText) {
    const container = buildSurface();
    installSelection(container, selectedText);
    initSurface();
    await flush();

    document.dispatchEvent(new Event('mouseup'));
    await flush();
    const toolbar = document.querySelector('.ldr-selection-toolbar');
    expect(toolbar.style.display).toBe('inline-flex');
    toolbar.querySelector('button').dispatchEvent(new MouseEvent('mousedown', {
        bubbles: true,
        cancelable: true,
    }));
    return document.querySelector('.ldr-annotation-popover');
}

it('creates a bounded annotation through the shared FastAPI endpoint', async () => {
    postJson.mockResolvedValue({ success: true, note_id: 'note-1' });
    const selectedText = `  ${'x'.repeat(1010)}  `;
    const popover = await openCreatePopover(selectedText);
    const textarea = popover.querySelector('textarea');
    textarea.value = 'Migration comment';

    popover.querySelector('.btn-primary').click();

    await vi.waitFor(() => {
        expect(postJson).toHaveBeenCalledOnce();
    });
    const [url, body] = postJson.mock.calls[0];
    expect(url).toBe('/notes/api/research/run-1/annotations');
    expect(body.quote).toBe('x'.repeat(1000));
    expect(body.comment).toBe('Migration comment');
    expect(body).toEqual(expect.objectContaining({ prefix: '', suffix: '' }));
    expect(document.querySelector('.ldr-annotation-popover')).toBeNull();
    expect(toast).toHaveBeenCalledWith('Comment saved as a note', 'success');
    expect(onChanged).toHaveBeenCalledOnce();
});

it('keeps the creation popover retryable after a failed mutation', async () => {
    postJson.mockRejectedValue(new Error('annotation quota reached'));
    const popover = await openCreatePopover('A sufficiently long selected passage');
    const textarea = popover.querySelector('textarea');
    textarea.value = 'Keep this comment';
    const save = popover.querySelector('.btn-primary');

    save.click();

    await vi.waitFor(() => {
        expect(toast)
            .toHaveBeenCalledWith('annotation quota reached', 'error');
    });
    expect(save.disabled).toBe(false);
    expect(document.querySelector('.ldr-annotation-popover')).toBe(popover);
    expect(onChanged).not.toHaveBeenCalled();
});

it('does not submit an empty annotation comment', async () => {
    const popover = await openCreatePopover('A sufficiently long selected passage');

    popover.querySelector('.btn-primary').click();

    expect(postJson).not.toHaveBeenCalled();
    expect(document.querySelector('.ldr-annotation-popover')).toBe(popover);
});

it('deletes every segment of an annotation with CSRF ownership', async () => {
    const container = buildSurface(
        '<p>cost substantially, but<br>shared ownership matters</p>',
    );
    const annotation = {
        note_id: 'note /?#',
        quote: 'substantially, but shared ownership',
        prefix: '',
        suffix: '',
        comment_preview: '<img src=x onerror="window.pwned=true">\n> quote',
    };
    let deleted = false;
    globalThis.safeFetchWithAuth.mockImplementation((_url, options) => {
        if (options?.method === 'DELETE') {
            deleted = true;
            return Promise.resolve(apiResponse({ success: true }));
        }
        return Promise.resolve(apiResponse({
            success: true,
            annotations: deleted ? [] : [annotation],
        }));
    });
    initSurface();
    await vi.waitFor(() => {
        expect(container.querySelectorAll('mark')).toHaveLength(2);
    });

    container.querySelector('mark').click();
    const popover = document.querySelector('.ldr-annotation-popover');
    expect(popover.textContent).toContain('<img src=x');
    expect(popover.querySelector('img')).toBeNull();
    expect(popover.querySelector('a').getAttribute('href'))
        .toBe('/notes/note%20%2F%3F%23');
    popover.querySelector('button').click();

    await vi.waitFor(() => {
        expect(toast).toHaveBeenCalledWith('Comment deleted', 'success');
    });
    expect(globalThis.safeFetchWithAuth).toHaveBeenCalledWith(
        '/notes/api/research/run-1/annotations/note%20%2F%3F%23',
        {
            method: 'DELETE',
            headers: { 'X-CSRFToken': 'csrf-annotation' },
            credentials: 'same-origin',
        },
    );
    expect(container.querySelectorAll('mark')).toHaveLength(0);
    expect(onChanged).toHaveBeenCalledOnce();
});

it('surfaces FastAPI detail and keeps a failed deletion retryable', async () => {
    const container = buildSurface('<p>This annotated passage remains visible.</p>');
    const annotation = {
        note_id: 'note-2',
        quote: 'annotated passage',
        prefix: '',
        suffix: '',
        comment_preview: 'Keep this note',
    };
    globalThis.safeFetchWithAuth.mockImplementation((_url, options) => {
        if (options?.method === 'DELETE') {
            return Promise.resolve(apiResponse(
                { detail: 'note is locked' },
                { ok: false, status: 409 },
            ));
        }
        return Promise.resolve(apiResponse({
            success: true,
            annotations: [annotation],
        }));
    });
    initSurface();
    await vi.waitFor(() => {
        expect(container.querySelector('mark')).not.toBeNull();
    });

    container.querySelector('mark').click();
    const remove = document.querySelector('.ldr-annotation-popover button');
    remove.click();

    await vi.waitFor(() => {
        expect(toast).toHaveBeenCalledWith('note is locked', 'error');
    });
    expect(remove.disabled).toBe(false);
    expect(container.querySelector('mark')).not.toBeNull();
    expect(onChanged).not.toHaveBeenCalled();
});

it('honors deletion cancellation without calling the API', async () => {
    const container = buildSurface('<p>This annotated passage remains visible.</p>');
    const annotation = {
        note_id: 'note-3',
        quote: 'annotated passage',
        prefix: '',
        suffix: '',
        note_title: 'A note',
    };
    globalThis.safeFetchWithAuth.mockResolvedValue(apiResponse({
        success: true,
        annotations: [annotation],
    }));
    window.confirm.mockReturnValue(false);
    initSurface();
    await vi.waitFor(() => {
        expect(container.querySelector('mark')).not.toBeNull();
    });
    globalThis.safeFetchWithAuth.mockClear();

    container.querySelector('mark').click();
    document.querySelector('.ldr-annotation-popover button').click();

    expect(globalThis.safeFetchWithAuth).not.toHaveBeenCalled();
    expect(container.querySelector('mark')).not.toBeNull();
});