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.

34.3 KB
Raw Blame
/**
 * Runtime contracts for the library page's inline content viewer, sync and
 * document card actions.
 *
 * These execute the checked-in template functions, covering browser-visible
 * FastAPI request paths and modal/error state rather than duplicating them in
 * a hand-written fixture implementation.
 */

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/library.html',
);
const SSE_UTIL_PATH = resolve(
    __dirname,
    '../../../src/local_deep_research/web/static/js/utils/sse-completion.js',
);

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

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 compileContentViewer() {
    const template = readFileSync(TEMPLATE_PATH, 'utf8');
    const functions = [
        'openDocument',
        'openPDF',
        'closePDFModal',
        'handleEscapeKey',
        'openText',
        'closeTextModal',
        'handleTextEscapeKey',
    ].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
        `
            let currentPDFData = null;
            let currentTextData = null;
            let documentViewerRequestId = 0;
            ${functions}
            return {
                openDocument,
                openPDF,
                closePDFModal,
                openText,
                closeTextModal,
            };
        `,
    );
    return factory();
}

function compilePerformSync() {
    const template = readFileSync(TEMPLATE_PATH, 'utf8');
    const source = extractFunction(template, 'performSync');
    // The extracted source is repository-owned production code from the
    // template above, not user-controlled input.
    return new Function(`return (${source});`)(); // eslint-disable-line no-new-func
}

function loadSSEUtilities() {
    const utilitySource = readFileSync(SSE_UTIL_PATH, 'utf8');
    return new Function( // eslint-disable-line no-new-func
        `${utilitySource}\nreturn {
            createSSEJsonParser,
            handleSSECompletion,
        };`,
    )();
}

function compileTextDownload(dependencies) {
    const template = readFileSync(TEMPLATE_PATH, 'utf8');
    const source = [
        'resetDownloadProgress',
        'downloadAllAsText',
    ].map(name => extractFunction(template, name)).join('\n');
    const runtimeDependencies = {
        ...dependencies,
        createSSEJsonParser: loadSSEUtilities().createSSEJsonParser,
    };
    const dependencyNames = Object.keys(runtimeDependencies);
    // 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
        ...dependencyNames,
        `
            let currentDownloadController = null;
            let currentDownloadRunId = 0;
            ${source}
            return downloadAllAsText;
        `,
    );
    return factory(...Object.values(runtimeDependencies));
}

function compileLibraryDownloads(dependencies) {
    const template = readFileSync(TEMPLATE_PATH, 'utf8');
    const functions = [
        'resetDownloadProgress',
        'downloadAllAsText',
        'downloadAllNew',
        'closeProgressModal',
        'cancelDownloads',
    ].map(name => extractFunction(template, name)).join('\n');
    const runtimeDependencies = {
        ...dependencies,
        createSSEJsonParser: loadSSEUtilities().createSSEJsonParser,
    };
    const dependencyNames = Object.keys(runtimeDependencies);
    // 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
        ...dependencyNames,
        `
            let currentDownloadController = null;
            let currentDownloadRunId = 0;
            ${functions}
            return {
                downloadAllAsText,
                downloadAllNew,
                cancelDownloads,
                getCurrentDownloadController: () => currentDownloadController,
            };
        `,
    );
    return factory(...Object.values(runtimeDependencies));
}

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

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

function renderViewer() {
    document.body.innerHTML = `
        <div id="ldr-pdf-modal" style="display: none"></div>
        <iframe id="pdf-viewer"></iframe>
        <h2 id="ldr-pdf-title"></h2>
        <div id="text-modal" style="display: none"></div>
        <pre id="text-viewer"></pre>
        <h2 id="text-title"></h2>
    `;
}

function renderSync() {
    document.head.innerHTML = '<meta name="csrf-token" content="csrf-sync">';
    document.body.innerHTML = `
        <button id="sync-start-btn"><i></i>Start Sync</button>
        <div class="ldr-sync-explanation"></div>
        <div id="sync-progress" style="display: none"></div>
        <div id="sync-results" style="display: none"></div>
        <div id="sync-status"></div>
        <span id="files-found"></span>
        <span id="files-missing"></span>
        <div id="missing-files-action" style="display: none"></div>
    `;
}

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

