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.3 KB
Raw Blame
import '../helpers/safe-fetch-with-auth-to-fetch.js';

/**
 * Runtime contract for the migrated research-details metrics loader.
 *
 * The optional timeline/search/link/context endpoints are deliberately
 * unavailable here: the primary FastAPI responses must still populate and
 * reveal the page rather than turn optional telemetry into a fatal error.
 */

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

const RESEARCH_ID = 'metrics-runtime-42';

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

beforeEach(() => {
    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>
        <canvas id="timeline-chart"></canvas>
        <canvas id="search-chart"></canvas>
    `;
});

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

it('renders primary metrics through canonical routes when optional metrics are unavailable', async () => {
    vi.stubGlobal('URLBuilder', window.URLBuilder);
    vi.stubGlobal('URLS', window.URLS);
    vi.spyOn(window.URLBuilder, 'extractResearchIdFromPattern').mockReturnValue(RESEARCH_ID);

    const fetchMock = vi.fn(url => {
        if (url === `/history/details/${RESEARCH_ID}`) {
            return Promise.resolve(response({
                query: 'FastAPI migration evidence',
                mode: 'detailed',
                strategy: 'source-based',
                progress: 100,
            }));
        }
        if (url === `/metrics/api/metrics/research/${RESEARCH_ID}`) {
            return Promise.resolve(response({
                status: 'success',
                metrics: {
                    total_tokens: 12345,
                    total_calls: 3,
                    model_usage: [{
                        model: 'test-model',
                        prompt_tokens: 8000,
                        completion_tokens: 4345,
                        calls: 3,
                    }],
                },
            }));
        }
        return Promise.resolve(response({}, 503));
    });
    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');
    });

    const requestedUrls = fetchMock.mock.calls.map(([url]) => String(url));
    expect(requestedUrls).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`,
    ]));
    expect(document.getElementById('total-tokens').textContent).toBe('12,345');
    expect(document.getElementById('prompt-tokens').textContent).toBe('8,000');
    expect(document.getElementById('completion-tokens').textContent).toBe('4,345');
    expect(document.getElementById('llm-calls').textContent).toBe('3');
    expect(document.getElementById('model-used').textContent).toBe('test-model');
    expect(document.getElementById('research-query').textContent)
        .toBe('FastAPI migration evidence');
    expect(document.getElementById('token-metrics-section').style.display).toBe('block');
    expect(document.getElementById('error').style.display).toBe('none');
});