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.

22.8 KB
Raw Blame
/**
 * Endpoint-level browser contracts for the two inline "download all" clients.
 * These execute functions extracted from the checked-in Jinja templates.
 */

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

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

const TEMPLATE_DIR = resolve(
    __dirname,
    '../../../src/local_deep_research/web/templates/pages',
);
const DOWNLOAD_MANAGER_TEMPLATE = resolve(TEMPLATE_DIR, 'download_manager.html');
const LIBRARY_TEMPLATE = resolve(TEMPLATE_DIR, 'library.html');
const SSE_UTIL_PATH = resolve(
    __dirname,
    '../../../src/local_deep_research/web/static/js/utils/sse-completion.js',
);
const QUEUE_URL = '/library/api/queue-all-undownloaded';
const BULK_URL = '/library/api/download-bulk';

function jsonResponse(payload, { ok = true, status = 200 } = {}) {
    return {
        ok,
        status,
        json: vi.fn().mockResolvedValue(payload),
    };
}

function deferred() {
    let resolvePromise;
    const promise = new Promise(resolveDeferred => {
        resolvePromise = resolveDeferred;
    });
    return { promise, resolve: resolvePromise };
}

function loadSSEParser() {
    const source = readFileSync(SSE_UTIL_PATH, 'utf8');
    // Repository-owned production utility only; no user-controlled input.
    return new Function( // eslint-disable-line no-new-func
        `${source}\nreturn createSSEJsonParser;`,
    )();
}

function streamResponse(...events) {
    const encoded = new globalThis.TextEncoder().encode(events.map(event => (
        `data: ${JSON.stringify(event)}\n`
    )).join(''));
    const read = vi.fn()
        .mockResolvedValueOnce({ done: false, value: encoded })
        .mockResolvedValueOnce({ done: true, value: undefined });
    return {
        ok: true,
        status: 200,
        body: { getReader: vi.fn(() => ({ read })) },
    };
}

function pausableStreamResponse(event, terminalEvent) {
    let markPaused;
    let finishStream;
    const paused = new Promise(resolvePromise => {
        markPaused = resolvePromise;
    });
    const finished = new Promise(resolvePromise => {
        finishStream = resolvePromise;
    });
    const encoded = new globalThis.TextEncoder().encode(
        `data: ${JSON.stringify(event)}\n`,
    );
    const read = vi.fn()
        .mockResolvedValueOnce({ done: false, value: encoded })
        .mockImplementationOnce(() => {
            markPaused();
            return finished;
        })
        .mockResolvedValueOnce({ done: true, value: undefined });
    return {
        response: {
            ok: true,
            status: 200,
            body: { getReader: vi.fn(() => ({ read })) },
        },
        paused,
        finish: () => finishStream(terminalEvent ? {
            done: false,
            value: new globalThis.TextEncoder().encode(
                `data: ${JSON.stringify(terminalEvent)}\n`,
            ),
        } : { done: true, value: undefined }),
    };
}

function emptyStreamResponse() {
    const read = vi.fn().mockResolvedValue({
        done: true,
        value: undefined,
    });
    return {
        response: {
            ok: true,
            status: 200,
            body: { getReader: vi.fn(() => ({ read })) },
        },
        read,
    };
}

function compileManagerLoadCollections() {
    return compileTemplateHarness({
        templatePath: DOWNLOAD_MANAGER_TEMPLATE,
        functionNames: ['loadCollections'],
        returnExpression: 'loadCollections',
    });
}

function compileManagerDownloadAll({ button, handleSSECompletion }) {
    return compileTemplateHarness({
        templatePath: DOWNLOAD_MANAGER_TEMPLATE,
        functionNames: [
            'resetStatusCounts',
            'recordStatus',
            'updateProgress',
            'startBulkDownload',
            'downloadAllNew',
        ],
        dependencies: {
            event: { target: button },
            createSSEJsonParser: loadSSEParser(),
            handleSSECompletion,
        },
        preamble: `
            let statusCounts = { succeeded: 0, skipped: 0, failed: 0 };
            let currentDownloadController = null;
            let currentDownloadRunId = 0;
        `,
        returnExpression: `({
            startBulkDownload,
            downloadAllNew,
            getCurrentDownloadController: () => currentDownloadController,
        })`,
    });
}

