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.

17.4 KB
Raw Blame
/** Response-envelope contracts for unchanged metrics dashboard consumers. */

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

const TEMPLATE_DIR = resolve(
    __dirname,
    '../../../src/local_deep_research/web/templates/pages',
);

function extract(file, pattern, name) {
    const template = readFileSync(resolve(TEMPLATE_DIR, file), 'utf8');
    const match = template.match(pattern);
    expect(match, `${name} source block not found`).toBeTruthy();
    return match[1];
}

function deferred() {
    let resolvePromise;
    let rejectPromise;
    const promise = new Promise((resolveValue, rejectError) => {
        resolvePromise = resolveValue;
        rejectPromise = rejectError;
    });
    return { promise, reject: rejectPromise, resolve: resolvePromise };
}

function starReviewCalls() {
    return Object.fromEntries([
        'showLoading',
        'showError',
        'updateOverallStats',
        'updateDoughnutChart',
        'updateLLMChart',
        'updateSearchEngineChart',
        'updateTrendsChart',
        'updateRecentRatings',
        'updateModeChart',
        'updateLLMBreakdownChart',
        'updateQualityRadar',
        'updateRecentFeedback',
        'showContent',
        'announceChartStates',
    ].map(name => [name, vi.fn()]));
}

function compileStarReviews(calls, registerPeriodChange = false) {
    const loaderSource = extract(
        'star_reviews.html',
        /(async function loadStarReviews\(\)\s*\{[\s\S]*?\n\})\n\n\/\/ Announce the refresh/,
        'loadStarReviews()',
    );
    const registrationSource = registerPeriodChange
        ? extract(
            'star_reviews.html',
            /(document\.getElementById\('period-select'\)\.addEventListener\('change', loadStarReviews\);)/,
            'period-select change registration',
        )
        : '';
    const declarations = Object.keys(calls)
        .map(name => `const ${name} = (...args) => calls.${name}(...args);`)
        .join('\n');

    // Repository-owned production source only; no user-controlled input.
    return new Function( // eslint-disable-line no-new-func
        'calls', `
        ${declarations}
        let starReviewsRequestId = 0;
        ${loaderSource}
        ${registrationSource}
        return { loadStarReviews };
    `)(calls);
}

function starReviewPayload(period) {
    return {
        overall_stats: { avg_rating: period === '7d' ? 4.7 : 3.0, period },
        llm_ratings: [{ model: period }],
        search_engine_ratings: [{ engine: period }],
        rating_trends: [{ date: period }],
        recent_ratings: [{ period }],
        mode_ratings: [{ mode: period }],
        quality_dimensions: { period },
        recent_feedback: [{ feedback: period }],
    };
}

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

it('fans the star-review envelope out to every dashboard consumer', async () => {
    const calls = starReviewCalls();
    const { loadStarReviews } = compileStarReviews(calls);
    document.body.innerHTML = '<select id="period-select"><option value="90d" selected>90 days</option></select>';
    const payload = {
        overall_stats: { avg_rating: 4.5 },
        llm_ratings: [{ model: 'm1' }],
        search_engine_ratings: [{ engine: 'e1' }],
        rating_trends: [{ date: '2026-08-31' }],
        recent_ratings: [{ rating: 5 }],
        mode_ratings: [{ mode: 'quick' }],
        quality_dimensions: { accuracy: 4.8 },
        recent_feedback: [{ feedback: 'useful' }],
    };
    const fetchMock = vi.fn().mockResolvedValue(new Response(
        JSON.stringify(payload),
        { status: 200 },
    ));
    vi.stubGlobal('fetch', fetchMock);

    await loadStarReviews();

    expect(fetchMock).toHaveBeenCalledWith(
        '/metrics/api/star-reviews?period=90d',
    );
    expect(calls.updateOverallStats)
        .toHaveBeenCalledWith(payload.overall_stats);
    expect(calls.updateDoughnutChart)
        .toHaveBeenCalledWith(payload.overall_stats);
    expect(calls.updateLLMChart).toHaveBeenCalledWith(payload.llm_ratings);
    expect(calls.updateSearchEngineChart)
        .toHaveBeenCalledWith(payload.search_engine_ratings);
    expect(calls.updateTrendsChart).toHaveBeenCalledWith(payload.rating_trends);
    expect(calls.updateRecentRatings)
        .toHaveBeenCalledWith(payload.recent_ratings);
    expect(calls.updateModeChart).toHaveBeenCalledWith(payload.mode_ratings);
    expect(calls.updateLLMBreakdownChart)
        .toHaveBeenCalledWith(payload.llm_ratings);
    expect(calls.updateQualityRadar)
        .toHaveBeenCalledWith(payload.quality_dimensions);
    expect(calls.updateRecentFeedback)
        .toHaveBeenCalledWith(payload.recent_feedback);
    expect(calls.showContent).toHaveBeenCalledOnce();
    expect(calls.announceChartStates).toHaveBeenCalledOnce();
    expect(calls.showError).not.toHaveBeenCalled();
});

