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.

3.2 KB
Raw Blame
/**
 * Runtime contracts for the completion gate shared by the library and
 * download-manager FastAPI SSE streams.
 */

import '@js/utils/sse-completion.js';

const handleSSECompletion = window.handleSSECompletion;

beforeEach(() => {
    vi.useFakeTimers();
    window.closeProgressModal = vi.fn();
    vi.stubGlobal('alert', vi.fn());
});

afterEach(() => {
    vi.clearAllTimers();
    vi.useRealTimers();
    vi.restoreAllMocks();
    vi.unstubAllGlobals();
    delete window.closeProgressModal;
});

it('leaves an in-progress event open without scheduling side effects', () => {
    const onSuccess = vi.fn();

    expect(handleSSECompletion({ complete: false, current: 3 }, onSuccess))
        .toBe(false);
    vi.runAllTimers();

    expect(window.closeProgressModal).not.toHaveBeenCalled();
    expect(alert).not.toHaveBeenCalled();
    expect(onSuccess).not.toHaveBeenCalled();
});

it('closes an errored stream and reports its FastAPI error payload', () => {
    const onSuccess = vi.fn();
    const data = { complete: true, error: 'Document extraction failed' };

    expect(handleSSECompletion(data, onSuccess)).toBe(true);
    vi.advanceTimersByTime(999);
    expect(window.closeProgressModal).not.toHaveBeenCalled();

    vi.advanceTimersByTime(1);
    expect(window.closeProgressModal).toHaveBeenCalledOnce();
    expect(alert).toHaveBeenCalledWith('Document extraction failed');
    expect(onSuccess).not.toHaveBeenCalled();
});

it('closes a successful stream and forwards the terminal totals', () => {
    const onSuccess = vi.fn();
    const data = { complete: true, total: 17, downloaded: 15, skipped: 2 };

    expect(handleSSECompletion(data, onSuccess)).toBe(true);
    vi.advanceTimersByTime(2000);

    expect(window.closeProgressModal).toHaveBeenCalledOnce();
    expect(onSuccess).toHaveBeenCalledOnce();
    expect(onSuccess).toHaveBeenCalledWith(data);
    expect(alert).not.toHaveBeenCalled();
});

it('drops delayed terminal effects after the caller loses ownership', () => {
    const onSuccess = vi.fn();
    let isCurrent = true;
    const data = { complete: true, total: 1 };

    expect(handleSSECompletion(data, onSuccess, () => isCurrent)).toBe(true);
    isCurrent = false;
    vi.advanceTimersByTime(2000);

    expect(window.closeProgressModal).not.toHaveBeenCalled();
    expect(onSuccess).not.toHaveBeenCalled();
    expect(alert).not.toHaveBeenCalled();
});

it('drops a delayed terminal error after the caller loses ownership', () => {
    const onSuccess = vi.fn();
    let isCurrent = true;

    expect(handleSSECompletion(
        { complete: true, error: 'stale stream failure' },
        onSuccess,
        () => isCurrent,
    )).toBe(true);
    isCurrent = false;
    vi.advanceTimersByTime(1000);

    expect(window.closeProgressModal).not.toHaveBeenCalled();
    expect(onSuccess).not.toHaveBeenCalled();
    expect(alert).not.toHaveBeenCalled();
});

it('completes successfully when the current page has no progress modal', () => {
    delete window.closeProgressModal;
    const onSuccess = vi.fn();
    const data = { complete: true, downloaded: 1 };

    expect(handleSSECompletion(data, onSuccess)).toBe(true);
    vi.advanceTimersByTime(2000);

    expect(onSuccess).toHaveBeenCalledWith(data);
    expect(alert).not.toHaveBeenCalled();
});