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.2 KB
Raw Blame
/**
 * Live consumer contract for the research form's favorite-star callback.
 * It executes the shipped callback body, including server-authoritative
 * regrouping and sorting, instead of only checking the URL constant.
 */

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

import '@js/config/urls.js';

const RESEARCH_SOURCE_PATH = resolve(
    __dirname,
    '../../../src/local_deep_research/web/static/js/components/research.js',
);

function extractFunction(source, name) {
    const signature = new RegExp(`function\\s+${name}\\s*\\(`);
    const match = signature.exec(source);
    if (!match) throw new Error(`Function ${name} not found in research.js`);

    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 initialOptions() {
    return [
        {
            value: 'searxng',
            label: 'Alpha Search',
            is_favorite: false,
            group_label: 'General',
            group_order: 4,
            base_group_label: 'General',
            base_group_order: 4,
        },
        {
            value: 'brave',
            label: 'Zulu Search',
            is_favorite: false,
            group_label: 'Privacy',
            group_order: 3,
            base_group_label: 'Privacy',
            base_group_order: 3,
        },
    ];
}

function compileHarness(options = initialOptions()) {
    const callback = extractFunction(
        readFileSync(RESEARCH_SOURCE_PATH, 'utf8'),
        'handleSearchEngineFavoriteToggle',
    );
    const invalidateCacheKey = vi.fn();
    const factory = new Function( // eslint-disable-line no-new-func
        'URLS',
        'SafeLogger',
        'invalidateCacheKey',
        'initialSearchEngineOptions',
        `
            const CACHE_KEYS = {
                SEARCH_ENGINES: 'deepResearch.searchEngines',
            };
            let searchEngineOptions = initialSearchEngineOptions;
            ${callback}
            return {
                toggle: handleSearchEngineFavoriteToggle,
                getOptions: () => searchEngineOptions,
            };
        `,
    );
    return {
        runtime: factory(
            window.URLS,
            { log: vi.fn(), error: vi.fn() },
            invalidateCacheKey,
            options,
        ),
        invalidateCacheKey,
    };
}

beforeEach(() => {
    window.api = { getCsrfToken: vi.fn(() => 'favorite-csrf') };
    window.ui = { showMessage: vi.fn() };
});

afterEach(() => {
    vi.restoreAllMocks();
    vi.unstubAllGlobals();
    delete window.api;
    delete window.ui;
});

it('posts the engine id and moves server-confirmed favorites to the first band', async () => {
    const fetchMock = vi.fn().mockResolvedValue({
        ok: true,
        status: 200,
        json: vi.fn().mockResolvedValue({
            favorites: ['brave'],
            is_favorite: true,
        }),
    });
    vi.stubGlobal('fetch', fetchMock);
    const { runtime, invalidateCacheKey } = compileHarness();

    runtime.toggle('brave', null, true);

    await vi.waitFor(() => {
        expect(invalidateCacheKey).toHaveBeenCalledWith(
            'deepResearch.searchEngines',
        );
    });
    expect(fetchMock).toHaveBeenCalledWith(
        '/settings/api/search-favorites/toggle',
        {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
                'X-CSRFToken': 'favorite-csrf',
            },
            body: JSON.stringify({ engine_id: 'brave' }),
        },
    );
    expect(runtime.getOptions().map(option => option.value)).toEqual([
        'brave',
        'searxng',
    ]);
    expect(runtime.getOptions()[0]).toMatchObject({
        is_favorite: true,
        group_label: 'Favorites',
        group_order: 0,
    });
    expect(runtime.getOptions()[1]).toMatchObject({
        is_favorite: false,
        group_label: 'General',
        group_order: 4,
    });
    expect(window.ui.showMessage).toHaveBeenCalledWith(
        'Search engine added to favorites',
        'success',
        2000,
    );
});

it('does not mutate or report success when FastAPI rejects the toggle', async () => {
    const options = initialOptions();
    const fetchMock = vi.fn().mockResolvedValue({
        ok: false,
        status: 403,
        json: vi.fn().mockResolvedValue({ detail: 'favorite not allowed' }),
    });
    vi.stubGlobal('fetch', fetchMock);
    const { runtime, invalidateCacheKey } = compileHarness(options);

    runtime.toggle('brave', null, true);

    await vi.waitFor(() => {
        expect(window.ui.showMessage).toHaveBeenCalledWith(
            'Error updating favorites: favorite not allowed',
            'error',
            3000,
        );
    });
    expect(runtime.getOptions()).toEqual(options);
    expect(invalidateCacheKey).not.toHaveBeenCalled();
    expect(window.ui.showMessage).not.toHaveBeenCalledWith(
        expect.stringContaining('added to'),
        'success',
        2000,
    );
});