function compileLibraryDownloadAll({
    button,
    handleSSECompletion,
    showDownloadProgressModal,
    updateDownloadProgress,
}) {
    return compileTemplateHarness({
        templatePath: LIBRARY_TEMPLATE,
        functionNames: ['resetDownloadProgress', 'downloadAllNew'],
        dependencies: {
            event: { target: button },
            createSSEJsonParser: loadSSEParser(),
            handleSSECompletion,
            showDownloadProgressModal,
            updateDownloadProgress,
        },
        preamble: `
            let currentDownloadController = null;
            let currentDownloadRunId = 0;
        `,
        returnExpression: `({
            downloadAllNew,
            getCurrentDownloadController: () => currentDownloadController,
        })`,
    });
}

function mountManagerDownloadUi() {
    document.head.innerHTML = '<meta name="csrf-token" content="csrf-download-all">';
    document.body.innerHTML = `
        <button id="download-all"><span>Download all</span></button>
        <div id="download-progress-modal" style="display: none"><h2></h2></div>
        <div class="ldr-status-breakdown" style="display: none"></div>
        <span id="succeeded-count">8</span>
        <span id="skipped-count">8</span>
        <span id="failed-count">8</span>
        <div id="overall-progress" style="width: 20%"></div>
        <span id="current-count">8</span>
        <span id="total-count">8</span>
        <div id="download-log">old log</div>
    `;
    return document.getElementById('download-all');
}

afterEach(() => {
    vi.restoreAllMocks();
    vi.unstubAllGlobals();
    delete window.escapeHtml;
    delete window.createSSEJsonParser;
    delete window.handleSSECompletion;
    document.head.replaceChildren();
    document.body.replaceChildren();
});

it('loads collection options from the list envelope and keeps names inert', async () => {
    document.body.innerHTML = `
        <select id="target-collection"><option>Loading…</option></select>
    `;
    const fetchMock = vi.fn().mockResolvedValue(jsonResponse({
        success: true,
        collections: [
            { id: 'collection-library', name: 'Library' },
            { id: 'collection-unsafe', name: '<img src=x onerror=alert(1)>' },
        ],
    }));
    vi.stubGlobal('fetch', fetchMock);

    await compileManagerLoadCollections()();

    expect(fetchMock).toHaveBeenCalledOnce();
    expect(fetchMock).toHaveBeenCalledWith('/library/api/collections/list');
    const options = [...document.querySelectorAll('#target-collection option')];
    expect(options.map(option => ({
        value: option.value,
        text: option.textContent,
        selected: option.selected,
    }))).toEqual([
        {
            value: 'collection-library',
            text: 'Library',
            selected: true,
        },
        {
            value: 'collection-unsafe',
            text: '<img src=x onerror=alert(1)>',
            selected: false,
        },
    ]);
    expect(document.querySelector('#target-collection img')).toBeNull();
});

