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.

43.4 KB
Raw Blame
#!/usr/bin/env node
/**
 * Error / Browser Session-Loss UX Tests (Flask -> FastAPI migration)
 *
 * What a user actually SEES when something goes wrong. The migration
 * rewrote session handling (Flask's `session` -> Starlette's
 * SessionMiddleware, `@login_required` -> `Depends(require_auth)`) and
 * error responses (Flask's error handlers -> FastAPI's
 * `_register_exception_handlers` in fastapi_app.py), so these are real
 * regression risks: a broken page, a raw JSON blob, or a silent no-op are
 * all things a real user could hit without a single failed HTTP status
 * code anywhere to flag it in a shallower test.
 *
 * Overlap check against existing error/session coverage (read before
 * writing this file) -- what is already covered and therefore NOT
 * repeated here:
 *   - test_error_handling_ci.js's `Error404Tests.nonExistentPageShows404`
 *     already navigates to a bogus URL and checks the response, BUT its
 *     `passed` condition is `statusCode === 404 || has404Text ||
 *     hasErrorPage` -- since the response IS a 404, that check passes
 *     REGARDLESS of whether the body is HTML or raw JSON. It does not
 *     read Content-Type and does not assert the page rendered as a page.
 *     This file's Test 2 below closes exactly that gap (and found a real
 *     migration regression it was structurally unable to catch -- now
 *     fixed; Test 2 is the regression pin -- see below).
 *   - test_error_handling_ci.js's `Error401Tests.unauthenticatedRedirectsToLogin`
 *     and test_download_and_csrf_flows_ci.js's Test 7 ("logout invalidates
 *     access to an authenticated page") both do a FRESH FULL-PAGE
 *     NAVIGATION to a protected route without a valid session (from a new,
 *     cookieless browser context / after logging out), which
 *     exercises the server-side HTML-route redirect
 *     (`_register_exception_handlers`'s 401 handler -> 302 to
 *     /auth/login) -- a request that never runs any app JS at all. This
 *     file's Tests 3+4 are a materially different code path: the session
 *     is invalidated WHILE an already-rendered page is open, and the
 *     triggering action is an in-page AJAX call
 *     (`safeFetchWithAuth`/`fetchWithErrorHandling` in
 *     security/safe-fetch.js + services/api.js), which must notice the
 *     401 itself and client-side-redirect
 *     (`window.location.href = '/auth/login?next=...'`). A regression in
 *     that client-side logic (e.g. a forgotten status check) would leave
 *     the user on a page that looks alive but silently does nothing --
 *     exactly the "silent no-op" risk this task calls out, and exactly
 *     what neither existing test can detect since both drive a fresh
 *     navigation, not an in-page fetch. (Test 3 proves this for a WRITE,
 *     whose 401 comes from CSRFMiddleware's sign-in check, and Test 4 for
 *     a GET, whose 401 comes from `require_auth` -- see below.)
 *   - test_download_and_csrf_flows_ci.js's Tests 1-3 already assert that
 *     CSRF-missing/invalid mutations are rejected with 403 -- but ONLY at
 *     the HTTP-response level (`r.status !== 403` from a raw
 *     `page.evaluate(fetch(...))` call). None of them ever look at the
 *     DOM to confirm the app surfaces a VISIBLE error to the user. A
 *     regression that broke `showMessage`/the notification banner while
 *     leaving the HTTP layer intact (a silent-failure UX bug) would pass
 *     every one of those tests. This file's Test 1 drives the exact same
 *     rejection through the real UI (a real button click, not a raw
 *     fetch) and asserts the visible `#notification-banner-assertive`
 *     toast the user would actually see. The app refreshes a rejected
 *     token once and retries, so Test 1 stubs only that refresh, with a
 *     token the server rejects again (see Test 1).
 *   - test_settings_save_error_ci.js proves the SAME banner mechanism for
 *     a mocked 5xx on the settings-save endpoint. Not duplicated: this
 *     file exercises the notes surface with REAL (not mocked) CSRF
 *     rejections from the actual middleware, shows the middleware's own
 *     message in the banner, and additionally proves the rejected
 *     mutation left no trace server-side.
 *
 * How errors are reported (surveyed, not invented):
 *   static/js/services/ui.js's `showMessage(message, type)` mutates one of
 *   two persistent, lazily-created live regions --
 *   `#notification-banner-polite` (success/info) or
 *   `#notification-banner-assertive` (error/warning, role="alert") -- by
 *   setting the `<span>` child's textContent. note-detail.js's
 *   `showNoteError()` / notes.js's `showNotesError()` both call
 *   `window.ui.showMessage(message, 'error')` on a failed mutation, so a
 *   failed save/create routes here. static/js/services/api.js's
 *   `fetchWithErrorHandling()` and security/safe-fetch.js's
 *   `safeFetchWithAuth()` both special-case a 401 from an internal
 *   (`/`-prefixed) URL: instead of throwing an opaque error, they call
 *   `redirectToLogin()`, which sends the browser to
 *   `/auth/login?next=<current-path>` -- a CLIENT-SIDE navigation, not a
 *   server 302 (the notes API paths contain `/api/`, so
 *   `_is_api_request()` in fastapi_app.py's exception handler returns a
 *   JSON 401 rather than a redirect for them -- confirmed by reading that
 *   function; it's what makes this path exercisable at all instead of
 *   fetch() transparently following a redirect).
 *
 * ===========================================================================
 * ONE MIGRATION REGRESSION FOUND, AND FIXED; ONE DEAD-SESSION BEHAVIOR
 * PINNED:
 * ===========================================================================
 *
 * REGRESSION, NOW FIXED -- an unrouted URL used to render as raw JSON,
 * not a page. `_register_exception_handlers()`'s 404 handler in
 * src/local_deep_research/web/fastapi_app.py returned
 * `JSONResponse({"error": "Not found"}, status_code=404)`
 * unconditionally -- unlike the 401 handler defined immediately above it
 * in the same function, which already branches on `_is_api_request(request)`
 * to decide between a JSON body and an HTML redirect, and with no 404 HTML
 * template anywhere under templates/ to fall back to. So every unrouted
 * path, for every client including a real browser tab, got Chrome's raw
 * built-in JSON viewer instead of this app's normal page chrome -- a
 * plausible, everyday user action (typo a URL, follow a stale link)
 * produced what looked like a broken/bare page. This WAS a migration
 * regression, not a pre-existing wart: pre-migration `main` branched on
 * exactly this (`app_factory.py`'s `@app.errorhandler(404)` returned
 * `make_response("Not found", 404)`, which Flask serves as text/html).
 * FIXED in fastapi_app.py: the 404 handler (and, identically, the 500
 * handler) now calls `_is_api_request(request)` and returns
 * `HTMLResponse(...)` for non-API/browser requests, JSON only for API
 * callers -- restoring the same branch `main` had. Test 2 below is the
 * regression pin: it asserts `content-type: text/html` on a plain
 * top-level navigation to an unrouted URL.
 *
 * DEAD-SESSION WRITES REDIRECT TO LOGIN, LIKE GETS -- once the session
 * cookie is cleared from the browser, a WRITE from an already-rendered
 * page is rejected with 401 and the app sends the user to login.
 * services/api.js's `fetchWithCsrfRecovery()` adds the page's
 * X-LDR-Auth-Context header to eligible same-origin writes, and
 * CSRFMiddleware (web/dependencies/csrf.py) checks that header BEFORE the
 * session's CSRF token. With no session, `get_auth_context()` finds no
 * login, so the middleware answers 401 {"error": "Authentication
 * required"} before any handler runs, and safeFetchWithAuth turns that
 * into the same client-side redirect a dead-session GET gets (Test 4).
 * A write sent without the context header (a FormData upload, for
 * example) still gets the middleware's 403 "CSRF token missing: fetch
 * /auth/csrf-token first" instead. Test 3 below is the pin.
 *
 * Screenshots: opt-in only via tests/ui_tests/screenshot_helper.js (no-op
 * unless LDR_UI_SCREENSHOTS is set -- see that file's header). Captured on
 * the visible error banner, on the 404 response, on both login redirects,
 * and on any assertion failure.
 *
 * Registered in the `error-benchmark` shard (tests/ui_tests/run_all_tests.js)
 * -- same theme as test_error_handling_ci.js / test_error_recovery.js.
 *
 * Run: CI=true node test_error_and_session_ux_ci.js
 *      LDR_UI_SCREENSHOTS=1 CI=true node test_error_and_session_ux_ci.js
 */

