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.

41.8 KB
Raw Blame
/**
 * Live-template contracts for benchmark endpoints that are not shared with
 * static page modules: cancellation and evaluation settings/model discovery.
 */

import { readFileSync } from 'node:fs';
import { resolve } from 'node:path';

const TEMPLATE_PATH = resolve(
    __dirname,
    '../../../src/local_deep_research/web/templates/pages/benchmark.html',
);

function templateSource() {
    return readFileSync(TEMPLATE_PATH, 'utf8');
}

function extractFunction(source, name) {
    const signature = new RegExp(`(?:async\\s+)?function\\s+${name}\\s*\\(`);
    const match = signature.exec(source);
    if (!match) throw new Error(`Function ${name} not found in template`);

    const openBrace = source.indexOf('{', match.index + match[0].length);
    let depth = 0;
    for (let index = openBrace; index < source.length; index += 1) {
        if (source[index] === '{') depth += 1;
        if (source[index] === '}') {
            depth -= 1;
            if (depth === 0) return source.slice(match.index, index + 1);
        }
    }
    throw new Error(`Function ${name} has an unterminated body`);
}

function extractEvaluationModelLoader(source) {
    const start = source.indexOf('const loadEvaluationModelsFromAPI =');
    const end = source.indexOf('\n\nfunction filterModelsForProvider', start);
    if (start === -1 || end === -1) {
        throw new Error('Evaluation model loader not found in template');
    }
    return source.slice(start, end);
}

function compileCancellation(dependencies) {
    const source = extractFunction(templateSource(), 'cancelBenchmark');
    const factory = new Function( // eslint-disable-line no-new-func
        'csrfToken',
        'showAlert',
        'resetForm',
        'clearInterval',
        `
            let currentBenchmarkId = 3299;
            let progressInterval = 77;
            ${source}
            return {
                cancelBenchmark,
                getProgressInterval: () => progressInterval,
            };
        `,
    );
    return factory(
        'csrf-benchmark',
        dependencies.showAlert,
        dependencies.resetForm,
        dependencies.clearInterval,
    );
}

function compileSaveEvaluationSetting(csrfToken) {
    const source = extractFunction(templateSource(), 'saveEvaluationSetting');
    const factory = new Function( // eslint-disable-line no-new-func
        'csrfToken',
        `${source}\nreturn saveEvaluationSetting;`,
    );
    return factory(csrfToken);
}

// startBenchmark's second gate reads saveEvaluationSetting.pending and
// .failedKeys. Compiling the function with `saveEvaluationSetting` left
// undeclared makes `typeof saveEvaluationSetting === 'function'` false, so that
// whole clause is unreachable and a full revert of it would be caught by
// nothing. Binding it as a parameter makes it testable; callers that pass
// nothing get the previous behaviour exactly, since an unsupplied parameter is
// `undefined` and `typeof undefined` is still not 'function'.
function compileStartBenchmark(showAlert, saveEvaluationSetting) {
    const source = extractFunction(templateSource(), 'startBenchmark');
    const factory = new Function( // eslint-disable-line no-new-func
        'showAlert',
        'saveEvaluationSetting',
        `let benchmarkStartInFlight = false; ${source}; return startBenchmark;`,
    );
    return factory(showAlert, saveEvaluationSetting);
}

function compileEvaluationModelsHarness(initialProviders = []) {
    const source = templateSource();
    const functions = [
        'debounce',
        'applyEvaluationProvider',
        'isOpenAIEndpointProvider',
        'populateEvaluationProviders',
        'setupEvaluationModelDropdown',
        'getEvaluationModelOptions',
        'refreshEvaluationModels',
        'saveEvaluationSetting',
    ].map(name => extractFunction(source, name)).join('\n');
    const loader = extractEvaluationModelLoader(source);
    // The extracted source is repository-owned production code from the
    // template above, not user-controlled input.
    const factory = new Function( // eslint-disable-line no-new-func
        `
            const csrfToken = 'csrf-benchmark';
            let EVAL_MODEL_PROVIDERS = ${JSON.stringify(initialProviders)};
            let evaluationModelsRequestVersion = 0;
            let evaluationProviderSelect = document.getElementById('evaluation_provider');
            let evaluationModelInput = document.getElementById('evaluation_model');
            let evaluationEndpointInput = document.getElementById('evaluation_endpoint_url');
            ${functions}
            ${loader}
            return {
                loadEvaluationModelsFromAPI,
                populateEvaluationProviders,
                setupEvaluationModelDropdown,
                getEvaluationModelOptions,
                getProviders: () => EVAL_MODEL_PROVIDERS,
            };
        `,
    );
    return factory();
}

// Option values as /settings/api/available-models really emits them: the
// provider key from auto_discovery.ProviderInfo, which is the class-level
// `provider_key` ("OPENAI", "OLLAMA", ...) upper-cased. The per-provider
// model map is keyed separately and IS lower-case (`openai_models`), which is
// why the template lower-cases the selected value before looking models up.
const PROVIDER_OPTIONS = [
    { value: 'OPENAI', label: 'OpenAI' },
    { value: 'OLLAMA', label: 'Ollama' },
];

// getEvaluationModelOptions' "while loading" fallback for OpenAI. The same list
// has to come back for both spellings of the provider, which is why the two
// tests below assert against this one constant.
const OPENAI_DEFAULT_MODELS = [
    { value: 'gpt-4o', label: 'GPT-4o' },
    { value: 'gpt-4', label: 'GPT-4' },
    { value: 'gpt-3.5-turbo', label: 'GPT-3.5 Turbo' },
];