it('prefers the PDF endpoint and closes the populated modal on Escape', async () => {
    renderViewer();
    const fetchMock = vi.fn().mockResolvedValue({
        ok: true,
        json: vi.fn().mockResolvedValue({
            url: '/library/api/document/doc-7/pdf',
            title: 'Migration report',
        }),
    });
    vi.stubGlobal('fetch', fetchMock);

    compileContentViewer().openDocument('doc-7', true, true);

    await vi.waitFor(() => {
        expect(document.getElementById('ldr-pdf-modal').style.display)
            .toBe('block');
    });
    expect(fetchMock).toHaveBeenCalledWith(
        '/library/api/document/doc-7/pdf-url',
    );
    expect(document.getElementById('pdf-viewer').getAttribute('src'))
        .toBe('/library/api/document/doc-7/pdf');
    expect(document.getElementById('ldr-pdf-title').textContent)
        .toBe('Migration report');

    document.dispatchEvent(new KeyboardEvent('keydown', { key: 'Escape' }));
    expect(document.getElementById('ldr-pdf-modal').style.display).toBe('none');
    expect(document.getElementById('pdf-viewer').getAttribute('src')).toBe('');
});

it('falls back to encrypted text and renders content as text, not markup', async () => {
    renderViewer();
    const fetchMock = vi.fn().mockResolvedValue({
        ok: true,
        json: vi.fn().mockResolvedValue({
            text_content: '<img src=x onerror=alert(1)>plain text',
            title: 'Text-only document',
        }),
    });
    vi.stubGlobal('fetch', fetchMock);

    compileContentViewer().openDocument('doc-8', false, true);

    await vi.waitFor(() => {
        expect(document.getElementById('text-modal').style.display)
            .toBe('block');
    });
    expect(fetchMock).toHaveBeenCalledWith('/library/api/document/doc-8/text');
    const viewer = document.getElementById('text-viewer');
    expect(viewer.textContent).toBe('<img src=x onerror=alert(1)>plain text');
    expect(viewer.querySelector('img')).toBeNull();

    document.dispatchEvent(new KeyboardEvent('keydown', { key: 'Escape' }));
    expect(document.getElementById('text-modal').style.display).toBe('none');
    expect(viewer.textContent).toBe('');
});

it('does not fetch when no stored content exists and gives the user feedback', () => {
    renderViewer();
    const fetchMock = vi.fn();
    const alertMock = vi.fn();
    vi.stubGlobal('fetch', fetchMock);
    vi.stubGlobal('alert', alertMock);
    vi.spyOn(console, 'error').mockImplementation(() => {});

    compileContentViewer().openDocument('doc-9', false, false);

    expect(fetchMock).not.toHaveBeenCalled();
    expect(alertMock).toHaveBeenCalledWith(
        'No content available for this document',
    );
});

it.each([
    {
        kind: 'PDF',
        open: viewer => viewer.openPDF('missing-pdf'),
        expectedUrl: '/library/api/document/missing-pdf/pdf-url',
        expectedAlert: 'Failed to load PDF content',
    },
    {
        kind: 'text',
        open: viewer => viewer.openText('missing-text'),
        expectedUrl: '/library/api/document/missing-text/text',
        expectedAlert: 'Failed to load text content',
    },
])('surfaces a non-ok $kind viewer response without opening a blank modal', async ({
    open,
    expectedUrl,
    expectedAlert,
}) => {
    renderViewer();
    const json = vi.fn();
    const fetchMock = vi.fn().mockResolvedValue({
        ok: false,
        status: 404,
        json,
    });
    vi.stubGlobal('fetch', fetchMock);
    const alertMock = vi.fn();
    vi.stubGlobal('alert', alertMock);
    vi.spyOn(console, 'error').mockImplementation(() => {});

    await open(compileContentViewer());

    expect(fetchMock).toHaveBeenCalledWith(expectedUrl);
    expect(json).not.toHaveBeenCalled();
    expect(alertMock).toHaveBeenCalledWith(expectedAlert);
    expect(document.getElementById('ldr-pdf-modal').style.display).toBe('none');
    expect(document.getElementById('text-modal').style.display).toBe('none');
});

