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.0 KB
Raw Blame
/**
 * Runtime contract for the journal-data download action embedded in the
 * dashboard template.  This executes the checked-in handler so its FastAPI
 * request and the real progress element IDs stay in sync.
 */

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

const TEMPLATE_PATH = resolve(
    __dirname,
    '../../../src/local_deep_research/web/templates/pages/journal_quality.html',
);

const JOURNAL_PAYLOAD_HELPERS = [
    '_journalPayloadText',
    '_appendRetryAfter',
    '_journal503Text',
];

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 journalPayloadHelperSource(template) {
    return JOURNAL_PAYLOAD_HELPERS
        .filter(name => template.includes(`function ${name}(`))
        .map(name => extractFunction(template, name))
        .join('\n');
}

function extractAsyncListener(source, target, eventName) {
    const signature = `${target}.addEventListener('${eventName}', async () => {`;
    const start = source.indexOf(signature);
    if (start === -1) {
        throw new Error(`${target} ${eventName} listener not found`);
    }

    const openBrace = source.indexOf('{', start + signature.length - 1);
    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) {
                const registrationEnd = source.indexOf(');', index);
                return source.slice(start, registrationEnd + 2);
            }
        }
    }

    throw new Error(`${target} ${eventName} listener is unterminated`);
}

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

function loadDownloadHandler({ renderProgress, pollDownloadProgress }) {
    const template = readFileSync(TEMPLATE_PATH, 'utf8');
    const source = [
        journalPayloadHelperSource(template),
        extractFunction(template, 'downloadJournalData'),
    ].filter(Boolean).join('\n');
    // Repository-owned production source only; no user-controlled input.
    return new Function( // eslint-disable-line no-new-func
        '_renderProgress',
        '_pollDownloadProgress',
        `
            let _ldrProgressTimer = null;
            let _ldrProgressGeneration = 0;
            let _ldrProgressRequestId = 0;
            let _ldrLatestAppliedProgressRequestId = 0;
            ${source}
            return downloadJournalData;
        `,
    )(renderProgress, pollDownloadProgress);
}

function loadDownloadRuntime(renderProgress) {
    const template = readFileSync(TEMPLATE_PATH, 'utf8');
    const pollSource = extractFunction(template, '_pollDownloadProgress');
    const downloadSource = extractFunction(template, 'downloadJournalData');
    const helperSource = journalPayloadHelperSource(template);
    // Repository-owned production source only; no user-controlled input.
    return new Function( // eslint-disable-line no-new-func
        '_renderProgress', `
            let _ldrProgressTimer = null;
            let _ldrProgressGeneration = 0;
            let _ldrProgressRequestId = 0;
            let _ldrLatestAppliedProgressRequestId = 0;
            ${helperSource}
            ${pollSource}
            ${downloadSource}
            return {
                pollDownloadProgress: _pollDownloadProgress,
                downloadJournalData,
            };
        `,
    )(renderProgress);
}

function loadPageOwnershipRuntime({ renderProgress, renderSourcesBanner }) {
    const template = readFileSync(TEMPLATE_PATH, 'utf8');
    const productionSource = [
        journalPayloadHelperSource(template),
        extractFunction(template, 'checkDataStatus'),
        extractFunction(template, '_pollDownloadProgress'),
        extractFunction(template, 'downloadJournalData'),
        extractAsyncListener(template, 'document', 'DOMContentLoaded'),
        extractAsyncListener(template, 'window', 'load'),
    ].filter(Boolean).join('\n');
    let domReadyHandler;
    let windowLoadHandler;
    const pageDocument = {
        addEventListener: vi.fn((eventName, handler) => {
            if (eventName === 'DOMContentLoaded') domReadyHandler = handler;
        }),
        getElementById: id => document.getElementById(id),
    };
    const pageWindow = {
        api: window.api,
        addEventListener: vi.fn((eventName, handler) => {
            if (eventName === 'load') windowLoadHandler = handler;
        }),
    };
    // Repository-owned production source and listener registrations only;
    // the surrounding declarations replace unrelated dashboard consumers.
    const runtime = new Function( // eslint-disable-line no-new-func
        'document',
        'window',
        '_renderProgress',
        'renderSourcesBanner',
        `
            let _ldrProgressTimer = null;
            let _ldrProgressGeneration = 0;
            let _ldrProgressRequestId = 0;
            let _ldrLatestAppliedProgressRequestId = 0;
            let thresholdLoaded = false;
            let userResearchLoaded = false;
            const loadThreshold = () => {};
            const loadUserResearchJournals = () => {};
            ${productionSource}
            return {
                checkDataStatus,
                downloadJournalData,
                getProgressTimer: () => _ldrProgressTimer,
                getProgressGeneration: () => _ldrProgressGeneration,
            };
        `,
    )(
        pageDocument,
        pageWindow,
        renderProgress,
        renderSourcesBanner,
    );

    return {
        ...runtime,
        runDomReady: () => domReadyHandler(),
        runWindowLoad: () => windowLoadHandler(),
    };
}

