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.

29.0 KB
Raw Blame
#!/usr/bin/env node
/**
 * Download + CSRF-protected Form/API Flow Tests (Flask -> FastAPI migration)
 *
 * Two request shapes changed meaning during the migration and neither is
 * exercised end-to-end in a real browser anywhere else in this suite:
 *
 * 1. CSRF enforcement moved from flask-wtf's CSRFProtect to a hand-rolled
 *    ASGI middleware (src/local_deep_research/web/dependencies/csrf.py).
 *    It validates the `X-CSRFToken` header (the path the app's own JS
 *    always uses, e.g. api.js's fetchWithErrorHandling / collections_manager.js)
 *    against a per-session token minted by `csrf_token()` (rendered into
 *    every page's `<meta name="csrf-token">` by base.html, same value
 *    `/auth/csrf-token` mints) or, for legacy urlencoded forms with no JS,
 *    a buffered `csrf_token` body field. This file proves BOTH directions
 *    on a real mutation: a request missing the token is rejected (403),
 *    and the SAME mutation performed the way the app itself does it (the
 *    minted header) succeeds. Testing only rejection would still pass if
 *    CSRF were broken so badly nothing worked at all — the success half is
 *    the half that actually proves the feature functions.
 *
 * 2. Binary "download" endpoints were changed from
 *    `StreamingResponse(BytesIO(...))` to a plain `Response(content=...)`
 *    (see library.py's PDF route and research.py's report-export route —
 *    both carry the same comment). The reason: iterating a BytesIO yields
 *    one chunk per 0x0A byte, which for binary data is thousands of tiny
 *    ASGI sends AND suppresses Content-Length entirely (Starlette only
 *    sets Content-Length when it knows the full body up front). This file
 *    downloads a real file through a real browser fetch and asserts
 *    Content-Length is present and matches the actual byte count — the
 *    exact thing that regresses if a download route reverts to streaming
 *    a fully-buffered payload.
 *
 * Endpoint chosen for the download check: GET /library/document/<id>/pdf.
 * It needs a document with PDF bytes but NOT a completed research/LLM job —
 * a document collection + file upload (pdf_storage=database) is a normal,
 * cheap, real user action. The research-report export endpoint
 * (POST /api/v1/research/<id>/export/<format>) carries the identical
 * plain-Response fix but requires an existing ResearchHistory row with
 * assembled report content, which this suite has no LLM-free way to
 * produce without seeding the server's encrypted DB directly (see
 * scripts/ci/seed_research_cancellation.py for how heavy that is — it
 * seeds BEFORE the server boots). The library PDF route is the reachable
 * sibling call site of the same bug fix, so it is pinned here instead of
 * faking the research export.
 *
 * Fixture: FIXTURE_PDF_B64 below is a hand-built, byte-exact, minimal
 * one-page PDF (Catalog/Pages/Page/Font/Contents objects + a correct xref
 * table) containing the text "LDR UI download test fixture 12345" drawn
 * with the standard Helvetica font. It was verified directly against this
 * repo's own extraction path:
 *   PYTHONPATH=src .venv/bin/python -c
 *     "from local_deep_research.document_loaders import extract_text_from_bytes;
 *      print(extract_text_from_bytes(open('fixture.pdf','rb').read(), '.pdf', 'f.pdf'))"
 *   -> 'LDR UI download test fixture 12345'
 * so the upload's text-extraction step (which upload_to_collection requires
 * to succeed) does not reject it.
 *
 * No LLM required. Registered in the `api-crud` shard (CSRF + download are
 * both API-mutation concerns, same theme as its siblings).
 *
 * Prerequisites: Web server on http://127.0.0.1:5000 (override BASE_URL).
 */

const { randomUUID } = require('node:crypto');
const puppeteer = require('puppeteer');
const AuthHelper = require('./auth_helper');
const { getPuppeteerLaunchOptions } = require('./puppeteer_config');
const { captureOnFailure } = require('./screenshot_helper');