it('keeps a newer text viewer authoritative over an older PDF request', async () => {
    renderViewer();
    const olderPdf = deferred();
    const fetchMock = vi.fn()
        .mockImplementationOnce(() => olderPdf.promise)
        .mockResolvedValueOnce({
            ok: true,
            json: vi.fn().mockResolvedValue({
                text_content: 'Current encrypted text',
                title: 'Current document',
            }),
        });
    vi.stubGlobal('fetch', fetchMock);
    const viewer = compileContentViewer();

    const oldLoad = viewer.openPDF('old-pdf');
    await viewer.openText('current-text');
    olderPdf.resolve({
        ok: true,
        json: vi.fn().mockResolvedValue({
            url: '/library/api/document/old-pdf/pdf',
            title: 'Stale document',
        }),
    });
    await oldLoad;

    expect(document.getElementById('text-modal').style.display).toBe('block');
    expect(document.getElementById('text-viewer').textContent)
        .toBe('Current encrypted text');
    expect(document.getElementById('text-title').textContent)
        .toBe('Current document');
    expect(document.getElementById('ldr-pdf-modal').style.display).toBe('none');
    expect(document.getElementById('pdf-viewer').getAttribute('src')).toBeNull();
});

it.each([
    {
        label: 'PDF success',
        modalId: 'ldr-pdf-modal',
        open: viewer => viewer.openPDF('late-pdf'),
        close: viewer => viewer.closePDFModal(),
        response: {
            ok: true,
            json: vi.fn().mockResolvedValue({
                url: '/library/api/document/late-pdf/pdf',
                title: 'Must stay closed',
            }),
        },
    },
    {
        label: 'text failure',
        modalId: 'text-modal',
        open: viewer => viewer.openText('late-text'),
        close: viewer => viewer.closeTextModal(),
        response: { ok: false, status: 503 },
    },
])('keeps an explicit close authoritative over a late $label', async ({
    modalId,
    open,
    close,
    response,
}) => {
    renderViewer();
    const pendingResponse = deferred();
    vi.stubGlobal('fetch', vi.fn(() => pendingResponse.promise));
    const alertMock = vi.fn();
    vi.stubGlobal('alert', alertMock);
    vi.spyOn(console, 'error').mockImplementation(() => {});
    const viewer = compileContentViewer();
    document.getElementById(modalId).style.display = 'block';

    const request = open(viewer);
    close(viewer);
    pendingResponse.resolve(response);
    await request;

    expect(document.getElementById(modalId).style.display).toBe('none');
    expect(document.getElementById('pdf-viewer').getAttribute('src') || '')
        .toBe('');
    expect(document.getElementById('ldr-pdf-title').textContent).toBe('');
    expect(document.getElementById('text-viewer').textContent).toBe('');
    expect(document.getElementById('text-title').textContent).toBe('');
    expect(alertMock).not.toHaveBeenCalled();
});

it('syncs through the FastAPI route and exposes missing-file recovery', async () => {
    renderSync();
    const fetchMock = vi.fn().mockResolvedValue({
        ok: true,
        json: vi.fn().mockResolvedValue({ files_found: 12, files_missing: 2 }),
    });
    vi.stubGlobal('fetch', fetchMock);

    await compilePerformSync()();

    expect(fetchMock).toHaveBeenCalledWith('/library/api/sync-library', {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
            'X-CSRFToken': 'csrf-sync',
        },
    });
    expect(document.getElementById('sync-progress').style.display).toBe('none');
    expect(document.getElementById('sync-results').style.display).toBe('block');
    expect(document.getElementById('files-found').textContent).toBe('12');
    expect(document.getElementById('files-missing').textContent).toBe('2');
    expect(document.getElementById('missing-files-action').style.display)
        .toBe('block');
    expect(document.getElementById('sync-start-btn').style.display).toBe('none');
});

it('restores the sync controls after an HTTP failure', async () => {
    renderSync();
    vi.stubGlobal('fetch', vi.fn().mockResolvedValue({ ok: false, status: 503 }));
    const alertMock = vi.fn();
    vi.stubGlobal('alert', alertMock);
    vi.spyOn(console, 'error').mockImplementation(() => {});

    await compilePerformSync()();

    const button = document.getElementById('sync-start-btn');
    expect(button.disabled).toBe(false);
    expect(button.textContent).toContain('Start Sync');
    expect(document.querySelector('.ldr-sync-explanation').style.display)
        .toBe('block');
    expect(document.getElementById('sync-progress').style.display).toBe('none');
    expect(document.getElementById('sync-status').textContent)
        .toBe('Sync failed. Please try again.');
    expect(alertMock).toHaveBeenCalledWith(
        'Failed to sync library. Please check your connection and try again.',
    );
});

