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.

5.2 KB
Raw Blame
/**
 * Tests for pages/note-detail.js — processWikiLinks (inline [[link]] rendering).
 *
 * processWikiLinks rewrites [[Title]] text into anchors, resolving each to a
 * target_id from note.outgoing_links (rename-safe) and falling back to a
 * title-search query (data-note-title) when unresolved. It supports the
 * [[Target|Display]] alias syntax, leaves [[X]] inside <code>/<pre> literal,
 * and treats a whitespace-only target as plain text.
 *
 * Driven via the production-inert window.__noteDetailTest hook.
 */

let hook;

beforeAll(async () => {
    window.__VITEST_TEST__ = true;
    globalThis.safeFetch = vi.fn(() =>
        Promise.resolve({ ok: true, json: () => Promise.resolve({ success: true }) })
    );
    globalThis.escapeHtml = (s) =>
        String(s ?? '').replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;');
    // Shared wiki-link target pattern (parse/render must agree on it).
    window.formatting = { WIKILINK_TARGET: '[^\\]\\n]+' };
    await import('@js/pages/note-detail.js');
    hook = window.__noteDetailTest;
});

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

beforeEach(() => {
    globalThis.safeFetch = vi.fn(() =>
        Promise.resolve({
            ok: true,
            json: () => Promise.resolve({ success: true }),
        })
    );
    window.URLValidator = { safeAssign: vi.fn() };
    hook.setNote({
        id: 'note-1',
        outgoing_links: [{ link_text: 'Target Note', target_id: 't1' }],
    });
});

function render(html) {
    const el = document.createElement('div');
    // eslint-disable-next-line no-unsanitized/property -- test-controlled literal HTML
    el.innerHTML = html;
    hook.processWikiLinks(el);
    return el;
}

describe('processWikiLinks', () => {
    it('resolves a known link to its target_id', () => {
        const el = render('See [[Target Note]] here');
        const a = el.querySelector('a.ldr-wiki-link');
        expect(a).not.toBeNull();
        expect(a.getAttribute('data-target-id')).toBe('t1');
        expect(a.textContent).toBe('Target Note');
    });

    it('leaves an unresolved link with an empty target_id (title-search fallback)', () => {
        const el = render('See [[Unknown Note]] here');
        const a = el.querySelector('a.ldr-wiki-link');
        expect(a.getAttribute('data-target-id')).toBe('');
        expect(a.getAttribute('data-note-title')).toBe(encodeURIComponent('Unknown Note'));
    });

    it('navigates an unresolved link to the exact title match, not the source note content match', async () => {
        hook.setNote({ id: 'source-note', outgoing_links: [] });
        globalThis.safeFetch = vi.fn().mockResolvedValue({
            ok: true,
            json: vi.fn().mockResolvedValue({
                success: true,
                notes: [{
                    id: 'source-note',
                    title: 'Source note containing the link',
                }, {
                    id: 'exact-target-3299',
                    title: 'Unknown Note',
                }],
            }),
        });
        const el = render('See [[Unknown Note]] here');

        el.querySelector('a.ldr-wiki-link').click();

        await vi.waitFor(() => {
            expect(window.URLValidator.safeAssign).toHaveBeenCalledWith(
                window.location,
                'href',
                '/notes/exact-target-3299',
            );
        });
        expect(globalThis.safeFetch).toHaveBeenCalledWith(
            '/notes/api/notes?search=Unknown%20Note&limit=20',
            { credentials: 'same-origin' },
        );
    });

    it('supports the [[Target|Display]] alias syntax', () => {
        const el = render('[[Target Note|click here]]');
        const a = el.querySelector('a.ldr-wiki-link');
        expect(a.textContent).toBe('click here'); // display half
        expect(a.getAttribute('data-target-id')).toBe('t1'); // resolved via target half
    });

    it('leaves [[X]] inside <code> literal', () => {
        const el = render('<code>[[Target Note]]</code>');
        expect(el.querySelector('a.ldr-wiki-link')).toBeNull();
        expect(el.textContent).toContain('[[Target Note]]');
    });

    it('treats a whitespace-only target as plain text', () => {
        const el = render('[[   ]]');
        expect(el.querySelector('a.ldr-wiki-link')).toBeNull();
    });

    it('escapes an empty-target alias so it cannot inject HTML (stored-XSS guard)', () => {
        // Simulates the post-markdown/DOMPurify state: the payload arrives as
        // INERT TEXT (angle brackets already entity-encoded), exactly as the
        // first render pass leaves it. processWikiLinks runs a SECOND pass and
        // must NOT re-animate it. The alias form "[[|...]]" has an empty
        // target, hitting renderLink's fallback branch.
        const el = render('[[|&lt;img src=x onerror=window.__wikiXss=1&gt;]]');
        // Pre-fix, the fallback returned the raw capture and innerHTML
        // re-parsed it into a live <img>; post-fix it is escaped literal text.
        expect(el.querySelector('img')).toBeNull();
        expect(window.__wikiXss).toBeUndefined();
        expect(el.querySelector('a.ldr-wiki-link')).toBeNull();
        expect(el.textContent).toContain('[[|<img src=x onerror=window.__wikiXss=1>]]');
    });
});