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.

6.8 KB
Raw Blame
/**
 * Tests for news subscription form egress-scope search engine filtering.
 *
 * Exercises the loadSearchEngines URL construction and egress policy mapping logic
 * used by news-subscription-form.html (Issue #5204).
 */

import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';

describe('news-subscription-form egress search engine loading', () => {
    let originalFetch;

    beforeEach(() => {
        originalFetch = globalThis.fetch;
        document.body.innerHTML = `
            <input type="hidden" id="subscription-search-engine" value="searxng" />
        `;
    });

    afterEach(() => {
        globalThis.fetch = originalFetch;
        document.body.innerHTML = '';
    });

    async function runLoadSearchEngines(scope, primary, engineOptionsResponse) {
        let requestedUrl = null;
        globalThis.fetch = vi.fn((url) => {
            requestedUrl = url;
            return Promise.resolve({
                ok: true,
                json: () => Promise.resolve({ engine_options: engineOptionsResponse }),
            });
        });

        let searchEngineOptions = [];
        const windowUpdateOptions = vi.fn();
        window.updateDropdownOptions = windowUpdateOptions;

        // Function logic matching news-subscription-form.html loadSearchEngines()
        const filterScope = (scope && scope !== 'unprotected') ? scope : null;
        let url = '/settings/api/available-search-engines';
        if (filterScope) {
            const parts = [`egress_scope=${encodeURIComponent(filterScope)}`];
            if (primary) parts.push(`primary=${encodeURIComponent(primary)}`);
            url += '?' + parts.join('&');
        }

        const response = await fetch(url, { credentials: 'same-origin' });
        const data = await response.json();

        if (data.engine_options) {
            searchEngineOptions = data.engine_options.map((engine) => {
                const egress = engine.egress;
                const isDenied = !!egress && egress.allowed === false;
                const display = engine.display_name || engine.label || engine.value || 'engine';
                let disabledReason = null;
                if (isDenied && egress) {
                    switch (egress.reason) {
                        case 'scope_mismatch_private_only':
                            disabledReason = 'Blocked: not a local source under Private only';
                            break;
                        case 'scope_mismatch_public_only':
                            disabledReason = 'Blocked: local source under Public only';
                            break;
                        case 'strict_not_primary':
                            disabledReason = 'Blocked: only the primary search engine is allowed under Strict';
                            break;
                        case 'unclassified':
                        case 'engine_unknown':
                            disabledReason = `Blocked: ${display} is not recognized by the egress policy`;
                            break;
                        case 'engine_denied':
                            disabledReason = `Blocked: ${display} is denied by the egress policy`;
                            break;
                        default:
                            disabledReason = `Blocked by egress policy (${egress.reason})`;
                            break;
                    }
                }
                return {
                    ...engine,
                    disabled: isDenied,
                    disabled_reason: disabledReason,
                };
            });

            if (window.updateDropdownOptions) {
                const searchEngineInput = document.getElementById('subscription-search-engine');
                window.updateDropdownOptions(searchEngineInput, searchEngineOptions);
            }
        }

        return { requestedUrl, searchEngineOptions, windowUpdateOptions };
    }

    it('builds egress_scope and primary query params for non-unprotected scopes', async () => {
        const { requestedUrl } = await runLoadSearchEngines('private_only', 'searxng', []);
        expect(requestedUrl).toBe('/settings/api/available-search-engines?egress_scope=private_only&primary=searxng');
    });

    it('omits query params for unprotected scope', async () => {
        const { requestedUrl } = await runLoadSearchEngines('unprotected', 'searxng', []);
        expect(requestedUrl).toBe('/settings/api/available-search-engines');
    });

    it('correctly maps egress policy restrictions onto dropdown options', async () => {
        const mockRawOptions = [
            { value: 'library', label: 'Library', egress: { allowed: true } },
            { value: 'google', label: 'Google Search', egress: { allowed: false, reason: 'scope_mismatch_private_only' } },
            { value: 'arxiv', label: 'ArXiv', egress: { allowed: false, reason: 'strict_not_primary' } },
            { value: 'custom_pub', label: 'Custom Pub', egress: { allowed: false, reason: 'scope_mismatch_public_only' } },
            { value: 'unknown_eng', label: 'Unknown Engine', egress: { allowed: false, reason: 'unclassified' } },
            { value: 'denied_eng', label: 'Denied Engine', egress: { allowed: false, reason: 'engine_denied' } },
            { value: 'other_blocked', label: 'Other Engine', egress: { allowed: false, reason: 'some_other_reason' } },
        ];

        const { searchEngineOptions, windowUpdateOptions } = await runLoadSearchEngines('private_only', 'library', mockRawOptions);

        expect(searchEngineOptions[0].disabled).toBe(false);
        expect(searchEngineOptions[0].disabled_reason).toBeNull();

        expect(searchEngineOptions[1].disabled).toBe(true);
        expect(searchEngineOptions[1].disabled_reason).toBe('Blocked: not a local source under Private only');

        expect(searchEngineOptions[2].disabled).toBe(true);
        expect(searchEngineOptions[2].disabled_reason).toBe('Blocked: only the primary search engine is allowed under Strict');

        expect(searchEngineOptions[3].disabled).toBe(true);
        expect(searchEngineOptions[3].disabled_reason).toBe('Blocked: local source under Public only');

        expect(searchEngineOptions[4].disabled).toBe(true);
        expect(searchEngineOptions[4].disabled_reason).toBe('Blocked: Unknown Engine is not recognized by the egress policy');

        expect(searchEngineOptions[5].disabled).toBe(true);
        expect(searchEngineOptions[5].disabled_reason).toBe('Blocked: Denied Engine is denied by the egress policy');

        expect(searchEngineOptions[6].disabled).toBe(true);
        expect(searchEngineOptions[6].disabled_reason).toBe('Blocked by egress policy (some_other_reason)');

        expect(windowUpdateOptions).toHaveBeenCalledWith(
            document.getElementById('subscription-search-engine'),
            searchEngineOptions
        );
    });
});