it('queues then streams manager downloads while retaining single-run ownership', async () => {
    const button = mountManagerDownloadUi();
    window.escapeHtml = vi.fn(value => String(value));
    vi.stubGlobal('alert', vi.fn());
    const stream = pausableStreamResponse({
        progress: 50,
        current: 1,
        total: 2,
        status: 'success',
        file: 'paper.pdf',
    }, {
        complete: true,
        progress: 100,
        current: 2,
        total: 2,
        status: 'complete',
    });
    const fetchMock = vi.fn()
        .mockResolvedValueOnce(jsonResponse({
            queued: 2,
            research_ids: ['research-1', 'research-2'],
        }))
        .mockResolvedValueOnce(stream.response)
        .mockRejectedValue(new Error('duplicate run should not fetch'));
    vi.stubGlobal('fetch', fetchMock);
    const handleSSECompletion = vi.fn(() => false);
    const runtime = compileManagerDownloadAll({ button, handleSSECompletion });

    const activeRun = runtime.downloadAllNew();
    await stream.paused;

    expect(button.disabled).toBe(true);
    expect(runtime.getCurrentDownloadController()).toBeInstanceOf(AbortController);
    expect(fetchMock.mock.calls.map(([url]) => url)).toEqual([
        QUEUE_URL,
        BULK_URL,
    ]);
    expect(fetchMock.mock.calls[0][1]).toMatchObject({
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
            'X-CSRFToken': 'csrf-download-all',
        },
    });
    expect(fetchMock.mock.calls[0][1].signal)
        .toBeInstanceOf(globalThis.AbortSignal);
    expect(fetchMock.mock.calls[1][1]).toMatchObject({
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
            'X-CSRFToken': 'csrf-download-all',
        },
        body: JSON.stringify({
            research_ids: ['research-1', 'research-2'],
        }),
    });
    expect(fetchMock.mock.calls[1][1].signal)
        .toBeInstanceOf(globalThis.AbortSignal);
    expect(document.getElementById('download-progress-modal').style.display)
        .toBe('block');
    expect(document.querySelector('#download-progress-modal h2').textContent)
        .toBe('Downloading ALL PDFs');
    expect(document.getElementById('overall-progress').style.width).toBe('50%');
    expect(document.getElementById('succeeded-count').textContent).toBe('1');
    expect(handleSSECompletion).toHaveBeenCalledWith(
        expect.objectContaining({ file: 'paper.pdf', status: 'success' }),
        expect.any(Function),
        expect.any(Function),
    );

    await runtime.downloadAllNew();
    expect(fetchMock).toHaveBeenCalledTimes(2);

    stream.finish();
    await activeRun;

    expect(button.disabled).toBe(false);
    expect(button.textContent).toContain('Download all');
    expect(runtime.getCurrentDownloadController()).toBeNull();
});

it('does not let an older queue response reclaim a newer manager run', async () => {
    const button = mountManagerDownloadUi();
    window.escapeHtml = vi.fn(value => String(value));
    vi.stubGlobal('alert', vi.fn());
    const pendingQueue = deferred();
    const newerStream = pausableStreamResponse({
        progress: 40,
        current: 1,
        total: 2,
        status: 'success',
        file: 'new-owner.pdf',
    }, {
        complete: true,
        progress: 100,
        current: 2,
        total: 2,
        status: 'complete',
    });
    const fetchMock = vi.fn()
        .mockImplementationOnce(() => pendingQueue.promise)
        .mockResolvedValueOnce(newerStream.response);
    vi.stubGlobal('fetch', fetchMock);
    const handleSSECompletion = vi.fn(() => false);
    const runtime = compileManagerDownloadAll({ button, handleSSECompletion });

    const olderQueueRun = runtime.downloadAllNew();
    await vi.waitFor(() => expect(fetchMock).toHaveBeenCalledOnce());
    const olderQueueSignal = fetchMock.mock.calls[0][1].signal;

    const newerRun = runtime.startBulkDownload(['new-owner'], 'pdf');
    await newerStream.paused;
    const newerSignal = fetchMock.mock.calls[1][1].signal;

    pendingQueue.resolve(jsonResponse({
        queued: 1,
        research_ids: ['stale-queued'],
    }));
    await olderQueueRun;

    expect(fetchMock).toHaveBeenCalledTimes(2);
    expect(olderQueueSignal.aborted).toBe(true);
    expect(newerSignal.aborted).toBe(false);
    expect(runtime.getCurrentDownloadController()?.signal).toBe(newerSignal);
    expect(document.querySelector('#download-progress-modal h2').textContent)
        .toBe('Downloading PDFs');
    expect(document.getElementById('download-log').textContent)
        .toContain('new-owner.pdf');
    expect(document.getElementById('download-log').textContent)
        .not.toContain('stale-queued');

    newerStream.finish();
    await newerRun;
});

