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.5 KB
Raw Blame
/**
 * Tests for pages/note-detail.js — renderBacklinkCard shared renderer.
 *
 * renderBacklinkCard() is the file-local helper that backs the three sidebar
 * card lists (related notes, backlinks, outgoing links). renderBacklinks() and
 * renderOutgoingLinks() were de-duplicated onto it; this test locks the
 * contract that:
 *   - every dynamic field (id/title/preview) is HTML-escaped via escapeHtml,
 *   - badgeHtml is appended verbatim inside the title div (NOT escaped — it is
 *     trusted static markup such as the AI-suggested badge),
 *   - the produced markup is byte-identical to the pre-refactor template.
 *
 * Reached through the production-inert window.__noteDetailTest hook; auto-init
 * is skipped because the harness sets window.__VITEST_TEST__ before import.
 */

let hook;

// Production escapeHtml (security/xss-protection.js) is a global the module
// reads at call time. Reproduce its exact semantics — note it also escapes "/".
const HTML_ESCAPE_MAP = {
    '&': '&',
    '<': '&lt;',
    '>': '&gt;',
    '"': '&quot;',
    "'": '&#39;',
    '/': '&#x2F;',
};
function escapeHtml(text) {
    if (text === null || text === undefined) {
        return '';
    }
    if (typeof text !== 'string') {
        text = String(text);
    }
    return text.replace(/[&<>"'/]/g, (m) => HTML_ESCAPE_MAP[m]);
}

beforeAll(async () => {
    window.__VITEST_TEST__ = true;
    globalThis.escapeHtml = escapeHtml;
    window.escapeHtml = escapeHtml;

    globalThis.safeFetch = vi.fn(() =>
        Promise.resolve({ json: () => Promise.resolve({ success: true, notes: [] }) })
    );

    await import('@js/pages/note-detail.js');
    hook = window.__noteDetailTest;
});

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

// Original 8-space-indented templates, copied verbatim from the pre-refactor
// renderBacklinks() / renderOutgoingLinks() so we diff against the real thing.
function origBacklinkTemplate(row) {
    return `
        <a class="ldr-sidebar-backlink" href="/notes/${escapeHtml(row.id)}">
            <div class="ldr-sidebar-backlink-title">${escapeHtml(row.title)}</div>
            <div class="ldr-sidebar-backlink-preview">${escapeHtml(row.content_preview || '')}</div>
        </a>
    `;
}
function origOutgoingTemplate(row) {
    return `
        <a class="ldr-sidebar-backlink" href="/notes/${escapeHtml(row.id)}">
            <div class="ldr-sidebar-backlink-title">${escapeHtml(row.title)}${row.auto_suggested ? ' <i class="fas fa-magic ldr-ai-link-badge" title="AI-suggested link" aria-label="AI-suggested link"></i>' : ''}</div>
            <div class="ldr-sidebar-backlink-preview">${escapeHtml(row.content_preview || '')}</div>
        </a>
    `;
}
const AI_BADGE =
    ' <i class="fas fa-magic ldr-ai-link-badge" title="AI-suggested link" aria-label="AI-suggested link"></i>';

const SAMPLE_ROWS = [
    { id: 'a"b<c', title: 'T & <x>', content_preview: 'preview "quoted"', auto_suggested: false },
    { id: 'plain-id', title: 'Title', content_preview: '', auto_suggested: true },
    { id: 'n3', title: '', content_preview: null, auto_suggested: false },
    { id: 'n4', title: 'X', content_preview: undefined, auto_suggested: true },
    { id: 'a/b/c', title: 'has/slash', content_preview: 'p/q', auto_suggested: false },
];

describe('renderBacklinkCard — escaping contract', () => {
    it('HTML-escapes id, title and preview', () => {
        const out = hook.renderBacklinkCard({
            id: '<script>',
            title: '<b>t</b>',
            preview: 'a & b',
        });
        expect(out).not.toContain('<script>');
        expect(out).toContain('href="/notes/&lt;script&gt;"');
        expect(out).toContain('&lt;b&gt;t&lt;&#x2F;b&gt;');
        expect(out).toContain('a &amp; b');
    });

    it('treats null/undefined preview as empty string', () => {
        expect(hook.renderBacklinkCard({ id: 'i', title: 't', preview: null })).toContain(
            '<div class="ldr-sidebar-backlink-preview"></div>'
        );
        expect(hook.renderBacklinkCard({ id: 'i', title: 't' })).toContain(
            '<div class="ldr-sidebar-backlink-preview"></div>'
        );
    });

    it('appends badgeHtml verbatim inside the title div (not escaped)', () => {
        const out = hook.renderBacklinkCard({
            id: 'i',
            title: 't',
            preview: 'p',
            badgeHtml: AI_BADGE,
        });
        expect(out).toContain(`<div class="ldr-sidebar-backlink-title">t${AI_BADGE}</div>`);
    });

    it('omits the badge when badgeHtml is empty (default)', () => {
        const out = hook.renderBacklinkCard({ id: 'i', title: 't', preview: 'p' });
        expect(out).toContain('<div class="ldr-sidebar-backlink-title">t</div>');
        expect(out).not.toContain('ldr-ai-link-badge');
    });
});

describe('renderBacklinkCard — byte-identical to pre-refactor templates', () => {
    it.each(SAMPLE_ROWS)('matches renderBacklinks markup for %o', (row) => {
        const refactored = hook.renderBacklinkCard({
            id: row.id,
            title: row.title,
            preview: row.content_preview,
        });
        expect(refactored).toBe(origBacklinkTemplate(row));
    });

    it.each(SAMPLE_ROWS)('matches renderOutgoingLinks markup for %o', (row) => {
        const refactored = hook.renderBacklinkCard({
            id: row.id,
            title: row.title,
            preview: row.content_preview,
            badgeHtml: row.auto_suggested ? AI_BADGE : '',
        });
        expect(refactored).toBe(origOutgoingTemplate(row));
    });
});