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.

5.2 KB
Raw Blame
/**
 * Settings Auto-Save Functionality UI Test
 *
 * Tests the settings auto-save workflow by monitoring network requests,
 * console messages, and response handling. Specifically tests the
 * save_all_settings endpoint, retains Reset to Defaults, and omits Fix
 * Corrupted Settings alongside the bulk-submit button.
 *
 * What this tests:
 * - Save All Settings button is absent
 * - Reset to Defaults button is present
 * - Fix Corrupted Settings button is absent
 * - Per-setting auto-save functionality
 * - Network request monitoring for save operations
 * - API response validation (200 vs 4xx/5xx errors)
 * - Success/error message display
 * - Console logging during save operations
 * - Checkbox change workflow
 *
 * Prerequisites: Web server running on http://127.0.0.1:5000
 *
 * Usage: node tests/ui_tests/test_settings_save.js
 */

const { setupTest, teardownTest, navigateTo, expandSettingsSectionFor } = require('./test_lib');

async function testSettingsSave() {
    const ctx = await setupTest({ authenticate: true });
    const { browser, page } = ctx;
    const baseUrl = ctx.config.baseUrl;

    // Monitor console errors
    page.on('console', msg => {
        if (msg.type() === 'error') {
            console.log(`  Browser error: ${msg.text()}`);
        }
    });

    // Monitor network responses (no request interception — it can hang
    // page.goto in CI environments when set up before the first navigation).
    page.on('response', response => {
        if (response.url().includes('/settings/')) {
            console.log('← RESPONSE:', response.status(), response.url());
            if (response.status() >= 400) {
                console.log('❌ ERROR RESPONSE:', response.status(), response.statusText());
            }
        }
    });

    let failed = false;

    try {
        console.log('🔧 Testing settings auto-save functionality...');
        await navigateTo(page, `${baseUrl}/settings/`);

        await page.waitForSelector('#settings-form', { timeout: 15000 });
        await page.waitForSelector('.ldr-settings-checkbox:not([disabled])', { timeout: 15000 });

        const bulkSubmitButton = await page.$('#settings-form button[type="submit"]');
        if (bulkSubmitButton) {
            throw new Error('Save All Settings button should not be present');
        }

        const resetToDefaultsButton = await page.$('#reset-to-defaults-button');
        if (!resetToDefaultsButton) {
            throw new Error('Reset to Defaults button should be present');
        }

        const fixCorruptedButton = await page.$('#fix-corrupted-button');
        if (fixCorruptedButton) {
            throw new Error('Fix Corrupted Settings button should not be present');
        }

        // Settings sections start collapsed on every viewport now, so the
        // checkbox is rendered inside a `display: none` body. Open its
        // section before clicking, or the click has no box to land on.
        const checkboxSelector = '.ldr-settings-checkbox:not([disabled])';
        await expandSettingsSectionFor(page, checkboxSelector, { timeout: 15000 });

        const checkbox = await page.$(checkboxSelector);
        const checkboxState = await checkbox.evaluate(element => ({
            key: element.name,
            value: element.checked,
        }));
        const expectedValue = !checkboxState.value;
        const responsePromise = page.waitForResponse(
            r => r.url().includes('/save_all_settings'),
            { timeout: 15000 }
        );

        await checkbox.click();
        const response = await responsePromise;
        if (!response.ok()) {
            throw new Error(`Auto-save failed with HTTP ${response.status()}`);
        }
        const responseData = await response.json();
        if (responseData.status !== 'success') {
            throw new Error(`Auto-save returned status ${responseData.status}`);
        }
        if (!responseData.updated?.includes(checkboxState.key)) {
            throw new Error(`Auto-save did not update ${checkboxState.key}`);
        }
        if (responseData.settings?.[checkboxState.key]?.value !== expectedValue) {
            throw new Error(
                `Auto-save response did not contain the new value for ${checkboxState.key}`
            );
        }

        const persisted = await page.evaluate(async key => {
            const persistedResponse = await fetch(`/settings/api/${encodeURIComponent(key)}`);
            return {
                ok: persistedResponse.ok,
                status: persistedResponse.status,
                data: await persistedResponse.json(),
            };
        }, checkboxState.key);
        if (!persisted.ok) {
            throw new Error(`Could not read saved setting: HTTP ${persisted.status}`);
        }
        if (persisted.data.value !== expectedValue) {
            throw new Error(`Auto-save did not persist ${checkboxState.key}`);
        }


        console.log('✅ Per-setting auto-save completed without a bulk-submit button');

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

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