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.

19.4 KB
Raw Blame
#!/usr/bin/env node
const {
    setupTest,
    teardownTest,
    TestResults,
    log,
    navigateTo,
    expandSettingsSectionFor,
} = require('./test_lib');

const checkboxSelector = 'input.ldr-settings-checkbox:not([disabled])';
const errorBannerSelector = '#notification-banner-assertive';
const saveEndpointPath = '/settings/save_all_settings';
const responseWaitTimeoutMs = 8000;

function assertCondition(condition, message) {
    if (!condition) throw new Error(message);
}

function getSaveUrl(baseUrl) {
    return new URL(saveEndpointPath, baseUrl).href;
}

function isSaveEndpointVariant(url) {
    try {
        return new URL(url).pathname.replace(/\/+$/, '') === saveEndpointPath;
    } catch {
        return false;
    }
}

function createInterceptionState() {
    return { requestHandlers: new Set(), interceptionEnabled: false };
}

async function disableSaveInterception(page, interceptionState) {
    try {
        for (const requestHandler of interceptionState.requestHandlers) {
            page.off('request', requestHandler);
        }
        interceptionState.requestHandlers.clear();
    } finally {
        if (interceptionState.interceptionEnabled) {
            interceptionState.interceptionEnabled = false;
            await page.setRequestInterception(false);
        }
    }
}

function createExactResponseWaiter(page, expectedUrl, timeoutMs = responseWaitTimeoutMs) {
    let resolveResponse;
    let rejectResponse;
    let settled = false;
    const responseHandler = (response) => {
        try {
            if (response.url() !== expectedUrl || response.request().method() !== 'POST') return;
            settle(resolveResponse, response);
        } catch (error) {
            settle(rejectResponse, error);
        }
    };
    const cleanup = () => {
        clearTimeout(timeoutId);
        page.off('response', responseHandler);
    };
    const settle = (settler, value) => {
        if (settled) return;
        settled = true;
        cleanup();
        settler(value);
    };
    const responsePromise = new Promise((resolve, reject) => {
        resolveResponse = resolve;
        rejectResponse = reject;
    });

    const timeoutId = setTimeout(
        () => settle(rejectResponse, new Error(`Timed out waiting for POST ${expectedUrl}`)),
        timeoutMs,
    );
    page.on('response', responseHandler);
    return {
        wait: () => responsePromise,
        cancel: () => settle(resolveResponse, null),
    };
}

async function navigateToSettings(page, baseUrl) {
    await navigateTo(page, `${baseUrl}/settings/`);
    await page.waitForSelector(checkboxSelector, { timeout: 15000 });
}

async function readCheckboxState(page, baseline) {
    return page.evaluate((selector, expectedId, expectedKey) => {
        const checkbox = Array.from(document.querySelectorAll(selector)).find(
            input => !expectedId || (input.id === expectedId && input.name === expectedKey)
        );
        if (!checkbox?.id || !checkbox.name) {
            throw new Error('Enabled settings checkbox with an id and name was not found');
        }

        const hiddenFallback = checkbox.dataset.hiddenFallback
            ? document.getElementById(checkbox.dataset.hiddenFallback)
            : null;
        return {
            id: checkbox.id,
            key: checkbox.name,
            domChecked: checkbox.checked,
            hiddenFallbackDisabled: hiddenFallback ? hiddenFallback.disabled : null,
        };
    }, checkboxSelector, baseline?.id || null, baseline?.key || null);
}

async function captureCheckboxBaseline(page, baseUrl) {
    await navigateToSettings(page, baseUrl);
    const initialState = await readCheckboxState(page);

    await page.reload({ waitUntil: 'domcontentloaded' });
    await page.waitForSelector(checkboxSelector, { timeout: 15000 });
    const persistedState = await readCheckboxState(page, initialState);

    return {
        ...persistedState,
        initialDomChecked: initialState.domChecked,
        persistedChecked: persistedState.domChecked,
    };
}

async function getTargetCheckbox(page, baseline) {
    // Settings sections start collapsed on every viewport now, so the
    // checkbox is in the DOM but inside a `display: none` body — a click on
    // it would have no box to land on. Open its section first. Every
    // .click() in this suite goes through the handle returned here, so this
    // one call covers the fresh-navigation and post-reload paths alike.
    await expandSettingsSectionFor(page, checkboxSelector, { timeout: 15000 });

    const checkbox = await page.$(checkboxSelector);
    assertCondition(checkbox, `Checkbox ${baseline.key} was not found`);
    const identity = await checkbox.evaluate(input => ({ id: input.id, key: input.name }));
    assertCondition(
        identity.id === baseline.id && identity.key === baseline.key,
        `Expected checkbox ${baseline.id}/${baseline.key}, found ${identity.id}/${identity.key}`,
    );
    return checkbox;
}

