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.

13.4 KB
Raw Blame
#!/usr/bin/env node
/**
 * News Subscription Form: Search-Engine Dropdown Scope Awareness (CI-safe)
 * ==========================================================================
 *
 * Companion to test_search_engine_dropdown_scope_ci.js. That suite covers
 * the research page's dropdown; this one covers the OTHER consumer of the
 * same egress-aware GET /settings/api/available-search-engines contract
 * (#5221 / issue #5204): the news-subscription create/edit forms.
 *
 * Unlike the research page (which has its own per-run egress-scope
 * selector), news-subscription-form.html has no scope control of its own —
 * it reads the user's SAVED `policy.egress_scope` setting server-side at
 * render time (`default_settings.egress_scope` in the Jinja context, ported
 * by hand into web/routers/news_pages.py) and bakes it into the page's
 * inline `loadSearchEngines()` script. Without that context var the merged
 * template silently falls back to "adaptive" and ignores the user's saved
 * scope — exactly the regression class this suite guards against.
 *
 * Covers both routes that render the form:
 *   - GET /news/subscriptions/new
 *   - GET /news/subscriptions/<id>/edit
 *
 * Run: node test_news_subscription_engine_dropdown_ci.js
 */

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

const ENGINES_PATH_SUFFIX = '/settings/api/available-search-engines';
const SEARCH_ENGINE_INPUT = '#subscription-search-engine';
const SEARCH_ENGINE_LIST = '#subscription-search-engine-dropdown-list';

function enginesResponseMatcher() {
    return (r) => r.request().method() === 'GET' && new URL(r.url()).pathname.endsWith(ENGINES_PATH_SUFFIX);
}

/**
 * Open the subscription form's search-engine custom dropdown.
 *
 * news-subscription-form.html wires up setupCustomDropdown() via
 * `setTimeout(initializeDropdowns, 100)`, decoupled from the
 * loadSearchEngines() fetch this test already waits on — so on a fast
 * local server the click handler can still be unattached at the moment
 * the input first appears. Retry the click (bounded, no fixed sleep)
 * rather than assume one click always lands after a listener exists.
 */
async function openSearchEngineDropdown(page, timeout = 15000) {
    await page.waitForSelector(SEARCH_ENGINE_INPUT, { timeout });
    const deadline = Date.now() + timeout;
    while (Date.now() < deadline) {
        await page.click(SEARCH_ENGINE_INPUT);
        const ready = await page
            .waitForFunction(
                (sel) => document.querySelectorAll(`${sel} .ldr-custom-dropdown-item`).length > 0,
                { timeout: 1000 },
                SEARCH_ENGINE_LIST,
            )
            .then(() => true)
            .catch(() => false);
        if (ready) return;
    }
    throw new Error(`Dropdown at ${SEARCH_ENGINE_INPUT} never rendered items within ${timeout}ms`);
}

async function closeOpenDropdown(page) {
    await page.click('.ldr-top-bar');
}

async function readDropdownItems(page) {
    return page.evaluate((sel) => {
        const items = Array.from(document.querySelectorAll(`${sel} .ldr-custom-dropdown-item`));
        return items.map((el) => ({
            value: el.getAttribute('data-value'),
            disabled: el.classList.contains('ldr-custom-dropdown-item--disabled'),
            ariaDisabled: el.getAttribute('aria-disabled'),
            reason: el.querySelector('.ldr-dropdown-item-disabled-reason')?.textContent || null,
        }));
    }, SEARCH_ENGINE_LIST);
}