function modelsPayload(modelValue, modelLabel = modelValue) {
    return {
        provider_options: PROVIDER_OPTIONS.map(provider => ({ ...provider })),
        benchmark_provider_options: PROVIDER_OPTIONS.map(provider => ({ ...provider })),
        providers: {
            openai_models: [{ value: modelValue, label: modelLabel }],
            ollama_models: [{ value: 'nomic-embed-text', label: 'Nomic' }],
        },
    };
}

function deferred() {
    let resolveDeferred;
    const promise = new Promise((resolvePromise) => {
        resolveDeferred = resolvePromise;
    });
    return { promise, resolve: resolveDeferred };
}

// `data-initial-value` is the stored benchmark.evaluation.provider echoed
// verbatim. Defaults may be lower-case while live options use upper-case;
// provider reconciliation handles both forms.
function renderEvaluationControls(initialProvider = 'OPENAI') {
    document.body.innerHTML = `
        <select id="evaluation_provider"></select>
        <div><input id="evaluation_endpoint_url"></div>
        <div data-target="evaluation-model-dropdown">
            <input id="evaluation_model">
            <input id="evaluation_model_hidden">
            <div id="evaluation-model-dropdown-list"></div>
            <button class="refresh-btn"><i></i></button>
        </div>
    `;
    document
        .getElementById('evaluation_provider')
        .setAttribute('data-initial-value', initialProvider);
    window.setupCustomDropdown = vi.fn(() => ({ setValue: vi.fn() }));
    window.updateDropdownOptions = vi.fn();
    window.evaluationModels = {};
    window.modelsLoading = false;
}

async function flushPromises() {
    for (let index = 0; index < 6; index += 1) {
        await Promise.resolve();
    }
}

afterEach(() => {
    vi.useRealTimers();
    vi.restoreAllMocks();
    vi.unstubAllGlobals();
    delete window.setupCustomDropdown;
    delete window.updateDropdownOptions;
    delete window.evaluationModels;
    delete window.evaluationDropdownInstance;
    delete window.modelsLoading;
    document.body.replaceChildren();
});

it('cancels the active benchmark with CSRF and resets only after success', async () => {
    const fetchMock = vi.fn().mockResolvedValue({
        json: vi.fn().mockResolvedValue({ success: true }),
    });
    vi.stubGlobal('fetch', fetchMock);
    const dependencies = {
        showAlert: vi.fn(),
        resetForm: vi.fn(),
        clearInterval: vi.fn(),
    };
    const harness = compileCancellation(dependencies);

    harness.cancelBenchmark();

    await vi.waitFor(() => {
        expect(dependencies.resetForm).toHaveBeenCalledOnce();
    });
    expect(fetchMock).toHaveBeenCalledWith('/benchmark/api/cancel/3299', {
        method: 'POST',
        headers: { 'X-CSRFToken': 'csrf-benchmark' },
    });
    expect(dependencies.showAlert).toHaveBeenCalledWith(
        'Benchmark cancelled successfully.',
        'info',
    );
    expect(dependencies.clearInterval).toHaveBeenCalledWith(77);
    expect(harness.getProgressInterval()).toBeNull();
});

it('keeps the active benchmark intact when cancellation is rejected', async () => {
    vi.stubGlobal('fetch', vi.fn().mockResolvedValue({
        json: vi.fn().mockResolvedValue({
            success: false,
            error: 'worker still finalizing',
        }),
    }));
    const dependencies = {
        showAlert: vi.fn(),
        resetForm: vi.fn(),
        clearInterval: vi.fn(),
    };
    const harness = compileCancellation(dependencies);

    harness.cancelBenchmark();

    await vi.waitFor(() => {
        expect(dependencies.showAlert).toHaveBeenCalledWith(
            'Error cancelling benchmark: worker still finalizing',
            'error',
        );
    });
    expect(dependencies.resetForm).not.toHaveBeenCalled();
    expect(dependencies.clearInterval).not.toHaveBeenCalled();
    expect(harness.getProgressInterval()).toBe(77);
});

it('saves an evaluation setting with the FastAPI value envelope and CSRF', async () => {
    const fetchMock = vi.fn().mockResolvedValue({
        ok: true,
        json: vi.fn().mockResolvedValue({ message: 'Setting benchmark.evaluation.temperature updated successfully' }),
    });
    vi.stubGlobal('fetch', fetchMock);
    vi.spyOn(console, 'log').mockImplementation(() => {});

    compileSaveEvaluationSetting('csrf-evaluation')(
        'benchmark.evaluation.temperature',
        0.35,
    );

    await vi.waitFor(() => {
        expect(fetchMock).toHaveBeenCalledWith(
            '/settings/api/benchmark.evaluation.temperature',
            {
                method: 'PUT',
                headers: {
                    'Content-Type': 'application/json',
                    'X-CSRFToken': 'csrf-evaluation',
                },
                body: JSON.stringify({ value: 0.35 }),
            },
        );
    });
});