it('aborts an active manager stream before a download-all queue takes ownership', async () => {
    const button = mountManagerDownloadUi();
    const olderResponse = deferred();
    const fetchMock = vi.fn()
        .mockImplementationOnce(() => olderResponse.promise)
        .mockResolvedValueOnce(jsonResponse({
            queued: 0,
            research_ids: [],
        }));
    vi.stubGlobal('fetch', fetchMock);
    vi.stubGlobal('alert', vi.fn());
    const runtime = compileManagerDownloadAll({
        button,
        handleSSECompletion: vi.fn(),
    });

    const olderRun = runtime.startBulkDownload(['older-manager-run'], 'pdf');
    await vi.waitFor(() => expect(fetchMock).toHaveBeenCalledOnce());
    const olderSignal = fetchMock.mock.calls[0][1].signal;

    await runtime.downloadAllNew();

    expect(olderSignal.aborted).toBe(true);
    expect(fetchMock.mock.calls.map(([url]) => url)).toEqual([
        BULK_URL,
        QUEUE_URL,
    ]);
    expect(runtime.getCurrentDownloadController()).toBeNull();

    olderResponse.resolve(jsonResponse({}));
    await olderRun;
});

it('restores the manager action when queueing is rejected', async () => {
    const button = mountManagerDownloadUi();
    const originalHtml = button.innerHTML;
    const alertMock = vi.fn();
    const errorSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
    vi.stubGlobal('alert', alertMock);
    const fetchMock = vi.fn().mockResolvedValue(jsonResponse({}, {
        ok: false,
        status: 503,
    }));
    vi.stubGlobal('fetch', fetchMock);
    const runtime = compileManagerDownloadAll({
        button,
        handleSSECompletion: vi.fn(),
    });

    await runtime.downloadAllNew();

    expect(fetchMock).toHaveBeenCalledOnce();
    expect(fetchMock.mock.calls[0][0]).toBe(QUEUE_URL);
    expect(errorSpy).toHaveBeenCalledWith(
        'Failed to start downloads:',
        expect.objectContaining({ message: 'HTTP error! status: 503' }),
    );
    expect(alertMock).toHaveBeenCalledWith(
        'Failed to start downloads. Please try again.',
    );
    expect(button.innerHTML).toBe(originalHtml);
    expect(button.disabled).toBe(false);
    expect(runtime.getCurrentDownloadController()).toBeNull();
});

it('restores the manager action when the second-stage bulk request is rejected', async () => {
    const button = mountManagerDownloadUi();
    const originalHtml = button.innerHTML;
    const getReader = vi.fn();
    const fetchMock = vi.fn()
        .mockResolvedValueOnce(jsonResponse({
            queued: 1,
            research_ids: ['manager-bulk-failure'],
        }))
        .mockResolvedValueOnce({
            ok: false,
            status: 502,
            body: { getReader },
        });
    vi.stubGlobal('fetch', fetchMock);
    const alertMock = vi.fn();
    vi.stubGlobal('alert', alertMock);
    const errorSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
    const handleSSECompletion = vi.fn();
    const runtime = compileManagerDownloadAll({ button, handleSSECompletion });

    await runtime.downloadAllNew();

    expect(fetchMock.mock.calls.map(([url]) => url)).toEqual([
        QUEUE_URL,
        BULK_URL,
    ]);
    expect(getReader).not.toHaveBeenCalled();
    expect(handleSSECompletion).not.toHaveBeenCalled();
    expect(errorSpy).toHaveBeenCalledWith(
        'Failed to start downloads:',
        expect.objectContaining({ message: 'HTTP error! status: 502' }),
    );
    expect(alertMock).toHaveBeenCalledWith(
        'Failed to start downloads. Please try again.',
    );
    expect(button.innerHTML).toBe(originalHtml);
    expect(button.disabled).toBe(false);
    expect(runtime.getCurrentDownloadController()).toBeNull();
});

