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.

30.7 KB
Raw Blame
#!/usr/bin/env node
/**
 * Search-Engine Dropdown: Scope + Strategy Awareness (CI-safe)
 * =============================================================
 *
 * Checks that the picker follows egress decisions and required-key presence,
 * reconciles selections after scope changes, and accepts an explicit primary
 * engine even when it is excluded from LangGraph's default tool list.
 *
 * Run: node test_search_engine_dropdown_scope_ci.js
 */

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

const ENGINES_PATH_SUFFIX = '/settings/api/available-search-engines';
const PRIMARY_SETTING_PATH = '/settings/api/search.tool';

// research.js's dropdown-refresh logic (getCurrentEgressScopeForDropdown) only
// treats these two as "filterable" scopes and appends ?egress_scope=<scope> to
// its own re-fetch. Every other scope (adaptive, strict, ...) re-fetches the
// plain, query-string-free endpoint — the same URL the unfiltered/no-params
// contract test above exercises directly.
const FILTERED_SCOPES = new Set(['private_only', 'public_only']);

function sameValueSet(left, right) {
    return !!left && !!right && left.size === right.size &&
        [...left].every((value) => right.has(value));
}

/**
 * Puppeteer response predicate for GET .../available-search-engines.
 *   - scope undefined: matches any request to the endpoint.
 *   - scope in FILTERED_SCOPES: matches ?egress_scope=<scope>.
 *   - any other scope (e.g. 'adaptive'): matches the bare endpoint with NO
 *     query string, since that's what the dropdown actually requests for it.
 */
function enginesResponseMatcher(scope) {
    return (r) => {
        if (r.request().method() !== 'GET') return false;
        let u;
        try {
            u = new URL(r.url());
        } catch {
            return false;
        }
        if (!u.pathname.endsWith(ENGINES_PATH_SUFFIX)) return false;
        if (scope === undefined) return true;
        if (FILTERED_SCOPES.has(scope)) {
            return u.searchParams.get('egress_scope') === scope;
        }
        return u.search === '';
    };
}

/** Fetch the endpoint directly from the page context (same-origin, cookies attached). */
async function fetchEngineOptions(page, query = '') {
    return page.evaluate(async (q) => {
        const r = await fetch(`/settings/api/available-search-engines${q}`, { credentials: 'same-origin' });
        if (!r.ok) return { ok: false, status: r.status };
        const data = await r.json();
        return { ok: true, status: r.status, data };
    }, query);
}

/**
 * Open the research page's search-engine custom dropdown (renders every
 * option, unfiltered by search text). Retries the click (bounded, no fixed
 * sleep) rather than assuming a single click always lands after
 * setupCustomDropdown() has attached its listener.
 */
async function openSearchEngineDropdown(page, timeout = 15000) {
    await page.waitForSelector('#search_engine', { timeout });
    const deadline = Date.now() + timeout;
    while (Date.now() < deadline) {
        await page.click('#search_engine');
        const ready = await page
            .waitForFunction(
                () => document.querySelectorAll('#search-engine-dropdown-list .ldr-custom-dropdown-item').length > 0,
                { timeout: 1000 },
            )
            .then(() => true)
            .catch(() => false);
        if (ready) return;
    }
    throw new Error(`Dropdown at #search_engine never rendered items within ${timeout}ms`);
}

/**
 * Close whatever custom dropdown is currently open via custom_dropdown.js's own
 * outside-click handler (`document.addEventListener('click', () => hideDropdown())`).
 * Targets the fixed top bar — present on every page, never covered by the
 * dropdown list (which renders below the search-engine input) — so the click
 * reliably lands outside the dropdown and bubbles to `document`.
 */
async function closeOpenDropdown(page) {
    await page.click('.ldr-top-bar');
}

async function readDropdownItems(page, listSelector = '#search-engine-dropdown-list') {
    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,
        }));
    }, listSelector);
}

/**
 * Force a fresh, unfiltered engine list fetch via the dropdown's own refresh
 * button and wait for it to fully land (network response + loading class
 * cleared). Used whenever a test needs `searchEngineOptions` to be
 * deterministically (re-)mapped against the CURRENT strategy/scope rather
 * than relying on a change-listener side effect that may not have been
 * attached yet this early in the page's own async init sequence.
 */
