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.8 KB
Raw Blame
/**
 * Settings Error Detection UI Test
 *
 * Tests settings page for error messages that appear when changing values.
 * Monitors console errors and checks for error elements on the page after
 * making changes to settings. This helps identify validation errors or
 * save failures.
 *
 * What this tests:
 * - Console error detection during setting changes
 * - Network error monitoring (4xx/5xx responses)
 * - DOM error element detection (.error, .alert-danger, etc.)
 * - Setting input interaction (dropdowns, text fields)
 *
 * Prerequisites: Web server running on http://127.0.0.1:5000
 *
 * Usage: node tests/ui_tests/test_settings_errors.js
 */

const puppeteer = require('puppeteer');
const AuthHelper = require('./auth_helper');
const { getPuppeteerLaunchOptions } = require('./puppeteer_config');
const { expandAllSettingsSections } = require('./test_lib');

async function testSettingsChange() {
    const browser = await puppeteer.launch(getPuppeteerLaunchOptions());
    const page = await browser.newPage();
    const baseUrl = 'http://127.0.0.1:5000';
    const authHelper = new AuthHelper(page, baseUrl);

    // Monitor console errors
    page.on('console', msg => {
        if (msg.type() === 'error') {
            console.log('❌ BROWSER ERROR:', msg.text());
        }
    });

    // Monitor network errors
    page.on('response', response => {
        if (response.status() >= 400) {
            console.log('❌ NETWORK ERROR:', response.status(), response.url());
        }
    });

    let failed = false;

    try {
        console.log('🔧 Testing settings change functionality...');
        await authHelper.ensureAuthenticatedWithTimeout();
        await page.goto('http://127.0.0.1:5000/settings/', {
            waitUntil: 'domcontentloaded',
            timeout: 30000
        });

        // Wait for page to load
        await new Promise(resolve => setTimeout(resolve, 3000));

        // Try to find and change a simple setting
        console.log('🔍 Looking for a setting to change...');

        // Find a dropdown or input field to change
        const settingInput = await page.$('select[data-key], input[data-key]');

        if (settingInput) {
            console.log('✅ Found setting input, attempting to change...');

            // Get the current value
            const currentValue = await page.evaluate(el => el.value, settingInput);
            console.log('Current value:', currentValue);

            // Try to change it
            await page.evaluate(el => {
                if (el.tagName === 'SELECT') {
                    // For dropdown, select a different option
                    if (el.options.length > 1) {
                        el.selectedIndex = el.selectedIndex === 0 ? 1 : 0;
                        el.dispatchEvent(new Event('change', { bubbles: true }));
                    }
                } else {
                    // For input, change the value
                    el.value += '_test';
                    el.dispatchEvent(new Event('change', { bubbles: true }));
                }
            }, settingInput);

            console.log('⏳ Waiting for any errors after change...');
            await new Promise(resolve => setTimeout(resolve, 2000));

        } else {
            console.log('❌ No setting inputs found');
            // Both branches below used to only log — this file exited 0
            // regardless of what it found, so it never actually detected
            // anything. Not finding a single `[data-key]` setting input on
            // the settings page is itself a real failure (the thing this
            // test exists to exercise never ran), so it is wired to `failed`.
            failed = true;
        }

        // Check for any error messages on the page.
        //
        // Only VISIBLE errors carrying text count. The selector below is
        // deliberately broad — `[class*="error"]` matches anything with
        // "error" anywhere in its class — and the templates ship several
        // always-present, initially-empty containers that it hits:
        // `ldr-error-message`, `ldr-error-text`, `ldr-field-error`,
        // `ldr-input-help ldr-text-error`. `page.$$` returns those regardless
        // of visibility, so failing on mere presence would fail every run
        // whatever the page did — swapping a test that could never fail for
        // one that could never pass, which is no better.
        // ...and "visible" is only meaningful once the sections are open.
        // Field-level errors are created on demand by settings.js's
        // `markInvalidInput`, which already reveals the section it just
        // wrote into, and a top-level `showAlert` banner writes into
        // `#settings-alert` / `#filtered-settings-alert`, both outside every
        // `.ldr-settings-section-body`. So neither real error source
        // strictly depends on this expand to be visible. It is defensive:
        // without it, an error container that does end up inside a
        // collapsed body could still be hidden from the filter below by
        // `offsetParent === null`, letting the suite pass no matter what the
        // page did.
        const expandedSections = await expandAllSettingsSections(page);
        console.log(`🔓 Expanded ${expandedSections} collapsed settings section(s) before checking`);

        const errorElements = await page.$$('.error, .alert-danger, .text-danger, [class*="error"]');
        const shownErrors = [];
        for (const el of errorElements) {
            const info = await page.evaluate((node) => {
                const style = window.getComputedStyle(node);
                const hidden =
                    style.display === 'none' ||
                    style.visibility === 'hidden' ||
                    node.offsetParent === null;
                return { hidden, text: (node.textContent || '').trim() };
            }, el);
            if (!info.hidden && info.text) {
                shownErrors.push(info.text);
            }
        }

        if (shownErrors.length > 0) {
            console.log(`❌ Found ${shownErrors.length} visible error message(s) after a routine setting change`);
            shownErrors.forEach((text, i) => console.log(`   Error ${i + 1}: ${text}`));
            // This is the whole point of the test — a visible error or
            // validation message after a routine setting change is exactly
            // what should fail it. It used to only log.
            failed = true;
        } else {
            console.log(`✅ No visible error messages (${errorElements.length} empty/hidden error containers ignored)`);
        }

    } catch (error) {
        console.error('❌ Test error:', error);
        failed = true;
    } finally {
        await browser.close();
        process.exit(failed ? 1 : 0);
    }
}

testSettingsChange().catch(err => { console.error(err); process.exit(1); });