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.

9.8 KB
Raw Blame
/**
 * Results-page export contracts affected by the FastAPI migration.
 *
 * POST /api/v1/* is no longer CSRF-exempt, so exercise the real results
 * component bootstrap and its actual export buttons to ensure every report
 * export carries the token minted for the current browser session.
 */

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

const RESEARCH_ID = 'migration-3299-export';
const REPORT_URL = `/api/report/${RESEARCH_ID}`;
const CONTEXT_URL = `/api/research/${RESEARCH_ID}/context-overflow`;
const CSRF_TOKEN = 'csrf-migration-3299';
const originalReadyState = Object.getOwnPropertyDescriptor(document, 'readyState');

const formatCases = [
    ['LaTeX', 'export-latex-btn', 'latex', 'tex'],
    ['Quarto', 'export-quarto-btn', 'quarto', 'qmd'],
    ['ODT', 'export-odt-btn', 'odt', 'odt'],
    ['RIS', 'export-ris-btn', 'ris', 'ris'],
];

function buildResultsPage() {
    document.body.innerHTML = `
        <main id="results-content"></main>
        <button id="export-markdown-btn" disabled>Markdown</button>
        <button id="download-pdf-btn" disabled>PDF</button>
        <button id="export-latex-btn">LaTeX</button>
        <button id="export-quarto-btn">Quarto</button>
        <button id="export-odt-btn">ODT</button>
        <button id="export-ris-btn">RIS</button>
    `;
}

function createFetchMock(exportUrl) {
    return vi.fn(input => {
        const url = String(input);
        if (url === REPORT_URL) {
            return Promise.resolve({
                ok: true,
                json: vi.fn().mockResolvedValue({
                    content: '# Completed migration report',
                    metadata: { query: 'FastAPI export coverage' },
                }),
            });
        }
        if (url === CONTEXT_URL) {
            return Promise.resolve({ ok: false, status: 404 });
        }
        if (url === exportUrl) {
            return Promise.resolve({
                ok: true,
                blob: vi.fn().mockResolvedValue(new Blob(['exported report'])),
            });
        }
        throw new Error(`Unexpected request: ${url}`);
    });
}

function bootstrapResults(fetchMock) {
    vi.stubGlobal('fetch', fetchMock);
    document.dispatchEvent(new Event('DOMContentLoaded'));
}

beforeAll(async () => {
    // Register one real DOMContentLoaded bootstrap listener. Dispatching the
    // event per test reinitializes the component against each fresh fixture.
    Object.defineProperty(document, 'readyState', {
        configurable: true,
        get: () => 'loading',
    });
    await import('@js/components/results.js');
});

beforeEach(() => {
    buildResultsPage();
    window.history.replaceState({}, '', `/results/${RESEARCH_ID}`);
    window.api = { getCsrfToken: vi.fn(() => CSRF_TOKEN) };

    vi.stubGlobal('alert', vi.fn());
    vi.stubGlobal('URLValidator', {
        safeAssign: vi.fn((element, attribute, value) => {
            element.setAttribute(attribute, value);
            return true;
        }),
    });
    vi.spyOn(URL, 'createObjectURL').mockReturnValue('blob:migration-export');
    vi.spyOn(URL, 'revokeObjectURL').mockImplementation(() => {});
    vi.spyOn(window.HTMLAnchorElement.prototype, 'click')
        .mockImplementation(() => {});
});

afterEach(() => {
    vi.restoreAllMocks();
    vi.unstubAllGlobals();
    delete window.api;
    document.body.replaceChildren();
    window.history.replaceState({}, '', '/');
});

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