it('loads and force-refreshes evaluation providers and model options', async () => {
    renderEvaluationControls();
    vi.useFakeTimers();
    const fetchMock = vi.fn()
        .mockResolvedValueOnce({
            json: vi.fn().mockResolvedValue(modelsPayload('initial-model')),
        })
        .mockResolvedValueOnce({
            json: vi.fn().mockResolvedValue(modelsPayload('fresh-model', 'Fresh Model')),
        });
    vi.stubGlobal('fetch', fetchMock);
    vi.spyOn(console, 'log').mockImplementation(() => {});
    const harness = compileEvaluationModelsHarness(PROVIDER_OPTIONS);
    harness.populateEvaluationProviders();
    harness.setupEvaluationModelDropdown();

    harness.loadEvaluationModelsFromAPI();
    await vi.advanceTimersByTimeAsync(500);

    expect(fetchMock.mock.calls[0]).toEqual([
        '/settings/api/available-models',
        { method: 'POST', headers: { 'X-CSRFToken': 'csrf-benchmark' } },
    ]);
    expect(window.evaluationModels.openai).toEqual([
        { value: 'initial-model', label: 'initial-model' },
    ]);

    document.querySelector('.refresh-btn').click();
    await flushPromises();

    expect(fetchMock.mock.calls[1]).toEqual([
        '/settings/api/available-models?force_refresh=true',
        { method: 'POST', headers: { 'X-CSRFToken': 'csrf-benchmark' } },
    ]);
    expect(harness.getProviders()).toEqual([
        { value: 'OPENAI', label: 'OpenAI' },
        { value: 'OLLAMA', label: 'Ollama' },
    ]);
    expect(window.evaluationModels.openai).toEqual([
        { value: 'fresh-model', label: 'Fresh Model' },
    ]);
    expect(window.updateDropdownOptions).toHaveBeenLastCalledWith(
        document.getElementById('evaluation_model'),
        [{ value: 'fresh-model', label: 'Fresh Model' }],
    );
    expect(document.querySelector('.refresh-btn i').classList.contains('fa-spin'))
        .toBe(false);
});

it('starts the initial model request when an empty dropdown asks for options', async () => {
    // Before the API answers, populateEvaluationProviders() falls back to the
    // template's own hard-coded provider list, which is lower-case; this test
    // deliberately stays on that path.
    renderEvaluationControls('openai');
    vi.useFakeTimers();
    const fetchMock = vi.fn().mockResolvedValue({
        json: vi.fn().mockResolvedValue(modelsPayload('initial-model')),
    });
    vi.stubGlobal('fetch', fetchMock);
    vi.spyOn(console, 'log').mockImplementation(() => {});
    const harness = compileEvaluationModelsHarness();
    harness.populateEvaluationProviders();

    expect(harness.getEvaluationModelOptions()).toEqual(OPENAI_DEFAULT_MODELS);
    expect(fetchMock).not.toHaveBeenCalled();

    await vi.advanceTimersByTimeAsync(500);
    await flushPromises();

    expect(fetchMock).toHaveBeenCalledOnce();
    expect(fetchMock).toHaveBeenCalledWith(
        '/settings/api/available-models',
        { method: 'POST', headers: { 'X-CSRFToken': 'csrf-benchmark' } },
    );
    expect(window.evaluationModels.openai).toEqual([
        { value: 'initial-model', label: 'initial-model' },
    ]);
});

it('does not schedule a fallback request while force refresh is in flight', async () => {
    renderEvaluationControls();
    vi.useFakeTimers();
    const refreshResponse = deferred();
    const fetchMock = vi.fn().mockReturnValue(refreshResponse.promise);
    vi.stubGlobal('fetch', fetchMock);
    vi.spyOn(console, 'log').mockImplementation(() => {});
    const harness = compileEvaluationModelsHarness(PROVIDER_OPTIONS);
    harness.populateEvaluationProviders();
    harness.setupEvaluationModelDropdown();

    document.querySelector('.refresh-btn').click();
    // populateEvaluationProviders() selected the provider under the API's
    // spelling, so this call goes through the same "while loading" fallback as
    // the lower-case test above but with an upper-cased value. It has to return
    // the same list: matching the raw select value instead of the normalised
    // key drops it to the empty list here, even though the cached lookup
    // higher up in the function already lower-cases.
    expect(harness.getEvaluationModelOptions()).toEqual(OPENAI_DEFAULT_MODELS);
    await vi.advanceTimersByTimeAsync(500);

    expect(fetchMock).toHaveBeenCalledOnce();
    expect(fetchMock).toHaveBeenCalledWith(
        '/settings/api/available-models?force_refresh=true',
        { method: 'POST', headers: { 'X-CSRFToken': 'csrf-benchmark' } },
    );

    refreshResponse.resolve({
        json: vi.fn().mockResolvedValue(modelsPayload('fresh-model')),
    });
    await flushPromises();
});

it('keeps the selected provider while force refresh rebuilds its options', async () => {
    renderEvaluationControls();
    vi.stubGlobal('fetch', vi.fn().mockResolvedValue({
        json: vi.fn().mockResolvedValue(modelsPayload('fresh-openai')),
    }));
    vi.spyOn(console, 'log').mockImplementation(() => {});
    const harness = compileEvaluationModelsHarness(PROVIDER_OPTIONS);
    harness.populateEvaluationProviders();
    harness.setupEvaluationModelDropdown();
    const provider = document.getElementById('evaluation_provider');
    provider.value = 'OLLAMA';

    document.querySelector('.refresh-btn').click();
    await flushPromises();

    expect(provider.value).toBe('OLLAMA');
    expect(window.updateDropdownOptions).toHaveBeenLastCalledWith(
        document.getElementById('evaluation_model'),
        [{ value: 'nomic-embed-text', label: 'Nomic' }],
    );
});