it('processes split text progress once and reports premature EOF', async () => {
    document.head.innerHTML = '<meta name="csrf-token" content="csrf-text">';
    document.body.innerHTML = `
        <button id="extract-text"><span>Extract</span></button>
        <div id="download-progress-modal"><h2></h2></div>
        <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('extract-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","url":"https://example.test/doc"}\n',
    ));
    vi.stubGlobal('fetch', fetchMock);
    const alertMock = vi.fn();
    vi.stubGlobal('alert', alertMock);
    const errorSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
    const updateTextExtractionProgress = vi.fn();
    const handleSSECompletion = vi.fn(() => false);
    const downloadAllAsText = compileTextDownload({
        showDownloadProgressModal: vi.fn(),
        updateTextExtractionProgress,
        handleSSECompletion,
    });

    await downloadAllAsText();

    expect(fetchMock).toHaveBeenCalledOnce();
    expect(fetchMock.mock.calls[0][0]).toBe('/library/api/download-all-text');
    expect(updateTextExtractionProgress).toHaveBeenCalledOnce();
    expect(updateTextExtractionProgress).toHaveBeenCalledWith({
        progress: 50,
        current: 1,
        total: 2,
        status: 'success',
        file: 'split.txt',
        url: 'https://example.test/doc',
    });
    expect(handleSSECompletion).toHaveBeenCalledOnce();
    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);
});

it('ignores superseded library stream frames and frames delivered after cancellation', async () => {
    document.head.innerHTML = '<meta name="csrf-token" content="csrf-stream">';
    document.body.innerHTML = `
        <button id="download-all"><span>Download all</span></button>
        <button id="extract-text"><span>Extract</span></button>
        <div id="download-progress-modal"><h2></h2></div>
        <div id="ldr-overall-progress" style="width: 0%"></div>
        <span id="current-count">0</span>
        <span id="total-count">0</span>
        <div id="ldr-download-log"></div>
    `;
    const olderBulk = pausableStreamResponse({
        complete: true,
        progress: 100,
        current: 1,
        total: 1,
        status: 'success',
        file: 'stale-library.pdf',
    });
    const newerText = pausableStreamResponse({
        complete: true,
        progress: 100,
        current: 1,
        total: 1,
        status: 'success',
        file: 'cancelled-library.txt',
    });
    const fetchMock = vi.fn()
        .mockResolvedValueOnce({
            ok: true,
            status: 200,
            json: vi.fn().mockResolvedValue({
                queued: 1,
                research_ids: ['stale-library'],
            }),
        })
        .mockResolvedValueOnce(olderBulk.response)
        .mockResolvedValueOnce(newerText.response);
    vi.stubGlobal('fetch', fetchMock);
    const alertMock = vi.fn();
    vi.stubGlobal('alert', alertMock);
    const handleSSECompletion = vi.fn(data => data.complete);
    const updateDownloadProgress = vi.fn();
    const updateTextExtractionProgress = vi.fn();
    const runtime = compileLibraryDownloads({
        showDownloadProgressModal: vi.fn(),
        updateDownloadProgress,
        updateTextExtractionProgress,
        handleSSECompletion,
    });

    vi.stubGlobal('event', {
        target: document.querySelector('#download-all span'),
    });
    const olderRun = runtime.downloadAllNew();
    await olderBulk.started;
    const olderSignal = fetchMock.mock.calls[1][1].signal;

    event.target = document.querySelector('#extract-text span');
    const newerRun = runtime.downloadAllAsText();
    await newerText.started;
    const newerSignal = fetchMock.mock.calls[2][1].signal;

    olderBulk.finish();
    await olderRun;

    expect(updateDownloadProgress).not.toHaveBeenCalled();
    expect(handleSSECompletion).not.toHaveBeenCalled();
    expect(document.querySelector('#download-progress-modal h2').textContent)
        .toBe('Text Extraction to Database');
    expect(olderSignal.aborted).toBe(true);
    expect(newerSignal.aborted).toBe(false);
    expect(runtime.getCurrentDownloadController()?.signal).toBe(newerSignal);

    const timeoutMock = vi.fn();
    vi.stubGlobal('setTimeout', timeoutMock);
    document.getElementById('download-progress-modal').style.display = 'block';
    runtime.cancelDownloads();
    expect(newerSignal.aborted).toBe(true);
    expect(timeoutMock).toHaveBeenCalledOnce();

    const cancelledProgress = document.getElementById('ldr-overall-progress');
    expect(cancelledProgress.style.backgroundColor).toBe('var(--error-color)');
    cancelledProgress.style.width = '75%';
    document.getElementById('current-count').textContent = '3';
    document.getElementById('total-count').textContent = '4';
    document.getElementById('ldr-download-log').textContent = 'cancelled run';

    const replacementText = pausableStreamResponse({
        complete: true,
        progress: 25,
        current: 1,
        total: 4,
        status: 'success',
        file: 'replacement.txt',
    });
    fetchMock.mockResolvedValueOnce(replacementText.response);
    event.target = document.getElementById('extract-text');
    const replacementRun = runtime.downloadAllAsText();
    await replacementText.started;

    expect(cancelledProgress.style.backgroundColor).toBe('');
    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('ldr-download-log').textContent).toBe('');
    timeoutMock.mock.calls[0][0]();
    expect(document.getElementById('download-progress-modal').style.display)
        .toBe('block');

    newerText.finishEOF();
    await newerRun;

    expect(updateTextExtractionProgress).not.toHaveBeenCalled();
    expect(handleSSECompletion).not.toHaveBeenCalled();
    expect(alertMock).not.toHaveBeenCalled();

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

it('aborts an active library text stream before download-all takes ownership', async () => {
    document.head.innerHTML = '<meta name="csrf-token" content="csrf-stream">';
    document.body.innerHTML = `
        <button id="download-all"><span>Download all</span></button>
        <button id="extract-text"><span>Extract</span></button>
        <div id="download-progress-modal"><h2></h2></div>
        <div id="ldr-overall-progress"></div>
        <span id="current-count"></span>
        <span id="total-count"></span>
        <div id="ldr-download-log"></div>
    `;
    const olderText = pausableStreamResponse({
        progress: 50,
        current: 1,
        total: 2,
        status: 'success',
        file: 'superseded.txt',
    });
    const fetchMock = vi.fn()
        .mockResolvedValueOnce(olderText.response)
        .mockResolvedValueOnce({
            ok: true,
            status: 200,
            json: vi.fn().mockResolvedValue({
                queued: 0,
                research_ids: [],
            }),
        });
    vi.stubGlobal('fetch', fetchMock);
    vi.stubGlobal('alert', vi.fn());
    const updateTextExtractionProgress = vi.fn();
    const runtime = compileLibraryDownloads({
        showDownloadProgressModal: vi.fn(),
        updateDownloadProgress: vi.fn(),
        updateTextExtractionProgress,
        handleSSECompletion: vi.fn(),
    });

    vi.stubGlobal('event', {
        target: document.querySelector('#extract-text span'),
    });
    const olderRun = runtime.downloadAllAsText();
    await olderText.started;
    const olderSignal = fetchMock.mock.calls[0][1].signal;

    event.target = document.querySelector('#download-all span');
    await runtime.downloadAllNew();

    expect(olderSignal.aborted).toBe(true);
    expect(fetchMock.mock.calls.map(([url]) => url)).toEqual([
        '/library/api/download-all-text',
        '/library/api/queue-all-undownloaded',
    ]);
    expect(runtime.getCurrentDownloadController()).toBeNull();

    olderText.finish();
    await olderRun;
    expect(updateTextExtractionProgress).not.toHaveBeenCalled();
});

it('surfaces a non-ok text response and restores the library action', async () => {
    document.head.innerHTML = '<meta name="csrf-token" content="csrf-text">';
    document.body.innerHTML = `
        <button id="extract-text"><span>Extract</span></button>
        <div id="download-progress-modal"><h2></h2></div>
        <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('extract-text');
    vi.stubGlobal('event', { target: button.querySelector('span') });
    vi.stubGlobal('fetch', vi.fn().mockResolvedValue(new Response(
        JSON.stringify({ detail: 'Text extraction unavailable' }),
        {
            status: 503,
            headers: { 'Content-Type': 'application/json' },
        },
    )));
    const alertMock = vi.fn();
    vi.stubGlobal('alert', alertMock);
    const errorSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
    const updateTextExtractionProgress = vi.fn();
    const handleSSECompletion = vi.fn();
    const downloadAllAsText = compileTextDownload({
        showDownloadProgressModal: vi.fn(),
        updateTextExtractionProgress,
        handleSSECompletion,
    });

    await downloadAllAsText();

    expect(errorSpy).toHaveBeenCalledWith(
        'Failed to start text extraction:',
        expect.objectContaining({ message: 'HTTP error! status: 503' }),
    );
    expect(alertMock).toHaveBeenCalledWith(
        'Failed to start text extraction. Please try again.',
    );
    expect(updateTextExtractionProgress).not.toHaveBeenCalled();
    expect(handleSSECompletion).not.toHaveBeenCalled();
    expect(button.disabled).toBe(false);
    expect(button.textContent).toContain('Extract');
});