const puppeteer = require('puppeteer');
const AuthHelper = require('./auth_helper');
const { getPuppeteerLaunchOptions } = require('./puppeteer_config');
const { capture, captureOnFailure, screenshotsEnabled } = require('./screenshot_helper');

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

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

const SCREENSHOT_PREFIX = 'error_session_ux';
const ERROR_BANNER_SELECTOR = '#notification-banner-assertive';

/**
 * A real, attributable console error -- not the browser's own speculative
 * /favicon.ico probe. Same rationale as test_frontend_bundle_integrity_ci.js
 * and test_navigation_and_theme_ci.js: base.html declares favicon.png, no
 * .ico exists, and "Failed to load resource" console messages carry no
 * URL/stack to attribute to a real bug.
 */
function isRealConsoleError(msg) {
    return msg.type() === 'error' && !msg.text().startsWith('Failed to load resource');
}

/** Wait until the assertive notification banner's text is non-empty, then return it. */
async function waitForBannerText(page, timeout) {
    await page.waitForFunction(
        (selector) => {
            const span = document.querySelector(`${selector} span`);
            return !!span && span.textContent.trim().length > 0;
        },
        { timeout },
        ERROR_BANNER_SELECTOR
    );
    return page.$eval(`${ERROR_BANNER_SELECTOR} span`, (el) => el.textContent.trim());
}