it('does not let a deferred initial model response overwrite a force refresh', async () => {
    renderEvaluationControls();
    vi.useFakeTimers();
    const initialResponse = deferred();
    const refreshResponse = deferred();
    const fetchMock = vi.fn((url, options) => {
        expect(options).toEqual({
            method: 'POST', headers: { 'X-CSRFToken': 'csrf-benchmark' },
        });
        if (url === '/settings/api/available-models?force_refresh=true') {
            return refreshResponse.promise;
        }
        expect(url).toBe('/settings/api/available-models');
        return initialResponse.promise;
    });
    vi.stubGlobal('fetch', fetchMock);
    vi.spyOn(console, 'log').mockImplementation(() => {});
    const harness = compileEvaluationModelsHarness(PROVIDER_OPTIONS);
    harness.populateEvaluationProviders();
    harness.setupEvaluationModelDropdown();

    harness.loadEvaluationModelsFromAPI();
    await vi.advanceTimersByTimeAsync(500);
    document.querySelector('.refresh-btn').click();
    expect(fetchMock).toHaveBeenCalledTimes(2);

    refreshResponse.resolve({
        json: vi.fn().mockResolvedValue(modelsPayload('fresh-model')),
    });
    await flushPromises();
    expect(window.evaluationModels.openai[0].value).toBe('fresh-model');

    initialResponse.resolve({
        json: vi.fn().mockResolvedValue(modelsPayload('stale-model')),
    });
    await flushPromises();

    expect(window.evaluationModels.openai[0].value).toBe('fresh-model');
    expect(window.updateDropdownOptions).toHaveBeenLastCalledWith(
        document.getElementById('evaluation_model'),
        [{ value: 'fresh-model', label: 'fresh-model' }],
    );
});

it('keeps the newest result when force-refresh responses finish out of order', async () => {
    renderEvaluationControls();
    const firstRefresh = deferred();
    const secondRefresh = deferred();
    const fetchMock = vi.fn()
        .mockReturnValueOnce(firstRefresh.promise)
        .mockReturnValueOnce(secondRefresh.promise);
    vi.stubGlobal('fetch', fetchMock);
    vi.spyOn(console, 'log').mockImplementation(() => {});
    const harness = compileEvaluationModelsHarness(PROVIDER_OPTIONS);
    harness.populateEvaluationProviders();
    harness.setupEvaluationModelDropdown();

    const refreshButton = document.querySelector('.refresh-btn');
    refreshButton.click();
    refreshButton.click();
    expect(fetchMock).toHaveBeenCalledTimes(2);

    secondRefresh.resolve({
        json: vi.fn().mockResolvedValue(modelsPayload('newest-model')),
    });
    await flushPromises();
    expect(window.evaluationModels.openai[0].value).toBe('newest-model');
    expect(refreshButton.querySelector('i').classList.contains('fa-spin'))
        .toBe(false);

    firstRefresh.resolve({
        json: vi.fn().mockResolvedValue(modelsPayload('older-model')),
    });
    await flushPromises();

    expect(window.evaluationModels.openai[0].value).toBe('newest-model');
    expect(window.updateDropdownOptions).toHaveBeenLastCalledWith(
        document.getElementById('evaluation_model'),
        [{ value: 'newest-model', label: 'newest-model' }],
    );
});

it('cancels the pending debounced load when refresh is requested immediately', async () => {
    renderEvaluationControls();
    vi.useFakeTimers();
    const fetchMock = vi.fn().mockResolvedValue({
        json: vi.fn().mockResolvedValue(modelsPayload('manual-model')),
    });
    vi.stubGlobal('fetch', fetchMock);
    vi.spyOn(console, 'log').mockImplementation(() => {});
    const harness = compileEvaluationModelsHarness(PROVIDER_OPTIONS);
    harness.populateEvaluationProviders();
    harness.setupEvaluationModelDropdown();

    harness.loadEvaluationModelsFromAPI();
    document.querySelector('.refresh-btn').click();
    await flushPromises();
    await vi.advanceTimersByTimeAsync(500);

    expect(fetchMock).toHaveBeenCalledOnce();
    expect(fetchMock.mock.calls[0]).toEqual([
        '/settings/api/available-models?force_refresh=true',
        { method: 'POST', headers: { 'X-CSRFToken': 'csrf-benchmark' } },
    ]);
    expect(window.evaluationModels.openai[0].value).toBe('manual-model');
});


it('clears a provider that becomes blocked during model refresh', async () => {
    renderEvaluationControls();
    const payload = modelsPayload('fresh-openai');
    payload.benchmark_provider_options = payload.benchmark_provider_options.map(provider => ({
        ...provider,
        disabled: provider.value === 'OLLAMA',
        disabled_reason: provider.value === 'OLLAMA' ? 'Endpoint is not local' : null,
    }));
    vi.stubGlobal('fetch', vi.fn().mockResolvedValue({
        json: vi.fn().mockResolvedValue(payload),
    }));
    vi.spyOn(console, 'log').mockImplementation(() => {});
    const harness = compileEvaluationModelsHarness(PROVIDER_OPTIONS);
    harness.populateEvaluationProviders();
    harness.setupEvaluationModelDropdown();
    const provider = document.getElementById('evaluation_provider');
    provider.value = 'OLLAMA';
    // The guard has to be what clears it: assert it was actually selectable
    // before the refresh, or a silent no-match would pass this test.
    expect(provider.value).toBe('OLLAMA');

    document.querySelector('.refresh-btn').click();
    await flushPromises();

    expect(provider.value).toBe('');
    const blocked = Array.from(provider.options).find(option => option.value === 'OLLAMA');
    expect(blocked.disabled).toBe(true);
    expect(blocked.textContent).toContain('Endpoint is not local');
});