beforeEach(() => {
    vi.useFakeTimers();
    document.body.innerHTML = `
        <button id="ldr-data-download-btn">Download Data</button>
        <div id="ldr-data-progress-intro">Idle</div>
        <div id="ldr-data-banner" style="display: none"></div>
        <span id="ldr-data-banner-text">Owned banner</span>
        <div id="ldr-loading"></div>
        <div id="ldr-content" style="display: none"></div>
        <div id="ldr-tab-your-research" style="display: none"></div>
        <div id="ldr-tab-global-db"></div>
        <div id="ldr-tab-how-it-works"></div>
    `;
    window.api = { getCsrfToken: vi.fn(() => 'csrf-journal') };
});

afterEach(() => {
    vi.clearAllTimers();
    vi.useRealTimers();
    vi.restoreAllMocks();
    vi.unstubAllGlobals();
    delete window.api;
    document.body.replaceChildren();
});

it('POSTs with CSRF and renders the successful download envelope', async () => {
    const fetchMock = vi.fn().mockResolvedValue(new Response(JSON.stringify({
        success: true,
        message: 'Reference database installed',
    }), { status: 200 }));
    vi.stubGlobal('fetch', fetchMock);
    const renderProgress = vi.fn();
    const downloadJournalData = loadDownloadHandler({
        renderProgress,
        pollDownloadProgress: vi.fn(),
    });

    await downloadJournalData();

    expect(fetchMock).toHaveBeenCalledWith(
        '/metrics/api/journal-data/download',
        {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
                'X-CSRFToken': 'csrf-journal',
            },
            body: JSON.stringify({ force: false }),
        },
    );
    expect(renderProgress).toHaveBeenCalledWith({
        state: 'running',
        sources: {},
        db_build: { state: 'pending' },
    });
    expect(document.getElementById('ldr-data-progress-intro').textContent)
        .toBe('[100%] Complete — Reference database installed');
    expect(document.getElementById('ldr-data-download-btn').disabled)
        .toBe(true);
});

it('restores the action and uses the real progress element on API rejection', async () => {
    const fetchMock = vi.fn().mockResolvedValue(new Response(JSON.stringify({
        success: false,
        message: 'OpenAlex download failed',
    }), { status: 502 }));
    vi.stubGlobal('fetch', fetchMock);
    const downloadJournalData = loadDownloadHandler({
        renderProgress: vi.fn(),
        pollDownloadProgress: vi.fn(),
    });

    await downloadJournalData();

    expect(document.getElementById('ldr-data-progress-intro').textContent)
        .toBe('Error: OpenAlex download failed');
    const button = document.getElementById('ldr-data-download-btn');
    expect(button.disabled).toBe(false);
    expect(button.textContent).toBe('Retry Download');
});

const DATABASE_UNAVAILABLE_DETAIL = 'Your database is temporarily unavailable. Please try again in a moment.';

function downloadResponse(body, { status = 503, retryAfter = null } = {}) {
    const headers = { 'Content-Type': 'application/json' };
    if (retryAfter != null) headers['Retry-After'] = String(retryAfter);
    return new Response(JSON.stringify(body), { status, headers });
}

