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.0 KB
Raw Blame
/**
 * Results-page migration regressions.
 *
 * Exercise the real page bootstrap and observe the recovery UI produced for
 * report-loading failures.  The home link is especially important after the
 * FastAPI migration: it must return to the mounted research page, rather than
 * to a removed Flask-era route.
 */

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

const REPORT_PATH = '/api/report/migration-3299';
const originalFetch = globalThis.fetch;
const originalReadyState = Object.getOwnPropertyDescriptor(document, 'readyState');

function buildResultsPage() {
    document.body.innerHTML = `
        <button id="export-markdown-btn">Export</button>
        <button id="download-pdf-btn">PDF</button>
        <div id="results-content"></div>
    `;
}

function dispatchPageLoad(path = '/results/migration-3299') {
    window.history.replaceState({}, '', path);
    document.dispatchEvent(new Event('DOMContentLoaded'));
}

function recoveryLink() {
    return document.querySelector('#results-content a');
}

beforeAll(async () => {
    // Register the component's normal DOMContentLoaded bootstrap so each test
    // can exercise a fresh DOM fixture without adding a test-only export.
    Object.defineProperty(document, 'readyState', {
        configurable: true,
        get: () => 'loading',
    });
    await import('@js/components/results.js');
});

beforeEach(() => {
    buildResultsPage();
});

afterEach(() => {
    vi.restoreAllMocks();
});

afterAll(() => {
    globalThis.fetch = originalFetch;
    if (originalReadyState) {
        Object.defineProperty(document, 'readyState', originalReadyState);
    } else {
        delete document.readyState;
    }
});

describe('results page error recovery', () => {
    it('offers a return to research when the migrated report route responds with an error', async () => {
        globalThis.fetch = vi.fn(() => Promise.resolve({
            ok: false,
            status: 404,
        }));

        dispatchPageLoad();

        await vi.waitFor(() => {
            expect(recoveryLink()).not.toBeNull();
        });

        expect(globalThis.fetch).toHaveBeenCalledWith(REPORT_PATH);
        expect(document.querySelector('[role="alert"]').textContent).toContain(
            'Error loading research results: HTTP error 404',
        );
        expect(recoveryLink().textContent).toContain('Back to Research');
        expect(recoveryLink().getAttribute('href')).toBe('/');
        expect(document.getElementById('export-markdown-btn').disabled).toBe(true);
        expect(document.getElementById('download-pdf-btn').disabled).toBe(true);
    });

    it('keeps the same recovery destination after a network failure', async () => {
        globalThis.fetch = vi.fn(() => Promise.reject(new Error('connection lost')));

        dispatchPageLoad();

        await vi.waitFor(() => {
            expect(document.querySelector('[role="alert"]')?.textContent).toContain(
                'connection lost',
            );
        });

        expect(recoveryLink().getAttribute('href')).toBe('/');
    });

    it('renders server failure details as text without corrupting the recovery link', async () => {
        const unsafeMessage = '<img src=x onerror="alert(1)"> unavailable';
        globalThis.fetch = vi.fn(() => Promise.reject(new Error(unsafeMessage)));

        dispatchPageLoad();

        await vi.waitFor(() => {
            expect(document.querySelector('[role="alert"]')?.textContent).toContain(
                unsafeMessage,
            );
        });

        expect(document.querySelector('#results-content img')).toBeNull();
        expect(recoveryLink().getAttribute('href')).toBe('/');
    });

    it('provides recovery without making a request when the results URL has no research id', () => {
        globalThis.fetch = vi.fn();

        dispatchPageLoad('/results/');

        expect(globalThis.fetch).not.toHaveBeenCalled();
        expect(document.querySelector('[role="alert"]').textContent).toContain(
            'Research ID not found in URL',
        );
        expect(recoveryLink().getAttribute('href')).toBe('/');
    });
});