// Option values once an OpenAI-compatible endpoint is configured, as
// /settings/api/available-models emits them: upper-case, while the stored
// benchmark.evaluation.provider that seeds `data-initial-value` is
// lower-case. The endpoint URL input is the only control that can repair a
// blocked endpoint, so it has to follow the remembered provider under either
// spelling.
const ENDPOINT_PROVIDER_OPTIONS = [
    { value: 'OPENAI_ENDPOINT', label: 'OpenAI-Compatible Endpoint' },
    { value: 'OLLAMA', label: 'Ollama' },
];

function endpointProviderPayload(blockEndpoint = false) {
    const block = provider => blockEndpoint && provider.value === 'OPENAI_ENDPOINT';
    return {
        providers: {},
        provider_options: ENDPOINT_PROVIDER_OPTIONS.map(provider => ({ ...provider })),
        benchmark_provider_options: ENDPOINT_PROVIDER_OPTIONS.map(provider => ({
            ...provider,
            disabled: block(provider),
            disabled_reason: block(provider) ? 'Endpoint is not local' : null,
        })),
    };
}

function endpointRowDisplay() {
    return document.getElementById('evaluation_endpoint_url').parentNode.style.display;
}

it('keeps the endpoint URL field visible when a refresh returns the saved provider upper-cased', async () => {
    renderEvaluationControls('openai_endpoint');
    vi.stubGlobal('fetch', vi.fn().mockImplementation(async () => ({
        json: async () => endpointProviderPayload(),
    })));
    vi.spyOn(console, 'log').mockImplementation(() => {});
    const harness = compileEvaluationModelsHarness(ENDPOINT_PROVIDER_OPTIONS);
    harness.populateEvaluationProviders();
    harness.setupEvaluationModelDropdown();
    expect(endpointRowDisplay()).toBe('block');

    document.querySelector('.refresh-btn').click();
    await flushPromises();

    // The refresh re-selects the same provider under the API's spelling, so
    // the field holding that provider's endpoint URL must survive it.
    const select = document.getElementById('evaluation_provider');
    expect(select.value).toBe('OPENAI_ENDPOINT');
    expect(endpointRowDisplay()).toBe('block');
});

it('keeps the endpoint URL field visible when the saved endpoint provider is blocked by policy', async () => {
    renderEvaluationControls('openai_endpoint');
    vi.stubGlobal('fetch', vi.fn().mockImplementation(async () => ({
        json: async () => endpointProviderPayload(true),
    })));
    vi.spyOn(console, 'log').mockImplementation(() => {});
    const harness = compileEvaluationModelsHarness(ENDPOINT_PROVIDER_OPTIONS);
    harness.populateEvaluationProviders();
    harness.setupEvaluationModelDropdown();
    const select = document.getElementById('evaluation_provider');

    document.querySelector('.refresh-btn').click();
    await flushPromises();

    // The selection is cleared because the endpoint is refused, which leaves
    // the endpoint URL field as the only control that can correct it.
    expect(select.value).toBe('');
    expect(select.dataset.initialValue).toBe('OPENAI_ENDPOINT');
    expect(endpointRowDisplay()).toBe('block');

    // A further refresh reaches the fallback with the remembered provider
    // already stored upper-cased, the shape the raw comparison missed.
    document.querySelector('.refresh-btn').click();
    await flushPromises();

    expect(select.value).toBe('');
    expect(endpointRowDisplay()).toBe('block');
});

it('hides the endpoint URL field for a saved provider that does not use one', async () => {
    renderEvaluationControls('ollama');
    vi.stubGlobal('fetch', vi.fn().mockImplementation(async () => ({
        json: async () => endpointProviderPayload(),
    })));
    vi.spyOn(console, 'log').mockImplementation(() => {});
    const harness = compileEvaluationModelsHarness(ENDPOINT_PROVIDER_OPTIONS);
    harness.populateEvaluationProviders();
    harness.setupEvaluationModelDropdown();

    document.querySelector('.refresh-btn').click();
    await flushPromises();

    expect(document.getElementById('evaluation_provider').value).toBe('OLLAMA');
    expect(endpointRowDisplay()).toBe('none');
});


it('uses evaluation availability even when the research endpoint is blocked', async () => {
    renderEvaluationControls('openai');
    const payload = modelsPayload('evaluation-model');
    payload.provider_options[0].disabled = true;
    vi.stubGlobal('fetch', vi.fn().mockResolvedValue({ json: async () => payload }));
    const harness = compileEvaluationModelsHarness();
    harness.populateEvaluationProviders();
    harness.setupEvaluationModelDropdown();
    document.querySelector('.refresh-btn').click();
    await flushPromises();
    const select = document.getElementById('evaluation_provider');
    expect(select.value).toBe('OPENAI');
    expect(select.selectedOptions[0].disabled).toBe(false);
});