it('completes text extraction with the DB-only backend contract', async () => {
    vi.useFakeTimers();
    document.head.innerHTML = '<meta name="csrf-token" content="csrf-text">';
    document.body.innerHTML = `
        <button id="extract-text"><span>Extract</span></button>
        <div id="download-progress-modal"><h2></h2></div>
        <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('extract-text');
    vi.stubGlobal('event', { target: button.querySelector('span') });
    vi.stubGlobal('fetch', vi.fn().mockResolvedValue(streamingChunks(
        'data: {"complete":true,"total":7}\n',
    )));
    const alertMock = vi.fn();
    vi.stubGlobal('alert', alertMock);
    window.closeProgressModal = vi.fn();
    const utilities = loadSSEUtilities();
    const updateTextExtractionProgress = vi.fn();
    const downloadAllAsText = compileTextDownload({
        showDownloadProgressModal: vi.fn(),
        updateTextExtractionProgress,
        handleSSECompletion: utilities.handleSSECompletion,
    });

    await downloadAllAsText();

    expect(updateTextExtractionProgress).toHaveBeenCalledOnce();
    expect(alertMock).not.toHaveBeenCalled();
    await vi.advanceTimersByTimeAsync(2000);

    expect(window.closeProgressModal).toHaveBeenCalledOnce();
    expect(alertMock).toHaveBeenCalledOnce();
    expect(alertMock).toHaveBeenCalledWith(
        'Text extraction complete! Extracted 7 documents to encrypted database.',
    );
    expect(button.disabled).toBe(false);
});

it('suppresses a delayed completion after a newer extraction becomes current', async () => {
    vi.useFakeTimers();
    document.head.innerHTML = '<meta name="csrf-token" content="csrf-text">';
    document.body.innerHTML = `
        <button id="extract-text"><span>Extract</span></button>
        <div id="download-progress-modal"><h2></h2></div>
        <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('extract-text');
    vi.stubGlobal('event', { target: button.querySelector('span') });
    vi.stubGlobal('fetch', vi.fn()
        .mockResolvedValueOnce(streamingChunks(
            'data: {"complete":true,"total":7}\n',
        ))
        .mockResolvedValueOnce(streamingChunks(
            'data: {"complete":true,"total":10}\n',
        )));
    const alertMock = vi.fn();
    vi.stubGlobal('alert', alertMock);
    window.closeProgressModal = vi.fn();
    const utilities = loadSSEUtilities();
    const downloadAllAsText = compileTextDownload({
        showDownloadProgressModal: vi.fn(),
        updateTextExtractionProgress: vi.fn(),
        handleSSECompletion: utilities.handleSSECompletion,
    });

    await downloadAllAsText();
    event.target = button.querySelector('span');
    await downloadAllAsText();
    await vi.advanceTimersByTimeAsync(2000);

    expect(window.closeProgressModal).toHaveBeenCalledOnce();
    expect(alertMock).toHaveBeenCalledOnce();
    expect(alertMock).toHaveBeenCalledWith(
        'Text extraction complete! Extracted 10 documents to encrypted database.',
    );
    expect(alertMock).not.toHaveBeenCalledWith(
        'Text extraction complete! Extracted 7 documents to encrypted database.',
    );
});

