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.

4.9 KB
Raw Blame
/**
 * Execute the production inline-onclick handler that probes the selected
 * embedding model.  The broader page runtime suite covers bootstrap/save;
 * this pins the distinct /library/api/rag/test-embedding contract.
 */

import { resolve } from 'node:path';

import { compileTemplateHarness } from './helpers/template-harness.js';

const SOURCE_PATH = resolve(
    __dirname,
    '../../src/local_deep_research/web/static/js/embedding_settings.js',
);

function loadTestConfiguration({ safeFetchWithAuth, showEmbeddingSettingsError, showSuccess }) {
    return compileTemplateHarness({
        templatePath: SOURCE_PATH,
        functionNames: ['testConfiguration'],
        dependencies: { safeFetchWithAuth, showEmbeddingSettingsError, showSuccess },
        returnExpression: 'testConfiguration',
    });
}

afterEach(() => {
    vi.restoreAllMocks();
    delete window.api;
    delete window.XSSProtection;
    document.body.replaceChildren();
});

it('POSTs only the selected model with CSRF for a server-owned text probe', async () => {
    document.body.innerHTML = `
        <select id="embedding-provider">
            <option value="sentence_transformers" selected>Sentence Transformers</option>
        </select>
        <select id="embedding-model">
            <option value="all-MiniLM-L6-v2" selected>MiniLM</option>
        </select>
        <button id="test-config-btn"></button>
        <div id="test-result" style="display: none"></div>
    `;
    window.api = { getCsrfToken: vi.fn(() => 'csrf-embedding-test') };
    window.XSSProtection = { escapeHtml: value => String(value) };
    const safeFetchWithAuth = vi.fn().mockResolvedValue(
        new Response(JSON.stringify({
            success: true,
            dimension: 384,
            response_time_ms: 42,
        }), { status: 200 }),
    );
    const showEmbeddingSettingsError = vi.fn();
    const showSuccess = vi.fn();
    const testConfiguration = loadTestConfiguration({
        safeFetchWithAuth,
        showEmbeddingSettingsError,
        showSuccess,
    });

    await testConfiguration();

    expect(safeFetchWithAuth).toHaveBeenCalledWith(
        '/library/api/rag/test-embedding',
        {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
                'X-CSRFToken': 'csrf-embedding-test',
            },
            body: JSON.stringify({
                provider: 'sentence_transformers',
                model: 'all-MiniLM-L6-v2',
            }),
        },
    );
    expect(document.getElementById('test-result').textContent)
        .toContain('Test Passed!');
    expect(document.getElementById('test-result').textContent)
        .toContain('Embedding dimension: 384');
    expect(document.getElementById('test-result').textContent)
        .toContain('Response time: 42ms');
    expect(document.getElementById('test-config-btn').disabled).toBe(false);
    expect(showSuccess).toHaveBeenCalledWith('Embedding test passed!');
    expect(showEmbeddingSettingsError).not.toHaveBeenCalled();
});

it('escapes a failed test envelope and restores the test button', async () => {
    document.body.innerHTML = `
        <select id="embedding-provider">
            <option value="openai" selected>OpenAI</option>
        </select>
        <select id="embedding-model">
            <option value="text-embedding-3-small" selected>Small</option>
        </select>
        <button id="test-config-btn"></button>
        <div id="test-result" style="display: none"></div>
    `;
    const payload = '<img src=x onerror="window.__embeddingXss = true">';
    const escapeHtml = value => String(value).replace(
        /[&<>"']/g,
        character => ({
            '&': '&amp;',
            '<': '&lt;',
            '>': '&gt;',
            '"': '&quot;',
            "'": '&#39;',
        })[character],
    );
    window.api = { getCsrfToken: vi.fn(() => 'csrf-embedding-test') };
    window.XSSProtection = { escapeHtml };
    const safeFetchWithAuth = vi.fn().mockResolvedValue(
        new Response(JSON.stringify({
            success: false,
            error: payload,
        }), { status: 422 }),
    );
    const showEmbeddingSettingsError = vi.fn();
    const showSuccess = vi.fn();
    const testConfiguration = loadTestConfiguration({
        safeFetchWithAuth,
        showEmbeddingSettingsError,
        showSuccess,
    });

    await testConfiguration();

    const result = document.getElementById('test-result');
    expect(result.style.display).toBe('block');
    expect(result.querySelector('img')).toBeNull();
    expect(result.textContent).toContain(payload);
    expect(window.__embeddingXss).toBeUndefined();
    expect(showEmbeddingSettingsError).toHaveBeenCalledWith(
        'Embedding test failed: ' + payload,
    );
    expect(showSuccess).not.toHaveBeenCalled();
    const button = document.getElementById('test-config-btn');
    expect(button.disabled).toBe(false);
    expect(button.textContent).toContain('Test Embedding Model');
});