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.

11.0 KB
Raw Blame
/** Runtime FastAPI contracts for benchmark_results.html. */

import { resolve } from 'node:path';
import { compileTemplateHarness } from '../helpers/template-harness.js';

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

function compileHistory(dependencies) {
    return compileTemplateHarness({
        templatePath: TEMPLATE_PATH,
        functionNames: ['loadBenchmarkHistory'],
        dependencies,
        preamble: 'let benchmarkRuns = []; let filteredRuns = [];',
        returnExpression: `({
            loadBenchmarkHistory,
            getRuns: () => benchmarkRuns,
            getFilteredRuns: () => filteredRuns,
        })`,
    });
}

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

it('loads the benchmark history success envelope into both result sets', async () => {
    const runs = [{ id: 3299 }, { id: 3300 }];
    const fetchMock = vi.fn().mockResolvedValue({
        json: vi.fn().mockResolvedValue({ success: true, runs }),
    });
    vi.stubGlobal('fetch', fetchMock);
    const dependencies = { populateFilters: vi.fn(), displayResults: vi.fn() };
    const harness = compileHistory(dependencies);

    await harness.loadBenchmarkHistory();

    expect(fetchMock).toHaveBeenCalledWith('/benchmark/api/history');
    expect(harness.getRuns()).toEqual(runs);
    expect(harness.getFilteredRuns()).toEqual(runs);
    expect(harness.getFilteredRuns()).not.toBe(harness.getRuns());
    expect(dependencies.populateFilters).toHaveBeenCalledOnce();
    expect(dependencies.displayResults).toHaveBeenCalledOnce();
});

it('renders the benchmark history error envelope and network failure', async () => {
    document.body.innerHTML = '<div id="results-container"></div>';
    const fetchMock = vi.fn()
        .mockResolvedValueOnce({
            json: vi.fn().mockResolvedValue({ success: false }),
        })
        .mockRejectedValueOnce(new Error('offline'));
    vi.stubGlobal('fetch', fetchMock);
    const error = vi.spyOn(console, 'error').mockImplementation(() => {});
    const dependencies = { populateFilters: vi.fn(), displayResults: vi.fn() };
    const harness = compileHistory(dependencies);

    await harness.loadBenchmarkHistory();
    expect(document.getElementById('results-container').textContent)
        .toBe('Error loading benchmark results');
    await harness.loadBenchmarkHistory();

    expect(error).toHaveBeenCalled();
    expect(dependencies.displayResults).not.toHaveBeenCalled();
});

it('DELETEs a benchmark run with CSRF and removes the accepted run locally', async () => {
    document.head.innerHTML = '<meta name="csrf-token" content="csrf-benchmark">';
    const fetchMock = vi.fn().mockResolvedValue({
        json: vi.fn().mockResolvedValue({ success: true }),
    });
    vi.stubGlobal('fetch', fetchMock);
    const showAlert = vi.fn();
    const applyFilters = vi.fn();
    const harness = compileTemplateHarness({
        templatePath: TEMPLATE_PATH,
        functionNames: ['deleteBenchmarkRun'],
        dependencies: { showAlert, applyFilters },
        preamble: 'let benchmarkRuns = [{ id: 3299 }, { id: 3300 }];',
        returnExpression: `({
            deleteBenchmarkRun,
            getRuns: () => benchmarkRuns,
        })`,
    });

    await harness.deleteBenchmarkRun(3299);

    expect(fetchMock).toHaveBeenCalledWith('/benchmark/api/delete/3299', {
        method: 'DELETE',
        headers: {
            'Content-Type': 'application/json',
            'X-CSRFToken': 'csrf-benchmark',
        },
    });
    expect(harness.getRuns()).toEqual([{ id: 3300 }]);
    expect(showAlert).toHaveBeenCalledWith(
        'Benchmark run deleted successfully!',
        'success',
    );
    expect(applyFilters).toHaveBeenCalledOnce();
});

it('keeps local benchmark state when the delete envelope is rejected', async () => {
    document.head.innerHTML = '<meta name="csrf-token" content="csrf-benchmark">';
    vi.stubGlobal('fetch', vi.fn().mockResolvedValue({
        json: vi.fn().mockResolvedValue({
            success: false,
            error: 'run is still active',
        }),
    }));
    const showAlert = vi.fn();
    const applyFilters = vi.fn();
    const harness = compileTemplateHarness({
        templatePath: TEMPLATE_PATH,
        functionNames: ['deleteBenchmarkRun'],
        dependencies: { showAlert, applyFilters },
        preamble: 'let benchmarkRuns = [{ id: 3299 }];',
        returnExpression: `({
            deleteBenchmarkRun,
            getRuns: () => benchmarkRuns,
        })`,
    });

    await harness.deleteBenchmarkRun(3299);

    expect(harness.getRuns()).toEqual([{ id: 3299 }]);
    expect(applyFilters).not.toHaveBeenCalled();
    expect(showAlert).toHaveBeenCalledWith(
        'Error deleting benchmark run: run is still active',
        'error',
    );
});

