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.6 KB
Raw Blame
/** Run both shipped settings picker initializers with the real dropdown. */
import { readFileSync } from 'node:fs';
import { resolve } from 'node:path';
import '@js/security/xss-protection.js';
import '@js/components/custom_dropdown.js';

const source = readFileSync(resolve(
    __dirname,
    '../../../src/local_deep_research/web/static/js/components/settings.js',
), 'utf8');
const initializers = ['initializeSearchEngineDropdowns', 'setupCustomDropdowns'];
const options = [
    { value: 'wikipedia', label: 'Wikipedia' },
    { value: 'duckduckgo', label: 'DuckDuckGo' },
    { value: 'local_all', label: 'Local All' },
];

function initialize(name, saved, availableOptions) {
    const start = source.indexOf(`    function ${name}(`);
    const end = source.indexOf('\n    }\n', start);
    if (start < 0 || end < 0) throw new Error(`Cannot locate ${name} in settings.js`);
    const body = source.slice(start, end + '\n    }'.length);
    const settings = [{ key: 'search.tool', value: saved }];
    const save = vi.fn();
    // The IIFE does not export these initializers. Execute their literal
    // source with the same closure values and the actual dropdown component.
    // eslint-disable-next-line no-new-func
    new Function(
        'searchEngineOptions', 'allSettings', 'handleInputChange', 'initAutoSaveHandlers',
        `${body}\n${name}();`,
    )(availableOptions, settings, save, () => {});
    return { save, settings };
}

describe.each(initializers)('%s — Wayback saved selection', (name) => {
    let input, hidden, list, controls, optionSources;

    beforeEach(() => {
        window.searchEngineDropdownInitialized = false;
        input = document.createElement('input');
        input.id = 'search.tool';
        input.className = 'ldr-custom-dropdown-input';
        hidden = document.createElement('input');
        hidden.type = 'hidden';
        hidden.id = 'search.tool_hidden';
        list = document.createElement('div');
        list.id = 'setting-search-tool-dropdown-list';
        list.className = 'ldr-custom-dropdown-list';
        const wrapper = document.createElement('div');
        wrapper.className = 'ldr-custom-dropdown';
        wrapper.append(input, hidden, list);
        document.body.append(wrapper);

        controls = [];
        optionSources = [];
        const setup = window.setupCustomDropdown;
        vi.spyOn(window, 'setupCustomDropdown').mockImplementation((...args) => {
            optionSources.push(args[2]);
            const control = setup(...args);
            controls.push(control);
            return control;
        });
    });

    afterEach(() => {
        controls.forEach(control => control.destroy());
        input.closest('.ldr-custom-dropdown')?.remove();
        list.remove();
        delete window.searchEngineDropdownInitialized;
        vi.restoreAllMocks();
    });

    it.each([['runtime options', options], ['fallback options', []]])(
        'preserves saved Wayback without saving or offering it in %s', (_label, available) => {
            const changed = vi.fn();
            hidden.addEventListener('change', changed);
            const { save, settings } = initialize(name, 'wayback', available);

            expect(hidden.value).toBe('wayback');
            expect(input.value).toContain('choose a configured search provider for topics');
            expect(optionSources[0]().some(option => option.value === 'wayback')).toBe(false);
            expect(changed).not.toHaveBeenCalled();
            expect(save).not.toHaveBeenCalled();
            expect(settings[0].value).toBe('wayback');
        },
    );

    it('retains unrelated dynamic provider options and normal saved selections', () => {
        const changed = vi.fn();
        hidden.addEventListener('change', changed);
        initialize(name, 'wikipedia', options);

        expect(optionSources[0]()).toEqual(options);
        expect(input.value).toBe('Wikipedia');
        expect(hidden.value).toBe('wikipedia');
        expect(changed).not.toHaveBeenCalled();
    });

    it('lets the user explicitly replace saved Wayback with a configured provider', () => {
        initialize(name, 'wayback', options);
        const changed = vi.fn();
        hidden.addEventListener('change', changed);
        input.value = 'Wiki';
        input.dispatchEvent(new Event('input'));
        expect(hidden.value).toBe('wayback');
        expect(changed).not.toHaveBeenCalled();
        list.querySelector('[data-value="wikipedia"]').click();

        expect(hidden.value).toBe('wikipedia');
        expect(input.value).toBe('Wikipedia');
        expect(changed).toHaveBeenCalled();
        expect(list.querySelector('[data-value="wayback"]')).toBeNull();
    });
});