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.

7.8 KB
Raw Blame
/**
 * The benchmark page's "Clear" for the grader API key must remove the stored
 * key. A PUT of {"value": ""} is answered 200 by the settings API but ignored
 * for a password setting (an empty password field means "left untouched"),
 * so Clear has to use DELETE and only report success when the server agrees.
 */

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 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 compileControls() {
    const template = readFileSync(TEMPLATE_PATH, 'utf8');
    const source = [
        'setEvaluationApiKeySaved',
        'clearEvaluationApiKey',
        'saveEvaluationSetting',
        'setupEvaluationEventHandlers',
    ].map(name => extractFunction(template, name)).join('\n');
    // Repository-owned template code, not user input.
    const factory = new Function( // eslint-disable-line no-new-func
        'csrfToken',
        `const evaluationProviderSelect = null;
         const evaluationModelInput = null;
         const evaluationEndpointInput = null;
         const evaluationTemperatureInput = null;
         ${source}
         return {clearEvaluationApiKey, saveEvaluationSetting,
                 setEvaluationApiKeySaved, setupEvaluationEventHandlers};`,
    );
    return factory('csrf-benchmark');
}

function response(status) {
    return {
        ok: status >= 200 && status < 300,
        status,
        json: vi.fn().mockResolvedValue({ message: 'ok' }),
    };
}

function enterDraft(value) {
    const field = document.getElementById('evaluation_api_key');
    field.value = value;
    field.dispatchEvent(new Event('input'));
}

describe('benchmark grader key Clear', () => {
    let input;

    beforeEach(() => {
        document.body.innerHTML = '<input id="evaluation_api_key" type="password">';
        input = document.getElementById('evaluation_api_key');
    });

    afterEach(() => {
        document.body.replaceChildren();
        vi.unstubAllGlobals();
        vi.restoreAllMocks();
    });

    it('deletes the stored key instead of writing an empty value', async () => {
        const fetchMock = vi.fn().mockResolvedValue(response(200));
        vi.stubGlobal('fetch', fetchMock);
        const controls = compileControls();

        await expect(controls.clearEvaluationApiKey()).resolves.toBe(true);

        expect(fetchMock).toHaveBeenCalledTimes(1);
        const [url, init] = fetchMock.mock.calls[0];
        expect(url).toBe('/settings/api/benchmark.evaluation.api_key');
        expect(init.method).toBe('DELETE');
        expect(init.body).toBeUndefined();
        expect(init.headers['X-CSRFToken']).toBe('csrf-benchmark');
        expect(input.dataset.keySaved).toBe('false');
    });

    it('treats "no stored key" (404) as cleared', async () => {
        vi.stubGlobal('fetch', vi.fn().mockResolvedValue(response(404)));
        const controls = compileControls();

        await expect(controls.clearEvaluationApiKey()).resolves.toBe(true);
        expect(input.dataset.keySaved).toBe('false');
    });

    it('does not report a refused delete as cleared', async () => {
        vi.spyOn(console, 'error').mockImplementation(() => {});
        vi.stubGlobal('fetch', vi.fn().mockResolvedValue(response(403)));
        const controls = compileControls();
        input.dataset.keySaved = 'true';

        await expect(controls.clearEvaluationApiKey()).resolves.toBe(false);
        expect(input.dataset.keySaved).toBe('true');
    });

    it('finishes a queued save before Clear removes the stored value', async () => {
        let finishSave;
        let stored = null;
        const fetchMock = vi.fn((_url, init) => {
            if (init.method === 'PUT') {
                return new Promise(resolveSave => {
                    finishSave = () => {
                        stored = JSON.parse(init.body).value;
                        resolveSave(response(200));
                    };
                });
            }
            stored = null;
            return Promise.resolve(response(200));
        });
        vi.stubGlobal('fetch', fetchMock);
        const controls = compileControls();
        controls.setupEvaluationEventHandlers();
        enterDraft('synthetic-first-value');
        input.dispatchEvent(new Event('change'));
        await vi.waitFor(() => expect(fetchMock).toHaveBeenCalledTimes(1));

        const clearing = controls.clearEvaluationApiKey();
        await Promise.resolve();
        const requestsBeforeSaveFinished = fetchMock.mock.calls.length;
        const pendingBeforeSaveFinished = controls.saveEvaluationSetting.pending;
        finishSave();
        await controls.saveEvaluationSetting.queue;
        await clearing;

        expect(requestsBeforeSaveFinished).toBe(1);
        expect(pendingBeforeSaveFinished).toBe(2);
        expect(fetchMock.mock.calls.map(([, init]) => init.method)).toEqual(['PUT', 'DELETE']);
        expect(stored).toBeNull();
        expect(controls.saveEvaluationSetting.pending).toBe(0);
        expect(input.dataset.keySaved).toBe('false');
    });

    it('keeps a newer draft when an older save completes', async () => {
        let finishSave;
        vi.stubGlobal('fetch', vi.fn(() => new Promise(resolveSave => {
            finishSave = () => resolveSave(response(200));
        })));
        const controls = compileControls();
        controls.setupEvaluationEventHandlers();
        enterDraft('synthetic-first-value');
        input.dispatchEvent(new Event('change'));
        await vi.waitFor(() => expect(finishSave).toBeTypeOf('function'));

        enterDraft('synthetic-newer-draft');
        finishSave();
        await controls.saveEvaluationSetting.queue;
        await Promise.resolve();

        expect(input.value).toBe('synthetic-newer-draft');
        expect(input.dataset.keySaved).toBe('true');
    });

    it('keeps a newer draft when Clear completes', async () => {
        let finishClear;
        vi.stubGlobal('fetch', vi.fn(() => new Promise(resolveClear => {
            finishClear = () => resolveClear(response(200));
        })));
        const controls = compileControls();
        enterDraft('synthetic-first-value');
        const clearing = controls.clearEvaluationApiKey();
        await vi.waitFor(() => expect(finishClear).toBeTypeOf('function'));

        enterDraft('synthetic-newer-draft');
        finishClear();
        await clearing;

        expect(input.value).toBe('synthetic-newer-draft');
        expect(input.dataset.keySaved).toBe('false');
    });

    it('tracks a refused Clear until a later successful mutation', async () => {
        vi.spyOn(console, 'error').mockImplementation(() => {});
        vi.stubGlobal('fetch', vi.fn()
            .mockResolvedValueOnce(response(403))
            .mockResolvedValueOnce(response(200)));
        const controls = compileControls();

        await expect(controls.clearEvaluationApiKey()).resolves.toBe(false);
        expect(controls.saveEvaluationSetting.pending).toBe(0);
        expect(controls.saveEvaluationSetting.failedKeys.has('benchmark.evaluation.api_key')).toBe(true);
        await expect(controls.clearEvaluationApiKey()).resolves.toBe(true);
        expect(controls.saveEvaluationSetting.failedKeys.size).toBe(0);
    });
});