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

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

const TEMPLATE_PATH = resolve(
    __dirname,
    '../../../src/local_deep_research/web/templates/pages/note_detail.html',
);
const NOTE_ID = 'note-3299';
let bootstrapFetchMock;

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

class ModalStub {
    show = vi.fn();
    hide = vi.fn();

    static getOrCreateInstance() {
        return new ModalStub();
    }

    static getInstance() {
        return new ModalStub();
    }
}

class IntersectionObserverStub {
    constructor(callback, options) {
        this.callback = callback;
        this.options = options;
    }

    observe = vi.fn();
    disconnect = vi.fn();
}

beforeAll(async () => {
    delete window.__VITEST_TEST__;
    // eslint-disable-next-line no-unsanitized/property -- checked-in repository template used as the browser fixture.
    document.body.innerHTML = readFileSync(TEMPLATE_PATH, 'utf8');
    window.noteId = NOTE_ID;
    window.api = { getCsrfToken: vi.fn(() => 'csrf-note-bootstrap') };
    window.ui = { showMessage: vi.fn() };
    window.bootstrap = { Modal: ModalStub };
    globalThis.bootstrap = window.bootstrap;
    vi.stubGlobal('IntersectionObserver', IntersectionObserverStub);
    vi.stubGlobal('URLValidator', { safeAssign: vi.fn() });
    vi.stubGlobal('escapeHtml', value => String(value ?? '').replace(
        /[&<>"']/g,
        character => ({
            '&': '&amp;',
            '<': '&lt;',
            '>': '&gt;',
            '"': '&quot;',
            "'": '&#39;',
        })[character],
    ));
    // The production renderer is already tested separately; this fixture
    // keeps the bootstrap focused while still producing headings/wiki links.
    vi.stubGlobal('renderMarkdown', vi.fn(() => (
        '<h1>Migration heading</h1>'
        + '<p>See [[Resolved note|the source]] for details.</p>'
    )));

    const routePayloads = new Map([
        [`/notes/api/notes/${NOTE_ID}`, {
            success: true,
            note: {
                id: NOTE_ID,
                title: '<img src=x onerror="window.__noteBootstrapXss=true"> Migration note',
                content: '# Migration heading\n\nSee [[Resolved note|the source]] for details.',
                tags: ['fastapi', '<script>unsafe</script>'],
                pinned: true,
                is_indexed: false,
                created_at: '2026-08-31T10:00:00Z',
                updated_at: '2026-09-01T10:00:00Z',
                outgoing_links: [{
                    link_text: 'Resolved note',
                    target_id: 'target-note-3299',
                }],
            },
        }],
        ['/library/api/collections', {
            success: true,
            collections: [{ id: 'collection-2', name: 'Available collection' }],
        }],
        [`/notes/api/notes/${NOTE_ID}/collections`, {
            success: true,
            collections: [{
                id: 'notes-home',
                name: 'Notes',
                collection_type: 'notes',
                indexed: true,
            }],
        }],
        [`/notes/api/notes/${NOTE_ID}/research`, {
            success: true,
            research: [],
        }],
        [`/notes/api/notes/${NOTE_ID}/backlinks`, {
            success: true,
            backlinks: [{
                id: 'backlink-1',
                title: 'Backlink note',
                content_preview: 'Linked this migration note',
            }],
        }],
        [`/notes/api/notes/${NOTE_ID}/outgoing-links`, {
            success: true,
            outgoing_links: [{
                id: 'target-note-3299',
                title: 'Resolved note',
                content_preview: 'Target preview',
            }],
        }],
        [`/notes/api/notes/${NOTE_ID}/versions?limit=20&offset=0`, {
            success: true,
            versions: [],
            total: 0,
        }],
    ]);
    bootstrapFetchMock = vi.fn((input) => {
        const url = String(input);
        if (!routePayloads.has(url)) {
            throw new Error(`Unexpected note bootstrap request: ${url}`);
        }
        return Promise.resolve(jsonResponse(routePayloads.get(url)));
    });
    vi.stubGlobal('safeFetchWithAuth', bootstrapFetchMock);

    await import('@js/pages/note-detail.js');
    document.dispatchEvent(new Event('DOMContentLoaded'));
    await vi.waitFor(() => {
        expect(document.getElementById('note-content-wrapper').style.display)
            .toBe('block');
        expect(document.getElementById('sidebar-backlinks').textContent)
            .toContain('Backlink note');
        expect(document.getElementById('sidebar-outgoing-links').textContent)
            .toContain('Resolved note');
    });

});

afterAll(() => {
    vi.restoreAllMocks();
    vi.unstubAllGlobals();
    document.body.replaceChildren();
    delete window.noteId;
    delete window.api;
    delete window.ui;
    delete window.bootstrap;
    delete window.__noteBootstrapXss;
});

it('loads every dependent panel and renders the note through safe DOM ownership', () => {
    expect(new Set(bootstrapFetchMock.mock.calls.map(([url]) => String(url)))).toEqual(
        new Set([
            `/notes/api/notes/${NOTE_ID}`,
            '/library/api/collections',
            `/notes/api/notes/${NOTE_ID}/collections`,
            `/notes/api/notes/${NOTE_ID}/research`,
            `/notes/api/notes/${NOTE_ID}/backlinks`,
            `/notes/api/notes/${NOTE_ID}/outgoing-links`,
            `/notes/api/notes/${NOTE_ID}/versions?limit=20&offset=0`,
        ]),
    );

    const title = document.getElementById('note-title-display');
    expect(title.textContent).toContain('<img src=x onerror=');
    expect(title.querySelector('img')).toBeNull();
    expect(window.__noteBootstrapXss).toBeUndefined();
    expect(document.getElementById('ldr-note-title').value)
        .toContain('Migration note');
    expect(document.getElementById('pin-btn').classList).toContain('ldr-pinned');
    expect(document.getElementById('note-indexed').style.display)
        .toBe('inline-flex');
    expect(document.querySelectorAll('#note-tags-display .ldr-tag'))
        .toHaveLength(2);
    expect(document.querySelector('#ldr-note-collections .ldr-collection-badge')
        .textContent).toContain('Notes');
    expect(document.querySelector('#ldr-note-collections [data-collection-id]'))
        .toBeNull();
    expect(document.querySelector('#toc-list [data-heading-id="heading-0"]'))
        .not.toBeNull();
    expect(document.getElementById('research-list').textContent)
        .toContain('No research runs yet');
    expect(document.getElementById('ldr-versions-list').textContent)
        .toContain('No version history yet');
    expect(document.title).toContain('Migration note - Deep Research System');

    const wikiLink = document.querySelector(
        '#note-content-rendered .ldr-wiki-link',
    );
    expect(wikiLink.textContent).toBe('the source');
    wikiLink.click();
    expect(URLValidator.safeAssign).toHaveBeenCalledWith(
        window.location,
        'href',
        '/notes/target-note-3299',
    );
});