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.

4.2 KB
Raw Blame
/** Browser-order contract for the checked-in Link Analytics template. */

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

const TEMPLATE_PATH = resolve(
    __dirname,
    '../../../src/local_deep_research/web/templates/pages/link_analytics.html',
);
const RENDER_PATH = resolve(
    __dirname,
    '../../../src/local_deep_research/web/static/js/pages/link_analytics_render.js',
);
const TEMPLATE_SOURCE = readFileSync(TEMPLATE_PATH, 'utf8');
const RENDER_SOURCE = readFileSync(RENDER_PATH, 'utf8');

function extractInlineBootstrap() {
    const match = TEMPLATE_SOURCE.match(
        /<script\b[^>]*>\s*(let topDomainsChart[\s\S]*?)<\/script>/,
    );
    if (!match) throw new Error('Link Analytics inline bootstrap not found');
    return match[1];
}

function jsonResponse(payload) {
    return {
        ok: true,
        json: vi.fn().mockResolvedValue(payload),
    };
}

afterEach(() => {
    vi.restoreAllMocks();
    vi.unstubAllGlobals();
    document.body.replaceChildren();
    delete window.URLValidator;
    delete window.updateEnhancedDomainList;
    delete window.escapeHtml;
});

it('safely completes the eager initial render before the deferred global escaper loads', async () => {
    delete window.escapeHtml;
    window.URLValidator = { isSafeUrl: vi.fn(() => true) };
    // Scripts inserted through innerHTML remain inert; execute them below in
    // their production order while retaining the template's real DOM.
    // eslint-disable-next-line no-unsanitized/property -- repository-owned template fixture.
    document.body.innerHTML = TEMPLATE_SOURCE;
    vi.spyOn(window.HTMLCanvasElement.prototype, 'getContext')
        .mockReturnValue({});

    const payload = '<img src=x onerror="window.__linkAnalyticsXss=true">';
    const fetchMock = vi.fn((url) => {
        if (url === '/metrics/api/link-analytics?period=30d') {
            return Promise.resolve(jsonResponse({
                status: 'success',
                data: {
                    total_links: 1,
                    total_unique_domains: 1,
                    avg_links_per_research: 1,
                    total_researches: 1,
                    domain_categories: { [payload]: 1 },
                    top_domains: [{
                        domain: 'safe.example.com',
                        count: 1,
                        percentage: 100,
                        recent_researches: [{ id: 'run-3299', query: payload }],
                    }],
                    domain_distribution: { top_10: 1, others: 0 },
                    temporal_trend: [],
                    domain_metrics: {},
                },
            }));
        }
        if (url === '/metrics/api/domain-classifications') {
            return Promise.resolve(jsonResponse({
                status: 'success',
                classifications: [],
            }));
        }
        throw new Error(`Unexpected Link Analytics request: ${url}`);
    });
    vi.stubGlobal('fetch', fetchMock);
    const ChartStub = vi.fn(function() {
        this.destroy = vi.fn();
    });

    // Repository-owned production scripts only; no user-controlled source.
    new Function(RENDER_SOURCE)(); // eslint-disable-line no-new-func
    new Function('Chart', extractInlineBootstrap())(ChartStub); // eslint-disable-line no-new-func

    await vi.waitFor(() => {
        expect(document.getElementById('content').style.display).toBe('block');
        expect(fetchMock).toHaveBeenCalledTimes(2);
    });

    expect(fetchMock.mock.calls.map(([url]) => url)).toEqual([
        '/metrics/api/link-analytics?period=30d',
        '/metrics/api/domain-classifications',
    ]);
    expect(window.escapeHtml).toBeUndefined();
    expect(document.getElementById('error').style.display).not.toBe('block');
    expect(document.querySelector('#domain-category-grid img')).toBeNull();
    expect(document.querySelector('#domain-list img')).toBeNull();
    expect(document.querySelector('#domain-category-grid .ldr-category-name')
        .textContent).toBe(payload);
    expect(document.querySelector('#domain-list .ldr-research-link').textContent.trim())
        .toBe(payload.substring(0, 30) + '...');
    expect(window.__linkAnalyticsXss).toBeUndefined();
});