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.

12.0 KB
Raw Blame
#!/usr/bin/env node
/**
 * Settings No-JS Form POST Fallback + Flash Messages (CI-safe)
 * ===============================================================
 *
 * General migration-risk coverage: settings_dashboard.html's #settings-form
 * (method="POST", action="/settings/save_settings") is a genuine, real
 * form-POST-and-redirect round trip — the "fallback when JavaScript is
 * disabled" (web/routers/settings.py::save_settings docstring). It is
 * unrelated to the AJAX save path settings.js normally intercepts, and
 * every existing settings suite exercises only that AJAX path
 * (fetch-based saves, checked via #settings-alert / toast). Nothing drives
 * a REAL native submission of #settings-form.
 *
 * This is exactly the migration-risk class the review asked for: the
 * handler's own comment says "Flask flashed the same success/error; the
 * migration had dropped it" — i.e. this flash-on-redirect behavior was
 * LOST by the Flask->FastAPI port and had to be hand-restored
 * (web/dependencies/flash.py, a from-scratch session-backed replacement
 * for Flask's flash()/get_flashed_messages()). A regression here is
 * invisible to every AJAX-path test: the JSON save endpoint doesn't touch
 * flash() at all.
 *
 * HTMLFormElement.prototype.submit() deliberately does NOT fire the
 * 'submit' event (web platform spec), so calling it bypasses settings.js's
 * AJAX interceptor entirely and drives the exact real POST a JS-disabled
 * browser would send — without needing page.setJavaScriptEnabled(false)
 * (which would also block Puppeteer's own page.evaluate() calls). The
 * settings fields are still JS-populated first so the submitted form
 * carries real data, matching what a normal page load produces before JS
 * fails/is blocked.
 *
 * Run: node test_settings_form_post_fallback_ci.js
 */

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

async function loadSettingsFormWithFields(page, baseUrl) {
    await page.goto(`${baseUrl}/settings/`, { waitUntil: 'domcontentloaded', timeout: 30000 });
    await page.waitForSelector(
        '#settings-form .ldr-settings-checkbox:not([disabled]), ' +
            '#settings-form input:not([type=hidden]):not([disabled]), ' +
            '#settings-form select:not([disabled])',
        { timeout: 15000 },
    );
}

/** Submit #settings-form as a real native form POST (bypasses the JS 'submit' interceptor) and wait for the redirect. */
async function submitSettingsFormNatively(page) {
    await Promise.all([
        page.waitForNavigation({ waitUntil: 'domcontentloaded', timeout: 30000 }),
        page.evaluate(() => window.HTMLFormElement.prototype.submit.call(document.getElementById('settings-form'))),
    ]);
}

async function readAlerts(page) {
    return page.evaluate(() =>
        Array.from(document.querySelectorAll('.ldr-alert')).map((el) => ({
            className: el.className,
            text: el.textContent.trim(),
        })),
    );
}