/** Set the SAVED egress scope via the research page's selector + wait for the PUT to persist. */
async function setSavedEgressScope(page, baseUrl, value) {
    const initialLoadP = page.waitForResponse(enginesResponseMatcher(), { timeout: 20000 }).catch(() => null);
    await page.goto(`${baseUrl}/`, { waitUntil: 'domcontentloaded', timeout: 30000 });
    await page.waitForSelector('#policy_egress_scope', { timeout: 15000 });
    await initialLoadP;

    const current = await page.$eval('#policy_egress_scope', (el) => el.value);
    if (current === value) return;

    const putP = page.waitForResponse(
        (r) => r.request().method() === 'PUT' && new URL(r.url()).pathname === '/settings/api/policy.egress_scope',
        { timeout: 20000 },
    );
    await page.select('#policy_egress_scope', value);
    const resp = await putP;
    if (!resp.ok()) {
        throw new Error(`Saving policy.egress_scope=${value} failed with HTTP ${resp.status()}`);
    }
}

async function loadSubscriptionForm(page, url) {
    const respP = page.waitForResponse(enginesResponseMatcher(), { timeout: 20000 });
    await page.goto(url, { waitUntil: 'domcontentloaded', timeout: 30000 });
    const response = await respP;
    await page.waitForSelector(SEARCH_ENGINE_INPUT, { timeout: 15000 });
    const payload = await response.json();
    if (!response.ok() || !Array.isArray(payload.engine_options)) {
        throw new Error(`Search-engine API failed while loading ${url}: HTTP ${response.status()}`);
    }
    return new Set(payload.engine_options.filter((o) => o.egress?.allowed === false).map((o) => o.value));
}

function matchesDisabledPolicy(items, policyDenied) {
    const disabled = items.filter((i) => i.disabled);
    const values = new Set(disabled.map((i) => i.value));
    return items.length > 5 && values.size === policyDenied.size &&
        [...values].every((value) => policyDenied.has(value)) &&
        disabled.every((i) => !!i.reason && i.ariaDisabled === 'true');
}