/** Keep response-body reads from hanging the entire shard without a diagnosis. */
async function withTimeout(promise, timeout, label) {
    let timer;
    try {
        return await Promise.race([
            promise,
            new Promise((_, reject) => {
                timer = setTimeout(() => reject(new Error(`${label} timed out after ${timeout}ms`)), timeout);
            }),
        ]);
    } finally {
        clearTimeout(timer);
    }
}

/**
 * page.waitForResponse(), limited to main-frame requests that start after
 * this call. A URL does not say which page sent a request, so without this
 * a response to an earlier request (one the previous page sent, or one
 * already in flight) could satisfy the wait. Puppeteer emits a request's
 * 'request' event before its 'response' event, so a response's request is
 * already recorded when the predicate sees it.
 */
function waitForResponseToNewRequest(page, predicate, options) {
    const newRequests = new WeakSet();
    const recordRequest = (request) => {
        if (request.frame() === page.mainFrame()) newRequests.add(request);
    };
    page.on('request', recordRequest);
    return page
        .waitForResponse((response) => newRequests.has(response.request()) && predicate(response), options)
        .finally(() => page.off('request', recordRequest));
}

/**
 * Track same-origin requests until their response headers arrive (or the
 * request fails). Set-Cookie is applied at the header boundary, so a short
 * header-idle window can cover late bootstrap responses without waiting
 * forever on WebSocket, SSE, or other long-lived response bodies.
 */
function trackSameOriginResponseHeaders(page, origin) {
    const pending = new Set();
    const stateWaiters = new Set();

    const isSameOrigin = (request) => {
        try {
            return new URL(request.url()).origin === origin;
        } catch {
            return false;
        }
    };
    const notifyStateChanged = () => {
        for (const waiter of [...stateWaiters]) waiter();
    };
    const onRequest = (request) => {
        if (isSameOrigin(request)) {
            pending.add(request);
            notifyStateChanged();
        }
    };
    const onResponse = (response) => {
        if (pending.delete(response.request())) notifyStateChanged();
    };
    const onRequestFailed = (request) => {
        if (pending.delete(request)) notifyStateChanged();
    };

    page.on('request', onRequest);
    page.on('response', onResponse);
    page.on('requestfailed', onRequestFailed);

    return {
        async waitForIdle(idleTime, timeout) {
            await new Promise((resolve, reject) => {
                let idleTimer = null;
                let timeoutTimer = null;
                let evaluate;

                const cleanup = () => {
                    if (idleTimer !== null) clearTimeout(idleTimer);
                    if (timeoutTimer !== null) clearTimeout(timeoutTimer);
                    stateWaiters.delete(evaluate);
                };
                const onIdle = () => {
                    cleanup();
                    resolve();
                };
                evaluate = () => {
                    if (pending.size === 0) {
                        if (idleTimer === null) idleTimer = setTimeout(onIdle, idleTime);
                    } else if (idleTimer !== null) {
                        clearTimeout(idleTimer);
                        idleTimer = null;
                    }
                };

                timeoutTimer = setTimeout(() => {
                    cleanup();
                    const urls = [...pending].map((request) => request.url());
                    const pendingDescription = urls.length > 0 ? ` Pending: ${urls.join(', ')}` : '';
                    reject(
                        new Error(
                            `FAILED SETUP: timed out waiting for ${idleTime}ms of same-origin response-header idle time.` +
                            pendingDescription
                        )
                    );
                }, timeout);
                stateWaiters.add(evaluate);
                evaluate();
            });
        },
        dispose() {
            page.off('request', onRequest);
            page.off('response', onResponse);
            page.off('requestfailed', onRequestFailed);
            stateWaiters.clear();
        },
    };
}

/**
 * Render /notes/ while signed in, let its bootstrap traffic settle, then
 * delete the browser's session cookie. The page keeps the signed-in user's
 * CSRF token and auth context, but the next request carries no session.
 * Throws a "FAILED SETUP" error if that state cannot be reached.
 *
 * The setup starts from about:blank, which sends no requests. The page that
 * was open before can still have requests in flight: the post-login home
 * page, for one, extends base.html, whose theme.js requests
 * /settings/api/app.theme, one of the three bootstrap paths below. Each
 * bootstrap wait also accepts only a request that starts after the wait is
 * set up, so a late response to an earlier page's request is never taken
 * for this page's own.
 *
 * A rendered button does not prove that the page's authenticated bootstrap
 * responses have finished. SessionMiddleware can emit Set-Cookie on any of
 * them; deleting the cookie while one is in flight lets that old response
 * recreate it. The header tracker stays active through deletion, and the
 * idle timer resets whenever another same-origin request starts. The three
 * named API checks also prove initialization worked. They check the status
 * only: Set-Cookie arrives with the headers, so no response body is read.
 */