it.each([
    {
        label: 'HTTP failure',
        response: {
            ok: false,
            status: 503,
            statusText: 'Unavailable',
        },
        expectedError: 'Failed to load star reviews data: HTTP 503: Unavailable',
    },
    {
        label: 'API error envelope',
        response: new Response(JSON.stringify({
            error: 'Ratings warehouse is unavailable',
        })),
        expectedError: 'Ratings warehouse is unavailable',
    },
])('surfaces the current star-review $label', async ({
    response,
    expectedError,
}) => {
    document.body.innerHTML = `
        <select id="period-select">
            <option value="30d" selected>30 days</option>
        </select>
    `;
    const calls = starReviewCalls();
    vi.stubGlobal('fetch', vi.fn().mockResolvedValue(response));
    vi.spyOn(console, 'error').mockImplementation(() => {});

    await compileStarReviews(calls).loadStarReviews();

    expect(calls.showError).toHaveBeenCalledOnce();
    expect(calls.showError).toHaveBeenCalledWith(expectedError);
    expect(calls.updateOverallStats).not.toHaveBeenCalled();
    expect(calls.showContent).not.toHaveBeenCalled();
    expect(calls.announceChartStates).not.toHaveBeenCalled();
});

it('keeps a late older star-review success from replacing a period change', async () => {
    document.body.innerHTML = `
        <select id="period-select">
            <option value="30d" selected>30 days</option>
            <option value="7d">7 days</option>
        </select>
    `;
    const calls = starReviewCalls();
    const runtime = compileStarReviews(calls, true);
    const olderResponse = deferred();
    const currentResponse = deferred();
    const fetchMock = vi.fn()
        .mockImplementationOnce(() => olderResponse.promise)
        .mockImplementationOnce(() => currentResponse.promise);
    vi.stubGlobal('fetch', fetchMock);

    const olderLoad = runtime.loadStarReviews();
    const periodSelect = document.getElementById('period-select');
    periodSelect.value = '7d';
    periodSelect.dispatchEvent(new Event('change', { bubbles: true }));

    expect(fetchMock.mock.calls.map(([url]) => url)).toEqual([
        '/metrics/api/star-reviews?period=30d',
        '/metrics/api/star-reviews?period=7d',
    ]);

    const currentPayload = starReviewPayload('7d');
    currentResponse.resolve(new Response(JSON.stringify(currentPayload)));
    await vi.waitFor(() => {
        expect(calls.updateOverallStats).toHaveBeenCalledWith(
            currentPayload.overall_stats,
        );
    });

    olderResponse.resolve(new Response(JSON.stringify(
        starReviewPayload('30d'),
    )));
    await olderLoad;

    expect(calls.updateOverallStats).toHaveBeenCalledOnce();
    expect(calls.updateLLMChart).toHaveBeenCalledWith(
        currentPayload.llm_ratings,
    );
    expect(calls.showContent).toHaveBeenCalledOnce();
    expect(calls.showError).not.toHaveBeenCalled();
});

it('ignores a stale star-review rejection after the newer period renders', async () => {
    document.body.innerHTML = `
        <select id="period-select">
            <option value="30d" selected>30 days</option>
            <option value="7d">7 days</option>
        </select>
    `;
    const calls = starReviewCalls();
    const runtime = compileStarReviews(calls, true);
    const olderResponse = deferred();
    const currentResponse = deferred();
    vi.stubGlobal('fetch', vi.fn()
        .mockImplementationOnce(() => olderResponse.promise)
        .mockImplementationOnce(() => currentResponse.promise));

    const olderLoad = runtime.loadStarReviews();
    const periodSelect = document.getElementById('period-select');
    periodSelect.value = '7d';
    periodSelect.dispatchEvent(new Event('change', { bubbles: true }));

    const currentPayload = starReviewPayload('7d');
    currentResponse.resolve(new Response(JSON.stringify(currentPayload)));
    await vi.waitFor(() => {
        expect(calls.showContent).toHaveBeenCalledOnce();
    });

    olderResponse.reject(new Error('late network failure'));
    await olderLoad;

    expect(calls.updateOverallStats).toHaveBeenCalledOnce();
    expect(calls.updateOverallStats).toHaveBeenCalledWith(
        currentPayload.overall_stats,
    );
    expect(calls.showError).not.toHaveBeenCalled();
    expect(calls.showContent).toHaveBeenCalledOnce();
});

