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.7 KB
Raw Blame
/**
 * Direct contract tests for the Vite application entry point.
 *
 * The browser bundle is also exercised by the build and UI suites, but these
 * tests pin the global compatibility surface consumed by classic scripts and
 * the DOMContentLoaded bootstrap without requiring a browser process.
 */

const mocks = vi.hoisted(() => ({
    marked: {
        setOptions: vi.fn(),
        use: vi.fn(),
    },
    io: vi.fn(),
    hljs: {
        getLanguage: vi.fn(),
        highlight: vi.fn(),
        highlightElement: vi.fn(),
    },
    jsPDF: vi.fn(),
    html2canvas: vi.fn(),
    DOMPurify: { sanitize: vi.fn() },
    markedKatex: vi.fn(() => ({ name: 'katex-extension' })),
    Chart: { register: vi.fn() },
    annotationPlugin: { id: 'annotation' },
    Tooltip: vi.fn(),
    Popover: vi.fn(),
}));

vi.mock('marked', () => ({ marked: mocks.marked }));
vi.mock('socket.io-client', () => ({ default: mocks.io }));
vi.mock('highlight.js', () => ({ default: mocks.hljs }));
vi.mock('jspdf', () => ({ default: mocks.jsPDF }));
vi.mock('html2canvas', () => ({ default: mocks.html2canvas }));
vi.mock('dompurify', () => ({ default: mocks.DOMPurify }));
vi.mock('marked-katex-extension', () => ({ default: mocks.markedKatex }));
vi.mock('bootstrap', () => ({
    Tooltip: mocks.Tooltip,
    Popover: mocks.Popover,
}));
vi.mock('chart.js/auto', () => ({ default: mocks.Chart }));
vi.mock('chartjs-adapter-date-fns', () => ({}));
vi.mock('chartjs-plugin-annotation', () => ({
    default: mocks.annotationPlugin,
}));

let app;

beforeAll(async () => {
    document.body.innerHTML = `
        <pre><code id="code-block">const value = 1;</code></pre>
        <button id="tooltip" data-bs-toggle="tooltip"></button>
        <button id="popover" data-bs-toggle="popover"></button>
    `;
    app = await import('@js/app.js');
});

afterAll(() => {
    document.body.innerHTML = '';
    for (const name of [
        'marked',
        'io',
        'Chart',
        'hljs',
        'jsPDF',
        'html2canvas',
        'bootstrap',
        'DOMPurify',
    ]) {
        delete window[name];
    }
});

it('publishes the checked-in vendor compatibility API on window', () => {
    expect(window.marked).toBe(mocks.marked);
    expect(window.io).toBe(mocks.io);
    expect(window.Chart).toBe(mocks.Chart);
    expect(window.hljs).toBe(mocks.hljs);
    expect(window.jsPDF).toBe(mocks.jsPDF);
    expect(window.html2canvas).toBe(mocks.html2canvas);
    expect(window.DOMPurify).toBe(mocks.DOMPurify);
    expect(window.bootstrap).toMatchObject({
        Tooltip: mocks.Tooltip,
        Popover: mocks.Popover,
    });

    expect(app).toMatchObject({
        marked: mocks.marked,
        io: mocks.io,
        Chart: mocks.Chart,
        DOMPurify: mocks.DOMPurify,
    });
    expect(mocks.Chart.register).toHaveBeenCalledWith(mocks.annotationPlugin);
});

it('configures safe Markdown highlighting and the KaTeX extension', () => {
    expect(mocks.marked.setOptions).toHaveBeenCalledOnce();
    const options = mocks.marked.setOptions.mock.calls[0][0];
    expect(options).toMatchObject({
        headerIds: false,
        mangle: false,
        smartypants: false,
    });

    mocks.hljs.getLanguage.mockReturnValueOnce(false);
    expect(options.highlight('plain code', 'unknown')).toBe('plain code');

    mocks.hljs.getLanguage.mockReturnValueOnce(true);
    mocks.hljs.highlight.mockReturnValueOnce({ value: '&lt;safe&gt;' });
    expect(options.highlight('<safe>', 'javascript')).toBe('&lt;safe&gt;');
    expect(mocks.hljs.highlight).toHaveBeenCalledWith('<safe>', {
        language: 'javascript',
    });

    const highlightError = new Error('bad grammar');
    mocks.hljs.getLanguage.mockReturnValueOnce(true);
    mocks.hljs.highlight.mockImplementationOnce(() => {
        throw highlightError;
    });
    const errorSpy = vi.spyOn(SafeLogger, 'error');
    expect(options.highlight('fallback', 'javascript')).toBe('fallback');
    expect(errorSpy).toHaveBeenCalledWith('Highlight error:', highlightError);
    errorSpy.mockRestore();

    expect(mocks.markedKatex).toHaveBeenCalledWith({
        throwOnError: false,
        errorColor: 'currentColor',
    });
    expect(mocks.marked.use).toHaveBeenCalledWith({
        name: 'katex-extension',
    });
});

it('initializes code highlighting, tooltips, and popovers when the DOM is ready', () => {
    mocks.hljs.highlightElement.mockClear();
    mocks.Tooltip.mockClear();
    mocks.Popover.mockClear();

    document.dispatchEvent(new Event('DOMContentLoaded'));

    expect(mocks.hljs.highlightElement)
        .toHaveBeenCalledWith(document.getElementById('code-block'));
    expect(mocks.Tooltip)
        .toHaveBeenCalledWith(document.getElementById('tooltip'));
    expect(mocks.Popover)
        .toHaveBeenCalledWith(document.getElementById('popover'));
});