async function restoreCheckboxDom(page, baseline) {
    return page.evaluate((selector, id, key, checked, hiddenFallbackDisabled) => {
        const checkbox = Array.from(document.querySelectorAll(selector)).find(
            input => input.id === id && input.name === key
        );
        if (!checkbox) throw new Error(`Checkbox ${id}/${key} was not found for DOM restoration`);

        // Do not dispatch change: a failed request must not produce another save.
        checkbox.checked = checked;
        const hiddenFallback = checkbox.dataset.hiddenFallback
            ? document.getElementById(checkbox.dataset.hiddenFallback)
            : null;
        if (hiddenFallback && hiddenFallbackDisabled !== null) {
            hiddenFallback.disabled = hiddenFallbackDisabled;
        }
        return {
            domChecked: checkbox.checked,
            hiddenFallbackDisabled: hiddenFallback ? hiddenFallback.disabled : null,
        };
    }, checkboxSelector, baseline.id, baseline.key, baseline.domChecked, baseline.hiddenFallbackDisabled);
}

function getRequestContractFailures(request, expectedUrl, baseline, expectedChecked) {
    if (!request) return [`expected one request to ${expectedUrl}, but observed none`];

    const failures = [];
    if (request.url !== expectedUrl) failures.push(`expected URL ${expectedUrl}, got ${request.url}`);
    if (request.method !== 'POST') failures.push(`expected POST, got ${request.method}`);

    let payload;
    try {
        payload = JSON.parse(request.postData || '');
    } catch {
        failures.push(`request body was not JSON: ${request.postData || '<empty>'}`);
        return failures;
    }

    const isPlainObject = payload !== null &&
        typeof payload === 'object' &&
        !Array.isArray(payload) &&
        Object.getPrototypeOf(payload) === Object.prototype;
    if (!isPlainObject) {
        failures.push('request body was not a plain object');
        return failures;
    }

    const keys = Object.keys(payload);
    if (keys.length !== 1 || keys[0] !== baseline.key || payload[baseline.key] !== expectedChecked) {
        failures.push(
            `expected one-key payload ${JSON.stringify({ [baseline.key]: expectedChecked })}, got ${request.postData}`,
        );
    }
    return failures;
}

async function triggerFailedSave(page, baseUrl, baseline, status, message, interceptionState = createInterceptionState()) {
    await navigateToSettings(page, baseUrl);
    const expectedUrl = getSaveUrl(baseUrl);
    const expectedChecked = !baseline.domChecked;
    const expectedToast = `Error saving settings: ${message}`;
    const observedRequests = [];
    let attemptedSave = false;
    let afterFailedSave;
    let restoredState;

    const requestHandler = async (request) => {
        const observedRequest = {
            url: request.url(),
            method: request.method(),
            postData: request.postData(),
        };
        if (!isSaveEndpointVariant(observedRequest.url)) {
            await request.continue();
            return;
        }
        observedRequests.push(observedRequest);
        await request.respond({
            status,
            contentType: 'application/json',
            body: JSON.stringify({ message }),
        });
    };

    try {
        await page.setRequestInterception(true);
        interceptionState.interceptionEnabled = true;
        page.on('request', requestHandler);
        interceptionState.requestHandlers.add(requestHandler);

        const beforeSave = await readCheckboxState(page, baseline);
        assertCondition(
            beforeSave.domChecked === baseline.domChecked,
            `Checkbox ${baseline.key} did not start at the captured DOM baseline`,
        );
        const checkbox = await getTargetCheckbox(page, baseline);
        attemptedSave = true;
        await checkbox.click();
        await page.waitForFunction(
            (selector, expectedText) =>
                document.querySelector(`${selector} span`)?.textContent.trim() === expectedText,
            { timeout: 8000 },
            errorBannerSelector,
            expectedToast,
        );
        afterFailedSave = await readCheckboxState(page, baseline);
    } finally {
        try {
            if (attemptedSave) {
                const requestCountBeforeRestore = observedRequests.length;
                restoredState = await restoreCheckboxDom(page, baseline);
                assertCondition(
                    observedRequests.length === requestCountBeforeRestore,
                    'Restoring the failed checkbox DOM state issued another request',
                );
            }
        } finally {
            await disableSaveInterception(page, interceptionState);
        }
    }

    const result = await page.evaluate((bannerSelector, selector, id, key) => {
        const input = Array.from(document.querySelectorAll(selector)).find(
            checkbox => checkbox.id === id && checkbox.name === key
        );
        const loadingContainer = input?.closest('.ldr-checkbox-label') || input;
        return {
            toastText: document.querySelector(`${bannerSelector} span`)?.textContent.trim() || '',
            hasSuccess: input?.classList.contains('ldr-save-success') === true,
            hasSaving: loadingContainer?.classList.contains('ldr-saving') === true,
        };
    }, errorBannerSelector, checkboxSelector, baseline.id, baseline.key);
    const observedRequest = observedRequests.length === 1 ? observedRequests[0] : null;
    const failures = getRequestContractFailures(
        observedRequest,
        expectedUrl,
        baseline,
        expectedChecked,
    );
    if (observedRequests.length !== 1) {
        failures.push(`expected one save-endpoint request, got ${observedRequests.length}`);
    }
    if (afterFailedSave.domChecked !== expectedChecked) {
        failures.push(`checkbox did not toggle to ${expectedChecked} before the failed save`);
    }
    if (result.toastText !== expectedToast) failures.push(`expected toast "${expectedToast}", got "${result.toastText}"`);
    if (result.hasSuccess) failures.push('failed save showed a success indicator');
    if (result.hasSaving) failures.push('failed save left the checkbox loading');
    if (restoredState.domChecked !== baseline.domChecked ||
        restoredState.hiddenFallbackDisabled !== baseline.hiddenFallbackDisabled) {
        failures.push('failed save did not restore the captured DOM checkbox state');
    }

    return {
        passed: failures.length === 0,
        message: failures.length === 0
            ? `${status} POST ${saveEndpointPath} sent ${baseline.key}=${expectedChecked} and rendered the exact error toast`
            : `${status} save contract failed: ${failures.join('; ')}`,
    };
}

