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.

21.1 KB
Raw Blame
/**
 * Runtime contracts for the download manager's inline bulk-download client
 * and its per-research row actions.
 *
 * The functions are executed from the checked-in Jinja template so route,
 * payload, CSRF, streaming, status aggregation, abort behavior and row action
 * dispatch cannot silently drift during the FastAPI migration.
 */

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

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

const TEMPLATE_PATH = resolve(
    __dirname,
    '../../../src/local_deep_research/web/templates/pages/download_manager.html',
);
const SSE_UTIL_PATH = resolve(
    __dirname,
    '../../../src/local_deep_research/web/static/js/utils/sse-completion.js',
);

function extractFunction(source, name) {
    const signature = new RegExp(`(?:async\\s+)?function\\s+${name}\\s*\\(`);
    const match = signature.exec(source);
    if (!match) throw new Error(`Function ${name} not found in template`);

    const openBrace = source.indexOf('{', match.index + match[0].length);
    let depth = 0;
    for (let index = openBrace; index < source.length; index += 1) {
        if (source[index] === '{') depth += 1;
        if (source[index] === '}') {
            depth -= 1;
            if (depth === 0) return source.slice(match.index, index + 1);
        }
    }

    throw new Error(`Function ${name} has an unterminated body`);
}

function compileBulkDownload(handleSSECompletion) {
    const template = readFileSync(TEMPLATE_PATH, 'utf8');
    const functions = [
        'resetStatusCounts',
        'recordStatus',
        'updateProgress',
        'updateTextExtractionProgress',
        'startBulkDownload',
        'downloadAllAsText',
        'closeProgressModal',
        'cancelDownloads',
    ].map(name => extractFunction(template, name)).join('\n');
    // The extracted source is repository-owned production code from the
    // template above, not user-controlled input.
    const factory = new Function( // eslint-disable-line no-new-func
        'handleSSECompletion',
        'createSSEJsonParser',
        `
            let statusCounts = {succeeded: 0, skipped: 0, failed: 0};
            let currentDownloadController = null;
            let currentDownloadRunId = 0;
            ${functions}
            return {
                startBulkDownload,
                downloadAllAsText,
                cancelDownloads,
                getCurrentDownloadController: () => currentDownloadController,
            };
        `,
    );
    const utilitySource = readFileSync(SSE_UTIL_PATH, 'utf8');
    const loadParser = new Function( // eslint-disable-line no-new-func
        `${utilitySource}\nreturn createSSEJsonParser;`,
    );
    return factory(handleSSECompletion, loadParser());
}

function renderDownloadProgress() {
    document.head.innerHTML = '<meta name="csrf-token" content="csrf-download">';
    document.body.innerHTML = `
        <select id="target-collection">
            <option value="collection-7" selected>Migration docs</option>
        </select>
        <button id="extract-all-text"><span>Extract all text</span></button>
        <div id="download-progress-modal"><h2></h2></div>
        <div class="ldr-status-breakdown" style="display: none"></div>
        <span id="succeeded-count">9</span>
        <span id="skipped-count">9</span>
        <span id="failed-count">9</span>
        <div id="overall-progress"></div>
        <span id="current-count"></span>
        <span id="total-count"></span>
        <div id="download-log"></div>
    `;
}

function streamingChunks(...chunks) {
    const reads = chunks.map(chunk => ({
        done: false,
        value: new globalThis.TextEncoder().encode(chunk),
    }));
    const read = vi.fn();
    for (const result of reads) read.mockResolvedValueOnce(result);
    read.mockResolvedValueOnce({ done: true, value: undefined });
    return {
        ok: true,
        status: 200,
        body: { getReader: vi.fn(() => ({ read })) },
    };
}

function streamingResponse(...payloads) {
    const chunk = payloads.map(payload => (
        `data: ${JSON.stringify(payload)}\n`
    )).join('');
    return streamingChunks(chunk);
}

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

beforeEach(() => {
    renderDownloadProgress();
    window.escapeHtml = vi.fn(value => String(value));
});

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

