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.

5.0 KB
Raw Blame
/**
 * Direct resilience contracts for the standalone UI fallback bundle.
 * It is loaded precisely when the main UI service is unavailable, so its XSS
 * and interaction behavior must be exercised independently of services/ui.js.
 */

async function loadFallback() {
    vi.resetModules();
    await import('@js/components/fallback/ui.js');
    return window.ui;
}

beforeEach(() => {
    delete window.ui;
    delete window.escapeHtml;
    delete window.URLValidator;
    document.head.replaceChildren();
    document.body.replaceChildren();
});

afterEach(() => {
    vi.restoreAllMocks();
    vi.unstubAllGlobals();
    vi.useRealTimers();
    delete window.ui;
    delete window.escapeHtml;
    delete window.URLValidator;
    document.head.replaceChildren();
    document.body.replaceChildren();
});

it('does not replace the primary UI service when it is already loaded', async () => {
    const primary = { showMessage: vi.fn() };
    window.ui = primary;

    await loadFallback();

    expect(window.ui).toBe(primary);
});

it('renders and removes an escaped loading indicator', async () => {
    const ui = await loadFallback();
    const container = document.createElement('div');
    document.body.appendChild(container);

    ui.showSpinner(container, '<img src=x onerror="window.pwned=true">');

    expect(container.textContent)
        .toContain('<img src=x onerror="window.pwned=true">');
    expect(container.querySelector('img')).toBeNull();
    expect(window.pwned).toBeUndefined();
    ui.hideSpinner(container);
    expect(container.querySelector('.ldr-loading-spinner')).toBeNull();
});

it('owns safe notification dismissal and expiry without the main UI', async () => {
    vi.useFakeTimers();
    const ui = await loadFallback();
    const notifications = document.createElement('div');
    notifications.className = 'ldr-notifications-container';
    document.body.appendChild(notifications);
    const payload = '<svg onload="window.pwned=true">';

    ui.showError(payload);
    ui.showMessage('Saved safely');

    expect(notifications.children).toHaveLength(2);
    expect(notifications.textContent).toContain(payload);
    expect(notifications.querySelector('svg')).toBeNull();
    expect(window.pwned).toBeUndefined();
    notifications.querySelector('.ldr-error .ldr-close-notification').click();
    vi.advanceTimersByTime(500);
    expect(notifications.querySelector('.ldr-error')).toBeNull();

    vi.advanceTimersByTime(4499);
    expect(notifications.querySelector('.ldr-success').classList)
        .not.toContain('ldr-removing');
    vi.advanceTimersByTime(1);
    expect(notifications.querySelector('.ldr-success').classList)
        .toContain('ldr-removing');
    vi.advanceTimersByTime(500);
    expect(notifications.children).toHaveLength(0);
});

it('falls back to alert when no notification host exists', async () => {
    const alert = vi.fn();
    vi.stubGlobal('alert', alert);
    const ui = await loadFallback();

    ui.showError('Offline');
    ui.showMessage('Recovered');

    expect(alert.mock.calls).toEqual([['Offline'], ['Recovered']]);
});

it('returns escaped plaintext Markdown with an explicit warning', async () => {
    const ui = await loadFallback();

    const rendered = ui.renderMarkdown('# Heading <img src=x>');

    expect(rendered).toContain('Markdown rendering unavailable');
    expect(rendered).toContain('&lt;img src=x&gt;');
    expect(rendered).not.toContain('<img src=x>');
    expect(ui.renderMarkdown('')).toBe('');
});

it('builds a dismissible inline error entirely with DOM nodes', async () => {
    const ui = await loadFallback();
    const container = document.createElement('div');
    container.id = 'fallback-errors';
    document.body.appendChild(container);
    const payload = '<img src=x onerror="window.pwned=true">';

    const first = ui.showInlineError('fallback-errors', payload);
    const replacement = ui.showInlineError(container, 'Replacement', {
        dismissible: false,
    });

    expect(first.isConnected).toBe(false);
    expect(replacement.textContent).toContain('Replacement');
    expect(replacement.querySelector('button')).toBeNull();
    expect(container.querySelector('img')).toBeNull();
    ui.clearInlineError('fallback-errors');
    expect(container.children).toHaveLength(0);
    expect(ui.showInlineError('missing', 'No host')).toBeNull();
});

it.each([
    ['active', '/static/img/favicon-active.ico'],
    ['complete', '/static/img/favicon-complete.ico'],
    ['error', '/static/img/favicon-error.ico'],
    ['unknown', '/static/img/favicon.ico'],
])('maps %s to the fallback favicon asset', async (status, expectedPath) => {
    vi.useFakeTimers();
    vi.setSystemTime(new Date('2026-09-01T12:00:00Z'));
    const ui = await loadFallback();
    const favicon = document.createElement('link');
    favicon.rel = 'icon';
    document.head.appendChild(favicon);
    window.URLValidator = { safeAssign: vi.fn() };

    ui.updateFavicon(status);

    expect(window.URLValidator.safeAssign).toHaveBeenCalledWith(
        favicon,
        'href',
        `${expectedPath}?v=${Date.now()}`,
    );
});