it('takes initial-load evaluation availability from benchmark_provider_options, not provider_options', async () => {
    // The refresh-button site is covered above ('uses evaluation availability
    // even when the research endpoint is blocked'); this is the same divergence
    // through the initial-load handler, which runs on every page load. Reverting
    // that handler to `EVAL_MODEL_PROVIDERS = data.provider_options` swaps the
    // two disabled sets below, so OPENAI would be cleared and OLLAMA offered.
    renderEvaluationControls('openai');
    vi.useFakeTimers();
    const payload = modelsPayload('evaluation-model');
    // Research: OPENAI blocked. Grading: OLLAMA blocked, OPENAI available.
    payload.provider_options = payload.provider_options.map(provider => ({
        ...provider,
        disabled: provider.value === 'OPENAI',
        disabled_reason: provider.value === 'OPENAI' ? 'Research endpoint is not local' : null,
    }));
    payload.benchmark_provider_options = payload.benchmark_provider_options.map(provider => ({
        ...provider,
        disabled: provider.value === 'OLLAMA',
        disabled_reason: provider.value === 'OLLAMA' ? 'Evaluation endpoint is not local' : null,
    }));
    vi.stubGlobal('fetch', vi.fn().mockResolvedValue({ json: async () => payload }));
    vi.spyOn(console, 'log').mockImplementation(() => {});
    const harness = compileEvaluationModelsHarness();
    harness.populateEvaluationProviders();
    const select = document.getElementById('evaluation_provider');
    select.dataset.policyPending = 'true';

    harness.loadEvaluationModelsFromAPI();
    await vi.advanceTimersByTimeAsync(500);
    await flushPromises();

    expect(harness.getProviders()).toEqual(payload.benchmark_provider_options);
    expect(select.value).toBe('OPENAI');
    expect(select.selectedOptions[0].disabled).toBe(false);
    const grading = Array.from(select.options).find(option => option.value === 'OLLAMA');
    expect(grading.disabled).toBe(true);
    expect(grading.textContent).toContain('Evaluation endpoint is not local');
    expect(select.dataset.policyPending).toBeUndefined();
});

it('keeps the start gate armed when the initial load advertises no evaluation providers', async () => {
    // Array.isArray([]) is true, so an empty list used to delete policyPending
    // while populateEvaluationProviders() fell back to the template's hardcoded
    // provider list, whose entries carry no `disabled` flag - re-selecting the
    // saved provider as an enabled option and letting the start gate pass.
    renderEvaluationControls('openai');
    vi.useFakeTimers();
    const payload = modelsPayload('evaluation-model');
    payload.benchmark_provider_options = [];
    vi.stubGlobal('fetch', vi.fn().mockResolvedValue({ json: async () => payload }));
    vi.spyOn(console, 'log').mockImplementation(() => {});
    const harness = compileEvaluationModelsHarness();
    harness.populateEvaluationProviders();
    const select = document.getElementById('evaluation_provider');
    select.dataset.policyPending = 'true';

    harness.loadEvaluationModelsFromAPI();
    await vi.advanceTimersByTimeAsync(500);
    await flushPromises();

    expect(harness.getProviders()).toEqual([]);
    expect(select.dataset.policyPending).toBe('true');
    // The hardcoded fallback is still rendered and still looks selectable, so
    // policyPending is the only thing standing between it and a blocked
    // grading call: assert the gate itself, not just the flag.
    expect(select.value).toBe('openai');
    const showAlert = vi.fn();
    const start = compileStartBenchmark(showAlert);
    // The gate has to return before the rest of startBenchmark runs: the
    // compiled wrapper deliberately leaves everything past it undefined, so a
    // gate that lets the start through surfaces here instead of posting.
    let startError = null;
    try {
        await start();
    } catch (error) {
        startError = error;
    }
    expect(startError).toBeNull();
    expect(showAlert).toHaveBeenCalledWith(
        expect.stringContaining('available evaluation provider'),
        'warning',
    );
});

it('does not start a benchmark while the selected evaluation provider is disabled', async () => {
    // Covers startBenchmark's selectedOptions[0]?.disabled clause on its own:
    // the value is set and policyPending is resolved, so neither of the other
    // two clauses can be what blocks the start.
    renderEvaluationControls();
    const select = document.getElementById('evaluation_provider');
    const showAlert = vi.fn();
    const fetchMock = vi.fn();
    vi.stubGlobal('fetch', fetchMock);
    const start = compileStartBenchmark(showAlert);

    // A blocked provider arrives flagged rather than omitted, so the option
    // exists and can still be the selected one.
    const blocked = document.createElement('option');
    blocked.value = 'OPENAI';
    blocked.textContent = 'OpenAI - Blocked by "Require Local LLM Endpoint"';
    blocked.disabled = true;
    select.appendChild(blocked);
    select.value = 'OPENAI';
    delete select.dataset.policyPending;

    await start();

    // Pin that the disabled clause is what fired, not an empty value or a
    // pending policy.
    expect(select.value).toBe('OPENAI');
    expect(select.selectedOptions[0].disabled).toBe(true);
    expect(select.dataset.policyPending).toBeUndefined();
    expect(fetchMock).not.toHaveBeenCalled();
    expect(showAlert).toHaveBeenCalledWith(
        expect.stringContaining('available evaluation provider'),
        'warning',
    );
});

