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.

2.9 KB
Raw Blame
/** Checked-in template contracts for scheduler jitter guidance. */

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

import { compileTemplateHarness } from '../helpers/template-harness.js';

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

function compileJitterLoader() {
    return compileTemplateHarness({
        templatePath: TEMPLATE_PATH,
        functionNames: ['loadJitterInfo'],
        dependencies: {
            getCSRFToken: () => 'csrf-jitter',
        },
        returnExpression: 'loadJitterInfo',
    });
}

beforeEach(() => {
    // Scripts inserted through innerHTML remain inert; this mounts the exact
    // checked-in element that loadJitterInfo updates in the browser.
    // eslint-disable-next-line no-unsanitized/property -- repository-owned template fixture.
    document.body.innerHTML = TEMPLATE_SOURCE;
});

afterEach(() => {
    vi.restoreAllMocks();
    vi.unstubAllGlobals();
    delete window.__jitterExecuted;
    document.body.replaceChildren();
});

it.each([
    {
        maxJitter: 0,
        expectedText: 'No jitter configured. Subscriptions will run exactly at scheduled times.',
    },
    {
        maxJitter: 300,
        expectedText: 'A random delay up to 5 minutes will be added',
    },
])('renders scheduler max jitter $maxJitter in the actual form hint', async ({
    maxJitter,
    expectedText,
}) => {
    const fetchMock = vi.fn().mockResolvedValue({
        ok: true,
        json: vi.fn().mockResolvedValue({
            config: { max_jitter_seconds: maxJitter },
        }),
    });
    vi.stubGlobal('fetch', fetchMock);

    await compileJitterLoader()();

    expect(fetchMock).toHaveBeenCalledWith('/news/api/scheduler/status', {
        credentials: 'same-origin',
        headers: { 'X-CSRFToken': 'csrf-jitter' },
    });
    const hint = document.getElementById('subscription-jitter-info');
    expect(hint).not.toBeNull();
    expect(hint.textContent).toContain(expectedText);
    expect(hint.querySelector('small')).toBeNull();
    expect(hint.querySelector('a').getAttribute('href'))
        .toBe('/settings#news_scheduler');
});

it('keeps a malformed scheduler jitter value inert and uses the safe default', async () => {
    const hostileValue = '<img src=x onerror="window.__jitterExecuted=true">';
    vi.stubGlobal('fetch', vi.fn().mockResolvedValue({
        ok: true,
        json: vi.fn().mockResolvedValue({
            config: { max_jitter_seconds: hostileValue },
        }),
    }));
    window.__jitterExecuted = false;

    await compileJitterLoader()();

    const hint = document.getElementById('subscription-jitter-info');
    expect(hint.textContent).toContain('delay up to 5 minutes');
    expect(hint.querySelector('img')).toBeNull();
    expect(hint.innerHTML).not.toContain(hostileValue);
    expect(window.__jitterExecuted).toBe(false);
});