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.

4.4 KB
Raw Blame
/** Warning refresh recovery and ordering, independent of the notice layout. */
import { readFileSync } from 'node:fs';
import { resolve as resolvePath } from 'node:path';
import { runInNewContext } from 'node:vm';
import '@js/config/urls.js';
import '@js/security/xss-protection.js';

beforeEach(async () => {
    vi.resetModules();
    vi.stubGlobal('URLS', window.URLS);
    document.body.innerHTML = '<div id="research-notices"></div><div id="research-alert"></div>';
    await import('@js/research_form.js');
});

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

const CURRENT_NOTICE = {
    type: 'configuration',
    title: 'Current configuration',
    message: 'Latest settings were applied',
    dismissKey: 'app.warnings.dismiss_research_hint',
};

it.each([{}, { warnings: null }, { warnings: {} }, { warnings: [null] }])(
    'retains loaded notices for an invalid refresh response: %j', async payload => {
        window.displayWarnings([CURRENT_NOTICE]);
        vi.stubGlobal('fetch', vi.fn(async () => new Response(JSON.stringify(payload))));
        await window.checkAndDisplayWarnings();
        expect(document.body.textContent)
            .toContain(CURRENT_NOTICE.message);
        expect(document.getElementById('research-warning-refresh-status').textContent)
            .toContain('Could not refresh notices');
    },
);

it('shows refresh failure without existing notices and clears it after an empty success', async () => {
    const fetchMock = vi.fn()
        .mockRejectedValueOnce(new Error('temporarily unavailable'))
        .mockResolvedValueOnce(new Response(JSON.stringify({ warnings: [] })));
    vi.stubGlobal('fetch', fetchMock);
    await window.checkAndDisplayWarnings();
    expect(document.getElementById('research-warning-refresh-status').textContent)
        .toContain('Could not refresh notices');
    await window.checkAndDisplayWarnings();
    expect(document.getElementById('research-warning-refresh-status')).toBeNull();
    expect(document.querySelector('.warning-banner')).toBeNull();
});

it.each(['update', 'empty update', 'clear'])('a newer %s supersedes an earlier read', async operation => {
    let resolve;
    vi.stubGlobal('fetch', vi.fn(() => new Promise(done => { resolve = done; })));
    const earlier = window.checkAndDisplayWarnings();
    if (operation === 'clear') window.clearAllWarnings();
    else window.displayWarnings(operation === 'update' ? [CURRENT_NOTICE] : []);
    resolve(new Response(JSON.stringify({ warnings: [{ ...CURRENT_NOTICE, message: 'Earlier snapshot' }] })));
    await earlier;
    const container = document.body;
    expect(container.textContent).not.toContain('Earlier snapshot');
    if (operation === 'update') expect(container.textContent).toContain(CURRENT_NOTICE.message);
    else expect(document.querySelector('.warning-banner')).toBeNull();
});

it('ignores a failed read after a newer settings update', async () => {
    let reject;
    vi.stubGlobal('fetch', vi.fn(() => new Promise((_resolve, done) => { reject = done; })));
    const earlier = window.checkAndDisplayWarnings();
    window.displayWarnings([CURRENT_NOTICE]);
    reject(new Error('earlier read failed'));
    await earlier;
    expect(document.getElementById('research-warning-refresh-status')).toBeNull();
    expect(document.body.textContent).toContain(CURRENT_NOTICE.message);
});


it.each(['network', 'http'])('retains loaded notices after a %s failure', async failure => {
    window.displayWarnings([CURRENT_NOTICE]);
    vi.stubGlobal('fetch', vi.fn(() => (failure === 'network'
        ? Promise.reject(new Error('temporarily unavailable'))
        : Promise.resolve(new Response('', { status: 503 })))));
    await window.checkAndDisplayWarnings();
    expect(document.body.textContent).toContain(CURRENT_NOTICE.message);
    expect(document.getElementById('research-warning-refresh-status')).not.toBeNull();
});


it('updates notices when loaded as a classic browser script', () => {
    const scope = {
        document,
        location: window.location,
        URL,
        URLS: window.URLS,
        escapeHtml: window.escapeHtml,
    };
    scope.window = scope;
    const script = readFileSync(resolvePath(__dirname, '../../src/local_deep_research/web/static/js/research_form.js'), 'utf8');
    runInNewContext(script, scope);
    scope.displayWarnings([CURRENT_NOTICE]);
    expect(document.body.textContent).toContain(CURRENT_NOTICE.message);
});