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.

21.9 KB
Raw Blame
/**
 * Runtime contracts for the news subscription form's immediate-run workflow.
 *
 * The checked-in inline functions are executed to pin the FastAPI requests,
 * normalized numeric configuration, storage handoff, and create-then-run
 * sequencing used by the browser.
 */

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

const TEMPLATE_PATH = resolve(
    __dirname,
    '../../../src/local_deep_research/web/templates/pages/news-subscription-form.html',
);

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

    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 renderCreateModeSubscriptionSubmit(source) {
    const rendered = source
        .replace(
            /\{%\s*if subscription\s*%\}[\s\S]*?\{%\s*else\s*%\}([\s\S]*?)\{%\s*endif\s*%\}/g,
            '$1',
        )
        .replace(
            /\{\{\s*"updated"\s+if\s+subscription\s+else\s+"created"\s*\}\}/g,
            'created',
        );

    if (/\{[{%]/.test(rendered)) {
        throw new Error('Unhandled Jinja remains in create-mode submit');
    }
    return rendered;
}

function compileSubscriptionRunner(dependencies) {
    const template = readFileSync(TEMPLATE_PATH, 'utf8');
    const functions = [
        'isValidResearchId',
        'scheduleSubscriptionRedirect',
        'handleSubscriptionSubmit',
        'resolveSavedSubscriptionModel',
        'handleTestRun',
        'handleCreateAndRun',
    ]
        .map(name => {
            const source = extractFunction(template, name);
            return name === 'handleSubscriptionSubmit'
                ? renderCreateModeSubscriptionSubmit(source)
                : source;
        })
        .join('\n');
    const dependencyNames = Object.keys(dependencies);
    // The extracted source is repository-owned production code from the
    // template above, not user-controlled input.
    const factory = new Function( // eslint-disable-line no-new-func
        ...dependencyNames,
        `let subscriptionActionInFlight = false;\nlet subscriptionNavigationPending = false;\n${functions}\nreturn { handleSubscriptionSubmit, handleTestRun, handleCreateAndRun };`,
    );
    return factory(...Object.values(dependencies));
}

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

function renderSubscriptionForm() {
    document.head.innerHTML = '<meta name="csrf-token" content="csrf-news">';
    document.body.innerHTML = `
        <input id="subscription-query" value="FastAPI migration status">
        <input id="subscription-name" value="Migration watch">
        <input id="subscription-interval" value="45">
        <select id="subscription-folder">
            <option value="folder-2" selected>Migration</option>
        </select>
        <input id="subscription-active" type="checkbox" checked>
        <input id="subscription-model_hidden" value="gpt-4.1-mini">
        <select id="subscription-provider">
            <option value="openai" selected>OpenAI</option>
        </select>
        <input id="subscription-custom-endpoint" value="">
        <input id="subscription-search-engine_hidden" value="searxng">
        <input id="subscription-search-engine" value="Visible fallback">
        <input id="subscription-iterations" value="3">
        <input id="subscription-questions" value="4">
        <select id="subscription-strategy">
            <option value="source-based" selected>Source based</option>
        </select>
    `;
}

function createDependencies() {
    return {
        showAlert: vi.fn(),
        getCSRFToken: vi.fn(() => 'csrf-news'),
    };
}

beforeEach(() => {
    vi.useFakeTimers();
    renderSubscriptionForm();
    localStorage.clear();
    sessionStorage.clear();
    localStorage.setItem('userId', 'user-7');
    window.sourceResearchId = 'source-12';
    vi.spyOn(console, 'log').mockImplementation(() => {});
});

afterEach(() => {
    vi.clearAllTimers();
    vi.useRealTimers();
    vi.restoreAllMocks();
    vi.unstubAllGlobals();
    delete window.sourceResearchId;
    localStorage.clear();
    sessionStorage.clear();
    document.head.replaceChildren();
    document.body.replaceChildren();
});

it('starts a configured test run and hands its ID to the news page', async () => {
    const fetchMock = vi.fn().mockResolvedValue({
        ok: true,
        status: 200,
        json: vi.fn().mockResolvedValue({
            status: 'success',
            research_id: 'research-3299',
        }),
    });
    vi.stubGlobal('fetch', fetchMock);
    const dependencies = createDependencies();
    const runner = compileSubscriptionRunner(dependencies);
    const event = {
        preventDefault: vi.fn(),
        stopPropagation: vi.fn(),
    };

    await runner.handleTestRun(event);

    expect(event.preventDefault).toHaveBeenCalledOnce();
    expect(event.stopPropagation).toHaveBeenCalledOnce();
    expect(fetchMock).toHaveBeenCalledWith('/api/start_research', {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
            'X-CSRFToken': 'csrf-news',
        },
        body: JSON.stringify({
            query: 'FastAPI migration status',
            mode: 'quick',
            strategy: 'source-based',
            metadata: {
                is_news_search: true,
                search_type: 'news_analysis',
                display_in: 'news_feed',
                triggered_by: 'subscription_test_run',
            },
            model_provider: 'openai',
            model: 'gpt-4.1-mini',
            search_engine: 'searxng',
            iterations: 3,
            questions_per_iteration: 4,
        }),
    });
    expect(sessionStorage.getItem('activeTestRunResearchId'))
        .toBe('research-3299');
    expect(sessionStorage.getItem('activeTestRunQuery'))
        .toBe('FastAPI migration status');
    expect(dependencies.showAlert).toHaveBeenCalledWith(
        'Research started successfully! Redirecting to news page...',
        'success',
    );
});

it('accepts a queued FastAPI start with a finite numeric research ID', async () => {
    const fetchMock = vi.fn().mockResolvedValue({
        ok: true,
        status: 202,
        json: vi.fn().mockResolvedValue({
            status: 'queued',
            research_id: 3299,
        }),
    });
    vi.stubGlobal('fetch', fetchMock);
    const dependencies = createDependencies();

    await compileSubscriptionRunner(dependencies).handleTestRun();

    expect(fetchMock).toHaveBeenCalledOnce();
    expect(sessionStorage.getItem('activeTestRunResearchId'))
        .toBe('3299');
    expect(sessionStorage.getItem('activeTestRunQuery'))
        .toBe('FastAPI migration status');
    expect(dependencies.showAlert).toHaveBeenCalledWith(
        'Research started successfully! Redirecting to news page...',
        'success',
    );
});

it.each([
    ['whitespace', '   '],
    ['an object', { value: 'research-object' }],
    ['a non-finite number', Number.POSITIVE_INFINITY],
])('rejects %s as a research ID and releases ownership', async (
    _label,
    invalidResearchId,
) => {
    const fetchMock = vi.fn()
        .mockResolvedValueOnce({
            ok: true,
            status: 202,
            json: vi.fn().mockResolvedValue({
                status: 'queued',
                research_id: invalidResearchId,
            }),
        })
        .mockResolvedValueOnce({
            ok: true,
            status: 202,
            json: vi.fn().mockResolvedValue({
                status: 'queued',
                research_id: 'research-valid-retry',
            }),
        });
    vi.stubGlobal('fetch', fetchMock);
    const dependencies = createDependencies();
    const runner = compileSubscriptionRunner(dependencies);

    await runner.handleTestRun();

    expect(sessionStorage.getItem('activeTestRunResearchId')).toBeNull();
    expect(vi.getTimerCount()).toBe(0);
    expect(dependencies.showAlert).toHaveBeenCalledWith(
        'Unexpected response from server',
        'error',
    );

    await runner.handleTestRun();

    expect(fetchMock).toHaveBeenCalledTimes(2);
    expect(sessionStorage.getItem('activeTestRunResearchId'))
        .toBe('research-valid-retry');
});

it('executes the create-mode submit and owns its redirect window', async () => {
    const fetchMock = vi.fn()
        .mockResolvedValueOnce({
            ok: true,
            json: vi.fn().mockResolvedValue({ id: 'subscription-submit' }),
        })
        .mockRejectedValue(new Error('unexpected duplicate request'));
    vi.stubGlobal('fetch', fetchMock);
    const dependencies = createDependencies();
    const runner = compileSubscriptionRunner(dependencies);
    const submitEvent = () => ({ preventDefault: vi.fn() });

    await runner.handleSubscriptionSubmit(submitEvent());
    await runner.handleSubscriptionSubmit(submitEvent());
    await vi.advanceTimersByTimeAsync(1499);
    await runner.handleSubscriptionSubmit(submitEvent());

    expect(fetchMock).toHaveBeenCalledOnce();
    expect(fetchMock).toHaveBeenCalledWith('/news/api/subscribe', {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
            'X-CSRFToken': 'csrf-news',
        },
        body: expect.any(String),
    });
    expect(vi.getTimerCount()).toBe(1);
    expect(dependencies.showAlert).toHaveBeenCalledWith(
        'Subscription created successfully!',
        'success',
    );
});

