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.

15.1 KB
Raw Blame
import '../helpers/safe-fetch-with-auth-to-fetch.js';

/**
 * Rich runtime coverage for the migrated research-details page.
 *
 * This drives every optional FastAPI metrics response through the real page
 * initializer. It also verifies the local escaping fallback used when the
 * shared XSS helper has not loaded, a state the production component claims
 * to support.
 */

import '@js/config/urls.js';
import '@js/services/api.js';

const RESEARCH_ID = 'rich-metrics-3299';

function response(body, status = 200) {
    return {
        ok: status >= 200 && status < 300,
        status,
        url: '',
        json: vi.fn().mockResolvedValue(body),
        text: vi.fn().mockResolvedValue(''),
    };
}

function installDetailsDom() {
    document.body.innerHTML = `
        <div id="loading"></div>
        <div id="error" style="display: none"></div>
        <main id="details-content" style="display: none"></main>
        <section id="token-metrics-section" style="display: none"></section>
        <section id="search-metrics-section" style="display: none"></section>
        <div id="token-usage-top-chart" style="display: none"></div>

        <div id="total-tokens"></div>
        <div id="prompt-tokens"></div>
        <div id="completion-tokens"></div>
        <div id="llm-calls"></div>
        <div id="avg-response-time"></div>
        <div id="model-used"></div>
        <div id="research-query"></div>
        <div id="research-mode"></div>
        <div id="research-date"></div>
        <div id="research-strategy"></div>
        <div id="total-cost"></div>
        <div id="phase-breakdown"></div>
        <div id="search-engine-breakdown"></div>
        <div id="search-engine-performance"></div>
        <div id="search-timeline"></div>
        <div id="total-searches"></div>
        <div id="total-search-results"></div>
        <div id="avg-search-response-time"></div>
        <div id="search-success-rate"></div>
        <section id="call-stack-card" style="display: none">
            <div id="call-stack-traces"></div>
        </section>
        <canvas id="timeline-chart"></canvas>
        <canvas id="search-chart"></canvas>
        <button id="chart-view-bars" type="button"></button>
        <button id="chart-view-line" type="button"></button>
        <button id="view-results-btn" type="button"></button>
        <button id="view-journals-btn" type="button"></button>
        <button id="back-to-history" type="button"></button>

        <section id="source-distribution-section" style="display: none">
            <div class="ldr-card-content"><div><canvas id="source-type-chart"></canvas></div></div>
        </section>
        <section id="link-analytics-section" style="display: none"></section>
        <div id="total-links"></div>
        <div id="unique-domains"></div>
        <div><span>Category one</span><span id="academic-sources"></span></div>
        <div><span>Category two</span><span id="news-sources"></span></div>
        <div id="domain-list"></div>
        <div id="resource-sample"></div>

        <section id="context-overflow-section" style="display: none">
            <div id="co-total-tokens"></div>
            <div id="co-context-limit"></div>
            <div id="co-max-tokens"></div>
            <div id="co-truncation-status"></div>
            <div id="co-phase-breakdown"></div>
            <table><tbody id="co-requests-table"></tbody></table>
            <canvas id="co-usage-chart"></canvas>
            <div id="co-performance-warning" style="display: none"></div>
        </section>
    `;
}

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