describe('results format exports after the FastAPI migration', () => {
    it.each(formatCases)(
        '%s button POSTs to its exact /api/v1 URL with CSRF',
        async (_label, buttonId, format, extension) => {
            const exportUrl = `/api/v1/research/${RESEARCH_ID}/export/${format}`;
            const fetchMock = createFetchMock(exportUrl);
            bootstrapResults(fetchMock);

            await vi.waitFor(() => {
                expect(fetchMock).toHaveBeenCalledWith(REPORT_URL);
                expect(document.getElementById('download-pdf-btn').disabled)
                    .toBe(false);
            });

            document.getElementById(buttonId).click();

            await vi.waitFor(() => {
                expect(fetchMock).toHaveBeenCalledWith(exportUrl, {
                    method: 'POST',
                    headers: { 'X-CSRFToken': CSRF_TOKEN },
                });
                expect(window.HTMLAnchorElement.prototype.click)
                    .toHaveBeenCalledOnce();
            });

            expect(window.api.getCsrfToken).toHaveBeenCalledOnce();
            expect(URL.revokeObjectURL).toHaveBeenCalledWith(
                'blob:migration-export',
            );
            const downloadLink = URLValidator.safeAssign.mock.calls.find(
                ([, attribute, value]) => (
                    attribute === 'href' && value === 'blob:migration-export'
                ),
            )[0];
            expect(downloadLink.download)
                .toBe(`research_${RESEARCH_ID}.${extension}`);
            expect(alert).not.toHaveBeenCalled();
        },
    );

    it('PDF button POSTs with CSRF, JSON content type, and same-origin credentials', async () => {
        const exportUrl = `/api/v1/research/${RESEARCH_ID}/export/pdf`;
        const fetchMock = createFetchMock(exportUrl);
        bootstrapResults(fetchMock);

        await vi.waitFor(() => {
            expect(document.getElementById('download-pdf-btn').disabled)
                .toBe(false);
        });

        document.getElementById('download-pdf-btn').click();

        await vi.waitFor(() => {
            expect(fetchMock).toHaveBeenCalledWith(exportUrl, {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                    'X-CSRFToken': CSRF_TOKEN,
                },
                credentials: 'same-origin',
            });
            expect(URL.revokeObjectURL).toHaveBeenCalledWith(
                'blob:migration-export',
            );
        });

        expect(window.api.getCsrfToken).toHaveBeenCalledOnce();
        expect(window.HTMLAnchorElement.prototype.click).toHaveBeenCalledOnce();
        const downloadLink = URLValidator.safeAssign.mock.calls.find(
            ([, attribute, value]) => (
                attribute === 'href' && value === 'blob:migration-export'
            ),
        )[0];
        expect(downloadLink.download).toBe(`research_${RESEARCH_ID}.pdf`);
        expect(alert).not.toHaveBeenCalled();
    });

    it('surfaces FastAPI detail when a format export is rejected', async () => {
        const exportUrl = `/api/v1/research/${RESEARCH_ID}/export/latex`;
        const fetchMock = createFetchMock(exportUrl);
        fetchMock.mockImplementation(input => {
            const url = String(input);
            if (url === REPORT_URL) {
                return Promise.resolve({
                    ok: true,
                    json: vi.fn().mockResolvedValue({
                        content: '# Export failure report',
                        metadata: { query: 'FastAPI error detail' },
                    }),
                });
            }
            if (url === CONTEXT_URL) {
                return Promise.resolve({ ok: false, status: 404 });
            }
            if (url === exportUrl) {
                return Promise.resolve({
                    ok: false,
                    json: vi.fn().mockResolvedValue({
                        detail: 'LaTeX conversion is unavailable',
                    }),
                });
            }
            throw new Error(`Unexpected request: ${url}`);
        });
        bootstrapResults(fetchMock);

        await vi.waitFor(() => {
            expect(document.getElementById('download-pdf-btn').disabled)
                .toBe(false);
        });
        document.getElementById('export-latex-btn').click();

        await vi.waitFor(() => {
            expect(alert).toHaveBeenCalledWith(
                'Failed to export to latex: LaTeX conversion is unavailable',
            );
        });
        expect(window.HTMLAnchorElement.prototype.click).not.toHaveBeenCalled();
    });

    it('restores the PDF button when generation fails', async () => {
        const exportUrl = `/api/v1/research/${RESEARCH_ID}/export/pdf`;
        const fetchMock = createFetchMock(exportUrl);
        fetchMock.mockImplementation(input => {
            const url = String(input);
            if (url === REPORT_URL) {
                return Promise.resolve({
                    ok: true,
                    json: vi.fn().mockResolvedValue({
                        content: '# PDF recovery report',
                        metadata: { query: 'PDF failure recovery' },
                    }),
                });
            }
            if (url === CONTEXT_URL) {
                return Promise.resolve({ ok: false, status: 404 });
            }
            if (url === exportUrl) {
                return Promise.resolve({ ok: false, status: 503 });
            }
            throw new Error(`Unexpected request: ${url}`);
        });
        bootstrapResults(fetchMock);

        await vi.waitFor(() => {
            expect(document.getElementById('download-pdf-btn').disabled)
                .toBe(false);
        });
        const pdfButton = document.getElementById('download-pdf-btn');
        pdfButton.click();
        expect(pdfButton.disabled).toBe(true);
        expect(pdfButton.textContent).toContain('Generating PDF');

        await vi.waitFor(() => {
            expect(alert).toHaveBeenCalledWith(
                'Error generating PDF: HTTP error! status: 503',
            );
        });
        expect(pdfButton.disabled).toBe(false);
        expect(pdfButton.textContent).toContain('Download PDF');
        expect(window.HTMLAnchorElement.prototype.click).not.toHaveBeenCalled();
    });
});