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

/**
 * Regression coverage for the details page's per-research context-overflow
 * request. The page initializes through its production DOMContentLoaded path.
 */

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

const RESEARCH_ID = 'details-overflow-42';
const CONTEXT_URL = `/api/research/${RESEARCH_ID}/context-overflow`;

function response({ ok, status, body }) {
    return {
        ok,
        status,
        url: '',
        json: vi.fn().mockResolvedValue(body),
        text: vi.fn().mockResolvedValue(''),
    };
}

beforeEach(() => {
    document.body.innerHTML = `
        <div id="loading"></div>
        <div id="error"></div>
        <div id="details-content"></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>

        <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>
            <div id="co-performance-warning" style="display: none"></div>
        </section>
    `;

    window.contextOverflowShared = {
        renderTruncationBadge: vi.fn(count => `<span>${count} truncations</span>`),
    };
});

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

it('loads the canonical per-research endpoint and reveals successful overflow metrics', 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 === CONTEXT_URL) {
            return Promise.resolve(response({
                ok: true,
                status: 200,
                body: {
                    status: 'success',
                    data: {
                        overview: {
                            total_tokens: 12345,
                            context_limit: 8192,
                            max_tokens_used: 9000,
                            truncation_occurred: true,
                            truncated_count: 2,
                        },
                        phase_stats: {},
                        requests: [],
                        model: 'test-model',
                        provider: 'test-provider',
                    },
                },
            }));
        }

        // The details initializer also starts its independent metrics and link
        // requests. They are outside this contract and may fail without
        // preventing context-overflow data from rendering.
        return Promise.resolve(response({ ok: false, status: 503, body: {} }));
    });
    vi.stubGlobal('fetch', fetchMock);

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

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

    expect(fetchMock).toHaveBeenCalledWith(CONTEXT_URL);
    expect(document.getElementById('co-total-tokens').textContent).toBe('12,345');
    expect(document.getElementById('co-context-limit').textContent).toBe('8,192');
    expect(document.getElementById('co-max-tokens').textContent).toBe('9,000');
    expect(window.contextOverflowShared.renderTruncationBadge).toHaveBeenCalledWith(2);
    expect(document.getElementById('co-performance-warning').style.display).toBe('flex');
});