it('consumes the nested link-analytics data envelope and period', async () => {
    const loaderSource = extract(
        'link_analytics.html',
        /(async function loadLinkAnalytics\(period = '30d'\)\s*\{[\s\S]*?\n\})\n\nfunction generateCategoryGrid/,
        'loadLinkAnalytics()',
    );
    const calls = Object.fromEntries([
        'generateCategoryGrid',
        'updateTopDomainsChart',
        'updateDomainDistributionChart',
        'updateSourceTypeChart',
        'updateTemporalTrendChart',
        'updateEnhancedDomainList',
    ].map(name => [name, vi.fn()]));
    const declarations = Object.keys(calls)
        .map(name => `const ${name} = (...args) => calls.${name}(...args);`)
        .join('\n');
    // Repository-owned production source only; no user-controlled input.
    const loadLinkAnalytics = new Function( // eslint-disable-line no-new-func
        'calls', `
        ${declarations}
        let linkAnalyticsRequestId = 0;
        ${loaderSource}
        return loadLinkAnalytics;
    `)(calls);
    document.body.innerHTML = `
        <div id="loading"></div><div id="content"></div><div id="error"></div>
        <div id="total-links"></div><div id="unique-domains"></div>
        <div id="avg-links"></div><div id="total-researches"></div>
    `;
    const data = {
        total_links: 10,
        total_unique_domains: 4,
        avg_links_per_research: 2.5,
        total_researches: 4,
        domain_categories: { academic: 3 },
        top_domains: [{ domain: 'example.org' }],
        domain_distribution: { example: 2 },
        temporal_trend: [{ date: '2026-08-31', count: 2 }],
        domain_metrics: { 'example.org': { usage_count: 2 } },
    };
    const fetchMock = vi.fn().mockResolvedValue(new Response(JSON.stringify({
        status: 'success',
        data,
    }), { status: 200 }));
    vi.stubGlobal('fetch', fetchMock);

    await loadLinkAnalytics('7d');

    expect(fetchMock).toHaveBeenCalledWith(
        '/metrics/api/link-analytics?period=7d',
    );
    expect(document.getElementById('total-links').textContent).toBe('10');
    expect(document.getElementById('unique-domains').textContent).toBe('4');
    expect(calls.generateCategoryGrid)
        .toHaveBeenCalledWith(data.domain_categories);
    expect(calls.updateTopDomainsChart).toHaveBeenCalledWith(data.top_domains);
    expect(calls.updateDomainDistributionChart)
        .toHaveBeenCalledWith(data.domain_distribution);
    expect(calls.updateSourceTypeChart)
        .toHaveBeenCalledWith(data.domain_categories);
    expect(calls.updateTemporalTrendChart)
        .toHaveBeenCalledWith(data.temporal_trend);
    expect(calls.updateEnhancedDomainList).toHaveBeenCalledWith(
        data.top_domains,
        data.domain_metrics,
    );
    expect(document.getElementById('loading').style.display).toBe('none');
    expect(document.getElementById('content').style.display).toBe('block');
});

it('keeps the current link-analytics error visible and the content hidden', async () => {
    const loaderSource = extract(
        'link_analytics.html',
        /(async function loadLinkAnalytics\(period = '30d'\)\s*\{[\s\S]*?\n\})\n\nfunction generateCategoryGrid/,
        'loadLinkAnalytics()',
    );
    const calls = Object.fromEntries([
        'generateCategoryGrid',
        'updateTopDomainsChart',
        'updateDomainDistributionChart',
        'updateSourceTypeChart',
        'updateTemporalTrendChart',
        'updateEnhancedDomainList',
    ].map(name => [name, vi.fn()]));
    const declarations = Object.keys(calls)
        .map(name => `const ${name} = (...args) => calls.${name}(...args);`)
        .join('\n');
    const loadLinkAnalytics = new Function( // eslint-disable-line no-new-func
        'calls', `
        ${declarations}
        let linkAnalyticsRequestId = 0;
        ${loaderSource}
        return loadLinkAnalytics;
    `)(calls);
    document.body.innerHTML = `
        <div id="loading"></div><div id="content"></div>
        <div id="error"></div><div id="total-links"></div>
        <div id="unique-domains"></div><div id="avg-links"></div>
        <div id="total-researches"></div>
    `;
    vi.stubGlobal('fetch', vi.fn().mockResolvedValue(new Response(
        JSON.stringify({
            status: 'error',
            message: 'Link metrics index is rebuilding',
        }),
    )));
    vi.spyOn(console, 'error').mockImplementation(() => {});

    await loadLinkAnalytics('7d');

    const error = document.getElementById('error');
    expect(error.textContent)
        .toBe('Failed to load link analytics data. Please try again later.');
    expect(error.style.display).toBe('block');
    expect(document.getElementById('loading').style.display).toBe('none');
    expect(document.getElementById('content').style.display).toBe('none');
    for (const consumer of Object.values(calls)) {
        expect(consumer).not.toHaveBeenCalled();
    }
});