it('posts the selected collection and renders streamed success/skip/failure totals', async () => {
    const events = [
        { progress: 25, current: 1, total: 4, status: 'success', file: 'one.pdf' },
        { progress: 50, current: 2, total: 4, status: 'skipped', file: 'two.pdf', error: 'already stored' },
        { progress: 75, current: 3, total: 4, status: 'failed', file: 'three.pdf', error: 'timeout' },
        {
            complete: true,
            progress: 100,
            current: 4,
            total: 4,
            status: 'complete',
        },
    ];
    const fetchMock = vi.fn().mockResolvedValue(streamingResponse(...events));
    vi.stubGlobal('fetch', fetchMock);
    const handleSSECompletion = vi.fn(data => data.status === 'complete');
    const harness = compileBulkDownload(handleSSECompletion);

    await harness.startBulkDownload(['research-1', 'research-2'], 'pdf');

    expect(fetchMock).toHaveBeenCalledOnce();
    const [url, options] = fetchMock.mock.calls[0];
    expect(url).toBe('/library/api/download-bulk');
    expect(options).toMatchObject({
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
            'X-CSRFToken': 'csrf-download',
        },
        body: JSON.stringify({
            research_ids: ['research-1', 'research-2'],
            mode: 'pdf',
            collection_id: 'collection-7',
        }),
    });
    expect(options.signal).toBeInstanceOf(globalThis.AbortSignal);
    expect(document.querySelector('#download-progress-modal h2').textContent)
        .toBe('Downloading PDFs');
    expect(document.getElementById('overall-progress').style.width).toBe('100%');
    expect(document.getElementById('current-count').textContent).toBe('4');
    expect(document.getElementById('total-count').textContent).toBe('4');
    expect(document.getElementById('succeeded-count').textContent).toBe('1');
    expect(document.getElementById('skipped-count').textContent).toBe('1');
    expect(document.getElementById('failed-count').textContent).toBe('1');
    expect(document.querySelector('.ldr-status-breakdown').style.display).toBe('');
    expect(document.querySelectorAll('#download-log .ldr-download-entry'))
        .toHaveLength(3);
    expect(handleSSECompletion).toHaveBeenCalledTimes(4);
    expect(harness.getCurrentDownloadController()).toBeNull();
});

it('omits an empty collection and selects the text-only progress title', async () => {
    document.getElementById('target-collection').value = '';
    const fetchMock = vi.fn().mockResolvedValue(streamingResponse({
        complete: true,
        progress: 100,
        current: 1,
        total: 1,
        status: 'complete',
    }));
    vi.stubGlobal('fetch', fetchMock);

    await compileBulkDownload(vi.fn(() => true))
        .startBulkDownload(['research-3'], 'text_only');

    const requestBody = JSON.parse(fetchMock.mock.calls[0][1].body);
    expect(requestBody).toEqual({
        research_ids: ['research-3'],
        mode: 'text_only',
    });
    expect(document.querySelector('#download-progress-modal h2').textContent)
        .toBe('Extracting Text to Database');
});

it('processes split progress once and reports EOF without a terminal frame', async () => {
    const fetchMock = vi.fn().mockResolvedValue(streamingChunks(
        'data: {"progress":50,"current":1,"total":2,"status":"suc',
        'cess","file":"split.pdf"}\n',
    ));
    vi.stubGlobal('fetch', fetchMock);
    const alertMock = vi.fn();
    vi.stubGlobal('alert', alertMock);
    const errorSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
    const handleSSECompletion = vi.fn(() => false);
    const harness = compileBulkDownload(handleSSECompletion);

    await harness.startBulkDownload(['research-split'], 'pdf');

    expect(document.querySelectorAll('#download-log .ldr-download-entry'))
        .toHaveLength(1);
    expect(document.getElementById('succeeded-count').textContent).toBe('1');
    expect(handleSSECompletion).toHaveBeenCalledOnce();
    expect(handleSSECompletion.mock.calls[0][0]).toEqual({
        progress: 50,
        current: 1,
        total: 2,
        status: 'success',
        file: 'split.pdf',
    });
    expect(errorSpy).toHaveBeenCalledWith(
        'Download error:',
        expect.objectContaining({
            message: 'Download stream ended before completion',
        }),
    );
    expect(alertMock).toHaveBeenCalledWith(
        'Failed to start downloads. Please try again.',
    );
    expect(harness.getCurrentDownloadController()).toBeNull();
});