async function saveCheckboxValue(page, expectedUrl, baseline, expectedChecked) {
    const beforeSave = await readCheckboxState(page, baseline);
    assertCondition(
        beforeSave.domChecked !== expectedChecked,
        `Checkbox ${baseline.key} was already ${expectedChecked} before normal save`,
    );
    const responseWaiter = createExactResponseWaiter(page, expectedUrl);
    try {
        const checkbox = await getTargetCheckbox(page, baseline);
        await checkbox.click();
        const response = await responseWaiter.wait();
        assertCondition(response.ok(), `Normal save returned HTTP ${response.status()}`);

        const request = response.request();
        const failures = getRequestContractFailures({
            url: request.url(),
            method: request.method(),
            postData: request.postData(),
        }, expectedUrl, baseline, expectedChecked);
        assertCondition(failures.length === 0, `Normal save contract failed: ${failures.join('; ')}`);

        await page.waitForFunction((selector, id, key, expected) => {
            const input = Array.from(document.querySelectorAll(selector)).find(
                candidate => candidate.id === id && candidate.name === key
            );
            const loadingContainer = input?.closest('.ldr-checkbox-label') || input;
            return input?.checked === expected &&
                input.classList.contains('ldr-save-success') &&
                !loadingContainer?.classList.contains('ldr-saving');
        }, { timeout: 8000 }, checkboxSelector, baseline.id, baseline.key, expectedChecked);
    } finally {
        responseWaiter.cancel();
    }
}

async function restorePersistedCheckboxState(page, baseUrl, baseline) {
    await navigateToSettings(page, baseUrl);
    await page.reload({ waitUntil: 'domcontentloaded' });
    await page.waitForSelector(checkboxSelector, { timeout: 15000 });
    const expectedUrl = getSaveUrl(baseUrl);
    const currentState = await readCheckboxState(page, baseline);
    if (currentState.domChecked !== baseline.persistedChecked) {
        await saveCheckboxValue(page, expectedUrl, baseline, baseline.persistedChecked);
    }
    await page.reload({ waitUntil: 'domcontentloaded' });
    await page.waitForSelector(checkboxSelector, { timeout: 15000 });
    const reloadedState = await readCheckboxState(page, baseline);
    assertCondition(
        reloadedState.domChecked === baseline.persistedChecked,
        `Checkbox ${baseline.key} did not restore its persisted baseline`,
    );
}