it('releases create-mode submit ownership after a failed response', async () => {
    const fetchMock = vi.fn()
        .mockResolvedValueOnce({
            ok: false,
            json: vi.fn().mockResolvedValue({ error: 'duplicate name' }),
        })
        .mockResolvedValueOnce({
            ok: true,
            json: vi.fn().mockResolvedValue({ id: 'subscription-retry' }),
        });
    vi.stubGlobal('fetch', fetchMock);
    const dependencies = createDependencies();
    const runner = compileSubscriptionRunner(dependencies);

    await runner.handleSubscriptionSubmit({ preventDefault: vi.fn() });
    await runner.handleSubscriptionSubmit({ preventDefault: vi.fn() });

    expect(fetchMock).toHaveBeenCalledTimes(2);
    expect(dependencies.showAlert).toHaveBeenCalledWith(
        'Error: duplicate name',
        'error',
    );
});

it('allows only one test-run request while the owned start is pending', async () => {
    const pendingStart = deferred();
    const fetchMock = vi.fn()
        .mockImplementationOnce(() => pendingStart.promise)
        .mockRejectedValue(new Error('unexpected duplicate request'));
    vi.stubGlobal('fetch', fetchMock);
    const runner = compileSubscriptionRunner(createDependencies());

    const firstRun = runner.handleTestRun();
    const duplicateRun = runner.handleTestRun();

    expect(fetchMock).toHaveBeenCalledOnce();
    await duplicateRun;

    pendingStart.resolve({
        ok: true,
        status: 200,
        json: vi.fn().mockResolvedValue({
            status: 'success',
            research_id: 'research-first',
        }),
    });
    await firstRun;

    expect(vi.getTimerCount()).toBe(1);
    await runner.handleTestRun();
    await vi.advanceTimersByTimeAsync(1499);
    await runner.handleTestRun();

    expect(fetchMock).toHaveBeenCalledOnce();
    expect(sessionStorage.getItem('activeTestRunResearchId'))
        .toBe('research-first');
});