it('renders rich optional metrics, safe untrusted text, and both timeline views', async () => {
    installDetailsDom();
    vi.stubGlobal('URLBuilder', window.URLBuilder);
    vi.stubGlobal('URLS', window.URLS);
    vi.spyOn(window.URLBuilder, 'extractResearchIdFromPattern').mockReturnValue(RESEARCH_ID);
    const resultsPage = vi.spyOn(window.URLBuilder, 'resultsPage')
        .mockReturnValue('#results-route');
    const journalQualityPage = vi.spyOn(window.URLBuilder, 'journalQualityPage')
        .mockReturnValue('#journals-route');

    // Exercise the component's documented defense-in-depth fallback rather
    // than relying on xss-protection.js being present.
    delete window.escapeHtml;
    delete window.escapeHtmlAttribute;
    delete window.sanitizeHtml;
    delete window.safeSetInnerHTML;

    const chartInstances = [];
    class ChartMock {
        constructor(context, config) {
            this.context = context;
            this.config = config;
            this.destroy = vi.fn();
            chartInstances.push(this);
        }
    }
    vi.stubGlobal('Chart', ChartMock);
    window.contextOverflowShared = {
        renderTruncationBadge: vi.fn(count => `<span>${count} truncations</span>`),
    };

    const hostile = '<img src=x onerror="globalThis.__detailsXss = true">';
    const timelineMetrics = {
        research_details: {
            query: 'Timeline query',
            mode: 'detailed',
            created_at: '2026-08-30T10:00:00Z',
        },
        summary: { avg_response_time: 1250 },
        phase_stats: {
            [hostile]: { tokens: 900, count: 2 },
        },
        timeline: [{
            research_phase: 'analysis',
            model_name: 'safe-model',
            prompt_tokens: 600,
            completion_tokens: 300,
            tokens: 900,
            cumulative_tokens: 900,
            cumulative_prompt_tokens: 600,
            cumulative_completion_tokens: 300,
            response_time_ms: 1250,
            timestamp: '2026-08-30T10:00:01Z',
            search_engine_selected: 'searxng',
            success_status: 'success',
            calling_function: hostile,
            calling_file: '/tmp/worker.py',
            call_stack: hostile,
        }],
    };
    const searchMetrics = {
        total_searches: 2,
        total_results: 17,
        avg_response_time: 750,
        success_rate: 50,
        engine_stats: [{
            engine: hostile,
            call_count: 2,
            success_rate: 50,
            avg_response_time: 750,
            total_results: 17,
        }],
        search_calls: [{
            engine: hostile,
            query: hostile,
            results_count: 17,
            response_time_ms: 750,
            timestamp: '2026-08-30T10:00:02Z',
            success_status: 'success',
        }, {
            // Never sent: the engine was disabled for this research.
            engine: 'pubmed',
            query: 'skipped query',
            results_count: 0,
            response_time_ms: 0,
            timestamp: '2026-08-30T10:00:03Z',
            success_status: 'skipped_unavailable',
        }],
    };

    const fetchMock = vi.fn(url => {
        const routes = {
            [`/history/details/${RESEARCH_ID}`]: {
                query: 'FastAPI details',
                mode: 'detailed',
                strategy: 'source-based',
                progress: 100,
            },
            [`/metrics/api/metrics/research/${RESEARCH_ID}`]: {
                status: 'success',
                metrics: {
                    total_tokens: 900,
                    total_calls: 2,
                    model_usage: [{
                        model: 'safe-model',
                        prompt_tokens: 600,
                        completion_tokens: 300,
                        calls: 2,
                    }],
                },
            },
            [`/metrics/api/metrics/research/${RESEARCH_ID}/timeline`]: {
                status: 'success', metrics: timelineMetrics,
            },
            [`/metrics/api/metrics/research/${RESEARCH_ID}/search`]: {
                status: 'success', metrics: searchMetrics,
            },
            [`/metrics/api/metrics/research/${RESEARCH_ID}/links`]: {
                status: 'success',
                data: {
                    total_links: 3,
                    unique_domains: 1,
                    domain_categories: { Academic: 3 },
                    domains: [{
                        domain: hostile,
                        count: '3',
                        percentage: '100',
                    }],
                    resources: [{
                        title: hostile,
                        url: `https://example.test/?q=${hostile}`,
                        preview: hostile,
                    }],
                },
            },
            [`/api/research/${RESEARCH_ID}/context-overflow`]: {
                status: 'success',
                data: {
                    overview: {
                        total_tokens: 900,
                        context_limit: 4096,
                        max_tokens_used: 900,
                        truncation_occurred: false,
                        truncated_count: 0,
                    },
                    phase_stats: {
                        analysis: {
                            count: 1,
                            prompt_tokens: 600,
                            completion_tokens: 300,
                            total_tokens: 900,
                            truncated_count: 0,
                        },
                    },
                    requests: [{
                        timestamp: '2026-08-30T10:00:01Z',
                        phase: 'analysis',
                        calling_function: 'run',
                        prompt_tokens: 600,
                        completion_tokens: 300,
                        total_tokens: 900,
                        context_limit: 4096,
                        context_truncated: false,
                        response_time_ms: 1250,
                    }],
                },
            },
        };
        if (!(url in routes)) throw new Error(`Unexpected request: ${url}`);
        return Promise.resolve(response(routes[url]));
    });
    vi.stubGlobal('fetch', fetchMock);

    await import('@js/components/details.js');
    document.dispatchEvent(new Event('DOMContentLoaded'));

    await vi.waitFor(() => {
        expect(document.getElementById('details-content').style.display).toBe('block');
        expect(document.getElementById('link-analytics-section').style.display).toBe('block');
        expect(document.getElementById('context-overflow-section').style.display).toBe('block');
    });

    expect(fetchMock.mock.calls.map(([url]) => url)).toEqual(expect.arrayContaining([
        `/history/details/${RESEARCH_ID}`,
        `/metrics/api/metrics/research/${RESEARCH_ID}`,
        `/metrics/api/metrics/research/${RESEARCH_ID}/timeline`,
        `/metrics/api/metrics/research/${RESEARCH_ID}/search`,
        `/metrics/api/metrics/research/${RESEARCH_ID}/links`,
        `/api/research/${RESEARCH_ID}/context-overflow`,
    ]));
    expect(document.getElementById('avg-response-time').textContent).toBe('1.3s');
    expect(document.getElementById('search-success-rate').textContent).toBe('50.0%');
    expect(document.getElementById('call-stack-card').style.display).toBe('block');
    expect(document.getElementById('total-links').textContent).toBe('3');
    expect(document.getElementById('domain-list').textContent).toContain(hostile);
    expect(document.getElementById('resource-sample').textContent).toContain(hostile);
    expect(document.querySelectorAll('img')).toHaveLength(0);
    expect(globalThis.__detailsXss).not.toBe(true);

    const chartTypes = chartInstances.map(instance => instance.config.type);
    expect(chartTypes).toEqual(expect.arrayContaining(['pie', 'bar', 'line']));
    const barChart = chartInstances.find(instance => instance.config.type === 'bar');
    expect(barChart.config.data.datasets.map(dataset => dataset.data)).toEqual([
        [600],
        [300],
    ]);
    const barTooltips = barChart.config.options.plugins.tooltip.callbacks;
    expect(barTooltips.title([{ dataIndex: 0 }])).toBe('analysis - safe-model');
    expect(barTooltips.beforeBody([{ dataIndex: 0 }])).toEqual([
        'Total: 900 tokens',
    ]);
    expect(barTooltips.afterBody([{ dataIndex: 0 }])).toEqual(expect.arrayContaining([
        'Engine: searxng',
        'Status: success',
        'Response time: 1.3s',
    ]));
    expect(barChart.config.options.scales.y.ticks.callback(12345)).toBe('12,345');

    const searchChart = chartInstances.find(instance => (
        instance.config.type === 'line'
        && instance.config.data.datasets[0].label === 'Results Found'
    ));
    // Skipped searches are not plotted as 0-result searches.
    expect(searchChart.config.data.datasets[0].data).toEqual([17]);
    expect(searchChart.config.data.labels).toHaveLength(1);
    const breakdownItems = document.querySelectorAll(
        '#search-engine-breakdown .ldr-search-engine-item',
    );
    expect(breakdownItems).toHaveLength(2);
    expect(breakdownItems[0].textContent).toContain(hostile);
    expect(breakdownItems[0].textContent).toContain('17 results');
    const skippedItem = breakdownItems[1];
    expect(skippedItem.querySelector('.ldr-search-status-skipped').textContent)
        .toBe('Skipped (engine unavailable)');
    expect(skippedItem.textContent).not.toContain('results');
    const timelineItems = document.querySelectorAll(
        '#search-timeline .ldr-search-timeline-item',
    );
    expect(timelineItems[1].textContent).toContain('Not sent');
    expect(timelineItems[1].textContent).not.toContain('0 results');
    const searchTooltips = searchChart.config.options.plugins.tooltip.callbacks;
    expect(searchTooltips.title([{ dataIndex: 0 }])).toContain('<img src=x');
    expect(searchTooltips.beforeBody([{ dataIndex: 0 }])).toEqual([
        `Engine: ${hostile}`,
    ]);
    expect(searchTooltips.afterBody([{ dataIndex: 0 }])).toEqual([
        'Response time: 0.8s',
    ]);
    expect(searchChart.config.options.scales.y.ticks.callback(12345)).toBe('12,345');

    document.getElementById('chart-view-line').click();
    expect(barChart.destroy).toHaveBeenCalledOnce();
    const cumulativeChart = chartInstances.at(-1);
    expect(cumulativeChart.config.type).toBe('line');
    expect(cumulativeChart.config.data.datasets[0].data).toEqual([900]);
    expect(cumulativeChart.config.options.plugins.tooltip.callbacks.label({
        dataset: { label: 'Cumulative Total Tokens' },
        parsed: { y: 900 },
    })).toBe('Cumulative Total Tokens: 900');
    expect(document.getElementById('chart-view-line').classList).toContain('active');

    document.getElementById('chart-view-bars').click();
    expect(cumulativeChart.destroy).toHaveBeenCalledOnce();
    expect(chartInstances.at(-1).config.type).toBe('bar');
    expect(document.getElementById('chart-view-bars').classList).toContain('active');

    document.getElementById('view-results-btn').click();
    expect(resultsPage).toHaveBeenCalledWith(RESEARCH_ID);
    expect(window.location.hash).toBe('#results-route');
    document.getElementById('view-journals-btn').click();
    expect(journalQualityPage).toHaveBeenCalledWith(RESEARCH_ID);
    expect(window.location.hash).toBe('#journals-route');
    document.getElementById('back-to-history').click();
    expect(window.location.pathname).toBe(URLS.PAGES.HISTORY);
    document.getElementById('classify-domains-btn').click();
    expect(window.location.pathname).toBe('/metrics/links');
});