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.

5.1 KB
Raw Blame
/**
 * Static wiring contract for the research form's per-provider API-key
 * fields: every provider that ships an API-key input must have the
 * container and label in research.html AND a change listener in
 * research.js persisting to the matching llm.<provider>.api_key setting.
 *
 * Complements research-api-runtime.test.js (which exercises the openai
 * listener end-to-end for every provider) by pinning the template itself —
 * the puppeteer script that drove these fields is not part of CI.
 */

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

const RESEARCH_TEMPLATE = readFileSync(resolvePath(
    __dirname,
    '../../../src/local_deep_research/web/templates/pages/research.html',
), 'utf8');

const RESEARCH_COMPONENT = readFileSync(resolvePath(
    __dirname,
    '../../../src/local_deep_research/web/static/js/components/research.js',
), 'utf8');

const CONTEXT_OVERFLOW_COMPONENT = readFileSync(resolvePath(
    __dirname,
    '../../../src/local_deep_research/web/static/js/components/context-overflow.js',
), 'utf8');

// The context-overflow dashboard's provider display names. This map
// silently rotted once on main (deepseek/requesty were missing until this
// PR added them) — pin it so the next provider addition cannot forget it.
const PROVIDER_DISPLAY_NAMES = [
    ['openai_endpoint', 'OpenAI Endpoint'],
    ['anthropic_endpoint', 'Anthropic Endpoint'],
    ['ollama', 'Ollama'],
    ['openai', 'OpenAI'],
    ['anthropic', 'Anthropic'],
    ['google', 'Google'],
    ['openrouter', 'OpenRouter'],
    ['orcarouter', 'OrcaRouter'],
    ['atlascloud', 'Atlas Cloud'],
    ['qwen', 'QwenCloud'],
    ['deepseek', 'DeepSeek'],
    ['requesty', 'Requesty'],
    ['lmstudio', 'LM Studio'],
    ['llamacpp', 'Llama.cpp'],
    ['xai', 'xAI'],
    ['ionos', 'IONOS'],
];

// Deliberately NOT imported from helpers/api-key-providers.js: this is a
// static contract pinning the real template, so it must carry its own
// literal list — a provider removed from the shared helper must still fail
// here rather than shrink the contract silently.
const API_KEY_PROVIDERS = [
    'openai',
    'anthropic',
    'google',
    'openrouter',
    'orcarouter',
    'atlascloud',
    'qwen',
    'xai',
    'ionos',
    'openai_endpoint',
    'anthropic_endpoint',
    'ollama',
    'lmstudio',
];

it.each(API_KEY_PROVIDERS)(
    'ships the %s API-key container, input, and label in research.html',
    (key) => {
        expect(RESEARCH_TEMPLATE).toContain(`id="${key}_api_key_container"`);
        expect(RESEARCH_TEMPLATE).toContain(`id="${key}_api_key"`);
        expect(RESEARCH_TEMPLATE).toContain(`for="${key}_api_key"`);
        expect(RESEARCH_TEMPLATE).toContain(`name="${key}_api_key"`);
    },
);

for (const [key, label] of PROVIDER_DISPLAY_NAMES) {
    it(`labels the ${key} provider on the context-overflow dashboard`, () => {
        expect(CONTEXT_OVERFLOW_COMPONENT)
            .toContain(`'${key}': '${label}'`);
    });
}

it.each(API_KEY_PROVIDERS)(
    'persists the %s API key through a research.js change listener',
    (key) => {
        // Quote-agnostic: matches saveSearchSetting('llm.<key>.api_key'
        // and saveSearchSetting("llm.<key>.api_key".
        const listenerPattern = new RegExp(
            `saveSearchSetting\\(['"]llm\\.${key}\\.api_key`,
        );
        expect(RESEARCH_COMPONENT).toMatch(listenerPattern);
    },
);

// The success toast masks a setting's value when its key names a secret.
// The PUT response carries no ui_element, so research.js classifies by key
// name; pin that classifier against the real source so it cannot narrow
// back to an ``api_key``-only suffix check.
const SECRET_KEY_RE_SOURCE = RESEARCH_COMPONENT.match(
    /const SECRET_SETTING_KEY_RE = \/(.+)\/([a-z]*);/,
);

it('declares the secret-setting classifier used by the toast', () => {
    expect(SECRET_KEY_RE_SOURCE).not.toBeNull();
    // The toast's label/detail must key off it, not the api_key suffix.
    expect(RESEARCH_COMPONENT).toMatch(
        /const isSecret = SECRET_SETTING_KEY_RE\.test\(settingKey\);/,
    );
    expect(RESEARCH_COMPONENT).toMatch(
        /const hideValue = isSecret \|\| URL_SETTING_KEY_RE\.test\(settingKey\);/,
    );
    expect(RESEARCH_COMPONENT).toMatch(
        /const detail = hideValue \? "updated" : `updated to: \$\{value\}`;/,
    );
});

describe('secret-setting classifier', () => {
    const secretKeyRe = SECRET_KEY_RE_SOURCE
        ? new RegExp(SECRET_KEY_RE_SOURCE[1], SECRET_KEY_RE_SOURCE[2])
        : null;

    it.each([
        ...API_KEY_PROVIDERS.map(key => `llm.${key}.api_key`),
        'llm.example.API_KEY',
        'search.engine.web.example.access_token',
        'notifications.service_token',
        'llm.example.client_secret',
        'app.admin_password',
    ])('masks %s', (settingKey) => {
        expect(secretKeyRe.test(settingKey)).toBe(true);
    });

    it.each([
        'llm.model',
        'llm.provider',
        'search.tool',
        'search.iterations',
        'llm.lmstudio.url',
        'llm.local_context_window_size',
        'embeddings.require_local',
    ])('shows the value for %s', (settingKey) => {
        expect(secretKeyRe.test(settingKey)).toBe(false);
    });
});