async function main() {
    log.section('News Subscription Form: Search-Engine Dropdown Scope Awareness');
    const ctx = await setupTest({ authenticate: true });
    const results = new TestResults('News Subscription Form Dropdown Scope 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}`);
        }
    }

    let restoreScopeTo = 'adaptive';
    let seededSub = null;

    try {
        const initialLoadP = page.waitForResponse(enginesResponseMatcher(), { timeout: 20000 }).catch(() => null);
        await page.goto(`${baseUrl}/`, { waitUntil: 'domcontentloaded', timeout: 30000 });
        await page.waitForSelector('#policy_egress_scope', { timeout: 15000 });
        await initialLoadP;
        restoreScopeTo = await page.$eval('#policy_egress_scope', (el) => el.value);

        // ===============================================================
        // 1. New-subscription form: adaptive scope still guards private URLs
        // ===============================================================
        let adaptiveDeniedValues = null;
        await run('New form', 'Default/adaptive saved scope matches guarded URL decisions', async () => {
            await setSavedEgressScope(page, baseUrl, 'adaptive');
            adaptiveDeniedValues = await loadSubscriptionForm(page, `${baseUrl}/news/subscriptions/new`);
            await openSearchEngineDropdown(page);
            const items = await readDropdownItems(page);
            await closeOpenDropdown(page);
            const disabled = items.filter((i) => i.disabled);
            return {
                passed: matchesDisabledPolicy(items, adaptiveDeniedValues),
                message: `${items.length} items rendered, ${disabled.length} disabled (API guarded=${adaptiveDeniedValues.size})`,
            };
        });

        // ===============================================================
        // 2. New-subscription form: saved private_only scope -> filtered
        // ===============================================================
        let privateOnlyDisabledValues = null;
        await run('New form', 'Saved private_only scope disables non-local engines with a reason', async () => {
            await setSavedEgressScope(page, baseUrl, 'private_only');
            const policyDenied = await loadSubscriptionForm(page, `${baseUrl}/news/subscriptions/new`);
            await openSearchEngineDropdown(page);
            const items = await readDropdownItems(page);
            await closeOpenDropdown(page);

            const disabled = items.filter((i) => i.disabled);
            const disabledWithoutReason = disabled.filter((i) => !i.reason);
            const reasonsLookRight = disabled.every((i) => /blocked/i.test(i.reason || ''));
            privateOnlyDisabledValues = new Set(disabled.map((i) => i.value));

            const passed = matchesDisabledPolicy(items, policyDenied) && disabled.length > 0 &&
                disabledWithoutReason.length === 0 && reasonsLookRight;
            return {
                passed,
                message: passed
                    ? `${disabled.length}/${items.length} options disabled with a visible reason ` +
                      `(e.g. "${disabled[0].value}": "${disabled[0].reason}")`
                    : `disabled=${disabled.length} missingReason=${disabledWithoutReason.length} reasonsLookRight=${reasonsLookRight}`,
            };
        });

        // ===============================================================
        // 3. Edit-subscription form reads the same saved scope
        // ===============================================================
        await run('Edit form', 'GET /news/subscriptions/<id>/edit applies the same saved-scope filtering', async () => {
            if (!privateOnlyDisabledValues) {
                return { passed: null, skipped: true, message: 'No private_only disabled-set from the new-form test to compare against' };
            }
            seededSub = await seedSubscription(page);
            if (!seededSub) return { passed: false, message: 'Could not seed a subscription fixture' };

            // Scope is still saved as private_only from the previous test.
            await loadSubscriptionForm(page, `${baseUrl}/news/subscriptions/${seededSub.id}/edit`);
            const hasForm = await page.$(SEARCH_ENGINE_INPUT);
            if (!hasForm) return { passed: false, message: 'Edit form did not render a search-engine field (regression: see #5204 hand-port notes on default_settings gaps)' };

            await openSearchEngineDropdown(page);
            const items = await readDropdownItems(page);
            await closeOpenDropdown(page);
            const disabled = new Set(items.filter((i) => i.disabled).map((i) => i.value));

            // Same account, same saved scope, same engine catalog -> the disabled
            // set on /edit must match the disabled set the /new form produced.
            const missing = [...privateOnlyDisabledValues].filter((v) => !disabled.has(v));
            const extra = [...disabled].filter((v) => !privateOnlyDisabledValues.has(v));
            const passed = missing.length === 0 && extra.length === 0 && disabled.size > 0;
            return {
                passed,
                message: passed
                    ? `Edit form disabled set matches the new form's (${disabled.size} engines)`
                    : `missingFromEdit=${missing} extraInEdit=${extra}`,
            };
        });

        // ===============================================================
        // 4. Edit-subscription form: adaptive scope preserves the URL guard
        // ===============================================================
        await run('Edit form', 'Default/adaptive saved scope retains the new form guarded set', async () => {
            if (!seededSub) {
                return { passed: null, skipped: true, message: 'No seeded subscription available' };
            }
            await setSavedEgressScope(page, baseUrl, 'adaptive');
            const editPolicyDenied = await loadSubscriptionForm(page, `${baseUrl}/news/subscriptions/${seededSub.id}/edit`);
            await openSearchEngineDropdown(page);
            const items = await readDropdownItems(page);
            await closeOpenDropdown(page);
            const disabled = items.filter((i) => i.disabled);
            const sameAsNew = adaptiveDeniedValues && editPolicyDenied.size === adaptiveDeniedValues.size &&
                [...editPolicyDenied].every((value) => adaptiveDeniedValues.has(value));
            return {
                passed: !!sameAsNew && matchesDisabledPolicy(items, editPolicyDenied),
                message: `${items.length} items rendered, ${disabled.length} disabled (API guarded=${editPolicyDenied.size}, matches new=${!!sameAsNew})`,
            };
        });
    } catch (error) {
        log.error(`Fatal error: ${error.message}`);
        console.error(error.stack);
        results.add('Suite', 'Unexpected Suite Error', false, error.message);
    } finally {
        try {
            if (seededSub) await deleteSubscription(page, seededSub.id);
        } catch {
            /* ignore */
        }
        try {
            await setSavedEgressScope(page, baseUrl, restoreScopeTo);
        } catch {
            /* ignore */
        }

        results.print();
        results.save();
        await teardownTest(ctx);
        process.exit(results.exitCode());
    }
}

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