const BASE_URL = process.env.BASE_URL || 'http://127.0.0.1:5000';
const isCI = !!process.env.CI;

const TIMEOUTS = {
    navigation: isCI ? 60000 : 30000,
    selector: isCI ? 30000 : 10000,
};

// See the file-header comment for how this was generated and verified.
const FIXTURE_PDF_B64 =
    'JVBERi0xLjQKMSAwIG9iago8PCAvVHlwZSAvQ2F0YWxvZyAvUGFnZXMgMiAwIFIgPj4KZW5kb2Jq' +
    'CjIgMCBvYmoKPDwgL1R5cGUgL1BhZ2VzIC9LaWRzIFszIDAgUl0gL0NvdW50IDEgPj4KZW5kb2Jq' +
    'CjMgMCBvYmoKPDwgL1R5cGUgL1BhZ2UgL1BhcmVudCAyIDAgUiAvUmVzb3VyY2VzIDw8IC9Gb250' +
    'IDw8IC9GMSA0IDAgUiA+PiA+PiAvTWVkaWFCb3ggWzAgMCAzMDAgMjAwXSAvQ29udGVudHMgNSAw' +
    'IFIgPj4KZW5kb2JqCjQgMCBvYmoKPDwgL1R5cGUgL0ZvbnQgL1N1YnR5cGUgL1R5cGUxIC9CYXNl' +
    'Rm9udCAvSGVsdmV0aWNhID4+CmVuZG9iago1IDAgb2JqCjw8IC9MZW5ndGggNjUgPj4Kc3RyZWFt' +
    'CkJUIC9GMSAxOCBUZiAyMCAxMDAgVGQgKExEUiBVSSBkb3dubG9hZCB0ZXN0IGZpeHR1cmUgMTIz' +
    'NDUpIFRqIEVUCmVuZHN0cmVhbQplbmRvYmoKeHJlZgowIDYKMDAwMDAwMDAwMCA2NTUzNSBmIAow' +
    'MDAwMDAwMDA5IDAwMDAwIG4gCjAwMDAwMDAwNTggMDAwMDAgbiAKMDAwMDAwMDExNSAwMDAwMCBu' +
    'IAowMDAwMDAwMjQxIDAwMDAwIG4gCjAwMDAwMDAzMTEgMDAwMDAgbiAKdHJhaWxlcgo8PCAvU2l6' +
    'ZSA2IC9Sb290IDEgMCBSID4+CnN0YXJ0eHJlZgo0MjYKJSVFT0Y=';
const FIXTURE_PDF_FILENAME = 'ldr-ui-download-fixture.pdf';
const SCREENSHOT_PREFIX = 'download_csrf';

async function getCsrf(page) {
    return page.evaluate(() => {
        const m = document.querySelector('meta[name="csrf-token"]');
        return m ? m.content : '';
    });
}

/** JSON POST from page context. `csrf=null` deliberately omits the header. */
async function postJson(page, urlOuter, csrfOuter, bodyOuter) {
    return page.evaluate(
        async ({ url, csrf, body }) => {
            const headers = { 'Content-Type': 'application/json' };
            if (csrf) headers['X-CSRFToken'] = csrf;
            const resp = await fetch(url, {
                method: 'POST',
                credentials: 'same-origin',
                headers,
                body: JSON.stringify(body),
            });
            let json = null;
            try {
                json = await resp.json();
            } catch (_) {}
            return { status: resp.status, ok: resp.ok, json };
        },
        { url: urlOuter, csrf: csrfOuter, body: bodyOuter }
    );
}