it('renders a 503 detail envelope without the word undefined', async () => {
    vi.stubGlobal('fetch', vi.fn().mockResolvedValue(downloadResponse(
        { detail: DATABASE_UNAVAILABLE_DETAIL },
        { retryAfter: '5' },
    )));
    const downloadJournalData = loadDownloadHandler({
        renderProgress: vi.fn(),
        pollDownloadProgress: vi.fn(),
    });

    await downloadJournalData();

    const progress = document.getElementById('ldr-data-progress-intro');
    expect.soft(progress.textContent).toBe(
        `Error: ${DATABASE_UNAVAILABLE_DETAIL} Retry after 5 seconds.`,
    );
    expect.soft(progress.textContent).not.toContain('undefined');
    expect.soft(progress.innerHTML).not.toContain('<');
    const button = document.getElementById('ldr-data-download-btn');
    expect(button.disabled).toBe(false);
    expect(button.textContent).toBe('Retry Download');
});

it.each([
    {
        envelope: 'error string',
        body: { error: 'Reference rebuild is busy' },
        expectedText: 'Error: Reference rebuild is busy',
    },
    {
        envelope: 'empty message falling through to detail',
        body: { message: '', detail: DATABASE_UNAVAILABLE_DETAIL },
        expectedText: `Error: ${DATABASE_UNAVAILABLE_DETAIL}`,
    },
    {
        envelope: 'message preferred over detail',
        body: {
            message: 'OpenAlex download failed',
            detail: DATABASE_UNAVAILABLE_DETAIL,
        },
        expectedText: 'Error: OpenAlex download failed',
    },
    {
        envelope: 'non-string detail',
        body: { detail: [{ msg: 'nope' }] },
        expectedText: 'Error: Download failed',
    },
    {
        envelope: 'HTTP-date Retry-After',
        body: { detail: DATABASE_UNAVAILABLE_DETAIL },
        retryAfter: 'Wed, 21 Oct 2026 07:28:00 GMT',
        expectedText: `Error: ${DATABASE_UNAVAILABLE_DETAIL} Retry after Wed, 21 Oct 2026 07:28:00 GMT.`,
    },
    {
        envelope: 'one-second Retry-After',
        body: { detail: DATABASE_UNAVAILABLE_DETAIL },
        retryAfter: '1',
        expectedText: `Error: ${DATABASE_UNAVAILABLE_DETAIL} Retry after 1 second.`,
    },
    {
        envelope: 'markup detail',
        body: { detail: '<b>retry</b>' },
        expectedText: 'Error: <b>retry</b>',
    },
])('does not render undefined for a 503 $envelope envelope', async ({
    body,
    retryAfter = null,
    expectedText,
}) => {
    vi.stubGlobal('fetch', vi.fn().mockResolvedValue(
        downloadResponse(body, { retryAfter }),
    ));
    const downloadJournalData = loadDownloadHandler({
        renderProgress: vi.fn(),
        pollDownloadProgress: vi.fn(),
    });

    await downloadJournalData();

    const progress = document.getElementById('ldr-data-progress-intro');
    expect.soft(progress.textContent).not.toContain('undefined');
    expect.soft(progress.textContent).toBe(expectedText);
    if (body.detail === '<b>retry</b>') {
        expect(progress.innerHTML).not.toContain('<b>');
    }
});

it.each([
    {
        terminalState: 'completion',
        result: { success: true, message: 'Reference database installed' },
        expectedText: '[100%] Complete — Reference database installed',
        expectedButtonDisabled: true,
    },
    {
        terminalState: 'error',
        result: { success: false, message: 'OpenAlex download failed' },
        expectedText: 'Error: OpenAlex download failed',
        expectedButtonDisabled: false,
    },
])('does not let an in-flight poll overwrite POST $terminalState', async ({
    result,
    expectedText,
    expectedButtonDisabled,
}) => {
    const postResponse = deferred();
    const pollJson = deferred();
    const fetchMock = vi.fn(url => {
        if (url === '/metrics/api/journal-data/download') {
            return postResponse.promise;
        }
        if (url === '/metrics/api/journal-data/status') {
            return Promise.resolve({
                ok: true,
                json: vi.fn(() => pollJson.promise),
            });
        }
        throw new Error(`Unexpected request: ${url}`);
    });
    vi.stubGlobal('fetch', fetchMock);
    const renderProgress = vi.fn();
    const runtime = loadDownloadRuntime(renderProgress);

    const downloadRun = runtime.downloadJournalData();
    const stalePoll = runtime.pollDownloadProgress();
    expect(fetchMock.mock.calls.map(([url]) => url)).toEqual([
        '/metrics/api/journal-data/download',
        '/metrics/api/journal-data/status',
    ]);

    postResponse.resolve(new Response(JSON.stringify(result), { status: 200 }));
    await downloadRun;
    expect(document.getElementById('ldr-data-progress-intro').textContent)
        .toBe(expectedText);

    pollJson.resolve({
        download_progress: {
            state: 'running',
            sources: { openalex: { state: 'running', percent: 80 } },
        },
    });
    await stalePoll;

    expect(renderProgress).toHaveBeenCalledOnce();
    expect(renderProgress).toHaveBeenCalledWith({
        state: 'running',
        sources: {},
        db_build: { state: 'pending' },
    });
    expect(document.getElementById('ldr-data-progress-intro').textContent)
        .toBe(expectedText);
    expect(document.getElementById('ldr-data-download-btn').disabled)
        .toBe(expectedButtonDisabled);
});