it('reports an empty manager download-all stream and restores its action', async () => {
    const button = mountManagerDownloadUi();
    const originalHtml = button.innerHTML;
    const emptyStream = emptyStreamResponse();
    const fetchMock = vi.fn()
        .mockResolvedValueOnce(jsonResponse({
            queued: 1,
            research_ids: ['manager-empty-stream'],
        }))
        .mockResolvedValueOnce(emptyStream.response);
    vi.stubGlobal('fetch', fetchMock);
    const alertMock = vi.fn();
    vi.stubGlobal('alert', alertMock);
    const errorSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
    const handleSSECompletion = vi.fn();
    const runtime = compileManagerDownloadAll({ button, handleSSECompletion });

    await runtime.downloadAllNew();

    expect(emptyStream.read).toHaveBeenCalledOnce();
    expect(handleSSECompletion).not.toHaveBeenCalled();
    expect(errorSpy).toHaveBeenCalledWith(
        'Failed to start downloads:',
        expect.objectContaining({
            message: 'Download stream ended before completion',
        }),
    );
    expect(alertMock).toHaveBeenCalledWith(
        'Failed to start downloads. Please try again.',
    );
    expect(button.innerHTML).toBe(originalHtml);
    expect(button.disabled).toBe(false);
    expect(runtime.getCurrentDownloadController()).toBeNull();
});

it('uses the library page modal and progress consumer for the same queue handshake', async () => {
    document.head.innerHTML = '<meta name="csrf-token" content="csrf-library-all">';
    document.body.innerHTML = `
        <button id="download-all"><span>Download all new</span></button>
        <div id="ldr-overall-progress"></div>
        <span id="current-count"></span>
        <span id="total-count"></span>
        <div id="ldr-download-log"></div>
    `;
    const button = document.getElementById('download-all');
    const event = {
        progress: 100,
        current: 1,
        total: 1,
        status: 'success',
        complete: true,
    };
    const fetchMock = vi.fn()
        .mockResolvedValueOnce(jsonResponse({
            queued: 1,
            research_ids: ['library-research'],
        }))
        .mockResolvedValueOnce(streamResponse(event));
    vi.stubGlobal('fetch', fetchMock);
    vi.stubGlobal('alert', vi.fn());
    const showDownloadProgressModal = vi.fn();
    const updateDownloadProgress = vi.fn();
    const handleSSECompletion = vi.fn(data => data.complete);
    const runtime = compileLibraryDownloadAll({
        button,
        handleSSECompletion,
        showDownloadProgressModal,
        updateDownloadProgress,
    });

    await runtime.downloadAllNew();

    expect(fetchMock.mock.calls.map(([url]) => url)).toEqual([
        QUEUE_URL,
        BULK_URL,
    ]);
    expect(JSON.parse(fetchMock.mock.calls[1][1].body)).toEqual({
        research_ids: ['library-research'],
    });
    expect(fetchMock.mock.calls[1][1].headers['X-CSRFToken'])
        .toBe('csrf-library-all');
    expect(showDownloadProgressModal).toHaveBeenCalledOnce();
    expect(updateDownloadProgress).toHaveBeenCalledWith(event);
    expect(handleSSECompletion).toHaveBeenCalledWith(
        event,
        expect.any(Function),
        expect.any(Function),
    );
    expect(button.disabled).toBe(false);
    expect(runtime.getCurrentDownloadController()).toBeNull();
});

