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.

3.4 KB
Raw Blame
/**
 * Tests for pages/note-detail.js — renderVerdicts source-link degradation.
 *
 * Fact-check source links are gated on window.SemanticSearch.isSafeExternalUrl
 * because escapeHtml alone does NOT neutralize a javascript: href. When that
 * shared helper is unavailable, the pre-fix code substituted `() => false`,
 * which silently filtered out EVERY citation — the user saw verdicts with no
 * sources and no indication anything was dropped (fail-closed silent
 * degradation). The fix logs the failure and renders a "Sources unavailable"
 * marker per verdict that had sources, while still rendering real links when
 * the safety check IS present.
 *
 * Exercised via the production-inert window.__noteDetailTest hook; output is
 * observed off the #ldr-ai-results-content panel showAIResults writes to.
 */

let hook;

beforeAll(async () => {
    window.__VITEST_TEST__ = true;
    globalThis.safeFetch = vi.fn(() =>
        Promise.resolve({ json: () => Promise.resolve({ success: true }) })
    );
    globalThis.escapeHtml = (s) =>
        String(s ?? '').replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;');
    globalThis.SafeLogger = { log: vi.fn(), warn: vi.fn(), error: vi.fn() };

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

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

beforeEach(() => {
    document.body.innerHTML =
        '<div id="ldr-ai-results-panel"><span id="ldr-ai-results-title"></span>' +
        '<div id="ldr-ai-results-content"></div></div>';
    globalThis.SafeLogger.error.mockClear();
});

function content() {
    return document.getElementById('ldr-ai-results-content').innerHTML;
}

const VERDICTS = [
    {
        verdict: 'supported',
        claim: 'The sky is blue.',
        confidence: 90,
        sources: [{ url: 'https://example.com/a', title: 'Source A' }],
    },
];

describe('renderVerdicts — source-link safety degradation', () => {
    it('renders real source links when isSafeExternalUrl is available', () => {
        window.SemanticSearch = { isSafeExternalUrl: () => true };

        hook.renderVerdicts(VERDICTS, 'res-1');

        const html = content();
        expect(html).toContain('href="https://example.com/a"');
        expect(html).toContain('Source A');
        expect(html).not.toContain('Sources unavailable');
        expect(globalThis.SafeLogger.error).not.toHaveBeenCalled();
    });

    it('shows "Sources unavailable" and logs when isSafeExternalUrl is missing', () => {
        // No SemanticSearch helper → the pre-fix code silently dropped every
        // source. The fix must surface it instead.
        delete window.SemanticSearch;

        hook.renderVerdicts(VERDICTS, 'res-1');

        const html = content();
        expect(html).toContain('Sources unavailable');
        // The unsafe link must NOT be rendered (no anchor for the source url).
        expect(html).not.toContain('href="https://example.com/a"');
        expect(globalThis.SafeLogger.error).toHaveBeenCalledTimes(1);
    });

    it('does not show "Sources unavailable" for a verdict that genuinely had no sources', () => {
        window.SemanticSearch = { isSafeExternalUrl: () => true };

        hook.renderVerdicts(
            [{ verdict: 'unverified', claim: 'No evidence.', confidence: 0, sources: [] }],
            'res-1'
        );

        expect(content()).not.toContain('Sources unavailable');
    });
});