it('releases test-run ownership after a failed start response', async () => {
    const fetchMock = vi.fn()
        .mockResolvedValueOnce({
            ok: false,
            status: 503,
            json: vi.fn().mockResolvedValue({ error: 'queue unavailable' }),
        })
        .mockResolvedValueOnce({
            ok: true,
            status: 202,
            json: vi.fn().mockResolvedValue({
                status: 'queued',
                research_id: 'research-retry',
            }),
        });
    vi.stubGlobal('fetch', fetchMock);
    const dependencies = createDependencies();
    const runner = compileSubscriptionRunner(dependencies);

    await runner.handleTestRun();
    await runner.handleTestRun();

    expect(fetchMock).toHaveBeenCalledTimes(2);
    expect(dependencies.showAlert).toHaveBeenCalledWith(
        'queue unavailable',
        'error',
    );
    expect(sessionStorage.getItem('activeTestRunResearchId'))
        .toBe('research-retry');
});

it('requires the dropdown-backed search engine before starting research', async () => {
    document.getElementById('subscription-search-engine_hidden').value = '';
    document.getElementById('subscription-search-engine').value = '';
    const fetchMock = vi.fn();
    vi.stubGlobal('fetch', fetchMock);
    const dependencies = createDependencies();

    await compileSubscriptionRunner(dependencies).handleTestRun();

    expect(fetchMock).not.toHaveBeenCalled();
    expect(dependencies.showAlert).toHaveBeenCalledWith(
        'Please select a search engine',
        'warning',
    );
});