it('restores the library action when the second-stage bulk request is rejected', async () => {
    document.head.innerHTML = '<meta name="csrf-token" content="csrf-library-all">';
    document.body.innerHTML = `
        <button id="download-all"><span>Download all new</span></button>
        <div id="ldr-overall-progress"></div>
        <span id="current-count"></span>
        <span id="total-count"></span>
        <div id="ldr-download-log"></div>
    `;
    const button = document.getElementById('download-all');
    const originalHtml = button.innerHTML;
    const getReader = vi.fn();
    const fetchMock = vi.fn()
        .mockResolvedValueOnce(jsonResponse({
            queued: 1,
            research_ids: ['library-bulk-failure'],
        }))
        .mockResolvedValueOnce({
            ok: false,
            status: 503,
            body: { getReader },
        });
    vi.stubGlobal('fetch', fetchMock);
    const alertMock = vi.fn();
    vi.stubGlobal('alert', alertMock);
    const errorSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
    const handleSSECompletion = vi.fn();
    const updateDownloadProgress = vi.fn();
    const runtime = compileLibraryDownloadAll({
        button,
        handleSSECompletion,
        showDownloadProgressModal: vi.fn(),
        updateDownloadProgress,
    });

    await runtime.downloadAllNew();

    expect(fetchMock.mock.calls.map(([url]) => url)).toEqual([
        QUEUE_URL,
        BULK_URL,
    ]);
    expect(getReader).not.toHaveBeenCalled();
    expect(updateDownloadProgress).not.toHaveBeenCalled();
    expect(handleSSECompletion).not.toHaveBeenCalled();
    expect(errorSpy).toHaveBeenCalledWith(
        'Failed to start downloads:',
        expect.objectContaining({ message: 'HTTP error! status: 503' }),
    );
    expect(alertMock).toHaveBeenCalledWith(
        'Failed to start downloads. Please try again.',
    );
    expect(button.innerHTML).toBe(originalHtml);
    expect(button.disabled).toBe(false);
    expect(runtime.getCurrentDownloadController()).toBeNull();
});

it('reports an empty library download-all stream and restores its action', async () => {
    document.head.innerHTML = '<meta name="csrf-token" content="csrf-library-all">';
    document.body.innerHTML = `
        <button id="download-all"><span>Download all new</span></button>
        <div id="ldr-overall-progress"></div>
        <span id="current-count"></span>
        <span id="total-count"></span>
        <div id="ldr-download-log"></div>
    `;
    const button = document.getElementById('download-all');
    const originalHtml = button.innerHTML;
    const emptyStream = emptyStreamResponse();
    const fetchMock = vi.fn()
        .mockResolvedValueOnce(jsonResponse({
            queued: 1,
            research_ids: ['library-empty-stream'],
        }))
        .mockResolvedValueOnce(emptyStream.response);
    vi.stubGlobal('fetch', fetchMock);
    const alertMock = vi.fn();
    vi.stubGlobal('alert', alertMock);
    const errorSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
    const handleSSECompletion = vi.fn();
    const updateDownloadProgress = vi.fn();
    const runtime = compileLibraryDownloadAll({
        button,
        handleSSECompletion,
        showDownloadProgressModal: vi.fn(),
        updateDownloadProgress,
    });

    await runtime.downloadAllNew();

    expect(emptyStream.read).toHaveBeenCalledOnce();
    expect(updateDownloadProgress).not.toHaveBeenCalled();
    expect(handleSSECompletion).not.toHaveBeenCalled();
    expect(errorSpy).toHaveBeenCalledWith(
        'Failed to start downloads:',
        expect.objectContaining({
            message: 'Download stream ended before completion',
        }),
    );
    expect(alertMock).toHaveBeenCalledWith(
        'Failed to start downloads. Please try again.',
    );
    expect(button.innerHTML).toBe(originalHtml);
    expect(button.disabled).toBe(false);
    expect(runtime.getCurrentDownloadController()).toBeNull();
});