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.5 KB
Raw Blame
#!/usr/bin/env node
/**
 * Pagination Query-Param Robustness (CI-safe)
 * =============================================
 *
 * General migration-risk coverage (not tied to a specific feature PR):
 * every UI test that touches /library/ or /library/download-manager clicks
 * "Next" at most, and none of them probe the ?page= query-string parsing
 * itself. Both routes (web/routers/library.py::library_page and
 * ::download_manager_page) hand-parse pagination straight off
 * request.query_params — the FastAPI equivalent of Flask's
 * request.args.get('page', 1, type=int), which silently defaults on a
 * malformed value instead of raising. The hand-rolled version in this repo
 * DOES wrap the int() conversion in try/except ValueError and clamps the
 * result (`max(1, min(page, total_pages))`), matching Flask's forgiving
 * behavior — this suite is the browser-level proof that survived the port,
 * across a battery of adversarial values a user could produce just by
 * editing the address bar or a bookmarked/shared URL (non-numeric,
 * negative, zero, absurdly large).
 *
 * Also covers GET /library/api/documents?limit=&offset=, which has its own
 * independent try/except ValueError + clamp (issue #4560: an unclamped
 * negative SQLite LIMIT means "no limit", so a malformed/negative limit
 * must never reach the query unclamped).
 *
 * A regression here would surface as a raw JSON 500
 * (`{"error": "Server error"}`, the FastAPI catch-all in fastapi_app.py)
 * rendered in the browser instead of the page — exactly what this suite's
 * "crashed" check looks for, independent of the HTTP status Puppeteer
 * reports (a custom exception handler can still return 200-shaped noise).
 *
 * Run: node test_pagination_query_params_ci.js
 */

const { setupTest, teardownTest, TestResults, log, withTimeout } = require('./test_lib');

// Values a user could produce by hand-editing the address bar, a stale
// bookmark, or a shared link — never through clicking a rendered pagination
// link (those are always positive integers <= total_pages).
const ADVERSARIAL_PAGE_VALUES = ['abc', '-1', '0', '999999', '1.5', ''];

async function loadAndCheckPage(page, url, containerSelector) {
    const response = await page.goto(url, { waitUntil: 'domcontentloaded', timeout: 30000 });
    const status = response ? response.status() : null;
    const bodyText = await page.evaluate(() => document.body.innerText.slice(0, 500)).catch(() => '');
    const crashed = /"error"\s*:\s*"Server error"/.test(bodyText) || status === 500;
    const hasContainer = !!(await page.$(containerSelector));
    return { status, crashed, hasContainer, bodyText };
}

