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.

3.3 KB
Raw Blame
/**
 * Regression coverage for the results page's per-research context-overflow
 * request after a report loads successfully.
 */

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

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

beforeEach(() => {
    document.body.innerHTML = `
        <main id="results-content"></main>
        <button id="export-markdown-btn" disabled></button>
        <button id="download-pdf-btn" disabled></button>
        <button id="view-metrics-btn">View metrics</button>
        <div id="context-overflow-warning" style="display: none">
            <span id="context-overflow-message"></span>
            <a id="context-overflow-action">View details</a>
        </div>
    `;
});

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

it('uses the canonical endpoint and decorates the page when truncation occurred', async () => {
    vi.stubGlobal('URLBuilder', window.URLBuilder);
    vi.stubGlobal('URLS', window.URLS);
    vi.spyOn(window.URLBuilder, 'extractResearchIdFromPattern').mockReturnValue(RESEARCH_ID);
    const safeAssign = vi.fn((target, attribute, value) => {
        target.setAttribute(attribute, value);
    });
    vi.stubGlobal('URLValidator', { safeAssign });

    const fetchMock = vi.fn(url => {
        if (url === `/api/report/${RESEARCH_ID}`) {
            return Promise.resolve({
                ok: true,
                json: vi.fn().mockResolvedValue({
                    content: '# Finished report',
                    metadata: { query: 'Migration coverage' },
                }),
            });
        }
        if (url === CONTEXT_URL) {
            return Promise.resolve({
                ok: true,
                json: vi.fn().mockResolvedValue({
                    status: 'success',
                    data: {
                        overview: {
                            truncation_occurred: true,
                            truncated_count: 1,
                            tokens_lost: 4096,
                            context_limit: 32768,
                        },
                    },
                }),
            });
        }
        throw new Error(`Unexpected request: ${url}`);
    });
    vi.stubGlobal('fetch', fetchMock);

    await import('@js/components/results.js');
    if (!fetchMock.mock.calls.some(([url]) => url === `/api/report/${RESEARCH_ID}`)) {
        document.dispatchEvent(new Event('DOMContentLoaded'));
    }

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

    expect(fetchMock).toHaveBeenCalledWith(CONTEXT_URL);
    expect(document.getElementById('context-overflow-message').textContent)
        .toContain('Research was truncated 1 time(s) due to context limits.');
    expect(document.getElementById('context-overflow-message').textContent)
        .toContain('~4,096 tokens lost.');
    expect(document.getElementById('context-overflow-action').getAttribute('href'))
        .toBe(`/details/${RESEARCH_ID}#context-overflow-section`);
    expect(document.getElementById('view-metrics-btn').classList)
        .toContain('ldr-metrics-btn-overflow');
    expect(document.querySelector('#view-metrics-btn .ldr-badge-overflow').textContent)
        .toBe('OVERFLOW');
});