it('does not start a benchmark with cleared or unresolved evaluation availability', async () => {
    renderEvaluationControls();
    const select = document.getElementById('evaluation_provider');
    const showAlert = vi.fn();
    const fetchMock = vi.fn();
    vi.stubGlobal('fetch', fetchMock);
    const start = compileStartBenchmark(showAlert);
    await start();
    expect(fetchMock).not.toHaveBeenCalled();
    expect(showAlert).toHaveBeenCalledWith(expect.stringContaining('available evaluation provider'), 'warning');
    const option = document.createElement('option');
    option.value = 'OPENAI';
    option.textContent = 'OpenAI';
    select.appendChild(option);
    select.value = 'OPENAI';
    select.dataset.policyPending = 'true';
    showAlert.mockClear();
    await start();
    expect(fetchMock).not.toHaveBeenCalled();
    expect(showAlert).toHaveBeenCalledOnce();
});

function selectableEvaluationProvider() {
    const select = document.getElementById('evaluation_provider');
    const option = document.createElement('option');
    option.value = 'OPENAI';
    option.textContent = 'OpenAI';
    select.appendChild(option);
    select.value = 'OPENAI';
    delete select.dataset.policyPending;
    return select;
}

it('does not start a benchmark while an evaluation setting save is still in flight', async () => {
    // The first gate is deliberately satisfied - the provider is selected,
    // enabled and not pending - so the save gate is the only thing left that
    // can block the start.
    renderEvaluationControls();
    const select = selectableEvaluationProvider();
    const showAlert = vi.fn();
    const fetchMock = vi.fn();
    vi.stubGlobal('fetch', fetchMock);
    const saveEvaluationSetting = vi.fn(() => Promise.resolve(true));
    saveEvaluationSetting.pending = 1;
    saveEvaluationSetting.failedKeys = new Set();
    const start = compileStartBenchmark(showAlert, saveEvaluationSetting);

    await start();

    expect(select.value).toBe('OPENAI');
    expect(select.selectedOptions[0].disabled).toBe(false);
    expect(select.dataset.policyPending).toBeUndefined();
    expect(fetchMock).not.toHaveBeenCalled();
    expect(showAlert).toHaveBeenCalledWith(
        expect.stringContaining('finish saving evaluation settings'),
        'warning',
    );

    // Control: with nothing pending and nothing failed the gate must let the
    // start through, or a stub that blocks unconditionally would pass the two
    // assertions above. Everything past the gate is left undefined in the
    // compiled wrapper, so getting there surfaces as a throw rather than a POST.
    showAlert.mockClear();
    saveEvaluationSetting.pending = 0;
    let startError = null;
    try {
        await start();
    } catch (error) {
        startError = error;
    }
    expect(startError).not.toBeNull();
    expect(showAlert).not.toHaveBeenCalled();
});

it('does not start a benchmark while an evaluation setting save has failed', async () => {
    // Same gate, other clause: pending is back to 0 but a key is still in
    // failedKeys, so the settings the grader would read are not the ones on
    // screen.
    renderEvaluationControls();
    const select = selectableEvaluationProvider();
    const showAlert = vi.fn();
    const fetchMock = vi.fn();
    vi.stubGlobal('fetch', fetchMock);
    const saveEvaluationSetting = vi.fn(() => Promise.resolve(false));
    saveEvaluationSetting.pending = 0;
    saveEvaluationSetting.failedKeys = new Set(['benchmark.evaluation.provider']);
    const start = compileStartBenchmark(showAlert, saveEvaluationSetting);

    await start();

    expect(select.selectedOptions[0].disabled).toBe(false);
    expect(fetchMock).not.toHaveBeenCalled();
    expect(showAlert).toHaveBeenCalledWith(
        expect.stringContaining('finish saving evaluation settings'),
        'warning',
    );

    // Control: clearing the failed key must reopen the gate.
    showAlert.mockClear();
    saveEvaluationSetting.failedKeys.clear();
    let startError = null;
    try {
        await start();
    } catch (error) {
        startError = error;
    }
    expect(startError).not.toBeNull();
    expect(showAlert).not.toHaveBeenCalled();
});

it('serializes evaluation saves and retains failed keys until that key is retried', async () => {
    const first = deferred();
    const fetchMock = vi.fn()
        .mockReturnValueOnce(first.promise)
        .mockResolvedValue({ ok: true, json: async () => ({ message: 'Setting updated successfully' }) });
    vi.stubGlobal('fetch', fetchMock);
    vi.spyOn(console, 'error').mockImplementation(() => {});
    const save = compileSaveEvaluationSetting('csrf-test');
    const failed = save('benchmark.evaluation.provider', 'openai');
    const next = save('benchmark.evaluation.temperature', 0.5);
    await flushPromises();
    expect(fetchMock).toHaveBeenCalledOnce();
    expect(save.pending).toBe(2);
    first.resolve({ ok: false, json: async () => ({ success: false }) });
    expect(await failed).toBe(false);
    expect(await next).toBe(true);
    expect(save.failedKeys.has('benchmark.evaluation.provider')).toBe(true);
    expect(save.pending).toBe(0);
    expect(await save('benchmark.evaluation.provider', 'ollama')).toBe(true);
    expect(save.failedKeys.size).toBe(0);
});