/** Multipart file upload from page context. `csrf=null` omits the header. */
async function uploadPdf(page, urlOuter, csrfOuter, base64Outer, filenameOuter, extraFieldsOuter) {
    return page.evaluate(
        async ({ url, csrf, base64, filename, extraFields }) => {
            // window.atob, not bare atob: this repo's eslint config does
            // not allowlist `atob` as a global for page.evaluate bodies,
            // but does allowlist `window`.
            const binaryStr = window.atob(base64);
            const bytes = new Uint8Array(binaryStr.length);
            for (let i = 0; i < binaryStr.length; i++) bytes[i] = binaryStr.charCodeAt(i);
            const file = new File([bytes], filename, { type: 'application/pdf' });
            const fd = new FormData();
            fd.append('files', file);
            for (const [k, v] of Object.entries(extraFields || {})) fd.append(k, v);
            const headers = {};
            if (csrf) headers['X-CSRFToken'] = csrf;
            const resp = await fetch(url, {
                method: 'POST',
                credentials: 'same-origin',
                headers,
                body: fd,
            });
            let json = null;
            try {
                json = await resp.json();
            } catch (_) {}
            return { status: resp.status, ok: resp.ok, json };
        },
        { url: urlOuter, csrf: csrfOuter, base64: base64Outer, filename: filenameOuter, extraFields: extraFieldsOuter }
    );
}

/** DELETE from page context (best-effort cleanup helper). */
async function deleteWithCsrf(page, urlOuter, csrfOuter) {
    return page.evaluate(
        async ({ url, csrf }) => {
            const headers = {};
            if (csrf) headers['X-CSRFToken'] = csrf;
            try {
                const resp = await fetch(url, { method: 'DELETE', credentials: 'same-origin', headers });
                return { status: resp.status };
            } catch (e) {
                return { status: 0, error: String(e) };
            }
        },
        { url: urlOuter, csrf: csrfOuter }
    );
}

/** GET a binary response and report status/headers/actual byte count/magic bytes. */
async function fetchBinaryMeta(page, urlOuter) {
    return page.evaluate(async (url) => {
        const resp = await fetch(url, { credentials: 'same-origin' });
        const buf = await resp.arrayBuffer();
        const bytes = new Uint8Array(buf);
        let magic = '';
        for (let i = 0; i < Math.min(4, bytes.length); i++) magic += String.fromCharCode(bytes[i]);
        const headers = {};
        resp.headers.forEach((v, k) => {
            headers[k] = v;
        });
        return { status: resp.status, headers, byteLength: buf.byteLength, magic };
    }, urlOuter);
}

