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.

7.2 KB
Raw Blame
import { readFileSync } from 'node:fs';
import { resolve } from 'node:path';
import { runInNewContext } from 'node:vm';
import { describe, expect, it, vi } from 'vitest';

const source = readFileSync(resolve(
    __dirname, '../ui_tests/test_search_engine_dropdown_scope_ci.js',
), 'utf8');
const settingPath = '/settings/api/search.tool';
const fixture = { id: 'fixture' };

function deferred() {
    let resolvePromise;
    const promise = new Promise(finish => { resolvePromise = finish; });
    return { promise, resolve: resolvePromise };
}

function setup({ initial = 'collection_fixture', writeGate, writeFails = false, persist = true } = {}) {
    let savedPrimary = initial;
    const events = [];
    const fetch = vi.fn(async (path, options) => {
        expect(path).toBe(settingPath);
        expect(options.credentials).toBe('same-origin');
        const method = options.method || 'GET';
        events.push(method);
        if (method === 'PUT') {
            expect(options.headers['X-CSRFToken']).toBe('test-csrf');
            if (writeGate) await writeGate.promise;
            if (writeFails) return { ok: false, status: 503 };
            if (persist) savedPrimary = JSON.parse(options.body).value;
        }
        return { ok: true, status: 200, json: async () => ({ value: savedPrimary }) };
    });
    const deleteCollection = vi.fn(async () => { events.push('DELETE'); });
    const require = id => {
        if (id === './test_lib') return {};
        if (id === './test_lib/fixtures') return { deleteCollection };
        throw new Error(`Unexpected module: ${id}`);
    };
    require.main = {};
    const module = { exports: {} };
    runInNewContext(source, {
        module,
        require,
        fetch,
        URL,
        document: { querySelector: () => ({ content: 'test-csrf' }) },
    });
    const page = { evaluate: vi.fn((callback, ...args) => callback(...args)) };
    return {
        ...module.exports, page, fetch, deleteCollection, events,
        savedPrimary: () => savedPrimary,
    };
}

describe('search-engine scope suite cleanup', () => {
    it('captures the persisted primary instead of a reconciled form value', async () => {
        const state = setup({ initial: 'collection_original' });
        expect(await state.readSavedPrimaryEngine(state.page)).toBe('collection_original');
        expect(state.fetch).toHaveBeenCalledWith(settingPath, { credentials: 'same-origin' });
    });

    it.each(['searxng', 'collection_original', ''])(
        'restores the original primary %j before deleting the fixture', async original => {
            const state = setup();
            await state.restorePrimaryAndDeleteCollection(state.page, original, fixture);
            expect(state.savedPrimary()).toBe(original);
            expect(state.events).toEqual(['GET', 'PUT', 'GET', 'DELETE']);
            expect(state.deleteCollection).toHaveBeenCalledWith(state.page, fixture.id);
        },
    );

    it('waits for restoration to finish before deleting the collection', async () => {
        const writeGate = deferred();
        const state = setup({ writeGate });
        const cleanup = state.restorePrimaryAndDeleteCollection(state.page, 'searxng', fixture);
        await vi.waitFor(() => expect(state.events).toContain('PUT'));
        expect(state.deleteCollection).not.toHaveBeenCalled();
        expect(state.savedPrimary()).toBe('collection_fixture');

        writeGate.resolve();
        await cleanup;
        expect(state.savedPrimary()).toBe('searxng');
        expect(state.events.at(-1)).toBe('DELETE');
    });

    it('keeps the collection if restoring the setting fails', async () => {
        const state = setup({ writeFails: true });
        await expect(state.restorePrimaryAndDeleteCollection(state.page, 'searxng', fixture))
            .rejects.toThrow('Could not restore primary engine (HTTP 503)');
        expect(state.deleteCollection).not.toHaveBeenCalled();
        expect(state.savedPrimary()).toBe('collection_fixture');
    });

    it('verifies persistence before deleting the collection', async () => {
        const state = setup({ persist: false });
        await expect(state.restorePrimaryAndDeleteCollection(state.page, 'searxng', fixture))
            .rejects.toThrow('Original primary engine was not restored');
        expect(state.deleteCollection).not.toHaveBeenCalled();
    });

    it('does not rewrite an unchanged primary', async () => {
        const state = setup({ initial: 'searxng' });
        await state.restorePrimaryAndDeleteCollection(state.page, 'searxng', fixture);
        expect(state.events).toEqual(['GET', 'DELETE']);
    });

    it('leaves the fixture intact if the original primary was not captured', async () => {
        const state = setup();
        await expect(state.restorePrimaryAndDeleteCollection(state.page, undefined, fixture))
            .rejects.toThrow('Original primary engine was not captured');
        expect(state.fetch).not.toHaveBeenCalled();
        expect(state.deleteCollection).not.toHaveBeenCalled();
    });

    it('does not change settings after setup fails before a fixture exists', async () => {
        const state = setup();
        await state.restorePrimaryAndDeleteCollection(state.page, undefined, null);
        expect(state.fetch).not.toHaveBeenCalled();
        expect(state.deleteCollection).not.toHaveBeenCalled();
    });

    it('waits for the selected fixture save before allowing restoration', async () => {
        const state = setup();
        const saved = deferred();
        const events = [];
        let matches;
        const page = {
            $: vi.fn(async () => ({ click: async () => { events.push('click'); } })),
            waitForResponse: vi.fn(predicate => {
                matches = predicate;
                events.push('watch');
                return saved.promise;
            }),
        };
        const selection = state.selectPrimaryEngineAndWaitForSave(page, 'collection_fixture')
            .then(() => { events.push('done'); });
        await vi.waitFor(() => expect(events).toEqual(['watch', 'click']));
        const response = (method, path, body) => ({
            url: () => `http://localhost${path}`,
            request: () => ({ method: () => method, postData: () => body }),
        });
        expect(matches(response('PUT', settingPath, '{"value":"collection_fixture"}'))).toBe(true);
        expect(matches(response('GET', settingPath, '{"value":"collection_fixture"}'))).toBe(false);
        expect(matches(response('PUT', settingPath, '{"value":"another_engine"}'))).toBe(false);
        expect(matches(response('PUT', '/settings/api/search.search_strategy', '{"value":"collection_fixture"}'))).toBe(false);
        expect(matches(response('PUT', settingPath, 'invalid-json'))).toBe(false);
        saved.resolve({ ok: () => true });
        await selection;
        expect(events).toEqual(['watch', 'click', 'done']);
    });

    it('reports a rejected selection save', async () => {
        const state = setup();
        const page = {
            $: vi.fn(async () => ({ click: async () => {} })),
            waitForResponse: vi.fn(async () => ({ ok: () => false })),
        };
        await expect(state.selectPrimaryEngineAndWaitForSave(page, 'collection_fixture'))
            .rejects.toThrow('Primary-engine selection was not saved');
    });
});