it('keeps deferred page-load status listeners from stealing download ownership', async () => {
    const domReadyStatus = deferred();
    const windowLoadStatus = deferred();
    const activeDownloadStatus = deferred();
    const postResponse = deferred();
    let statusRequestCount = 0;
    const fetchMock = vi.fn(url => {
        if (url === '/metrics/api/journal-data/status') {
            statusRequestCount += 1;
            if (statusRequestCount === 1) return domReadyStatus.promise;
            if (statusRequestCount === 2) return windowLoadStatus.promise;
            return activeDownloadStatus.promise;
        }
        if (url === '/metrics/api/journal-data/download') {
            return postResponse.promise;
        }
        throw new Error(`Unexpected request: ${url}`);
    });
    vi.stubGlobal('fetch', fetchMock);
    const renderProgress = vi.fn();
    const renderSourcesBanner = vi.fn();
    const runtime = loadPageOwnershipRuntime({
        renderProgress,
        renderSourcesBanner,
    });

    const domReadyRun = runtime.runDomReady();
    const windowLoadRun = runtime.runWindowLoad();
    const downloadRun = runtime.downloadJournalData();
    const activeStatusRun = runtime.checkDataStatus();
    const ownedProgressTimer = runtime.getProgressTimer();

    expect(fetchMock.mock.calls.map(([url]) => url)).toEqual([
        '/metrics/api/journal-data/status',
        '/metrics/api/journal-data/status',
        '/metrics/api/journal-data/download',
        '/metrics/api/journal-data/status',
    ]);
    expect(runtime.getProgressGeneration()).toBe(1);
    expect(ownedProgressTimer).not.toBeNull();
    expect(vi.getTimerCount()).toBe(1);

    const staleStatus = {
        available: false,
        needs_update: false,
        sources: [{ name: 'Stale source', present: false }],
        download_progress: {
            state: 'running',
            sources: { openalex: { state: 'running', percent: 80 } },
        },
    };
    domReadyStatus.resolve(new Response(JSON.stringify(staleStatus), {
        status: 200,
    }));
    activeDownloadStatus.resolve(new Response(JSON.stringify(staleStatus), {
        status: 200,
    }));
    await Promise.all([domReadyRun, activeStatusRun]);

    expect(runtime.getProgressTimer()).toBe(ownedProgressTimer);
    expect(vi.getTimerCount()).toBe(1);
    expect(renderProgress).toHaveBeenCalledOnce();
    expect(renderProgress).toHaveBeenCalledWith({
        state: 'running',
        sources: {},
        db_build: { state: 'pending' },
    });
    expect(renderSourcesBanner).not.toHaveBeenCalled();
    expect(document.getElementById('ldr-data-download-btn').textContent)
        .toBe('Downloading...');
    expect(document.getElementById('ldr-data-banner-text').textContent)
        .toBe('Owned banner');

    postResponse.resolve(new Response(JSON.stringify({
        success: false,
        message: 'OpenAlex download failed',
    }), { status: 502 }));
    await downloadRun;

    expect(runtime.getProgressGeneration()).toBe(2);
    expect(runtime.getProgressTimer()).toBeNull();
    expect(vi.getTimerCount()).toBe(0);
    expect(document.getElementById('ldr-data-progress-intro').textContent)
        .toBe('Error: OpenAlex download failed');
    const button = document.getElementById('ldr-data-download-btn');
    expect(button.disabled).toBe(false);
    expect(button.textContent).toBe('Retry Download');

    // The window-load checkDataStatus request was also started before the
    // click, but finishes after the POST reached its terminal state.
    windowLoadStatus.resolve(new Response(JSON.stringify(staleStatus), {
        status: 200,
    }));
    await windowLoadRun;

    expect(renderSourcesBanner).not.toHaveBeenCalled();
    expect(runtime.getProgressTimer()).toBeNull();
    expect(vi.getTimerCount()).toBe(0);
    expect(document.getElementById('ldr-data-progress-intro').textContent)
        .toBe('Error: OpenAlex download failed');
    expect(button.disabled).toBe(false);
    expect(button.textContent).toBe('Retry Download');
    expect(document.getElementById('ldr-data-banner-text').textContent)
        .toBe('Owned banner');
});

