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.

6.3 KB
Raw Blame
/**
 * Frontend bundle integrity.
 *
 * The FastAPI migration kept `base.html`'s `{{ vite_asset('js/app.js') }}`
 * helper, which resolves through the Vite manifest at
 * `static/dist/.vite/manifest.json`. `dist/` is gitignored and produced by
 * `npm run build` (the Dockerfile runs `npm ci && npm run build`), so the
 * bundle is a BUILD ARTEFACT, not source — nothing in the repo proves it
 * actually reaches the browser.
 *
 * That matters because the rest of the UI suite does not notice when it
 * is absent. Verified while porting this branch: with `dist/` empty, every
 * page still returns HTTP 200 and `test_login_validation.js` still passes
 * 5/5 — it exercises native HTML5 form validation, which needs no app JS
 * at all. A broken or empty build would therefore ship with a green suite.
 *
 * This test closes that hole. It asserts, for each page, that:
 *   1. the page references a hashed bundle under /static/dist/,
 *   2. that bundle actually loads (no failed request for it),
 *   3. the browser reports no uncaught errors while loading it,
 *   4. app JS genuinely executed, proven by a DOM side effect that only
 *      the bundle produces (not merely by the <script> tag existing).
 *
 * Deliberately uses only pre-auth pages so it stays fast and cannot fail
 * for credential/DB reasons — the point is asset delivery, not app logic.
 */

const puppeteer = require('puppeteer');

const BASE_URL = process.env.LDR_BASE_URL || 'http://127.0.0.1:5000';
const PAGES = ['/auth/login', '/auth/register'];

async function checkPage(browser, path) {
    const page = await browser.newPage();
    const consoleErrors = [];
    const failedRequests = [];

    // Console "Failed to load resource" messages carry no URL, so they
    // cannot be attributed. Resource failures are already checked below,
    // URL-aware and scoped to /static/dist/ — which is what this guard is
    // about. Counting them here too would only add an unattributable
    // duplicate, and would fail every page over the browser's own
    // speculative /favicon.ico probe (the app ships favicon.png, declared
    // by base.html, and no .ico). So this channel carries real JS errors
    // only.
    page.on('console', (msg) => {
        if (msg.type() === 'error' && !msg.text().startsWith('Failed to load resource')) {
            consoleErrors.push(msg.text());
        }
    });
    page.on('pageerror', (err) => consoleErrors.push(`pageerror: ${err.message}`));
    page.on('requestfailed', (req) => {
        failedRequests.push(`${req.url()} (${req.failure()?.errorText})`);
    });

    const responses = [];
    page.on('response', (res) => responses.push({ url: res.url(), status: res.status() }));

    await page.goto(`${BASE_URL}${path}`, { waitUntil: 'networkidle2', timeout: 30000 });

    // 1. the page must reference a hashed bundle
    const bundleSrcs = await page.$$eval('script[src]', (els) =>
        els.map((e) => e.getAttribute('src')).filter((s) => s && s.includes('/static/dist/js/'))
    );
    if (bundleSrcs.length === 0) {
        throw new Error(
            `${path}: no /static/dist/js/ bundle referenced. The Vite manifest is ` +
            `missing or vite_asset() fell back to nothing — run 'npm run build'. ` +
            `Pages still return 200 in this state, which is why the rest of the ` +
            `UI suite does not catch it.`
        );
    }

    // 2. every dist asset the page requested must have loaded
    const badAssets = responses.filter(
        (r) => r.url.includes('/static/dist/') && r.status >= 400
    );
    if (badAssets.length > 0) {
        throw new Error(
            `${path}: dist asset(s) failed to load: ` +
            badAssets.map((a) => `${a.url} -> ${a.status}`).join(', ')
        );
    }
    const failedDist = failedRequests.filter((f) => f.includes('/static/dist/'));
    if (failedDist.length > 0) {
        throw new Error(`${path}: dist request(s) failed outright: ${failedDist.join(', ')}`);
    }

    // 3. no uncaught errors while the bundle initialised
    if (consoleErrors.length > 0) {
        throw new Error(
            `${path}: browser reported ${consoleErrors.length} error(s) — a bundle ` +
            `that loads but throws on init leaves the UI dead while the page still ` +
            `renders 200:\n  ${consoleErrors.join('\n  ')}`
        );
    }

    // 4. prove the bundle EXECUTED, not merely that the tag was present.
    //    app.js's own top-level code assigns window.bootstrap (and the other
    //    vendor libs) as a side effect of module init — a signal only the
    //    bundle itself can produce.
    //    NOTE: <html data-theme="..."> is NOT usable here — base.html hardcodes
    //    data-theme="hashed" directly in the markup (line 2) AND sets it again
    //    via an inline <head> script that is independent of app.js/Vite. Both
    //    fire whether or not the dist bundle loads, so that attribute is always
    //    present and previously made this check vacuous (always true).
    const executed = await page.evaluate(() => Boolean(window.bootstrap));
    if (!executed) {
        throw new Error(
            `${path}: the bundle was referenced and fetched, but no evidence it ran ` +
            `(window.bootstrap was never set, though app.js's top-level code sets it ` +
            `unconditionally on init). A silently broken build would look exactly like this.`
        );
    }

    await page.close();
    return bundleSrcs[0];
}

(async () => {
    let browser;
    let failures = 0;
    try {
        browser = await puppeteer.launch({
            headless: 'new',
            args: ['--no-sandbox', '--disable-setuid-sandbox'],
        });

        for (const path of PAGES) {
            try {
                const bundle = await checkPage(browser, path);
                console.log(`✅ ${path} — bundle served and executed (${bundle})`);
            } catch (err) {
                failures += 1;
                console.error(`❌ ${err.message}`);
            }
        }
    } catch (err) {
        console.error(`❌ harness error: ${err.message}`);
        failures += 1;
    } finally {
        if (browser) await browser.close();
    }

    console.log('='.repeat(50));
    console.log(
        `📊 Frontend bundle integrity: ${PAGES.length - failures} passed, ${failures} failed`
    );
    process.exit(failures === 0 ? 0 : 1);
})();