it('surfaces a non-ok bulk response instead of consuming its JSON as SSE', async () => {
    const fetchMock = vi.fn().mockResolvedValue(new Response(JSON.stringify({
        detail: 'CSRF token rejected',
    }), {
        status: 403,
        headers: { 'Content-Type': 'application/json' },
    }));
    vi.stubGlobal('fetch', fetchMock);
    const alertMock = vi.fn();
    vi.stubGlobal('alert', alertMock);
    const errorSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
    const handleSSECompletion = vi.fn();
    const harness = compileBulkDownload(handleSSECompletion);

    await harness.startBulkDownload(['research-forbidden'], 'pdf');

    expect(errorSpy).toHaveBeenCalledWith(
        'Download error:',
        expect.objectContaining({ message: 'HTTP error! status: 403' }),
    );
    expect(alertMock).toHaveBeenCalledWith(
        'Failed to start downloads. Please try again.',
    );
    expect(handleSSECompletion).not.toHaveBeenCalled();
    expect(harness.getCurrentDownloadController()).toBeNull();
});

it('keeps cancellation bound to a newer text run after an older PDF completes', async () => {
    const olderPdf = pausableStreamResponse({
        complete: true,
        progress: 100,
        current: 1,
        total: 1,
        status: 'success',
        file: 'stale-old.pdf',
    });
    const newerText = pausableStreamResponse({
        complete: true,
        progress: 100,
        current: 1,
        total: 1,
        status: 'success',
        file: 'cancelled-late.txt',
    });
    const fetchMock = vi.fn()
        .mockResolvedValueOnce(olderPdf.response)
        .mockResolvedValueOnce(newerText.response);
    vi.stubGlobal('fetch', fetchMock);
    const alertMock = vi.fn();
    vi.stubGlobal('alert', alertMock);
    const handleSSECompletion = vi.fn(data => data.complete === true);
    const harness = compileBulkDownload(handleSSECompletion);

    const olderRun = harness.startBulkDownload(['research-pdf'], 'pdf');
    await vi.waitFor(() => {
        expect(fetchMock).toHaveBeenCalledOnce();
    });
    const olderSignal = fetchMock.mock.calls[0][1].signal;

    const textButton = document.getElementById('extract-all-text');
    vi.stubGlobal('event', { target: textButton.querySelector('span') });
    const newerRun = harness.downloadAllAsText();
    await vi.waitFor(() => {
        expect(fetchMock).toHaveBeenCalledTimes(2);
    });
    const newerSignal = fetchMock.mock.calls[1][1].signal;
    expect(document.querySelector('#download-progress-modal h2').textContent)
        .toBe('Text Extraction to Database');
    expect(document.getElementById('overall-progress').style.width).toBe('0%');

    olderPdf.finish();
    await olderRun;

    expect(handleSSECompletion).not.toHaveBeenCalled();
    expect(document.getElementById('overall-progress').style.width).toBe('0%');
    expect(document.getElementById('download-log').textContent)
        .not.toContain('stale-old.pdf');
    expect(document.querySelector('#download-progress-modal h2').textContent)
        .toBe('Text Extraction to Database');
    expect(olderSignal.aborted).toBe(true);
    expect(newerSignal.aborted).toBe(false);
    expect(harness.getCurrentDownloadController()?.signal).toBe(newerSignal);

    const timeoutMock = vi.fn();
    vi.stubGlobal('setTimeout', timeoutMock);
    document.getElementById('download-progress-modal').style.display = 'block';
    harness.cancelDownloads();

    expect(olderSignal.aborted).toBe(true);
    expect(newerSignal.aborted).toBe(true);
    expect(harness.getCurrentDownloadController()).toBeNull();
    expect(timeoutMock).toHaveBeenCalledOnce();

    const cancelledProgress = document.getElementById('overall-progress');
    expect(cancelledProgress.classList.contains('ldr-cancelled')).toBe(true);
    cancelledProgress.style.width = '75%';
    document.getElementById('current-count').textContent = '3';
    document.getElementById('total-count').textContent = '4';

    const replacementPdf = pausableStreamResponse({
        complete: true,
        progress: 100,
        current: 1,
        total: 1,
        status: 'success',
        file: 'replacement.pdf',
    });
    fetchMock.mockResolvedValueOnce(replacementPdf.response);
    const replacementRun = harness.startBulkDownload(['replacement'], 'pdf');

    expect(cancelledProgress.classList.contains('ldr-cancelled')).toBe(false);
    expect(cancelledProgress.style.width).toBe('0%');
    expect(document.getElementById('current-count').textContent).toBe('0');
    expect(document.getElementById('total-count').textContent).toBe('0');
    expect(document.getElementById('download-log').textContent).toBe('');
    timeoutMock.mock.calls[0][0]();
    expect(document.getElementById('download-progress-modal').style.display)
        .toBe('block');

    newerText.finishEOF();
    await newerRun;

    expect(handleSSECompletion).not.toHaveBeenCalled();
    expect(alertMock).not.toHaveBeenCalled();
    expect(document.getElementById('download-log').textContent)
        .not.toContain('cancelled-late.txt');

    replacementPdf.finish();
    await replacementRun;
});

