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.8 KB
Raw Blame
/** Browser-shaped bootstrap contract for the checked-in notes page. */

import { readFileSync } from 'node:fs';
import { resolve } from 'node:path';

const TEMPLATE_PATH = resolve(
    __dirname,
    '../../../src/local_deep_research/web/templates/pages/notes.html',
);

class ModalStub {
    static instances = new Map();

    constructor(element) {
        this.element = element;
        this.show = vi.fn();
        this.hide = vi.fn();
        ModalStub.instances.set(element.id, this);
    }
}

function jsonResponse(payload, status = 200) {
    return new Response(JSON.stringify(payload), { status });
}

let fetchMock;

beforeAll(async () => {
    delete window.__VITEST_TEST__;
    ModalStub.instances.clear();
    // eslint-disable-next-line no-unsanitized/property -- checked-in repository template used as the browser fixture.
    document.body.innerHTML = readFileSync(TEMPLATE_PATH, 'utf8');
    window.bootstrap = { Modal: ModalStub };
    globalThis.bootstrap = window.bootstrap;
    window.ui = { showMessage: vi.fn() };
    window.api = { getCsrfToken: vi.fn(() => 'csrf-notes-bootstrap') };

    const notePayload = {
        success: true,
        total: 1,
        notes: [{
            id: 'note/3299',
            title: '<img src=x onerror="window.__notesBootstrapXss=true"> Migration note',
            content: '# FastAPI\n<script>window.__notesBootstrapXss=true</script>',
            tags: ['migration', '<svg onload="window.__notesBootstrapXss=true">'],
            pinned: true,
            is_indexed: true,
            research_count: 2,
            updated_at: '2026-09-01T12:00:00Z',
        }],
    };
    fetchMock = vi.fn((input) => {
        const url = String(input);
        if (url.startsWith('/notes/api/notes?')) {
            return Promise.resolve(jsonResponse(notePayload));
        }
        if (url === '/library/api/collections') {
            return Promise.resolve(jsonResponse({
                success: true,
                collections: [{
                    id: 'collection/3299',
                    name: '<img src=x onerror="window.__notesBootstrapXss=true"> Evidence',
                }],
            }));
        }
        throw new Error(`Unexpected notes bootstrap request: ${url}`);
    });
    vi.stubGlobal('safeFetchWithAuth', fetchMock);

    await import('@js/security/xss-protection.js');
    await import('@js/services/formatting.js');
    await import('@js/pages/notes.js');
    document.dispatchEvent(new Event('DOMContentLoaded'));

    await vi.waitFor(() => {
        expect(document.querySelectorAll('#ldr-notes-grid .ldr-note-card'))
            .toHaveLength(1);
        expect(document.querySelectorAll('#ldr-collection-filter option'))
            .toHaveLength(2);
    });
});

afterAll(() => {
    vi.useRealTimers();
    vi.restoreAllMocks();
    vi.unstubAllGlobals();
    ModalStub.instances.clear();
    document.body.replaceChildren();
    delete window.bootstrap;
    delete window.ui;
    delete window.api;
    delete window.__notesBootstrapXss;
    delete window.__notesToolbarXss;
});

it('loads, safely renders, filters, and switches mode', async () => {
    expect(fetchMock).toHaveBeenCalledWith(
        '/notes/api/notes?limit=100',
        expect.objectContaining({ credentials: 'same-origin' }),
    );
    expect(fetchMock).toHaveBeenCalledWith(
        '/library/api/collections',
        { credentials: 'same-origin' },
    );

    const card = document.querySelector('#ldr-notes-grid .ldr-note-card');
    expect(card.getAttribute('href')).toBe('/notes/note%2F3299');
    expect(card.textContent).toContain('<img src=x onerror=');
    expect(card.textContent).toContain('FastAPI');
    expect(card.classList).toContain('ldr-pinned');
    expect(card.querySelector('img')).toBeNull();
    expect(card.querySelector('script')).toBeNull();
    expect(window.__notesBootstrapXss).toBeUndefined();

    const collectionFilter = document.getElementById('ldr-collection-filter');
    expect(collectionFilter.options[1].value).toBe('collection/3299');
    expect(collectionFilter.options[1].textContent).toContain('<img src=x');
    collectionFilter.value = 'collection/3299';
    collectionFilter.dispatchEvent(new Event('change', { bubbles: true }));
    await vi.waitFor(() => {
        expect(fetchMock).toHaveBeenCalledWith(
            '/notes/api/notes?collection_id=collection%2F3299&limit=100',
            expect.objectContaining({ credentials: 'same-origin' }),
        );
    });

    document.querySelector('[data-mode="text"]').click();
    await vi.waitFor(() => {
        expect(document.getElementById('notes-search-mode-label').textContent)
            .toBe('Text Only');
    });
    expect(document.getElementById('ldr-notes-search').placeholder)
        .toBe('Search notes by keyword...');
});

it('initializes the checked-in modal toolbar and preview after opening', async () => {
    vi.useFakeTimers();
    try {
        const originalBoldButton = document.querySelector(
            '#modal-markdown-toolbar [data-format="bold"]',
        );

        document.getElementById('note-title').value = 'stale title';
        document.getElementById('note-content').value = 'stale content';
        document.querySelector('[data-action="create-new-note"]').click();
        expect(document.getElementById('note-title').value).toBe('');
        expect(document.getElementById('note-content').value).toBe('');
        expect(ModalStub.instances.get('noteModal').show).toHaveBeenCalledOnce();

        await vi.advanceTimersByTimeAsync(100);
        const boldButton = document.querySelector(
            '#modal-markdown-toolbar [data-format="bold"]',
        );
        expect(boldButton).not.toBe(originalBoldButton);

        const textarea = document.getElementById('note-content');
        textarea.value = 'migration note';
        textarea.setSelectionRange(0, 9);
        boldButton.querySelector('i').click();
        expect(textarea.value).toBe('**migration** note');
        expect(textarea.selectionStart).toBe(2);
        expect(textarea.selectionEnd).toBe(11);

        delete window.__notesToolbarXss;
        textarea.value =
            '<img src=x onerror="window.__notesToolbarXss=true"> **safe preview**';
        document.querySelector('.ldr-editor-tab[data-mode="preview"] i').click();

        const preview = document.getElementById('modal-note-preview');
        expect(textarea.style.display).toBe('none');
        expect(document.getElementById('modal-markdown-toolbar').style.display)
            .toBe('none');
        expect(preview.style.display).toBe('block');
        expect(preview.querySelector('img')).toBeNull();
        expect(preview.querySelector('strong').textContent).toBe('safe preview');
        expect(preview.textContent).toContain('<img src=x onerror=');
        expect(window.__notesToolbarXss).toBeUndefined();
    } finally {
        vi.useRealTimers();
    }
});