async function main() {
    log.section('Settings No-JS Form POST Fallback + Flash Messages');
    const ctx = await setupTest({ authenticate: true });
    const results = new TestResults('Settings Form POST Fallback 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. Happy path: real POST -> 302 redirect -> success flash
        // ===============================================================
        await run(
            'Form POST fallback',
            'Native #settings-form submit redirects to /settings/ and flashes "Settings saved."',
            async () => {
                await loadSettingsFormWithFields(page, baseUrl);

                const formInfo = await page.evaluate(() => {
                    const form = document.getElementById('settings-form');
                    return {
                        hasForm: !!form,
                        action: form ? new URL(form.action).pathname : null,
                        method: form ? form.method.toUpperCase() : null,
                        hasCsrf: !!form?.querySelector('input[name="csrf_token"]')?.value,
                    };
                });
                if (!formInfo.hasForm || formInfo.action !== '/settings/save_settings' || formInfo.method !== 'POST' || !formInfo.hasCsrf) {
                    return { passed: false, message: `Form contract changed: ${JSON.stringify(formInfo)}` };
                }

                await submitSettingsFormNatively(page);

                const url = new URL(page.url());
                const alerts = await readAlerts(page);
                const successAlert = alerts.find((a) => a.className.includes('ldr-alert-success') && /settings saved/i.test(a.text));

                const passed = url.pathname === '/settings/' && !!successAlert;
                return {
                    passed,
                    message: passed
                        ? `Redirected to ${url.pathname}, flash="${successAlert.text}"`
                        : `path=${url.pathname}, alerts=${JSON.stringify(alerts)}`,
                };
            },
        );

        // ===============================================================
        // 2. An unrecognized new setting key is rejected with a VISIBLE
        //    warning (not a silent redirect) — the exact case the
        //    hand-restored flash() call was added to surface.
        // ===============================================================
        await run(
            'Form POST fallback',
            'A disallowed new setting key is silently ignored server-side but surfaced via a warning flash',
            async () => {
                await loadSettingsFormWithFields(page, baseUrl);

                await page.evaluate(() => {
                    const form = document.getElementById('settings-form');
                    const input = document.createElement('input');
                    input.type = 'hidden';
                    // Namespace not in ALLOWED_SETTING_PREFIXES (web/routers/settings.py) -> rejected.
                    input.name = 'zzz_ui_test_namespace.marker';
                    input.value = 'should-be-rejected';
                    form.appendChild(input);
                });

                await submitSettingsFormNatively(page);

                const url = new URL(page.url());
                const alerts = await readAlerts(page);
                const warningAlert = alerts.find(
                    (a) => a.className.includes('ldr-alert-warning') && /unrecognized key/i.test(a.text),
                );

                // Confirm the key was NOT actually created (rejected server-side, not silently accepted).
                const apiCheck = await page.evaluate(async () => {
                    const r = await fetch('/settings/api/zzz_ui_test_namespace.marker', { credentials: 'same-origin' });
                    return r.status;
                });

                const passed = url.pathname === '/settings/' && !!warningAlert && apiCheck === 404;
                return {
                    passed,
                    message: passed
                        ? `Redirected to ${url.pathname}, flash="${warningAlert.text}", key-not-created (404)`
                        : `path=${url.pathname}, alerts=${JSON.stringify(alerts)}, keyLookupStatus=${apiCheck}`,
                };
            },
        );

        await run('Form POST fallback', 'Invalid summary shows its constraint while a valid mixed field persists', async () => {
            await loadSettingsFormWithFields(page, baseUrl);
            const summaryKey = 'search.fetch.summary_input_max_length';
            await page.waitForSelector(`#settings-form [name="${summaryKey}"]`);
            await page.waitForSelector('#settings-form [name="llm.temperature"]');
            const before = await page.evaluate(async (key) => (await fetch(`/settings/api/${key}`)).json(), summaryKey);
            await page.evaluate((key) => {
                document.querySelector(`[name="${key}"]`).value = '0';
                document.querySelector('[name="llm.temperature"]').value = '0.55';
            }, summaryKey);
            const postedRequest = page.waitForRequest((request) =>
                request.method() === 'POST' && new URL(request.url()).pathname === '/settings/save_settings');
            await submitSettingsFormNatively(page);
            const posted = new URLSearchParams((await postedRequest).postData());
            const warning = await page.$eval('.ldr-alert-warning[role="alert"]', (el) => el.textContent.trim());
            const after = await page.evaluate(async (key) => ({
                summary: await (await fetch(`/settings/api/${key}`)).json(),
                temperature: await (await fetch('/settings/api/llm.temperature')).json(),
            }), summaryKey);
            const reload = await page.goto(`${baseUrl}/settings/`, { waitUntil: 'domcontentloaded' });
            const html = await reload.text();
            const passed = posted.get(summaryKey) === '0' && posted.get('llm.temperature') === '0.55' &&
                warning.includes('Saved with 1 setting(s) failing.') &&
                warning.includes(`${summaryKey}: Value must be a positive integer`) &&
                !warning.includes('llm.temperature:') && after.temperature.value === 0.55 &&
                after.summary.value === before.value && !html.includes('Value must be a positive integer');
            return { passed, message: `posted summary=${posted.get(summaryKey)}, temperature=${posted.get('llm.temperature')}; alert=${warning}; persisted temperature=${after.temperature.value}; summary unchanged=${after.summary.value === before.value}; flash consumed=${!html.includes('Value must be a positive integer')}` };
        });

        // ===============================================================
        // 3. Flash messages are one-shot: reloading the redirect target
        //    again must NOT re-show the same message (session .pop(), not
        //    .get() — a stuck flash would otherwise mislead every
        //    subsequent visit into thinking the save just happened).
        // ===============================================================
        await run('Form POST fallback', 'Flash message does not persist past a single subsequent page load', async () => {
            // Plain substring check on the raw HTML rather than DOM-parsing it —
            // avoids relying on a browser global (DOMParser) inside page.evaluate()
            // just to answer "does this exact phrase appear anywhere in the response."
            const reloadHtml = await page.evaluate(async () => {
                const r = await fetch(window.location.href, { credentials: 'same-origin' });
                return r.text();
            });
            const stillShowsSaveMessage = /settings saved|unrecognized key/i.test(reloadHtml);
            return {
                passed: !stillShowsSaveMessage,
                message: stillShowsSaveMessage
                    ? 'Flash message reappeared on reload (session not cleared)'
                    : 'Flash message consumed after one render, as expected',
            };
        });
    } catch (error) {
        log.error(`Fatal error: ${error.message}`);
        console.error(error.stack);
    } finally {
        results.print();
        results.save(ctx.config.resultsDir);
        await teardownTest(ctx);
        process.exit(results.exitCode());
    }
}

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