it('streams the dedicated text-extraction endpoint across SSE chunks', async () => {
    const button = document.getElementById('extract-all-text');
    vi.stubGlobal('event', { target: button.querySelector('span') });
    const fetchMock = vi.fn().mockResolvedValue(streamingChunks(
        'data: {"progress":50,"current":1,"total":2,"status":"suc',
        'cess","file":"split.txt"}\n',
        'data: {"complete":true,"progress":100,"current":2,"total":2,"status":"complete"}\n',
    ));
    vi.stubGlobal('fetch', fetchMock);
    const handleSSECompletion = vi.fn(data => data.complete === true);
    const harness = compileBulkDownload(handleSSECompletion);

    await harness.downloadAllAsText();

    expect(fetchMock).toHaveBeenCalledOnce();
    expect(fetchMock).toHaveBeenCalledWith(
        '/library/api/download-all-text',
        {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
                'X-CSRFToken': 'csrf-download',
            },
            body: JSON.stringify({}),
            signal: expect.any(globalThis.AbortSignal),
        },
    );
    expect(document.querySelector('#download-progress-modal h2').textContent)
        .toBe('Text Extraction to Database');
    expect(document.getElementById('overall-progress').style.width).toBe('100%');
    expect(document.getElementById('succeeded-count').textContent).toBe('1');
    expect(document.querySelectorAll('#download-log .ldr-download-entry'))
        .toHaveLength(1);
    expect(handleSSECompletion).toHaveBeenCalledTimes(2);
    expect(handleSSECompletion.mock.calls[0][0]).toEqual({
        progress: 50,
        current: 1,
        total: 2,
        status: 'success',
        file: 'split.txt',
    });
    expect(button.disabled).toBe(false);
    expect(button.textContent).toContain('Extract all text');
    expect(harness.getCurrentDownloadController()).toBeNull();
});

it('reports an empty text-extraction stream and restores its action', async () => {
    const button = document.getElementById('extract-all-text');
    vi.stubGlobal('event', { target: button.querySelector('span') });
    vi.stubGlobal('fetch', vi.fn().mockResolvedValue(streamingChunks()));
    const alertMock = vi.fn();
    vi.stubGlobal('alert', alertMock);
    const errorSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
    const handleSSECompletion = vi.fn();
    const harness = compileBulkDownload(handleSSECompletion);

    await harness.downloadAllAsText();

    expect(handleSSECompletion).not.toHaveBeenCalled();
    expect(errorSpy).toHaveBeenCalledWith(
        'Failed to start text extraction:',
        expect.objectContaining({
            message: 'Text extraction stream ended before completion',
        }),
    );
    expect(alertMock).toHaveBeenCalledWith(
        'Failed to start text extraction. Please try again.',
    );
    expect(button.disabled).toBe(false);
    expect(button.textContent).toContain('Extract all text');
    expect(harness.getCurrentDownloadController()).toBeNull();
});