it('keeps saving after a rejected link in the save queue', async () => {
    // saveEvaluationSetting chains on BOTH settlements (`.then(save, save)`).
    // With a plain `.then(save)` a rejected predecessor skips `save` entirely,
    // so its `.finally()` never runs: `pending` stays up for the life of the
    // page and startBenchmark's "finish saving" gate never reopens - and the
    // promise handed back to the caller rejects instead of reporting false.
    const fetchMock = vi.fn().mockResolvedValue({
        ok: true,
        json: async () => ({ message: 'Setting updated successfully' }),
    });
    vi.stubGlobal('fetch', fetchMock);
    vi.spyOn(console, 'error').mockImplementation(() => {});
    const save = compileSaveEvaluationSetting('csrf-test');
    const poisoned = Promise.reject(new Error('previous queue link rejected'));
    // The chain below is what handles it; this only keeps the unhandled
    // rejection reporter quiet in the window before that.
    poisoned.catch(() => {});
    save.queue = poisoned;

    let rejection = null;
    const result = await save('benchmark.evaluation.provider', 'openai')
        .catch((error) => { rejection = error; return 'REJECTED'; });

    expect(rejection).toBeNull();
    expect(result).toBe(true);
    expect(save.pending).toBe(0);
    expect(fetchMock).toHaveBeenCalledOnce();

    // The queue is usable again, so the gate can reopen.
    expect(await save('benchmark.evaluation.temperature', 0.5)).toBe(true);
    expect(save.pending).toBe(0);
    expect(fetchMock).toHaveBeenCalledTimes(2);
});

it('reports a value it cannot serialise instead of leaking a pending save', async () => {
    // The other half of the same totality guard, and the only shape that
    // reaches it: `save` runs as a .then(save, save) handler, so a synchronous
    // throw while building the request - JSON.stringify on a value it cannot
    // represent - rejects the queue itself. The .finally() that brings
    // `pending` back down is attached to the chain `save` RETURNS, so a throw
    // before that return leaks the increment: `pending` stays up for the life
    // of the page, startBenchmark's "finish saving" gate never reopens, and
    // the caller gets a rejection instead of the documented false. Without the
    // try/catch this resolves to 'REJECTED' with pending stuck at 1.
    const fetchMock = vi.fn().mockResolvedValue({
        ok: true,
        json: async () => ({ message: 'Setting updated successfully' }),
    });
    vi.stubGlobal('fetch', fetchMock);
    vi.spyOn(console, 'error').mockImplementation(() => {});
    const save = compileSaveEvaluationSetting('csrf-test');

    let rejection = null;
    const result = await save('benchmark.evaluation.temperature', 1n)
        .catch((error) => { rejection = error; return 'REJECTED'; });

    expect(rejection).toBeNull();
    expect(result).toBe(false);
    expect(save.pending).toBe(0);
    expect(save.failedKeys.has('benchmark.evaluation.temperature')).toBe(true);
    // The throw happens while evaluating the request body, so nothing is sent.
    expect(fetchMock).not.toHaveBeenCalled();

    // The queue survives it, so the gate can reopen.
    expect(await save('benchmark.evaluation.temperature', 0.5)).toBe(true);
    expect(save.pending).toBe(0);
    expect(save.failedKeys.size).toBe(0);
    expect(fetchMock).toHaveBeenCalledOnce();
});


it('retains the latest intended evaluation provider across repeated blocked refreshes', async () => {
    renderEvaluationControls('OPENAI');
    let blocked = false;
    const payload = () => ({
        providers: {},
        provider_options: PROVIDER_OPTIONS,
        benchmark_provider_options: PROVIDER_OPTIONS.map(p => ({ ...p, disabled: p.value === 'OLLAMA' && blocked })),
    });
    vi.stubGlobal('fetch', vi.fn().mockImplementation(async () => ({ json: async () => payload() })));
    const harness = compileEvaluationModelsHarness(PROVIDER_OPTIONS);
    harness.populateEvaluationProviders();
    harness.setupEvaluationModelDropdown();
    const select = document.getElementById('evaluation_provider');
    select.value = 'OLLAMA';
    blocked = true;
    for (let i = 0; i < 2; i += 1) {
        document.querySelector('.refresh-btn').click();
        await flushPromises();
        expect(select.value).toBe('');
        expect(select.dataset.initialValue).toBe('OLLAMA');
    }
    blocked = false;
    document.querySelector('.refresh-btn').click();
    await flushPromises();
    expect(select.value).toBe('OLLAMA');
});

it('accepts the message-only settings response and rejects an incompatible success envelope', async () => {
    const fetchMock = vi.fn()
        .mockResolvedValueOnce({ ok: true, json: async () => ({ message: 'Setting benchmark.evaluation.provider updated successfully' }) })
        .mockResolvedValueOnce({ ok: true, json: async () => ({ success: true }) });
    vi.stubGlobal('fetch', fetchMock);
    vi.spyOn(console, 'error').mockImplementation(() => {});
    const save = compileSaveEvaluationSetting('csrf-test');
    expect(await save('benchmark.evaluation.provider', 'ollama')).toBe(true);
    expect(save.failedKeys.size).toBe(0);
    expect(await save('benchmark.evaluation.provider', 'openai')).toBe(false);
    expect(save.failedKeys.has('benchmark.evaluation.provider')).toBe(true);
});