it('POSTs cancellation with CSRF before delegating to deletion', async () => {
    vi.useFakeTimers();
    document.head.innerHTML = '<meta name="csrf-token" content="csrf-cancel">';
    vi.stubGlobal('confirm', vi.fn(() => true));
    const fetchMock = vi.fn().mockResolvedValue({
        json: vi.fn().mockResolvedValue({ success: true }),
    });
    vi.stubGlobal('fetch', fetchMock);
    const showAlert = vi.fn();
    const deleteBenchmarkRun = vi.fn();
    const harness = compileTemplateHarness({
        templatePath: TEMPLATE_PATH,
        functionNames: ['cancelAndDeleteBenchmarkRun'],
        dependencies: { showAlert, deleteBenchmarkRun },
        returnExpression: '({ cancelAndDeleteBenchmarkRun })',
    });

    const cancellation = harness.cancelAndDeleteBenchmarkRun(3299);
    await vi.advanceTimersByTimeAsync(1000);
    await cancellation;

    expect(fetchMock).toHaveBeenCalledWith('/benchmark/api/cancel/3299', {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
            'X-CSRFToken': 'csrf-cancel',
        },
    });
    expect(showAlert).toHaveBeenCalledWith(
        'Benchmark cancelled successfully. Deleting...',
        'info',
    );
    expect(deleteBenchmarkRun).toHaveBeenCalledWith(3299);
});

it('renders a persistence error as text when detailed results cannot load', async () => {
    document.body.innerHTML = '<div id="examples-3299"></div>';
    vi.stubGlobal('fetch', vi.fn().mockResolvedValue({
        json: vi.fn().mockResolvedValue({
            persistence_error: {
                message: '<img src=x onerror=alert(1)> write failed',
            },
        }),
    }));
    const harness = compileTemplateHarness({
        templatePath: TEMPLATE_PATH,
        functionNames: ['loadExamples'],
        dependencies: { createExampleCard: vi.fn() },
        returnExpression: '({ loadExamples })',
    });

    await harness.loadExamples(3299);

    const container = document.getElementById('examples-3299');
    expect(container.textContent)
        .toBe('<img src=x onerror=alert(1)> write failed');
    expect(container.querySelector('img')).toBeNull();
});

it('loads detailed results and renders the migrated results envelope', async () => {
    document.body.innerHTML = '<div id="examples-3299"></div>';
    const results = [
        { id: 1, search_result_count: 2 },
        { id: 2, search_result_count: 6 },
    ];
    const fetchMock = vi.fn().mockResolvedValue({
        json: vi.fn().mockResolvedValue({ success: true, results }),
    });
    vi.stubGlobal('fetch', fetchMock);
    const createExampleCard = vi.fn(result => (
        `<article data-result="${result.id}">Result ${result.id}</article>`
    ));
    const harness = compileTemplateHarness({
        templatePath: TEMPLATE_PATH,
        functionNames: ['loadExamples'],
        dependencies: { createExampleCard },
        returnExpression: '({ loadExamples })',
    });

    await harness.loadExamples(3299);

    expect(fetchMock).toHaveBeenCalledWith(
        '/benchmark/api/results/3299?limit=50',
    );
    const container = document.getElementById('examples-3299');
    expect(container.textContent).toContain('Avg Search Results');
    expect(container.textContent).toContain('4.0');
    expect(container.querySelectorAll('[data-result]')).toHaveLength(2);
    expect(createExampleCard).toHaveBeenCalledTimes(2);
});

it('requests settings metadata only for an opted-in benchmark export', async () => {
    const run = {
        id: 3299,
        created_at: '2026-08-01T10:00:00Z',
        start_time: '2026-08-01T10:00:00Z',
        ldr_version: '1.10.7',
        overall_accuracy: 75,
        completed_examples: 4,
        total_examples: 4,
        avg_search_results: 3,
        avg_processing_time: 30,
        datasets_config: { simpleqa: { count: 4 } },
        evaluation_config: {},
        search_config: {
            model_name: 'model-a',
            provider: 'openai',
            search_tool: 'searxng',
            search_strategy: 'iterdrag',
            temperature: 0.2,
            iterations: 2,
            questions_per_iteration: 3,
        },
    };
    const settingsSnapshot = { llm: { model: 'model-a' } };
    const fetchMock = vi.fn().mockResolvedValue({
        json: vi.fn().mockResolvedValue({
            success: true,
            metadata: {
                started_at: run.start_time,
                ldr_version: run.ldr_version,
                settings_snapshot: settingsSnapshot,
            },
            results: [],
        }),
    });
    vi.stubGlobal('fetch', fetchMock);
    const createObjectURL = vi.fn(() => 'blob:benchmark-yaml');
    const revokeObjectURL = vi.fn();
    const URLStub = class extends window.URL {};
    Object.defineProperties(URLStub, {
        createObjectURL: { value: createObjectURL, configurable: true },
        revokeObjectURL: { value: revokeObjectURL, configurable: true },
    });
    vi.stubGlobal('URL', URLStub);
    vi.spyOn(window.HTMLAnchorElement.prototype, 'click')
        .mockImplementation(() => {});
    const formatSettingsSnapshot = vi.fn(() => 'settings:\n  redacted: true\n');
    const showAlert = vi.fn();
    const harness = compileTemplateHarness({
        templatePath: TEMPLATE_PATH,
        functionNames: ['downloadBenchmarkYAML'],
        dependencies: {
            initialRuns: [run],
            yamlEscape: value => String(value),
            formatSettingsSnapshot,
            formatAvgSearchResults: () => '3',
            formatAvgProcessingTime: () => '30.0s',
            showAlert,
        },
        preamble: 'let benchmarkRuns = initialRuns;',
        returnExpression: '({ downloadBenchmarkYAML })',
    });

    await harness.downloadBenchmarkYAML(3299, false, true);

    expect(fetchMock).toHaveBeenCalledWith(
        '/benchmark/api/results/3299/export?include_settings=1',
    );
    expect(formatSettingsSnapshot).toHaveBeenCalledWith(settingsSnapshot);
    expect(createObjectURL).toHaveBeenCalledWith(expect.any(Blob));
    expect(revokeObjectURL).toHaveBeenCalledWith('blob:benchmark-yaml');
    expect(showAlert).toHaveBeenCalledWith(
        'Benchmark YAML downloaded successfully.',
        'success',
    );
});