it('keeps a late older period response from replacing a real change event', async () => {
    const loaderSource = extract(
        'link_analytics.html',
        /(async function loadLinkAnalytics\(period = '30d'\)\s*\{[\s\S]*?\n\})\n\nfunction generateCategoryGrid/,
        'loadLinkAnalytics()',
    );
    const registrationSource = extract(
        'link_analytics.html',
        /(document\.getElementById\('period-select'\)\.addEventListener\('change',[\s\S]*?\n\}\);)/,
        'period-select change registration',
    );
    const calls = Object.fromEntries([
        'generateCategoryGrid',
        'updateTopDomainsChart',
        'updateDomainDistributionChart',
        'updateSourceTypeChart',
        'updateTemporalTrendChart',
        'updateEnhancedDomainList',
    ].map(name => [name, vi.fn()]));
    const declarations = Object.keys(calls)
        .map(name => `const ${name} = (...args) => calls.${name}(...args);`)
        .join('\n');
    document.body.innerHTML = `
        <select id="period-select">
            <option value="30d" selected>30 days</option>
            <option value="7d">7 days</option>
        </select>
        <div id="loading"></div><div id="content"></div><div id="error"></div>
        <div id="total-links"></div><div id="unique-domains"></div>
        <div id="avg-links"></div><div id="total-researches"></div>
    `;
    const runtime = new Function( // eslint-disable-line no-new-func
        'calls', `
        ${declarations}
        let linkAnalyticsRequestId = 0;
        ${loaderSource}
        ${registrationSource}
        return { loadLinkAnalytics };
    `)(calls);
    const olderResponse = deferred();
    const newerResponse = deferred();
    const fetchMock = vi.fn()
        .mockImplementationOnce(() => olderResponse.promise)
        .mockImplementationOnce(() => newerResponse.promise);
    vi.stubGlobal('fetch', fetchMock);

    const olderLoad = runtime.loadLinkAnalytics('30d');
    const periodSelect = document.getElementById('period-select');
    periodSelect.value = '7d';
    periodSelect.dispatchEvent(new Event('change', { bubbles: true }));

    expect(fetchMock.mock.calls.map(([url]) => url)).toEqual([
        '/metrics/api/link-analytics?period=30d',
        '/metrics/api/link-analytics?period=7d',
    ]);

    const newerData = {
        total_links: 7,
        total_unique_domains: 3,
        avg_links_per_research: 2.3,
        total_researches: 3,
        domain_categories: { current: 7 },
        top_domains: [{ domain: 'current.test' }],
        domain_distribution: { current: 7 },
        temporal_trend: [{ date: '2026-09-01', count: 7 }],
        domain_metrics: { 'current.test': { usage_count: 7 } },
    };
    newerResponse.resolve(new Response(JSON.stringify({
        status: 'success',
        data: newerData,
    }), { status: 200 }));
    await vi.waitFor(() => {
        expect(document.getElementById('total-links').textContent).toBe('7');
    });

    olderResponse.resolve(new Response(JSON.stringify({
        status: 'success',
        data: {
            ...newerData,
            total_links: 30,
            top_domains: [{ domain: 'stale.test' }],
        },
    }), { status: 200 }));
    await olderLoad;

    expect(document.getElementById('total-links').textContent).toBe('7');
    expect(calls.updateTopDomainsChart).toHaveBeenCalledOnce();
    expect(calls.updateTopDomainsChart)
        .toHaveBeenCalledWith([{ domain: 'current.test' }]);
    expect(document.getElementById('error').style.display).toBe('none');
    expect(document.getElementById('content').style.display).toBe('block');
});