const SettingsSaveErrorTests = {
    async serverErrorShowsToast(page, baseUrl, baseline, interceptionState) {
        return triggerFailedSave(
            page,
            baseUrl,
            baseline,
            500,
            'Internal server error (test injection)',
            interceptionState,
        );
    },

    async csrfExpiryShowsToast(page, baseUrl, baseline, interceptionState) {
        return triggerFailedSave(
            page,
            baseUrl,
            baseline,
            400,
            'CSRF token expired',
            interceptionState,
        );
    },

    async normalSaveShowsSuccess(page, baseUrl, baseline) {
        await navigateToSettings(page, baseUrl);
        const initialState = await readCheckboxState(page, baseline);
        assertCondition(
            initialState.domChecked === baseline.persistedChecked,
            `Checkbox ${baseline.key} did not start normal save at its persisted baseline`,
        );

        let needsPersistedRestore = true;
        try {
            const expectedUrl = getSaveUrl(baseUrl);
            await saveCheckboxValue(page, expectedUrl, baseline, !baseline.persistedChecked);
            await saveCheckboxValue(page, expectedUrl, baseline, baseline.persistedChecked);
            await page.reload({ waitUntil: 'domcontentloaded' });
            await page.waitForSelector(checkboxSelector, { timeout: 15000 });
            const reloadedState = await readCheckboxState(page, baseline);
            assertCondition(
                reloadedState.domChecked === baseline.persistedChecked,
                `Normal save did not restore ${baseline.key} after reload`,
            );
            needsPersistedRestore = false;
            return {
                passed: true,
                message: `Normal POST save toggled ${baseline.key} and restored its persisted baseline after reload`,
            };
        } finally {
            if (needsPersistedRestore) await restorePersistedCheckboxState(page, baseUrl, baseline);
        }
    },
};

async function runSettingsSaveErrorSuite(
    page,
    baseUrl,
    recordResult,
    testFunctions = SettingsSaveErrorTests,
) {
    const interceptionState = createInterceptionState();
    let checkboxBaseline;
    let baselineReady = false;

    async function run(name, testFn) {
        let result;
        try {
            result = await testFn(page, baseUrl, checkboxBaseline, interceptionState);
        } catch (error) {
            recordResult(name, false, `Error: ${error.message}`);
            throw error;
        }

        recordResult(name, result.passed, result.message, result.skipped);
        if (!result.skipped && !result.passed) {
            throw new Error(`${name} failed: ${result.message}`);
        }
    }

    try {
        checkboxBaseline = await captureCheckboxBaseline(page, baseUrl);
        assertCondition(
            checkboxBaseline.initialDomChecked === checkboxBaseline.persistedChecked,
            `Checkbox ${checkboxBaseline.key} changed before the test could establish a persisted baseline`,
        );
        baselineReady = true;
        await run('5xx shows exact error toast', testFunctions.serverErrorShowsToast);
        await run('CSRF expiry shows exact error toast', testFunctions.csrfExpiryShowsToast);
        await run('Normal save restores original value', testFunctions.normalSaveShowsSuccess);
    } catch (error) {
        if (!baselineReady) {
            recordResult('Capture checkbox baseline', false, `Error: ${error.message}`);
        }
    } finally {
        let interceptionDisabled = false;
        try {
            await disableSaveInterception(page, interceptionState);
            interceptionDisabled = true;
        } catch (error) {
            recordResult('Disable save interception', false, `Cleanup error: ${error.message}`);
        }

        if (checkboxBaseline && interceptionDisabled) {
            try {
                await restorePersistedCheckboxState(page, baseUrl, checkboxBaseline);
            } catch (error) {
                recordResult('Restore original persisted checkbox value', false, `Cleanup error: ${error.message}`);
            }
        } else if (checkboxBaseline) {
            recordResult(
                'Restore original persisted checkbox value',
                false,
                'Cleanup error: request interception could not be disabled before persisted restoration',
            );
        }
    }
}

async function main() {
    log.section('Settings Save Error CI Tests');
    const ctx = await setupTest({ authenticate: true });
    const results = new TestResults('Settings Save Error CI Tests');
    const { page } = ctx;
    const { baseUrl } = ctx.config;

    try {
        await runSettingsSaveErrorSuite(
            page,
            baseUrl,
            (name, passed, message, skipped) => {
                if (skipped) {
                    results.skip('Save Error', name, message);
                } else {
                    results.add('Save Error', name, passed, message);
                }
            },
        );
    } finally {
        results.print();
        results.save();
        await teardownTest(ctx);
        process.exit(results.exitCode());
    }
}

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

module.exports = { SettingsSaveErrorTests, runSettingsSaveErrorSuite };