async function openNotesWithDeadSession(page) {
    await page.goto('about:blank', { waitUntil: 'load', timeout: TIMEOUTS.navigation });

    const responseHeaderTracker = trackSameOriginResponseHeaders(page, new URL(BASE_URL).origin);
    try {
        const waitForBootstrapGet = (pathname) => waitForResponseToNewRequest(
            page,
            (response) => response.request().method() === 'GET' && new URL(response.url()).pathname === pathname,
            { timeout: TIMEOUTS.navigation }
        );
        const [notesBootstrapResponse, collectionsBootstrapResponse, themeBootstrapResponse] = await Promise.all([
            waitForBootstrapGet('/notes/api/notes'),
            waitForBootstrapGet('/library/api/collections'),
            waitForBootstrapGet('/settings/api/app.theme'),
            page.goto(`${BASE_URL}/notes/`, {
                waitUntil: 'load',
                timeout: TIMEOUTS.navigation,
            }),
        ]);
        await page.waitForSelector('[data-action="create-new-note"]', { timeout: TIMEOUTS.selector });

        for (const response of [notesBootstrapResponse, collectionsBootstrapResponse, themeBootstrapResponse]) {
            if (response.status() !== 200) {
                throw new Error(
                    `FAILED SETUP: ${new URL(response.url()).pathname} bootstrap returned HTTP ${response.status()}`
                );
            }
        }
        await responseHeaderTracker.waitForIdle(RESPONSE_HEADER_IDLE_MS, TIMEOUTS.navigation);

        const cookiesBeforeExpiry = await page.cookies();
        const sessionCookieToKill = cookiesBeforeExpiry.find((cookie) => cookie.name === 'session');
        if (!sessionCookieToKill) {
            throw new Error('FAILED SETUP: no "session" cookie found before invalidating it');
        }
        await page.deleteCookie({
            name: 'session',
            domain: sessionCookieToKill.domain,
            path: sessionCookieToKill.path,
        });

        // If a response crossed the first delete boundary, wait until header
        // activity is quiet again and clear only the session cookie it may
        // have recreated.
        await responseHeaderTracker.waitForIdle(RESPONSE_HEADER_IDLE_MS, TIMEOUTS.navigation);
        const cookiesAfterHeaderIdle = await page.cookies();
        for (const cookie of cookiesAfterHeaderIdle.filter((item) => item.name === 'session')) {
            await page.deleteCookie({ name: cookie.name, domain: cookie.domain, path: cookie.path });
        }

        const cookiesAfterExpiry = await page.cookies();
        if (cookiesAfterExpiry.some((cookie) => cookie.name === 'session')) {
            throw new Error('FAILED SETUP: session cookie still present after bootstrap settled');
        }
    } finally {
        responseHeaderTracker.dispose();
    }
}

/**
 * Assert that an in-page action on a dead session landed on a real login
 * form with next=/notes/, not a broken page or a raw JSON body.
 */
async function assertLoginPageRendered(page, action) {
    const finalUrl = new URL(page.url());
    if (finalUrl.pathname !== '/auth/login') {
        throw new Error(
            `GENUINE DEFECT: after a mid-session cookie invalidation, an in-page ${action} action ` +
            `did not send the user to login. Landed on: ${page.url()}`
        );
    }
    const nextParam = finalUrl.searchParams.get('next');
    if (nextParam !== '/notes/') {
        throw new Error(`Expected next=/notes/ so the user lands back where they were, got next=${nextParam}`);
    }

    await page.waitForSelector('input[name="username"]', { timeout: TIMEOUTS.selector });
    const hasPasswordField = await page.$('input[name="password"]');
    const hasSubmitButton = await page.$('button[type="submit"]');
    const contentType = (await page.evaluate(() => document.contentType)) || '';
    if (!hasPasswordField || !hasSubmitButton) {
        throw new Error('GENUINE DEFECT: landed on /auth/login but it is missing a functional login form (username/password/submit)');
    }
    if (!contentType.startsWith('text/html')) {
        throw new Error(`GENUINE DEFECT: /auth/login rendered as "${contentType}", not an HTML page`);
    }
}