it('turns an aborted stream into a visible cancellation entry', async () => {
    const abortError = new Error('cancelled');
    abortError.name = 'AbortError';
    vi.stubGlobal('fetch', vi.fn().mockRejectedValue(abortError));
    const log = vi.spyOn(console, 'log').mockImplementation(() => {});

    await compileBulkDownload(vi.fn()).startBulkDownload(['research-4']);

    expect(log).toHaveBeenCalledWith('Download cancelled by user');
    const entry = document.querySelector('#download-log .ldr-cancelled');
    expect(entry).not.toBeNull();
    expect(entry.textContent).toContain('Download cancelled by user');
});

const HOSTILE_RESEARCH_ID = "research\"&<');window.__researchActionXss=true;//";

function renderResearchRows() {
    document.body.innerHTML = `
        <div class="ldr-research-list">
            <div class="ldr-research-item">
                <div class="ldr-research-header"><h3>Delegated research</h3></div>
                <div class="ldr-research-actions">
                    <button class="btn btn-sm" data-research-action="view"><i></i> View Research</button>
                    <button class="btn btn-sm btn-success" data-research-action="download-pdf"><i></i> PDFs</button>
                    <button class="btn btn-sm btn-info" data-research-action="download-text"><i></i> Text Only</button>
                    <button class="btn btn-sm" data-research-action="view-downloaded"><i></i> View Downloaded</button>
                </div>
            </div>
            <div class="ldr-research-item" data-id="pdf-storage-off">
                <div class="ldr-research-actions">
                    <button class="btn btn-sm btn-success" data-research-action="download-pdf" disabled><i></i> PDFs</button>
                </div>
            </div>
        </div>
    `;
    // The ID goes in through the DOM API, never through markup interpolation.
    document.querySelector('.ldr-research-item')
        .setAttribute('data-id', HOSTILE_RESEARCH_ID);
}

it('keeps research IDs out of the row markup inline handlers', () => {
    const template = readFileSync(TEMPLATE_PATH, 'utf8');

    expect(template).not.toMatch(/\bon[a-z]+="[^"]*\{\{\s*research\.id\b/);
});

it('delegates every row action through the research list', () => {
    renderResearchRows();
    const actions = {
        viewResearch: vi.fn(),
        downloadResearch: vi.fn(),
        viewDownloaded: vi.fn(),
    };
    const bindResearchItemActions = compileTemplateHarness({
        templatePath: TEMPLATE_PATH,
        functionNames: ['handleResearchItemAction', 'bindResearchItemActions'],
        dependencies: actions,
        returnExpression: 'bindResearchItemActions',
    });
    bindResearchItemActions();

    const row = document.querySelector('.ldr-research-item');
    expect(row.dataset.id).toBe(HOSTILE_RESEARCH_ID);
    for (const action of ['view', 'download-pdf', 'download-text', 'view-downloaded']) {
        row.querySelector(`[data-research-action="${action}"] i`).click();
    }
    row.querySelector('h3').click();

    expect(actions.viewResearch.mock.calls).toEqual([[HOSTILE_RESEARCH_ID]]);
    expect(actions.downloadResearch.mock.calls).toEqual([
        [HOSTILE_RESEARCH_ID, 'pdf'],
        [HOSTILE_RESEARCH_ID, 'text_only'],
    ]);
    expect(actions.viewDownloaded.mock.calls).toEqual([[HOSTILE_RESEARCH_ID]]);

    // With PDF storage off the template disables the PDF button. A click that
    // lands on its icon must not start a download.
    document.querySelector('[data-id="pdf-storage-off"] i').click();
    expect(actions.downloadResearch).toHaveBeenCalledTimes(2);
});