async function run() {
    console.log(`Running download & CSRF-protected form/API flow tests (CI mode: ${isCI})`);

    const browser = await puppeteer.launch(getPuppeteerLaunchOptions());
    const page = await browser.newPage();
    await page.setViewport({ width: 1280, height: 800 });
    if (isCI) {
        page.setDefaultTimeout(60000);
        page.setDefaultNavigationTimeout(60000);
    }

    page.on('pageerror', (e) => console.log('PAGE ERROR:', e.message));
    page.on('console', (m) => {
        if (m.type() === 'error') console.log('BROWSER ERROR:', m.text());
    });

    const auth = new AuthHelper(page, BASE_URL);

    let passed = 0;
    let failed = 0;
    let collectionId = null;
    let documentId = null;

    try {
        await auth.ensureAuthenticatedWithTimeout();

        // Land on an authenticated page so base.html's csrf_token() call mints
        // a session-bound token and renders it into <meta name="csrf-token">
        // — the same source api.js's getCsrfToken() reads.
        await page.goto(`${BASE_URL}/library/`, {
            waitUntil: 'domcontentloaded',
            timeout: TIMEOUTS.navigation,
        });
        await page.waitForSelector('meta[name="csrf-token"]', { timeout: TIMEOUTS.selector });
        const csrf = await getCsrf(page);
        if (!csrf) throw new Error('Could not obtain CSRF token from /library/ meta tag');

        const collectionsUrl = `${BASE_URL}/library/api/collections`;
        const uniqueName = `ldr-ui-dl-csrf-${Date.now()}-${Math.floor(Math.random() * 1e6)}`;

        // ---------------------------------------------------------------
        // Test 1: JSON mutation WITHOUT a CSRF token is rejected.
        // ---------------------------------------------------------------
        console.log('Test 1: POST /library/api/collections without CSRF header is rejected (403)');
        try {
            const r = await postJson(page, collectionsUrl, null, {
                name: `${uniqueName}-should-not-exist`,
                type: 'user_uploads',
            });
            if (r.status !== 403) {
                throw new Error(`Expected 403 CSRF rejection, got status=${r.status} body=${JSON.stringify(r.json)}`);
            }
            console.log('PASSED');
            passed++;
        } catch (e) {
            console.log(`FAILED: ${e.message}`);
            await captureOnFailure(page, SCREENSHOT_PREFIX, 'collections_no_csrf', false);
            failed++;
        }

        // ---------------------------------------------------------------
        // Test 2: the SAME mutation, performed the way the app itself does
        // it (X-CSRFToken from the page's meta tag), succeeds. This is the
        // important half — a suite that only proves rejection would still
        // pass if CSRF were broken so badly nothing worked.
        // ---------------------------------------------------------------
        console.log("Test 2: same POST WITH the app's CSRF header succeeds");
        try {
            const r = await postJson(page, collectionsUrl, csrf, {
                name: uniqueName,
                type: 'user_uploads',
            });
            if (!r.ok || !r.json || r.json.success !== true || !r.json.collection?.id) {
                throw new Error(`Expected success with CSRF, got status=${r.status} body=${JSON.stringify(r.json)}`);
            }
            collectionId = r.json.collection.id;
            console.log(`PASSED (collection id=${collectionId})`);
            passed++;
        } catch (e) {
            console.log(`FAILED: ${e.message}`);
            await captureOnFailure(page, SCREENSHOT_PREFIX, 'collections_with_csrf', false);
            failed++;
        }

        // ---------------------------------------------------------------
        // Test 3: a DIFFERENT content-type (multipart/form-data upload)
        // without the header is also rejected. csrf.py only buffers/parses
        // a `csrf_token` body field for urlencoded forms; multipart never
        // gets that treatment, so this exercises the "header is mandatory"
        // branch specifically.
        // ---------------------------------------------------------------
        console.log('Test 3: multipart upload without CSRF header is rejected (403)');
        try {
            if (!collectionId) throw new Error('Skipped: no collection id (Test 2 did not create one)');
            const r = await uploadPdf(
                page,
                `${BASE_URL}/library/api/collections/${collectionId}/upload`,
                null,
                FIXTURE_PDF_B64,
                'should-not-upload.pdf',
                { pdf_storage: 'database' }
            );
            if (r.status !== 403) {
                throw new Error(`Expected 403 CSRF rejection, got status=${r.status} body=${JSON.stringify(r.json)}`);
            }
            console.log('PASSED');
            passed++;
        } catch (e) {
            console.log(`FAILED: ${e.message}`);
            await captureOnFailure(page, SCREENSHOT_PREFIX, 'upload_no_csrf', false);
            failed++;
        }

        // ---------------------------------------------------------------
        // Test 4: same upload WITH the app's CSRF header succeeds and
        // actually stores PDF bytes (pdf_storage=database) — sets up the
        // fixture for the download-bytes test below.
        // ---------------------------------------------------------------
        console.log('Test 4: multipart upload WITH CSRF succeeds and stores a real PDF');
        try {
            if (!collectionId) throw new Error('Skipped: no collection id (Test 2 did not create one)');
            const r = await uploadPdf(
                page,
                `${BASE_URL}/library/api/collections/${collectionId}/upload`,
                csrf,
                FIXTURE_PDF_B64,
                FIXTURE_PDF_FILENAME,
                { pdf_storage: 'database' }
            );
            const uploaded = r.json?.uploaded?.[0];
            if (!r.ok || r.json?.success !== true || !uploaded?.id || uploaded.pdf_stored !== true) {
                throw new Error(`Expected a pdf_stored upload, got status=${r.status} body=${JSON.stringify(r.json)}`);
            }
            documentId = uploaded.id;
            console.log(`PASSED (document id=${documentId})`);
            passed++;
        } catch (e) {
            console.log(`FAILED: ${e.message}`);
            await captureOnFailure(page, SCREENSHOT_PREFIX, 'upload_with_csrf', false);
            failed++;
        }

        // ---------------------------------------------------------------
        // Test 5: the download itself. This is the regression check for
        // the StreamingResponse(BytesIO(...)) -> Response(content=...)
        // migration: Content-Length must be present and must match the
        // actual delivered byte count, and the body must actually be the
        // uploaded file (not a truncated/duplicated/error payload).
        // ---------------------------------------------------------------
        console.log('Test 5: GET /library/document/<id>/pdf delivers correct bytes + headers');
        try {
            if (!documentId) throw new Error('Skipped: no document id (Test 4 did not upload one)');
            const meta = await fetchBinaryMeta(page, `${BASE_URL}/library/document/${documentId}/pdf`);
            if (meta.status !== 200) {
                throw new Error(`Expected 200, got ${meta.status}`);
            }
            const disposition = meta.headers['content-disposition'] || '';
            const fnMatch = disposition.match(/filename\*=UTF-8''([^;]+)/i);
            const decodedFilename = fnMatch ? decodeURIComponent(fnMatch[1]) : null;
            if (!fnMatch || decodedFilename !== FIXTURE_PDF_FILENAME) {
                throw new Error(
                    `Content-Disposition missing or has an unsane filename: "${disposition}" ` +
                    `(expected filename*=UTF-8''${FIXTURE_PDF_FILENAME})`
                );
            }
            const contentLengthHeader = meta.headers['content-length'];
            if (!contentLengthHeader) {
                throw new Error(
                    'Content-Length header is MISSING. This is the exact regression the ' +
                    'plain-Response migration fixes: StreamingResponse(BytesIO(...)) never ' +
                    'sets Content-Length even though the payload is fully buffered in memory.'
                );
            }
            const contentLength = parseInt(contentLengthHeader, 10);
            if (!(contentLength > 0)) {
                throw new Error(`Content-Length is not a positive number: "${contentLengthHeader}"`);
            }
            if (meta.byteLength === 0) {
                throw new Error('Response body is 0 bytes');
            }
            if (meta.byteLength !== contentLength) {
                throw new Error(
                    `Content-Length (${contentLength}) does not match the actual delivered ` +
                    `body size (${meta.byteLength}) — truncated or over-sent download`
                );
            }
            if (meta.magic !== '%PDF') {
                throw new Error(
                    `Body does not start with the PDF magic bytes; got ${JSON.stringify(meta.magic)} ` +
                    '— served content is not the uploaded file'
                );
            }
            console.log(
                `PASSED (status=200, Content-Length=${contentLength}, actual bytes=${meta.byteLength}, ` +
                `filename="${decodedFilename}")`
            );
            passed++;
        } catch (e) {
            console.log(`FAILED: ${e.message}`);
            await captureOnFailure(page, SCREENSHOT_PREFIX, 'download_pdf', false);
            failed++;
        }

        // ---------------------------------------------------------------
        // Cleanup (best-effort, while still authenticated — must run
        // before the logout test below). Deleting the collection cascades
        // to the orphaned uploaded document (delete_orphaned_documents=True
        // in library_delete.py), so no separate document delete is needed.
        // ---------------------------------------------------------------
        if (collectionId) {
            try {
                await deleteWithCsrf(page, `${BASE_URL}/library/api/collections/${collectionId}`, csrf);
            } catch (_) {
                /* best-effort cleanup; never masks a test result */
            }
        }

        // ---------------------------------------------------------------
        // Test 6: session cookie flags, per the actual SessionMiddleware
        // config in fastapi_app.py (session_cookie="session",
        // same_site="lax"). Secure is added dynamically by
        // SecureCookieMiddleware ONLY over an actually-HTTPS connection
        // (marking Secure over plain HTTP would make the browser drop the
        // cookie) — this suite talks to BASE_URL over plain HTTP, so
        // Secure is correctly expected to be absent here.
        // ---------------------------------------------------------------
        console.log('Test 6: session cookie carries HttpOnly + SameSite=Lax');
        try {
            const cookies = await page.cookies();
            const sessionCookie = cookies.find((c) => c.name === 'session');
            if (!sessionCookie) throw new Error('No "session" cookie found');
            if (sessionCookie.httpOnly !== true) {
                throw new Error(`Expected httpOnly=true, got ${sessionCookie.httpOnly}`);
            }
            if (String(sessionCookie.sameSite).toLowerCase() !== 'lax') {
                throw new Error(
                    `Expected sameSite=Lax (same_site="lax" in fastapi_app.py's ` +
                    `SessionMiddleware config), got ${sessionCookie.sameSite}`
                );
            }
            if (sessionCookie.secure !== false) {
                throw new Error(
                    `Expected secure=false over plain HTTP (SecureCookieMiddleware only adds ` +
                    `Secure over an actually-HTTPS connection), got ${sessionCookie.secure}`
                );
            }
            console.log(
                `PASSED (httpOnly=${sessionCookie.httpOnly}, sameSite=${sessionCookie.sameSite}, ` +
                `secure=${sessionCookie.secure})`
            );
            passed++;
        } catch (e) {
            console.log(`FAILED: ${e.message}`);
            failed++;
        }

        // External navigation must carry the same authenticated session.
        // Intercept the referring site locally: no DNS/server dependency and
        // no request leaves the browser for this deliberately different site.
        console.log('Test 6b: external link preserves authentication');
        const navigationPage = await browser.newPage();
        try {
            const direct = await navigationPage.goto(`${BASE_URL}/`, {
                waitUntil: 'domcontentloaded',
                timeout: TIMEOUTS.navigation,
            });
            const checkAuth = () => navigationPage.evaluate(async () => {
                const response = await fetch('/auth/check');
                return { status: response.status, data: await response.json() };
            });
            const before = await checkAuth();
            if (direct.status() !== 200 || before.status !== 200) {
                throw new Error('Direct-navigation authentication control failed');
            }

            const sourceUrl = 'http://ldr-external-navigation.test/';
            const target = `${BASE_URL}/`.replaceAll('&', '&amp;').replaceAll('"', '&quot;');
            await navigationPage.setRequestInterception(true);
            navigationPage.on('request', request => {
                if (request.url() === sourceUrl) {
                    request.respond({
                        status: 200,
                        contentType: 'text/html',
                        body: `<a id="open-ldr" href="${target}">Open LDR</a>`,
                    });
                } else {
                    request.continue();
                }
            });
            await navigationPage.goto(sourceUrl, { waitUntil: 'domcontentloaded' });
            await Promise.all([
                navigationPage.waitForNavigation({
                    waitUntil: 'domcontentloaded',
                    timeout: TIMEOUTS.navigation,
                }),
                navigationPage.click('#open-ldr'),
            ]);
            const after = await checkAuth();
            if (new URL(navigationPage.url()).pathname !== '/' || after.status !== 200
                || after.data.username !== before.data.username) {
                throw new Error('Following an external link lost the authenticated session');
            }
            console.log('PASSED');
            passed++;
        } catch (e) {
            console.log(`FAILED: ${e.message}`);
            failed++;
        } finally {
            await navigationPage.close();
        }

        // Such a link reaches any page signed in and chooses its URL, so no
        // page-load script may turn URL parameters into a state-changing
        // request, and the link must not even pre-fill the chat box: the
        // server redirects a cross-site `?q=` away (303) before the page
        // renders. The unknown-session link covers users who already have
        // chats; its failed restore used to create a session for the linked
        // query and start a research run.
        console.log('Test 6c: external chat links with ?q= send and pre-fill nothing');
        const chatLinkPage = await browser.newPage();
        try {
            const sourceUrl = 'http://ldr-external-navigation.test/chat-links';
            const probe = `external link probe ${Date.now()}`;
            const links = [
                ['bare', `${BASE_URL}/chat/?q=${encodeURIComponent(probe)}`],
                ['unknown-session', `${BASE_URL}/chat/${randomUUID()}?q=${encodeURIComponent(probe)}`],
            ];
            const escapeAttribute = (value) => value.replaceAll('&', '&amp;').replaceAll('"', '&quot;');
            const chatWrites = [];
            await chatLinkPage.setRequestInterception(true);
            chatLinkPage.on('request', request => {
                if (request.url() === sourceUrl) {
                    request.respond({
                        status: 200,
                        contentType: 'text/html',
                        body: links.map(([name, href]) => (
                            `<a id="open-${name}" href="${escapeAttribute(href)}">${name}</a>`
                        )).join(''),
                    });
                    return;
                }
                const { pathname } = new URL(request.url());
                if (request.method() !== 'GET' && pathname.startsWith('/api/chat/')) {
                    chatWrites.push(`${request.method()} ${pathname}`);
                }
                request.continue();
            });
            for (const [name] of links) {
                await chatLinkPage.goto(sourceUrl, { waitUntil: 'domcontentloaded' });
                const [response] = await Promise.all([
                    chatLinkPage.waitForNavigation({
                        waitUntil: 'domcontentloaded',
                        timeout: TIMEOUTS.navigation,
                    }),
                    chatLinkPage.click(`#open-${name}`),
                ]);
                const landed = new URL(chatLinkPage.url());
                if (!landed.pathname.startsWith('/chat/')) {
                    throw new Error(`The ${name} link did not open the chat page signed in: ${chatLinkPage.url()}`);
                }
                const hops = response ? response.request().redirectChain() : [];
                if (!hops.some((hop) => hop.response()?.status() === 303)) {
                    throw new Error(`The server did not redirect the ${name} link's q away`);
                }
                await chatLinkPage.waitForFunction(
                    () => document.getElementById('chat-input')?.dataset.initComplete === 'true',
                    { timeout: TIMEOUTS.selector },
                );
                await chatLinkPage
                    .waitForNetworkIdle({ idleTime: 500, timeout: TIMEOUTS.selector })
                    .catch(() => {});
                if (chatWrites.length > 0) {
                    throw new Error(`The ${name} link sent ${chatWrites.join(', ')}`);
                }
                const composer = await chatLinkPage.$eval('#chat-input', (el) => el.value);
                if (composer !== '') {
                    throw new Error(`The ${name} link pre-filled the composer: ${JSON.stringify(composer)}`);
                }
            }
            console.log('PASSED');
            passed++;
        } catch (e) {
            console.log(`FAILED: ${e.message}`);
            failed++;
        } finally {
            await chatLinkPage.close();
        }

        // ---------------------------------------------------------------
        // Test 7: logout actually invalidates access to an authenticated
        // page (require_auth's 401 -> 302 /auth/login redirect for HTML
        // requests, per fastapi_app.py's HTTPException handler).
        // ---------------------------------------------------------------
        console.log('Test 7: logout invalidates access to an authenticated page');
        try {
            await auth.logout();
            const workingPage = auth.getPage();
            await workingPage.goto(`${BASE_URL}/library/`, {
                waitUntil: 'domcontentloaded',
                timeout: TIMEOUTS.navigation,
            });
            const finalUrl = workingPage.url();
            if (!finalUrl.includes('/auth/login')) {
                throw new Error(`Expected redirect to /auth/login after logout, landed on: ${finalUrl}`);
            }
            console.log('PASSED');
            passed++;
        } catch (e) {
            console.log(`FAILED: ${e.message}`);
            failed++;
        }
    } catch (e) {
        console.log(`Test suite error: ${e.message}`);
        failed++;
    } finally {
        await browser.close();
    }

    console.log('-'.repeat(50));
    console.log(`Download & CSRF Flow Tests — passed: ${passed}, failed: ${failed}`);
    console.log('-'.repeat(50));
    if (failed > 0) process.exit(1);
}

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