async function run() {
    console.log(`Running error / browser-session-loss UX tests (CI mode: ${isCI})`);
    console.log(`Screenshots: ${screenshotsEnabled() ? 'ENABLED (LDR_UI_SCREENSHOTS set)' : 'disabled (default)'}`);

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

    // Aggregate JS-error tracking across the WHOLE run (Tests 1-4) -- this
    // is what Test 5 asserts on. Attached once, here, rather than per-test,
    // so nothing that happens between tests is missed.
    const allConsoleErrors = [];
    const allPageErrors = [];
    page.on('console', (m) => {
        if (isRealConsoleError(m)) {
            allConsoleErrors.push(m.text());
            console.log('BROWSER ERROR:', m.text());
        }
    });
    page.on('pageerror', (e) => {
        allPageErrors.push(e.message);
        console.log('PAGE ERROR:', e.message);
    });

    const uniqueSuffix = `${Date.now()}-${Math.floor(Math.random() * 1e6)}`;

    let passed = 0;
    let failed = 0;

    try {
        const auth = new AuthHelper(page, BASE_URL);
        await auth.ensureAuthenticatedWithTimeout();

        // ---------------------------------------------------------------
        // Test 1: a mutation the server rejects (missing/invalid CSRF
        // token) surfaces a VISIBLE error toast via the app's own JS path
        // -- not a silent no-op, not a raw JSON blob, not a page that
        // looks like nothing happened. Driven through the real "New Note"
        // modal (a real button click), not a raw fetch() -- see the file
        // header for why test_download_and_csrf_flows_ci.js's existing
        // CSRF tests don't already cover this.
        // ---------------------------------------------------------------
        console.log('Test 1: CSRF-rejected mutation shows a visible error toast (not a silent failure)');
        try {
            const shouldNotExistTitle = `ldr-ui-error-ux-should-not-exist-${uniqueSuffix}`;

            await page.goto(`${BASE_URL}/notes/`, {
                waitUntil: 'domcontentloaded',
                timeout: TIMEOUTS.navigation,
            });
            await page.waitForSelector('[data-action="create-new-note"]', { timeout: TIMEOUTS.selector });
            await page.click('[data-action="create-new-note"]');
            await page.waitForSelector('#note-title', { visible: true, timeout: TIMEOUTS.selector });
            await page.type('#note-title', shouldNotExistTitle);
            await page.type('#note-content', 'This save must be rejected by a tampered CSRF token.');

            // The app recovers a stale token once: on the middleware's marked
            // 403 it fetches /auth/csrf-token and retries with the token it
            // gets back. Stub only that refresh, with the endpoint's success
            // shape (200, a csrf_token, and this page's own auth_context) but
            // a token the server never issued. The one permitted retry then
            // reaches the real CSRF middleware and is rejected again. The
            // endpoint answers only 200, 401 or 409: a 200 whose token is
            // rejected again is how a write ends in a CSRF failure on this
            // page, while a 401 redirects to login and a 409 reports a changed
            // sign-in. The stub carries no message, so the banner can only
            // show the server's. Navigation in Test 2 restores fetch.
            const tamperedToken = 'tampered-invalid-csrf-token';
            const refreshedToken = 'refreshed-but-still-invalid-csrf-token';
            await page.evaluate((token) => {
                const realFetch = window.fetch.bind(window);
                window.fetch = (input, options) => {
                    const url = typeof input === 'string' ? input : input?.url;
                    if (url && new URL(url, window.location.href).pathname === '/auth/csrf-token') {
                        window.__csrfRefreshAttempts = (window.__csrfRefreshAttempts || 0) + 1;
                        const authContext = new window.Headers(options?.headers).get('X-LDR-Auth-Context');
                        return Promise.resolve(new Response(
                            JSON.stringify({ csrf_token: token, auth_context: authContext }),
                            { status: 200, headers: { 'Content-Type': 'application/json' } }
                        ));
                    }
                    return realFetch(input, options);
                };
            }, refreshedToken);

            // Tamper the CSRF token IN THE DOM only -- this mutates the
            // live <meta> the page's own JS reads (getCsrfToken() / both
            // notes.js's and note-detail.js's getCSRFToken() delegate to
            // it), not the server-side session token. The real session
            // token (request.session["_csrf_token"]) is untouched, so a
            // later page.goto() elsewhere in this file gets a fresh,
            // VALID token rendered straight from the server template --
            // no restore step is needed.
            await page.evaluate((token) => {
                const meta = document.querySelector('meta[name="csrf-token"]');
                if (meta) meta.setAttribute('content', token);
            }, tamperedToken);

            // Tell the two attempts apart by the token each one sent.
            const isCreateNotePostWithToken = (token) => (r) =>
                new URL(r.url()).pathname === '/notes/api/notes'
                && r.request().method() === 'POST'
                && r.request().headers()['x-csrftoken'] === token;
            const firstRespPromise = page.waitForResponse(
                isCreateNotePostWithToken(tamperedToken), { timeout: TIMEOUTS.navigation }
            );
            const retryRespPromise = page.waitForResponse(
                isCreateNotePostWithToken(refreshedToken), { timeout: TIMEOUTS.navigation }
            );
            await page.click('#save-note-btn');

            const [firstResp, retryResp] = await Promise.all([firstRespPromise, retryRespPromise]).catch((e) => {
                throw new Error(`Expected the original create and one retry with the refreshed token: ${e.message}`);
            });
            // Check status and headers only. fetchWithCsrfRecovery() never
            // reads the original 403's body, and Chrome does not finish
            // loading a no-store fetch body until the page reads it, so a
            // Puppeteer body read of that response would never settle.
            for (const [label, resp] of [['original', firstResp], ['retried', retryResp]]) {
                const marker = resp.headers()['x-ldr-csrf-rejected'];
                if (resp.status() !== 403 || marker !== '1') {
                    throw new Error(
                        `Expected the real CSRF middleware to reject the ${label} create with a marked 403, ` +
                        `got status=${resp.status()} x-ldr-csrf-rejected=${marker}`
                    );
                }
            }

            // The visible signal a real user would see.
            const bannerText = await waitForBannerText(page, TIMEOUTS.selector);
            await capture(page, SCREENSHOT_PREFIX, 'csrf_error_banner');
            // saveNote() read the retry's body before showing the banner, so
            // this read can settle; it is bounded anyway.
            const retryBody = await withTimeout(
                retryResp.json().catch(() => null), TIMEOUTS.selector, 'CSRF retry rejection body'
            );
            if (!retryBody || !/csrf/i.test(retryBody.error || '')) {
                throw new Error(`Expected a CSRF-flavored error body on the retried create, got: ${JSON.stringify(retryBody)}`);
            }
            const refreshAttempts = await page.evaluate(() => window.__csrfRefreshAttempts || 0);
            if (refreshAttempts !== 1) {
                throw new Error(`Expected one CSRF refresh attempt, got ${refreshAttempts}`);
            }
            if (bannerText !== retryBody.error) {
                throw new Error(
                    `GENUINE DEFECT: notification banner did not show the server's CSRF rejection ` +
                    `("${retryBody.error}") -- got: "${bannerText}"`
                );
            }

            // Not a silent no-op: the modal is still open with the user's
            // data intact (not quietly closed as if the save had worked),
            // and the page never navigated away.
            const modalStillOpen = await page.$eval('#noteModal', (el) => el.classList.contains('show')).catch(() => false);
            if (!modalStillOpen) {
                throw new Error('GENUINE DEFECT: the create-note modal closed after a rejected save, as if it had succeeded');
            }
            if (!page.url().endsWith('/notes/') && !page.url().endsWith('/notes')) {
                throw new Error(`GENUINE DEFECT: page navigated away after a rejected save: ${page.url()}`);
            }

            // Nothing was actually created server-side.
            const apiCheck = await withTimeout(page.evaluate(async (title) => {
                const r = await fetch(`/notes/api/notes?search=${encodeURIComponent(title)}`, { credentials: 'same-origin' });
                const body = await r.json().catch(() => null);
                return { status: r.status, count: (body?.notes || []).length };
            }, shouldNotExistTitle), TIMEOUTS.selector, 'note absence check');
            if (apiCheck.status !== 200 || apiCheck.count !== 0) {
                throw new Error(`GENUINE DEFECT: a note titled "${shouldNotExistTitle}" exists despite the rejected (403) create requests: ${JSON.stringify(apiCheck)}`);
            }

            console.log(`PASSED (original and retried creates rejected with 403, banner="${bannerText}", modal stayed open, nothing was created)`);
            passed++;
        } catch (e) {
            console.log(`FAILED: ${e.message}`);
            await captureOnFailure(page, SCREENSHOT_PREFIX, 'csrf_error_banner', false);
            failed++;
        }

        // ---------------------------------------------------------------
        // Test 2: an unrouted URL a user could type (typo, stale bookmark)
        // renders as a PAGE, not a raw JSON body.
        //
        // REGRESSION PIN: this used to fail on this branch -- see this
        // file's header comment for the full history. fastapi_app.py's
        // 404 handler was unconditional JSON with no 404 HTML template
        // anywhere in the app; it now branches on `_is_api_request()` the
        // same way the 401 handler above it does, restoring pre-migration
        // `main`'s behavior. Kept as a real, straight assertion rather
        // than weakened to only check the status code (which
        // test_error_handling_ci.js's existing 404 test already does, and
        // which is why it wouldn't have caught the regression this pins).
        // ---------------------------------------------------------------
        console.log('Test 2: unrouted URL renders as a page, not raw JSON (a user could type this)');
        try {
            const badPath = `/this-route-truly-does-not-exist-${uniqueSuffix}`;
            const response = await page.goto(`${BASE_URL}${badPath}`, {
                waitUntil: 'domcontentloaded',
                timeout: TIMEOUTS.navigation,
            });

            const status = response ? response.status() : null;
            const contentType = response ? (response.headers()['content-type'] || '') : '';
            const bodyText = await page.evaluate(() => document.body?.innerText || document.body?.textContent || '');
            const title = await page.title().catch(() => '');
            await capture(page, SCREENSHOT_PREFIX, 'unrouted_url');

            if (status !== 404) {
                throw new Error(`Expected HTTP 404 for an unrouted path, got ${status}`);
            }

            if (!contentType.startsWith('text/html')) {
                throw new Error(
                    `GENUINE DEFECT: unrouted URL ${badPath} returned Content-Type "${contentType}" ` +
                    `(a raw JSON body: ${JSON.stringify(bodyText.slice(0, 200))}) instead of an HTML page. ` +
                    'A user who mistypes a URL or follows a stale link sees the raw {"error":"Not found"} ' +
                    'body rendered by Chrome\'s built-in JSON viewer, not this app\'s normal page chrome ' +
                    '(sidebar/branding/a way back). Root cause: fastapi_app.py\'s ' +
                    '`@app.exception_handler(404)` unconditionally returns JSONResponse (no Accept-header ' +
                    'branching like the 401 handler right above it has, and no 404.html template exists ' +
                    'anywhere under templates/). See this file\'s header comment for full evidence.'
                );
            }

            console.log(`PASSED (404, Content-Type="${contentType}", title="${title}")`);
            passed++;
        } catch (e) {
            console.log(`FAILED: ${e.message}`);
            failed++;
        }

        // ---------------------------------------------------------------
        // Tests 3 + 4: a page is already loaded and the user is mid-session
        // when the browser loses its session cookie (for example, an
        // explicitly cleared cookie jar). The signed cookie carries the
        // browser's username and CSRF claims, so once it is gone the next
        // request has neither even though the app also validates a
        // server-side session id. Their next authenticated ACTION -- not a
        // fresh page load -- must be handled gracefully by the app's own JS
        // (safeFetchWithAuth), not leave them looking at a page that
        // silently does nothing.
        //
        // Both actions end on /auth/login, so each test starts from its own
        // signed-in /notes/ page and then deletes its cookie
        // (openNotesWithDeadSession). Loading /notes/ AFTER the cookie is
        // gone would instead hit the server-side HTML-route 302 before any
        // app JS ran, which is the already-covered full-navigation case
        // from test_error_handling_ci.js / test_download_and_csrf_flows_ci.js
        // (see file header).
        // ---------------------------------------------------------------

        // ---------------------------------------------------------------
        // Test 3: dead session + a WRITE (mutation) is rejected with 401,
        // and the app sends the user to login.
        //
        // Why a 401 and not a CSRF 403: fetchWithCsrfRecovery() in
        // services/api.js adds the page's X-LDR-Auth-Context header to this
        // same-origin JSON POST, and CSRFMiddleware
        // (web/dependencies/csrf.py) checks that header before the session's
        // CSRF token. The session is empty, so get_auth_context() returns
        // None and the middleware answers 401 {"error": "Authentication
        // required"} before any handler runs. That response carries no
        // X-LDR-CSRF-Rejected marker, so the app neither refreshes the token
        // nor retries; safeFetchWithAuth() calls redirectToLogin(), the same
        // path the GET in Test 4 takes.
        // ---------------------------------------------------------------
        console.log('Test 3: dead session + a WRITE action -- 401, then a client-side redirect to login (not a silent no-op)');
        try {
            await openNotesWithDeadSession(page);
            await page.click('[data-action="create-new-note"]');
            await page.waitForSelector('#note-title', { visible: true, timeout: TIMEOUTS.selector });
            await page.type('#note-title', `ldr-ui-session-expiry-write-${uniqueSuffix}`);
            await page.type('#note-content', 'This create must never reach the server as a success -- session is dead.');

            const pageAuthContext = await page
                .$eval('meta[name="auth-context"]', (el) => el.getAttribute('content'))
                .catch(() => null);
            let csrfRefreshRequests = 0;
            const countCsrfRefresh = (request) => {
                if (new URL(request.url()).pathname === '/auth/csrf-token') csrfRefreshRequests++;
            };
            page.on('request', countCsrfRefresh);
            try {
                const isCreateNotePost = (r) =>
                    new URL(r.url()).pathname === '/notes/api/notes' && r.request().method() === 'POST';
                const postRespPromise = waitForResponseToNewRequest(
                    page, isCreateNotePost, { timeout: TIMEOUTS.navigation }
                );
                const navPromise = page
                    .waitForNavigation({ waitUntil: 'domcontentloaded', timeout: TIMEOUTS.navigation })
                    .catch(() => null);
                await page.click('#save-note-btn');

                const postResp = await postRespPromise;
                const status = postResp.status();
                const sentContext = postResp.request().headers()['x-ldr-auth-context'];
                const marker = postResp.headers()['x-ldr-csrf-rejected'];
                console.log(`   (observed: status=${status} x-ldr-csrf-rejected=${marker})`);
                if (status !== 401) {
                    // Read the body only to explain a failure, and bound the
                    // read: Chrome does not finish loading a no-store fetch
                    // body that the page never reads (see Test 1).
                    const body = await withTimeout(postResp.text(), TIMEOUTS.selector, 'dead-session write body')
                        .catch((err) => `<unavailable: ${err.message}>`);
                    throw new Error(
                        `GENUINE DEFECT: expected CSRFMiddleware to reject a dead-session write with 401, ` +
                        `got HTTP ${status} (body: ${body})`
                    );
                }
                if (!pageAuthContext || sentContext !== pageAuthContext) {
                    throw new Error(
                        `Expected the write to carry this page's X-LDR-Auth-Context (${pageAuthContext}), got ${sentContext}`
                    );
                }
                if (marker !== undefined) {
                    throw new Error(`A dead-session write was answered as a CSRF rejection (x-ldr-csrf-rejected=${marker})`);
                }

                // The app's own JS must navigate to login itself; a broken
                // version would leave the page sitting there with no reaction.
                await navPromise;
                await assertLoginPageRendered(page, 'WRITE');
                if (csrfRefreshRequests !== 0) {
                    throw new Error(`Expected no CSRF token refresh for a signed-out write, saw ${csrfRefreshRequests}`);
                }
                await capture(page, SCREENSHOT_PREFIX, 'session_expiry_write_redirected_to_login');

                console.log(`PASSED (401 on the write, client-side redirect to ${page.url()}, real login form rendered)`);
                passed++;
            } finally {
                page.off('request', countCsrfRefresh);
            }
        } catch (e) {
            console.log(`FAILED: ${e.message}`);
            await captureOnFailure(page, SCREENSHOT_PREFIX, 'session_expiry_write', false);
            failed++;
        }

        // ---------------------------------------------------------------
        // Test 4: a dead session + a READ (GET) action -- isolates the auth
        // (401) code path from the CSRF layer above it (a GET carries no
        // CSRF token requirement; CSRFMiddleware only gates
        // POST/PUT/PATCH/DELETE). Uses the notes search box, typing 1
        // character -- below SemanticSearch.MIN_QUERY_LENGTH=2, so
        // loadNotes() always takes the plain keyword-listing GET path
        // regardless of AI search mode (see notes.js's `hasQuery` check),
        // avoiding any LLM/embeddings dependency.
        // ---------------------------------------------------------------
        console.log('Test 4: dead session + a GET action -- redirects to login (not a broken page/raw JSON/silent no-op)');
        try {
            // Test 3 leaves this browser on /auth/login without a session.
            // Load the login page first, so the sign-in check cannot mistake
            // a leftover /notes/ page for a live session, then sign in again.
            await page.goto(`${BASE_URL}/auth/login`, {
                waitUntil: 'domcontentloaded',
                timeout: TIMEOUTS.navigation,
            });
            await auth.ensureAuthenticatedWithTimeout();
            await openNotesWithDeadSession(page);

            await page.waitForSelector('#ldr-notes-search', { timeout: TIMEOUTS.selector });
            // The page's own bootstrap listing (/notes/api/notes?limit=...)
            // has the same path, so match the typed query too.
            const searchTerm = 'a';
            const isNotesSearchGet = (r) => {
                const url = new URL(r.url());
                return url.pathname === '/notes/api/notes'
                    && url.searchParams.get('search') === searchTerm
                    && r.request().method() === 'GET';
            };
            const getRespPromise = waitForResponseToNewRequest(
                page, isNotesSearchGet, { timeout: TIMEOUTS.navigation }
            );
            const navPromise = page
                .waitForNavigation({ waitUntil: 'domcontentloaded', timeout: TIMEOUTS.navigation })
                .catch(() => null);
            await page.type('#ldr-notes-search', searchTerm);

            const getResp = await getRespPromise;
            if (getResp.status() !== 401) {
                throw new Error(`Expected the invalidated session to 401 the notes-list GET, got ${getResp.status()}`);
            }

            // The app's own JS (safeFetchWithAuth) must notice the 401 and
            // navigate to login itself -- this await is what proves it's not
            // a silent no-op (a broken version would just leave the page
            // sitting there forever with no visible reaction).
            await navPromise;
            await assertLoginPageRendered(page, 'GET');
            await capture(page, SCREENSHOT_PREFIX, 'session_expiry_redirected_to_login');

            console.log(`PASSED (401 on the GET, client-side redirect to ${page.url()}, real login form rendered)`);
            passed++;
        } catch (e) {
            console.log(`FAILED: ${e.message}`);
            await captureOnFailure(page, SCREENSHOT_PREFIX, 'session_expiry_get', false);
            failed++;
        }

        // ---------------------------------------------------------------
        // Test 5: no uncaught JS errors were observed during any of the
        // above (Tests 1-4). Aggregated from listeners attached once at
        // the top of this file, not per-test, so nothing in between is
        // missed.
        // ---------------------------------------------------------------
        console.log('Test 5: no uncaught JS errors observed during Tests 1-4');
        try {
            if (allConsoleErrors.length > 0 || allPageErrors.length > 0) {
                throw new Error(
                    `${allConsoleErrors.length} console error(s), ${allPageErrors.length} page error(s):\n  ` +
                    [...allPageErrors, ...allConsoleErrors].join('\n  ')
                );
            }
            console.log('PASSED (0 console errors, 0 page errors)');
            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(`Error / Browser Session-Loss UX 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);
});