async function forceRefreshEngineList(page) {
    const refreshBtn = await page.$('#search_engine-refresh');
    if (!refreshBtn) throw new Error('#search_engine-refresh button not found');
    const refreshP = page.waitForResponse(enginesResponseMatcher(), { timeout: 20000 });
    await refreshBtn.click();
    await refreshP;
    await page.waitForFunction(
        () => !document.getElementById('search_engine-refresh')?.classList.contains('ldr-loading'),
        { timeout: 15000 },
    );
}

/** Set a known strategy and refresh before scope-focused assertions. */
async function setNonLangGraphBaselineAndVerify(page, strategy, attempts = 6) {
    for (let attempt = 1; attempt <= attempts; attempt++) {
        await page.select('#strategy', strategy);
        await forceRefreshEngineList(page);
        await openSearchEngineDropdown(page);
        const items = await readDropdownItems(page);
        await closeOpenDropdown(page);
        if (!items.some((i) => i.disabled && /langgraph/i.test(i.reason || ''))) {
            return;
        }
    }
    throw new Error(
        `Dropdown still showed a LangGraph-only disabled item after ${attempts} attempts to set #strategy="${strategy}"`,
    );
}

/** Verify strategy changes preserve the expected engine availability. */
async function selectStrategyLiveAndVerify(page, value, engineValue, expectDisabled, attempts = 5) {
    for (let attempt = 1; attempt <= attempts; attempt++) {
        await openSearchEngineDropdown(page);
        await page.select('#strategy', value);
        const ok = await page
            .waitForFunction(
                (val, wantDisabled) => {
                    const el = document.querySelector(`#search-engine-dropdown-list [data-value="${val}"]`);
                    if (!el) return false;
                    return el.classList.contains('ldr-custom-dropdown-item--disabled') === wantDisabled;
                },
                { timeout: 3000 },
                engineValue,
                expectDisabled,
            )
            .then(() => true)
            .catch(() => false);
        if (ok) return;
    }
    throw new Error(
        `#strategy="${value}" live re-render never settled to disabled=${expectDisabled} for "${engineValue}" after ${attempts} attempts`,
    );
}

/** Change the research page's egress-scope select and wait for the resulting engines re-fetch to land. */
async function selectScopeAndWait(page, value) {
    const respP = page.waitForResponse(enginesResponseMatcher(value), { timeout: 20000 });
    await page.select('#policy_egress_scope', value);
    await respP;
    // Let the .then() handler's synchronous DOM re-render (updateDropdownOptions /
    // reconcileSearchEngineSelection) finish before the caller reads DOM state.
    await page.waitForFunction(
        () => !document.getElementById('search_engine')?.parentNode?.classList.contains('ldr-loading'),
        { timeout: 10000 },
    );
}

async function readSavedPrimaryEngine(page) {
    return page.evaluate(async (path) => {
        const response = await fetch(path, { credentials: 'same-origin' });
        if (!response.ok) throw new Error(`Could not read primary engine (HTTP ${response.status})`);
        const setting = await response.json();
        if (!setting || !Object.prototype.hasOwnProperty.call(setting, 'value')) {
            throw new Error('Primary-engine setting response has no value');
        }
        return setting.value;
    }, PRIMARY_SETTING_PATH);
}

async function selectPrimaryEngineAndWaitForSave(page, value) {
    const item = await page.$(`#search-engine-dropdown-list .ldr-custom-dropdown-item[data-value="${value}"]`);
    if (!item) throw new Error('Primary-engine option was not found');
    const saved = page.waitForResponse((response) => {
        try {
            const request = response.request();
            return request.method() === 'PUT' &&
                new URL(response.url()).pathname === PRIMARY_SETTING_PATH &&
                JSON.parse(request.postData() || '{}').value === value;
        } catch {
            return false;
        }
    }, { timeout: 20000 }).catch(() => null);
    try {
        await item.click();
    } catch (error) {
        await saved;
        throw error;
    }
    const response = await saved;
    if (!response || !response.ok()) {
        throw new Error('Primary-engine selection was not saved');
    }
}