it.each([
    {
        state: 'success',
        expectedText: '[100%] Complete — Journal data is ready.',
        expectedButton: 'Downloaded',
        expectedDisabled: true,
    },
    {
        state: 'error',
        error_msg: 'Required OpenAlex source failed',
        expectedText: 'Error: Required OpenAlex source failed',
        expectedButton: 'Retry Download',
        expectedDisabled: false,
    },
])('settles resumed polling when the backend reports $state', async ({
    state,
    error_msg: errorMessage,
    expectedText,
    expectedButton,
    expectedDisabled,
}) => {
    const reload = vi.spyOn(window.location, 'reload').mockImplementation(() => {});
    const running = {
        state: 'running',
        sources: { openalex: { state: 'running', percent: 25 } },
        db_build: { state: 'pending' },
    };
    const terminal = {
        state,
        error_msg: errorMessage,
        sources: { openalex: { state } },
        db_build: { state },
    };
    const fetchMock = vi.fn()
        .mockResolvedValueOnce(new Response(JSON.stringify({
            download_progress: running,
        }), { status: 200 }))
        .mockResolvedValueOnce(new Response(JSON.stringify({
            download_progress: terminal,
        }), { status: 200 }));
    vi.stubGlobal('fetch', fetchMock);
    const renderProgress = vi.fn();
    const runtime = loadPageOwnershipRuntime({
        renderProgress,
        renderSourcesBanner: vi.fn(),
    });

    await runtime.runDomReady();
    expect(runtime.getProgressTimer()).not.toBeNull();
    expect(vi.getTimerCount()).toBe(1);

    await vi.advanceTimersByTimeAsync(2000);

    expect(fetchMock).toHaveBeenCalledTimes(2);
    expect(renderProgress).toHaveBeenNthCalledWith(1, running);
    expect(renderProgress).toHaveBeenNthCalledWith(2, terminal);
    expect(runtime.getProgressTimer()).toBeNull();
    expect(runtime.getProgressGeneration()).toBe(1);
    expect(vi.getTimerCount()).toBe(state === 'success' ? 1 : 0);
    expect(document.getElementById('ldr-data-progress-intro').textContent)
        .toBe(expectedText);
    const button = document.getElementById('ldr-data-download-btn');
    expect(button.textContent).toBe(expectedButton);
    expect(button.disabled).toBe(expectedDisabled);
    await vi.advanceTimersByTimeAsync(1500);
    expect(reload).toHaveBeenCalledTimes(state === 'success' ? 1 : 0);
});

it('refreshes the journal table once when polling completes before the POST', async () => {
    const postResponse = deferred();
    const reload = vi.spyOn(window.location, 'reload').mockImplementation(() => {});
    vi.stubGlobal('fetch', vi.fn()
        .mockImplementationOnce(() => postResponse.promise)
        .mockResolvedValueOnce(new Response(JSON.stringify({
            download_progress: { state: 'success', sources: {}, db_build: { state: 'success' } },
        }), { status: 200 })));
    const runtime = loadDownloadRuntime(vi.fn());

    const download = runtime.downloadJournalData();
    await vi.advanceTimersByTimeAsync(2000);
    expect(document.getElementById('ldr-data-download-btn').textContent).toBe('Downloaded');

    postResponse.resolve(new Response(JSON.stringify({
        success: true, message: 'Reference database installed',
    }), { status: 200 }));
    await download;
    await vi.advanceTimersByTimeAsync(1500);
    expect(reload).toHaveBeenCalledOnce();
    expect(vi.getTimerCount()).toBe(0);
});