const HOSTILE_DOCUMENT_ID = "doc\"&<');window.__libraryCardActionXss=true;//";

function renderDocumentCards() {
    // The wrapping action outside the container checks the listener ignores
    // matches that lie beyond the documents container.
    document.body.innerHTML = `
        <section data-doc-action="delete">
            <div id="documents-container">
                <div class="ldr-document-card">
                    <div class="card-body" data-doc-action="open">
                        <h3 class="ldr-doc-title">Delegated card</h3>
                    </div>
                    <div class="card-footer">
                        <div class="ldr-doc-tags"><span class="ldr-tag">example.org</span></div>
                        <div class="ldr-doc-actions">
                            <button class="ldr-btn-delete-blob" data-doc-action="delete-blob"><i class="fas fa-file-pdf"></i></button>
                            <button class="ldr-btn-delete-doc" data-doc-action="delete"><i class="fas fa-trash"></i></button>
                        </div>
                    </div>
                </div>
                <div class="ldr-semantic-result"><h4>Content-only match</h4></div>
            </div>
        </section>
    `;
    // The ID goes in through the DOM API, never through markup interpolation.
    document.querySelector('.ldr-document-card')
        .setAttribute('data-doc-id', HOSTILE_DOCUMENT_ID);
}

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

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

it('delegates opening and both deletions through the documents container', () => {
    renderDocumentCards();
    const deleteManager = {
        deleteDocument: vi.fn(),
        deleteDocumentBlob: vi.fn(),
    };
    const fakeLocation = { reload: vi.fn() };
    const fakeWindow = { location: { href: '/library/' } };
    const bindDocumentCardActions = compileTemplateHarness({
        templatePath: TEMPLATE_PATH,
        functionNames: ['handleDocumentCardAction', 'bindDocumentCardActions'],
        dependencies: {
            DeleteManager: deleteManager,
            location: fakeLocation,
            window: fakeWindow,
        },
        returnExpression: 'bindDocumentCardActions',
    });
    const reachedDocument = vi.fn();
    document.addEventListener('click', reachedDocument);
    try {
        bindDocumentCardActions();
        const card = document.querySelector('.ldr-document-card');
        expect(card.dataset.docId).toBe(HOSTILE_DOCUMENT_ID);

        card.querySelector('.ldr-btn-delete-doc i').click();
        expect(deleteManager.deleteDocument).toHaveBeenCalledOnce();
        expect(deleteManager.deleteDocument).toHaveBeenCalledWith(
            HOSTILE_DOCUMENT_ID, { onSuccess: expect.any(Function) },
        );
        deleteManager.deleteDocument.mock.calls[0][1].onSuccess();
        expect(fakeLocation.reload).toHaveBeenCalledOnce();

        card.querySelector('.ldr-btn-delete-blob i').click();
        expect(deleteManager.deleteDocumentBlob).toHaveBeenCalledOnce();
        expect(deleteManager.deleteDocumentBlob).toHaveBeenCalledWith(
            HOSTILE_DOCUMENT_ID, { onSuccess: expect.any(Function) },
        );
        // Deletions stay inside the card and never open the document.
        expect(reachedDocument).not.toHaveBeenCalled();
        expect(fakeWindow.location.href).toBe('/library/');

        card.querySelector('.ldr-doc-title').click();
        expect(fakeWindow.location.href).toBe(
            "/library/document/doc%22%26%3C')%3Bwindow.__libraryCardActionXss%3Dtrue%3B%2F%2F",
        );
        expect(reachedDocument).toHaveBeenCalledOnce();

        fakeWindow.location.href = '/library/';
        document.querySelector('.ldr-doc-tags').click();
        document.querySelector('.ldr-semantic-result h4').click();
        expect(fakeWindow.location.href).toBe('/library/');
        expect(deleteManager.deleteDocument).toHaveBeenCalledOnce();
        expect(deleteManager.deleteDocumentBlob).toHaveBeenCalledOnce();
    } finally {
        document.removeEventListener('click', reachedDocument);
    }
});