async function restorePrimaryAndDeleteCollection(page, originalPrimary, collection) {
    if (originalPrimary === undefined) {
        if (collection) throw new Error('Original primary engine was not captured');
        return;
    }
    if (await readSavedPrimaryEngine(page) !== originalPrimary) {
        await page.evaluate(async (path, value) => {
            const csrf = document.querySelector('meta[name="csrf-token"]')?.content;
            const response = await fetch(path, {
                method: 'PUT',
                credentials: 'same-origin',
                headers: { 'Content-Type': 'application/json', 'X-CSRFToken': csrf || '' },
                body: JSON.stringify({ value }),
            });
            if (!response.ok) throw new Error(`Could not restore primary engine (HTTP ${response.status})`);
        }, PRIMARY_SETTING_PATH, originalPrimary);
        if (await readSavedPrimaryEngine(page) !== originalPrimary) {
            throw new Error('Original primary engine was not restored');
        }
    }
    if (collection) await deleteCollection(page, collection.id);
}

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

    // Cross-test state shared by the DOM-classification test and the
    // live-reconciliation test that depends on its findings.
    let deniedUnderPrivateOnly = null; // { value, reason }
    let deniedValuesUnderPrivateOnly = null; // Set<string>
    let allowedUnderPrivateOnly = null; // Set<string>
    let deniedUnderAdaptive = null; // Set<string> blocked by URL guard or missing keys
    let seededCollection = null;
    let restoreScopeTo = 'adaptive';
    let restoreStrategyTo = null;
    let restorePrimaryTo;
    let primarySelectionSave = null;
    let cleanupStarted = false;

    try {
        // ---------------------------------------------------------------
        // Setup: land on the research page and normalize the egress scope
        // to a known baseline (adaptive) so every subsequent assertion is
        // deterministic regardless of what a prior shard test left behind.
        // ---------------------------------------------------------------
        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 page.waitForSelector('#strategy', { timeout: 15000 });
        await initialLoadP;

        restorePrimaryTo = await readSavedPrimaryEngine(page);
        restoreScopeTo = await page.$eval('#policy_egress_scope', (el) => el.value);
        if (restoreScopeTo !== 'adaptive') {
            await selectScopeAndWait(page, 'adaptive');
        }
        restoreStrategyTo = await page.$eval('#strategy', (el) => el.value);

        // Use a known strategy for the scope-focused assertions.
        const nonLangGraphStrategy = await page.$$eval('#strategy option', (els) =>
            els.map((e) => e.value).find((v) => v !== 'langgraph-agent'),
        );
        if (nonLangGraphStrategy) {
            await setNonLangGraphBaselineAndVerify(page, nonLangGraphStrategy);
        }

        // ===============================================================
        // 1. API contract
        // ===============================================================
        await run('API contract', 'No query params only marks unapproved private instance URLs', async () => {
            const r = await fetchEngineOptions(page, '');
            if (!r.ok || !Array.isArray(r.data.engine_options) || r.data.engine_options.length === 0) {
                return { passed: false, message: `Bad/empty response: ${JSON.stringify(r).slice(0, 200)}` };
            }
            const marked = r.data.engine_options.filter((o) => 'egress' in o);
            deniedUnderAdaptive = new Set(r.data.engine_options
                .filter(o => o.egress?.allowed === false || (o.requires_api_key && !o.has_api_key))
                .map(o => o.value));
            const unexpected = marked.filter((o) => o.egress?.allowed !== false || o.egress?.reason !== 'private_url_unapproved');
            return {
                passed: unexpected.length === 0,
                message: `${r.data.engine_options.length} options, ${marked.length} guarded URLs, ${unexpected.length} unexpected egress decisions`,
            };
        });

        await run('API contract', 'Blank egress_scope/primary preserves the guarded URL overlay', async () => {
            const r = await fetchEngineOptions(page, '?egress_scope=&primary=');
            if (!r.ok) return { passed: false, message: `HTTP ${r.status}` };
            const marked = r.data.engine_options.filter((o) => 'egress' in o);
            const markedValues = new Set(r.data.engine_options
                .filter(o => o.egress?.allowed === false || (o.requires_api_key && !o.has_api_key))
                .map(o => o.value));
            const unexpected = marked.filter((o) => o.egress?.allowed !== false || o.egress?.reason !== 'private_url_unapproved');
            const sameSet = sameValueSet(markedValues, deniedUnderAdaptive);
            return {
                passed: !!sameSet && unexpected.length === 0,
                message: `blank params: ${marked.length} guarded URLs, same as absent=${!!sameSet}, unexpected=${unexpected.length}`,
            };
        });

        await run('API contract', 'egress_scope=private_only stamps egress:{allowed,reason} on every option', async () => {
            const r = await fetchEngineOptions(page, '?egress_scope=private_only&primary=searxng');
            if (!r.ok) return { passed: false, message: `HTTP ${r.status}` };
            const opts = r.data.engine_options;
            const missing = opts.filter((o) => !o.egress || typeof o.egress.allowed !== 'boolean');
            const denied = opts.filter((o) => o.egress && o.egress.allowed === false);
            const allowed = opts.filter(o => o.egress?.allowed === true && !(o.requires_api_key && !o.has_api_key));
            const deniedWithoutReason = denied.filter((o) => !o.egress.reason);

            // A guarded private URL is denied even in adaptive scope. Choose
            // an option that is selectable before switching scopes below.
            const reconcilable = denied.find((o) => !deniedUnderAdaptive?.has(o.value));
            if (reconcilable) {
                deniedUnderPrivateOnly = { value: reconcilable.value, reason: reconcilable.egress.reason };
            }
            deniedValuesUnderPrivateOnly = new Set(opts
                .filter(o => o.egress?.allowed === false || (o.requires_api_key && !o.has_api_key))
                .map(o => o.value));
            allowedUnderPrivateOnly = new Set(allowed.map((o) => o.value));

            const passed = missing.length === 0 && denied.length > 0 && allowed.length > 0 && deniedWithoutReason.length === 0;
            return {
                passed,
                message: passed
                    ? `${opts.length} options stamped (${allowed.length} allowed, ${denied.length} denied); ` +
                      `e.g. denied="${denied[0].value}" reason="${denied[0].egress.reason}"`
                    : `missing-egress=${missing.length} denied=${denied.length} allowed=${allowed.length} ` +
                      `denied-without-reason=${deniedWithoutReason.length}`,
            };
        });

        // ===============================================================
        // 2. DOM: dropdown rendering matches the API classification
        // ===============================================================
        await run('Dropdown DOM', 'Adaptive scope disables guarded private URLs and missing required keys', async () => {
            // A still-in-flight refresh from setup can occasionally paint one more
            // stale frame after this test's own read. Re-verify via a forced
            // refresh (bounded, no fixed sleep) rather than assume one read is
            // final — this is about test synchronization, not the assertion.
            let items = [];
            for (let attempt = 1; attempt <= 3; attempt++) {
                await openSearchEngineDropdown(page);
                items = await readDropdownItems(page);
                await closeOpenDropdown(page);
                const disabledNow = new Set(items.filter((i) => i.disabled).map((i) => i.value));
                if (sameValueSet(disabledNow, deniedUnderAdaptive)) break;
                await forceRefreshEngineList(page);
            }
            const disabled = items.filter((i) => i.disabled);
            const disabledValues = new Set(disabled.map((i) => i.value));
            const sameSet = sameValueSet(disabledValues, deniedUnderAdaptive);
            return {
                passed: items.length > 5 && !!sameSet && disabled.every((i) => i.reason && i.ariaDisabled === 'true'),
                message:
                    `${items.length} items rendered, ${disabled.length} disabled (API guarded=${deniedUnderAdaptive?.size ?? 'unknown'})` +
                    (disabled.length > 0 ? ` [${disabled.map((i) => `${i.value}: ${i.reason}`).join('; ')}]` : ''),
            };
        });

        await run('Dropdown DOM', 'Private-only scope disables exactly the API-denied engines, with a reason', async () => {
            if (!deniedValuesUnderPrivateOnly || !allowedUnderPrivateOnly) {
                return { passed: false, message: 'API classification test did not produce a denied/allowed set' };
            }
            await selectScopeAndWait(page, 'private_only');
            await openSearchEngineDropdown(page);
            const items = await readDropdownItems(page);
            await closeOpenDropdown(page);

            const domDisabledValues = new Set(items.filter((i) => i.disabled).map((i) => i.value));
            const wronglyEnabled = [...deniedValuesUnderPrivateOnly].filter((value) => !domDisabledValues.has(value));
            const wronglyDisabled = [...domDisabledValues].filter((value) => !deniedValuesUnderPrivateOnly.has(value));
            const disabledWithoutReasonText = items.filter((i) => i.disabled && !i.reason);
            const disabledWithoutAria = items.filter((i) => i.disabled && i.ariaDisabled !== 'true');

            const passed =
                wronglyEnabled.length === 0 &&
                wronglyDisabled.length === 0 &&
                disabledWithoutReasonText.length === 0 &&
                disabledWithoutAria.length === 0 &&
                domDisabledValues.size > 0;

            return {
                passed,
                message: passed
                    ? `${domDisabledValues.size} items disabled in the DOM, all matching the API's denied set ` +
                      `(e.g. "${deniedUnderPrivateOnly?.value}": "${deniedUnderPrivateOnly?.reason}")`
                    : `wronglyEnabled=${wronglyEnabled} wronglyDisabled=${wronglyDisabled} ` +
                      `missingReasonText=${disabledWithoutReasonText.length} missingAria=${disabledWithoutAria.length}`,
            };
        });

        // ===============================================================
        // 3. Live reconciliation: scope change invalidates the selection
        // ===============================================================
        await run('Live reconciliation', 'Selecting a to-be-denied primary then switching scope reconciles the selection', async () => {
            if (!deniedUnderPrivateOnly) {
                return { passed: false, message: 'No selectable adaptive engine that becomes denied under private_only was discovered' };
            }
            // Back to adaptive so this scope-specific denied engine is selectable.
            await selectScopeAndWait(page, 'adaptive');
            await openSearchEngineDropdown(page);
            const itemHandle = await page.$(`#search-engine-dropdown-list [data-value="${deniedUnderPrivateOnly.value}"]`);
            if (!itemHandle) {
                await closeOpenDropdown(page);
                return { passed: false, message: `Candidate engine "${deniedUnderPrivateOnly.value}" not present in the adaptive-scope dropdown` };
            }
            await itemHandle.click();
            await page.waitForFunction(
                (val) => document.getElementById('search_engine_hidden')?.value === val,
                // 15000, matching every other wait in this file. This was the
                // lone 5000 and it was the lone flake (~1 run in 5 locally,
                // "Waiting failed: 5000ms exceeded"). Not papering over a
                // product bug: as the comment directly below explains, this
                // click legitimately races a background re-fetch, so the
                // settle is genuinely async — and CI is slower than local.
                { timeout: 15000 },
                deniedUnderPrivateOnly.value,
            );
            // Selecting an engine also fires its own background re-fetch
            // (applyEgressScopeToEngines, via the hidden input's 'change'
            // listener). Drain it before triggering the scope change below —
            // otherwise the scope-change response wait below could resolve
            // against this earlier, same-shaped, in-flight request instead.
            await page.waitForFunction(
                () => !document.getElementById('search_engine')?.parentNode?.classList.contains('ldr-loading'),
                { timeout: 10000 },
            );

            // Switch scope -> private_only. The engines re-fetch is keyed on the now-selected
            // primary, so wait for THAT specific request rather than any available-search-engines call.
            const respP = page.waitForResponse(
                (r) => {
                    if (r.request().method() !== 'GET') return false;
                    let u;
                    try {
                        u = new URL(r.url());
                    } catch {
                        return false;
                    }
                    return (
                        u.pathname.endsWith(ENGINES_PATH_SUFFIX) &&
                        u.searchParams.get('egress_scope') === 'private_only' &&
                        u.searchParams.get('primary') === deniedUnderPrivateOnly.value
                    );
                },
                { timeout: 20000 },
            );
            await page.select('#policy_egress_scope', 'private_only');
            await respP;

            // The reconciler runs synchronously inside the same .then() as the DOM update;
            // wait for the hidden input to actually change away from the denied value.
            await page
                .waitForFunction(
                    (val) => document.getElementById('search_engine_hidden')?.value !== val,
                    { timeout: 10000 },
                    deniedUnderPrivateOnly.value,
                )
                .catch(() => {});

            const reconciledValue = await page.$eval('#search_engine_hidden', (el) => el.value);
            const changed = reconciledValue !== deniedUnderPrivateOnly.value;
            const reconciledIsAllowed = !!reconciledValue && (allowedUnderPrivateOnly?.has(reconciledValue) ?? false);

            return {
                passed: changed && reconciledIsAllowed,
                message: changed
                    ? `Selection reconciled: "${deniedUnderPrivateOnly.value}" -> "${reconciledValue}" ` +
                      `(allowed-under-private_only=${reconciledIsAllowed})`
                    : `Selection was NOT reconciled — still "${reconciledValue}" after switching to a scope that denies it`,
            };
        });

        // ===============================================================
        // 4. agent_enabled + LangGraph strategy
        // ===============================================================
        await run('Explicit selection', 'agent-hidden collection stays selectable under LangGraph and other strategies', async () => {
            await selectScopeAndWait(page, 'adaptive');
            seededCollection = await seedCollection(page, { agentEnabled: false });
            if (!seededCollection) {
                return { passed: false, message: 'Could not seed an agent_enabled:false collection fixture' };
            }
            const engineValue = `collection_${seededCollection.id}`;

            // Force a fresh, unfiltered engine list so the newly created collection appears
            // (the in-memory cache would otherwise still hold the pre-creation list).
            await forceRefreshEngineList(page);

            // Baseline: a non-LangGraph strategy must leave the option enabled.
            const nonLangGraphOption = await page.$$eval('#strategy option', (els) =>
                els.map((e) => e.value).find((v) => v !== 'langgraph-agent'),
            );
            if (!nonLangGraphOption) return { passed: false, message: 'No non-LangGraph strategy option found' };
            await setNonLangGraphBaselineAndVerify(page, nonLangGraphOption);

            await openSearchEngineDropdown(page);
            await page.waitForFunction(
                (val) => !!document.querySelector(`#search-engine-dropdown-list [data-value="${val}"]`),
                { timeout: 10000 },
                engineValue,
            );
            const beforeItems = await readDropdownItems(page);
            const beforeItem = beforeItems.find((i) => i.value === engineValue);
            if (!beforeItem) {
                await closeOpenDropdown(page);
                return { passed: false, message: `Seeded collection engine "${engineValue}" not found in dropdown after refresh` };
            }
            const enabledUnderNonLangGraph = !beforeItem.disabled;

            await selectStrategyLiveAndVerify(page, 'langgraph-agent', engineValue, false);
            const afterItems = await readDropdownItems(page);
            const afterItem = afterItems.find((i) => i.value === engineValue);
            const enabledUnderLangGraph = !!afterItem && !afterItem.disabled;
            if (cleanupStarted) throw new Error('Selection check ended before primary selection');
            primarySelectionSave = selectPrimaryEngineAndWaitForSave(page, engineValue);
            await primarySelectionSave;
            await page.waitForFunction(
                value => document.getElementById('search_engine_hidden')?.value === value,
                { timeout: 10000 }, engineValue,
            );

            // Switching strategy must preserve the explicitly selected engine.
            await selectStrategyLiveAndVerify(page, nonLangGraphOption, engineValue, false);
            await closeOpenDropdown(page);

            const selected = await page.$eval('#search_engine_hidden', el => el.value);
            const passed = enabledUnderNonLangGraph && enabledUnderLangGraph && selected === engineValue;
            return {
                passed,
                message: `enabledUnderNonLangGraph=${enabledUnderNonLangGraph} ` +
                    `enabledUnderLangGraph=${enabledUnderLangGraph} selectionPreserved=${selected === engineValue}`,
            };
        });
    } catch (error) {
        log.error(`Fatal error: ${error.message}`);
        console.error(error.stack);
        results.add('Suite', 'Unexpected Suite Error', false, error.message);
    } finally {
        // Restore the persisted primary before deleting a selected fixture.
        // A failed restore must leave the collection intact and fail the suite.
        cleanupStarted = true;
        if (primarySelectionSave) await primarySelectionSave.catch(() => {});
        try {
            await restorePrimaryAndDeleteCollection(page, restorePrimaryTo, seededCollection);
        } catch (error) {
            results.add('Cleanup', 'Restore original primary engine', false, error.message);
        }
        try {
            const currentScope = await page.$eval('#policy_egress_scope', (el) => el.value).catch(() => null);
            if (currentScope && currentScope !== restoreScopeTo) {
                await selectScopeAndWait(page, restoreScopeTo);
            }
        } catch {
            /* ignore */
        }
        try {
            const currentStrategy = await page.$eval('#strategy', (el) => el.value).catch(() => null);
            if (restoreStrategyTo && currentStrategy && currentStrategy !== restoreStrategyTo) {
                await page.select('#strategy', restoreStrategyTo);
            }
        } catch {
            /* ignore */
        }

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

module.exports = { readSavedPrimaryEngine, selectPrimaryEngineAndWaitForSave, restorePrimaryAndDeleteCollection };

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