async function main() {
    log.section('Pagination Query-Param Robustness');
    const ctx = await setupTest({ authenticate: true });
    const results = new TestResults('Pagination Query-Param Robustness Tests');
    const { page } = ctx;
    const { baseUrl } = ctx.config;
    const subTestTimeout = ctx.config.isCI ? 60000 : 30000;

    async function run(category, name, testFn) {
        try {
            const result = await withTimeout(testFn(page, baseUrl), subTestTimeout, `${category}/${name}`);
            if (result && result.skipped) {
                results.skip(category, name, result.message);
            } else {
                results.add(category, name, result.passed, result.message || '');
            }
        } catch (error) {
            results.add(category, name, false, `Error: ${error.message}`);
        }
    }

    try {
        // ===============================================================
        // 1. Library documents page (/library/)
        // ===============================================================
        for (const pageValue of ADVERSARIAL_PAGE_VALUES) {
            await run(
                'Library page',
                `?page=${JSON.stringify(pageValue)} does not crash the page`,
                async () => {
                    const r = await loadAndCheckPage(
                        page,
                        `${baseUrl}/library/?page=${encodeURIComponent(pageValue)}`,
                        '.ldr-library-container',
                    );
                    const passed = !r.crashed && r.hasContainer && r.status !== null && r.status < 500;
                    return {
                        passed,
                        message: passed
                            ? `HTTP ${r.status}, page rendered normally`
                            : `HTTP ${r.status}, crashed=${r.crashed}, container=${r.hasContainer}, body="${r.bodyText.slice(0, 120)}"`,
                    };
                },
            );
        }

        // ===============================================================
        // 2. Download manager page (/library/download-manager)
        // ===============================================================
        for (const pageValue of ADVERSARIAL_PAGE_VALUES) {
            await run(
                'Download manager page',
                `?page=${JSON.stringify(pageValue)} does not crash the page`,
                async () => {
                    const r = await loadAndCheckPage(
                        page,
                        `${baseUrl}/library/download-manager?page=${encodeURIComponent(pageValue)}`,
                        '.ldr-download-manager-container',
                    );
                    const passed = !r.crashed && r.hasContainer && r.status !== null && r.status < 500;
                    return {
                        passed,
                        message: passed
                            ? `HTTP ${r.status}, page rendered normally`
                            : `HTTP ${r.status}, crashed=${r.crashed}, container=${r.hasContainer}, body="${r.bodyText.slice(0, 120)}"`,
                    };
                },
            );
        }

        // ===============================================================
        // 3. A genuinely valid page=1 still round-trips correctly
        //    (regression guard on the adversarial-value handling above:
        //    proves the try/except path isn't just swallowing everything)
        // ===============================================================
        await run('Library page', 'A well-formed ?page=1 renders normally', async () => {
            const r = await loadAndCheckPage(page, `${baseUrl}/library/?page=1`, '.ldr-library-container');
            const passed = !r.crashed && r.hasContainer && r.status === 200;
            return {
                passed,
                message: passed ? `HTTP ${r.status}, page rendered normally` : `HTTP ${r.status}, crashed=${r.crashed}, container=${r.hasContainer}`,
            };
        });

        // ===============================================================
        // 4. GET /library/api/documents?limit=&offset= — independent
        //    try/except + clamp (issue #4560: unclamped negative SQLite
        //    LIMIT means "no limit").
        // ===============================================================
        const adversarialLimitOffset = [
            { limit: 'abc', offset: 'abc', expectedStatus: 200 },
            { limit: '-1', offset: '-5', expectedStatus: 200 },
            { limit: '0', offset: '0', expectedStatus: 200 },
            { limit: '20', offset: '37', expectedStatus: 200 },
            { limit: '20', offset: '10000', expectedStatus: 200 },
            { limit: '20', offset: '10001', expectedStatus: 400 },
            { limit: '999999999', offset: '999999999', expectedStatus: 400 },
        ];
        for (const { limit, offset, expectedStatus } of adversarialLimitOffset) {
            await run(
                'Documents API',
                `?limit=${limit}&offset=${offset} returns HTTP ${expectedStatus}`,
                async () => {
                    const result = await page.evaluate(async (u) => {
                        const r = await fetch(u, { credentials: 'same-origin' });
                        const status = r.status;
                        const body = await r.json().catch(() => null);
                        return { status, body };
                    }, `/library/api/documents?limit=${encodeURIComponent(limit)}&offset=${encodeURIComponent(offset)}`);

                    // Ordinary offsets and malformed-number fallbacks must
                    // still return documents. Only offsets above the ceiling
                    // receive the specific pagination error.
                    const isBounded =
                        result.status === 200 &&
                        result.body &&
                        Array.isArray(result.body.documents);
                    const isRejected =
                        result.status === 400 &&
                        result.body &&
                        result.body.error === 'Offset must not exceed 10000';
                    const passed = expectedStatus === 400 ? isRejected : isBounded;
                    return {
                        passed,
                        message: passed
                            ? `HTTP ${result.status} (${isBounded ? 'bounded list' : 'rejected: ' + result.body.error.slice(0, 80)})`
                            : `HTTP ${result.status}, body=${JSON.stringify(result.body).slice(0, 150)}`,
                    };
                },
            );
        }
    } catch (error) {
        log.error(`Fatal error: ${error.message}`);
        console.error(error.stack);
    } finally {
        results.print();
        results.save();
        await teardownTest(ctx);
        process.exit(results.exitCode());
    }
}

main().catch((error) => {
    console.error('Test runner failed:', error);
    process.exit(1);
});