it('creates the subscription before starting its immediate research run', async () => {
    const fetchMock = vi.fn()
        .mockResolvedValueOnce({
            ok: true,
            json: vi.fn().mockResolvedValue({ id: 'subscription-5' }),
        })
        .mockResolvedValueOnce({
            ok: true,
            status: 200,
            json: vi.fn().mockResolvedValue({
                status: 'success',
                research_id: 'research-5',
            }),
        });
    vi.stubGlobal('fetch', fetchMock);
    const dependencies = createDependencies();

    await compileSubscriptionRunner(dependencies).handleCreateAndRun();

    expect(fetchMock).toHaveBeenCalledTimes(2);
    expect(fetchMock.mock.calls[0][0]).toBe('/news/api/subscribe');
    expect(fetchMock.mock.calls[0][1]).toEqual({
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
            'X-CSRFToken': 'csrf-news',
        },
        body: JSON.stringify({
            user_id: 'user-7',
            query: 'FastAPI migration status',
            name: 'Migration watch',
            subscription_type: 'search',
            refresh_minutes: 45,
            folder_id: 'folder-2',
            is_active: true,
            model_provider: 'openai',
            model: 'gpt-4.1-mini',
            custom_endpoint: null,
            search_engine: 'searxng',
            search_iterations: 3,
            questions_per_iteration: 4,
            search_strategy: 'source-based',
            source_id: 'source-12',
        }),
    });
    expect(fetchMock.mock.calls[1][0]).toBe('/api/start_research');
    expect(sessionStorage.getItem('activeTestRunResearchId'))
        .toBe('research-5');
});

it('allows only one create-and-run workflow while creation is pending', async () => {
    const pendingCreate = deferred();
    const fetchMock = vi.fn()
        .mockImplementationOnce(() => pendingCreate.promise)
        .mockResolvedValueOnce({
            ok: true,
            status: 200,
            json: vi.fn().mockResolvedValue({
                status: 'success',
                research_id: 'research-single-owner',
            }),
        })
        .mockRejectedValue(new Error('unexpected duplicate request'));
    vi.stubGlobal('fetch', fetchMock);
    const runner = compileSubscriptionRunner(createDependencies());

    const firstWorkflow = runner.handleCreateAndRun();
    const duplicateWorkflow = runner.handleCreateAndRun();

    expect(fetchMock).toHaveBeenCalledOnce();
    await duplicateWorkflow;

    pendingCreate.resolve({
        ok: true,
        json: vi.fn().mockResolvedValue({ id: 'subscription-single-owner' }),
    });
    await firstWorkflow;

    expect(vi.getTimerCount()).toBe(1);
    await runner.handleCreateAndRun();
    await vi.advanceTimersByTimeAsync(1499);
    await runner.handleCreateAndRun();

    expect(fetchMock).toHaveBeenCalledTimes(2);
    expect(fetchMock.mock.calls.map(([url]) => url)).toEqual([
        '/news/api/subscribe',
        '/api/start_research',
    ]);
    expect(sessionStorage.getItem('activeTestRunResearchId'))
        .toBe('research-single-owner');
});

it('releases create-and-run ownership when subscription creation fails', async () => {
    const fetchMock = vi.fn()
        .mockResolvedValueOnce({
            ok: false,
            json: vi.fn().mockResolvedValue({
                error: 'duplicate subscription',
            }),
        })
        .mockResolvedValueOnce({
            ok: true,
            json: vi.fn().mockResolvedValue({ id: 'subscription-retry' }),
        })
        .mockResolvedValueOnce({
            ok: true,
            status: 202,
            json: vi.fn().mockResolvedValue({
                status: 'queued',
                research_id: 'research-after-create-retry',
            }),
    });
    vi.stubGlobal('fetch', fetchMock);
    const dependencies = createDependencies();
    const runner = compileSubscriptionRunner(dependencies);

    await runner.handleCreateAndRun();
    await runner.handleCreateAndRun();

    expect(fetchMock).toHaveBeenCalledTimes(3);
    expect(dependencies.showAlert).toHaveBeenCalledWith(
        'Error: duplicate subscription',
        'error',
    );
    expect(sessionStorage.getItem('activeTestRunResearchId'))
        .toBe('research-after-create-retry');
});


it.each(['handleSubscriptionSubmit', 'handleTestRun', 'handleCreateAndRun'])(
    '%s retains the configured provider after policy clears its selection', async action => {
        const select = document.getElementById('subscription-provider');
        select.value = '';
        const fetchMock = vi.fn().mockResolvedValue({
            ok: false, status: 400, json: async () => ({ error: 'Policy refused provider' }),
        });
        vi.stubGlobal('fetch', fetchMock);
        const runner = compileSubscriptionRunner({ ...createDependencies(), currentProvider: 'OPENAI' });
        await runner[action]({ preventDefault() {}, stopPropagation() {} });
        expect(fetchMock).toHaveBeenCalledOnce();
        const body = JSON.parse(fetchMock.mock.calls[0][1].body);
        expect(body.model_provider).toBe('OPENAI');
        expect(body.model).toBe('gpt-4.1-mini');
    },
);

// A subscription saved from the news page before #7860 holds the whole
// "PROVIDER:model" selection as its model. Test Run resolves it the way the
// scheduled run does (resolve_subscription_model): an uppercase provider key
// (the form the dropdown's options use) is stripped, and a typed model tag
// whose first segment names the provider runs whole on the default provider.
// A provider that is not a built-in key is dropped the same way, so the run
// uses the user's default provider with the model kept whole.
it.each([
    ['OLLAMA', 'OLLAMA:llama3', 'OLLAMA', 'llama3'],
    ['OLLAMA', 'OLLAMA:llama3.1:8b', 'OLLAMA', 'llama3.1:8b'],
    ['QWEN', 'qwen:7b', undefined, 'qwen:7b'],
    ['OLLAMA', 'ollama:llama3', undefined, 'ollama:llama3'],
    ['OLLAMA', 'llama3.1:8b', 'OLLAMA', 'llama3.1:8b'],
    ['OPENAI', 'OPENAI_ENDPOINT:org/model', 'OPENAI', 'OPENAI_ENDPOINT:org/model'],
    ['CUSTOM', 'my-model', undefined, 'my-model'],
    ['CUSTOM', 'CUSTOM:my-model', undefined, 'CUSTOM:my-model'],
    ['CUSTOM', 'OLLAMA:llama3', undefined, 'OLLAMA:llama3'],
])('test-runs provider %s with saved model %s as %s + %s', async (
    selectedProvider,
    savedModel,
    provider,
    model,
) => {
    const select = document.getElementById('subscription-provider');
    select.innerHTML = `
        <option value="">Select a provider</option>
        <option value="OLLAMA">Ollama</option>
        <option value="OPENAI">OpenAI</option>
        <option value="OPENAI_ENDPOINT">Custom endpoint</option>
        <option value="QWEN">Qwen</option>
        <option value="CUSTOM">Custom</option>
    `;
    select.value = selectedProvider;
    document.getElementById('subscription-model_hidden').value = savedModel;
    const fetchMock = vi.fn().mockResolvedValue({
        ok: true,
        status: 200,
        json: vi.fn().mockResolvedValue({
            status: 'success',
            research_id: 'research-legacy-model',
        }),
    });
    vi.stubGlobal('fetch', fetchMock);

    await compileSubscriptionRunner(createDependencies()).handleTestRun();

    expect(fetchMock).toHaveBeenCalledOnce();
    const [url, options] = fetchMock.mock.calls[0];
    expect(url).toBe('/api/start_research');
    const body = JSON.parse(options.body);
    expect(body.model_provider).toBe(provider);
    expect(body.model).toBe(model);
});

// The provider keys are a fixed set, not the provider list the form loads, so
// a saved prefixed model resolves the same way before that list has loaded.
it.each([
    ['OLLAMA', 'OLLAMA:llama3.1:8b', 'OLLAMA', 'llama3.1:8b'],
    ['QWEN', 'qwen:7b', undefined, 'qwen:7b'],
])('test-runs saved provider %s with model %s as %s + %s before the provider list loads', async (
    savedProvider,
    savedModel,
    provider,
    model,
) => {
    document.getElementById('subscription-provider').innerHTML = '';
    document.getElementById('subscription-model_hidden').value = savedModel;
    const fetchMock = vi.fn().mockResolvedValue({
        ok: true,
        status: 200,
        json: vi.fn().mockResolvedValue({
            status: 'success',
            research_id: 'research-unloaded-providers',
        }),
    });
    vi.stubGlobal('fetch', fetchMock);

    await compileSubscriptionRunner({
        ...createDependencies(),
        currentProvider: savedProvider,
    }).handleTestRun();

    expect(fetchMock).toHaveBeenCalledOnce();
    const body = JSON.parse(fetchMock.mock.calls[0][1].body);
    expect(body.model_provider).toBe(provider);
    expect(body.model).toBe(model);
});