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.

35.7 KB
Raw Blame
/** Runtime FastAPI contracts for journal_quality.html. */

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/journal_quality.html',
);

const JOURNAL_REFERENCE_DB_UNAVAILABLE = 'Journal reference database not available.';
const DATABASE_UNAVAILABLE_DETAIL = 'Your database is temporarily unavailable. Please try again in a moment.';

const JOURNAL_PAYLOAD_HELPERS = [
    '_journalPayloadText',
    '_appendRetryAfter',
    '_journal503Text',
    '_isMissingReferenceDb',
];
const RETRY_RELOAD = ' Reload the page to retry.';

function withJournalPayloadHelpers(functionNames) {
    const source = readFileSync(TEMPLATE_PATH, 'utf8');
    const present = JOURNAL_PAYLOAD_HELPERS.filter(name =>
        source.includes(`function ${name}(`),
    );
    return [...present, ...functionNames];
}

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

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

function renderThresholdFixture() {
    document.head.innerHTML = '<meta name="csrf-token" content="csrf-journal">';
    document.body.innerHTML = `
        <span id="ldr-threshold-save-status"></span>
        <span id="ldr-threshold-save-status-top"></span>
    `;
}

function renderJournalFixture() {
    document.body.innerHTML = `
        <input id="ldr-journal-search" value="climate & health">
        <select id="ldr-filter-tier"><option value="elite" selected>Elite</option></select>
        <select id="ldr-filter-source"><option value="doaj" selected>DOAJ</option></select>
        <div id="ldr-error" style="display: none"></div>
        <div id="ldr-loading" style="display: block"></div>
        <span id="stat-total"></span>
        <span id="stat-avg-quality"></span>
        <span id="stat-predatory"></span>
        <span id="stat-doaj"></span>
        <span id="stat-h-index"></span>
    `;
}

function compileJournalPageRuntime(dependencies) {
    return compileTemplateHarness({
        templatePath: TEMPLATE_PATH,
        functionNames: withJournalPayloadHelpers([
            'buildApiUrl',
            'loadJournalPage',
        ]),
        dependencies,
        preamble: `
            const PAGE_SIZE = 50;
            let sortField = 'quality';
            let sortDir = 'desc';
            let currentPage = 1;
            let totalPages = 1;
            let totalCount = 0;
            let summaryLoaded = false;
            let journalPageRequestId = 0;
        `,
        returnExpression: `({
            loadJournalPage,
            getPage: () => currentPage,
            getTotalCount: () => totalCount,
        })`,
    });
}

afterEach(() => {
    vi.clearAllTimers();
    vi.useRealTimers();
    vi.restoreAllMocks();
    vi.unstubAllGlobals();
    delete window.api;
    delete window.ui;
    document.head.replaceChildren();
    document.body.replaceChildren();
    window.history.replaceState({}, '', '/');
});

it('PUTs the threshold with its CSRF token and renders the success envelope', async () => {
    vi.useFakeTimers();
    renderThresholdFixture();
    const fetchMock = vi.fn().mockResolvedValue({ ok: true });
    vi.stubGlobal('fetch', fetchMock);
    window.ui = { showMessage: vi.fn() };
    const harness = compileTemplateHarness({
        templatePath: TEMPLATE_PATH,
        functionNames: ['saveThreshold'],
        preamble: `
            let thresholdIntentGeneration = 0;
            let thresholdSaveTail = Promise.resolve();
        `,
        returnExpression: '({ saveThreshold })',
    });

    await harness.saveThreshold(7);

    expect(fetchMock).toHaveBeenCalledWith(
        '/settings/api/search.journal_reputation.threshold',
        {
            method: 'PUT',
            headers: {
                'Content-Type': 'application/json',
                'X-CSRFToken': 'csrf-journal',
            },
            body: JSON.stringify({ value: 7 }),
        },
    );
    expect(document.getElementById('ldr-threshold-save-status').textContent)
        .toBe('✓ Saved');
    expect(window.ui.showMessage).toHaveBeenCalledWith(
        'Journal quality threshold updated to 7',
        'success',
    );
});

it('surfaces a failed threshold update without claiming it was saved', async () => {
    renderThresholdFixture();
    vi.stubGlobal('fetch', vi.fn().mockResolvedValue({
        ok: false,
        status: 422,
    }));
    window.ui = { showMessage: vi.fn() };
    const error = vi.spyOn(console, 'error').mockImplementation(() => {});
    const harness = compileTemplateHarness({
        templatePath: TEMPLATE_PATH,
        functionNames: ['saveThreshold'],
        preamble: `
            let thresholdIntentGeneration = 0;
            let thresholdSaveTail = Promise.resolve();
        `,
        returnExpression: '({ saveThreshold })',
    });

    await harness.saveThreshold(11);

    expect(error).toHaveBeenCalled();
    expect(document.getElementById('ldr-threshold-save-status').textContent)
        .toBe('✗ Save failed');
    expect(window.ui.showMessage).toHaveBeenCalledWith(
        'Failed to save threshold setting',
        'error',
    );
});

it('loads the nested threshold setting and falls back after an HTTP error', async () => {
    const updateThresholdExplanation = vi.fn();
    const fetchMock = vi.fn()
        .mockResolvedValueOnce({
            ok: true,
            json: vi.fn().mockResolvedValue({
                setting: { value: 6 },
            }),
        })
        .mockResolvedValueOnce({ ok: false, status: 500 });
    vi.stubGlobal('fetch', fetchMock);
    vi.spyOn(console, 'error').mockImplementation(() => {});
    const harness = compileTemplateHarness({
        templatePath: TEMPLATE_PATH,
        functionNames: ['loadThreshold'],
        dependencies: { updateThresholdExplanation },
        preamble: 'let thresholdIntentGeneration = 0;',
        returnExpression: '({ loadThreshold })',
    });

    await harness.loadThreshold();
    await harness.loadThreshold();

    expect(fetchMock).toHaveBeenNthCalledWith(
        1,
        '/settings/api/search.journal_reputation.threshold',
    );
    expect(updateThresholdExplanation).toHaveBeenNthCalledWith(1, 6);
    expect(updateThresholdExplanation).toHaveBeenNthCalledWith(2, 2);
});

it('does not let threshold hydration overwrite newer slider input', async () => {
    const staleLoad = deferred();
    vi.stubGlobal('fetch', vi.fn(() => staleLoad.promise));
    const updateThresholdExplanation = vi.fn();
    const harness = compileTemplateHarness({
        templatePath: TEMPLATE_PATH,
        functionNames: ['onThresholdInput', 'loadThreshold'],
        dependencies: { updateThresholdExplanation },
        preamble: 'let thresholdIntentGeneration = 0;',
        returnExpression: '({ onThresholdInput, loadThreshold })',
    });

    const load = harness.loadThreshold();
    harness.onThresholdInput('9', 'top');
    staleLoad.resolve({
        ok: true,
        json: vi.fn().mockResolvedValue({ setting: { value: 2 } }),
    });
    await load;

    expect(updateThresholdExplanation).toHaveBeenCalledOnce();
    expect(updateThresholdExplanation).toHaveBeenCalledWith('9');
});

it('serializes threshold writes and releases the latest save after rejection', async () => {
    vi.useFakeTimers();
    renderThresholdFixture();
    const olderSave = deferred();
    const newerSave = deferred();
    const fetchMock = vi.fn()
        .mockImplementationOnce(() => olderSave.promise)
        .mockImplementationOnce(() => newerSave.promise);
    vi.stubGlobal('fetch', fetchMock);
    window.ui = { showMessage: vi.fn() };
    const harness = compileTemplateHarness({
        templatePath: TEMPLATE_PATH,
        functionNames: ['saveThreshold'],
        preamble: `
            let thresholdIntentGeneration = 0;
            let thresholdSaveTail = Promise.resolve();
        `,
        returnExpression: '({ saveThreshold })',
    });

    const olderRequest = harness.saveThreshold(3);
    const newerRequest = harness.saveThreshold(8);
    await Promise.resolve();

    expect(fetchMock).toHaveBeenCalledOnce();
    expect(JSON.parse(fetchMock.mock.calls[0][1].body)).toEqual({ value: 3 });

    olderSave.reject(new Error('older write failed'));
    await olderRequest;
    await Promise.resolve();

    expect(fetchMock).toHaveBeenCalledTimes(2);
    expect(JSON.parse(fetchMock.mock.calls[1][1].body)).toEqual({ value: 8 });

    newerSave.resolve({ ok: true });
    await newerRequest;

    expect(document.getElementById('ldr-threshold-save-status').textContent)
        .toBe('✓ Saved');
    expect(window.ui.showMessage).toHaveBeenCalledOnce();
    expect(window.ui.showMessage).toHaveBeenCalledWith(
        'Journal quality threshold updated to 8',
        'success',
    );
});

it('renders journal-data update status from the migrated status endpoint', async () => {
    document.body.innerHTML = `
        <div id="ldr-data-banner" style="display: none"></div>
        <span id="ldr-data-banner-text"></span>
        <button id="ldr-data-download-btn"></button>
    `;
    const sources = [{ name: 'OpenAlex', present: true }];
    const fetchMock = vi.fn().mockResolvedValue({
        json: vi.fn().mockResolvedValue({
            available: true,
            needs_update: true,
            latest_version: '2026.08',
            sources,
        }),
    });
    vi.stubGlobal('fetch', fetchMock);
    const renderSourcesBanner = vi.fn();
    const harness = compileTemplateHarness({
        templatePath: TEMPLATE_PATH,
        functionNames: withJournalPayloadHelpers(['checkDataStatus']),
        dependencies: { renderSourcesBanner },
        preamble: `
            let _ldrProgressGeneration = 0;
            let _ldrProgressTimer = null;
        `,
        returnExpression: '({ checkDataStatus })',
    });

    await expect(harness.checkDataStatus()).resolves.toBe(true);

    expect(fetchMock).toHaveBeenCalledWith('/metrics/api/journal-data/status');
    expect(renderSourcesBanner).toHaveBeenCalledWith(sources);
    expect(document.getElementById('ldr-data-banner').style.display)
        .toBe('block');
    expect(document.getElementById('ldr-data-banner-text').textContent)
        .toContain('2026.08');
    expect(document.getElementById('ldr-data-download-btn').textContent)
        .toBe('Update Data');
});

it('does not show the first-install download CTA for a generic 503 status', async () => {
    document.body.innerHTML = `
        <div id="ldr-data-banner" style="display: none"></div>
        <span id="ldr-data-banner-text"></span>
        <button id="ldr-data-download-btn"></button>
        <div id="ldr-sources-grid"></div>
    `;
    vi.stubGlobal('fetch', vi.fn().mockResolvedValue(journalListResponse(
        { detail: DATABASE_UNAVAILABLE_DETAIL },
        { retryAfter: '5' },
    )));
    const renderSourcesBanner = vi.fn();
    const harness = compileTemplateHarness({
        templatePath: TEMPLATE_PATH,
        functionNames: withJournalPayloadHelpers(['checkDataStatus']),
        dependencies: { renderSourcesBanner },
        preamble: `
            let _ldrProgressGeneration = 0;
            let _ldrProgressTimer = null;
        `,
        returnExpression: '({ checkDataStatus })',
    });

    const available = await harness.checkDataStatus();

    const bannerText = document.getElementById('ldr-data-banner-text').textContent;
    const button = document.getElementById('ldr-data-download-btn');
    expect.soft(available).toBe(false);
    expect.soft(document.getElementById('ldr-data-banner').style.display)
        .toBe('block');
    expect.soft(bannerText).toBe(
        `${DATABASE_UNAVAILABLE_DETAIL} Retry after 5 seconds.${RETRY_RELOAD}`,
    );
    expect.soft(bannerText).not.toContain('not installed');
    expect.soft(bannerText).not.toContain('undefined');
    expect.soft(button.textContent).toBe('Temporarily unavailable');
    expect.soft(button.textContent).not.toBe('Download Data');
    expect.soft(button.disabled).toBe(true);
    expect.soft(renderSourcesBanner).not.toHaveBeenCalled();
});

it('polls running journal downloads with an explicit JSON response contract', async () => {
    const snapshot = {
        state: 'running',
        sources: { openalex: { state: 'running' } },
    };
    const fetchMock = vi.fn().mockResolvedValue({
        ok: true,
        json: vi.fn().mockResolvedValue({ download_progress: snapshot }),
    });
    vi.stubGlobal('fetch', fetchMock);
    const renderProgress = vi.fn();
    const harness = compileTemplateHarness({
        templatePath: TEMPLATE_PATH,
        functionNames: ['_pollDownloadProgress'],
        dependencies: { _renderProgress: renderProgress },
        preamble: `
            let _ldrProgressGeneration = 0;
            let _ldrProgressTimer = null;
            let _ldrProgressRequestId = 0;
            let _ldrLatestAppliedProgressRequestId = 0;
        `,
        returnExpression: '({ pollDownloadProgress: _pollDownloadProgress })',
    });

    await harness.pollDownloadProgress();

    expect(fetchMock).toHaveBeenCalledWith(
        '/metrics/api/journal-data/status',
        { headers: { Accept: 'application/json' } },
    );
    expect(renderProgress).toHaveBeenCalledWith(snapshot);
});

it('does not regress journal download progress from an older poll', async () => {
    const olderPoll = deferred();
    const newerPoll = deferred();
    vi.stubGlobal('fetch', vi.fn()
        .mockImplementationOnce(() => olderPoll.promise)
        .mockImplementationOnce(() => newerPoll.promise));
    const renderProgress = vi.fn();
    const harness = compileTemplateHarness({
        templatePath: TEMPLATE_PATH,
        functionNames: ['_pollDownloadProgress'],
        dependencies: { _renderProgress: renderProgress },
        preamble: `
            let _ldrProgressGeneration = 0;
            let _ldrProgressTimer = 77;
            let _ldrProgressRequestId = 0;
            let _ldrLatestAppliedProgressRequestId = 0;
        `,
        returnExpression: '({ poll: _pollDownloadProgress })',
    });

    const olderRequest = harness.poll();
    const newerRequest = harness.poll();
    const current = { state: 'running', sources: { openalex: { percent: 80 } } };
    newerPoll.resolve({
        ok: true,
        json: vi.fn().mockResolvedValue({ download_progress: current }),
    });
    await newerRequest;
    olderPoll.resolve({
        ok: true,
        json: vi.fn().mockResolvedValue({
            download_progress: {
                state: 'running',
                sources: { openalex: { percent: 20 } },
            },
        }),
    });
    await olderRequest;

    expect(renderProgress).toHaveBeenCalledOnce();
    expect(renderProgress).toHaveBeenCalledWith(current);
});

it('accepts an older terminal journal snapshot after newer running progress', async () => {
    const terminalPoll = deferred();
    const newerPoll = deferred();
    vi.stubGlobal('fetch', vi.fn()
        .mockImplementationOnce(() => terminalPoll.promise)
        .mockImplementationOnce(() => newerPoll.promise));
    const renderProgress = vi.fn();
    const clearIntervalSpy = vi.spyOn(globalThis, 'clearInterval');
    const harness = compileTemplateHarness({
        templatePath: TEMPLATE_PATH,
        functionNames: ['_pollDownloadProgress'],
        dependencies: { _renderProgress: renderProgress },
        preamble: `
            let _ldrProgressGeneration = 0;
            let _ldrProgressTimer = 78;
            let _ldrProgressRequestId = 0;
            let _ldrLatestAppliedProgressRequestId = 0;
        `,
        returnExpression: `({
            poll: _pollDownloadProgress,
            getTimer: () => _ldrProgressTimer,
        })`,
    });

    const terminalRequest = harness.poll();
    const newerRequest = harness.poll();
    newerPoll.resolve({
        ok: true,
        json: vi.fn().mockResolvedValue({
            download_progress: { state: 'running', sources: {} },
        }),
    });
    await newerRequest;
    terminalPoll.resolve({
        ok: true,
        json: vi.fn().mockResolvedValue({
            download_progress: {
                state: 'success',
                sources: {},
                db_build: { state: 'success' },
            },
        }),
    });
    await terminalRequest;

    expect(renderProgress).toHaveBeenCalledTimes(2);
    expect(renderProgress).toHaveBeenLastCalledWith(expect.objectContaining({
        state: 'success',
    }));
    expect(clearIntervalSpy).toHaveBeenCalledWith(78);
    expect(harness.getTimer()).toBeNull();
});

it('builds the journal list URL from the active filters and summary flag', () => {
    renderJournalFixture();
    const harness = compileTemplateHarness({
        templatePath: TEMPLATE_PATH,
        functionNames: ['buildApiUrl'],
        preamble: `
            const PAGE_SIZE = 50;
            let sortField = 'quality';
            let sortDir = 'desc';
        `,
        returnExpression: '({ buildApiUrl })',
    });

    const url = new URL(harness.buildApiUrl(3, true), 'https://ldr.test');

    expect(url.pathname).toBe('/metrics/api/journals');
    expect(Object.fromEntries(url.searchParams)).toEqual({
        page: '3',
        per_page: '50',
        sort: 'quality',
        order: 'desc',
        search: 'climate & health',
        tier: 'elite',
        score_source: 'doaj',
        include_summary: 'true',
    });
});

function loadJournalListPage(response) {
    renderJournalFixture();
    vi.stubGlobal('fetch', vi.fn().mockResolvedValue(response));
    const renderTable = vi.fn();
    const harness = compileJournalPageRuntime({
        renderTable,
        renderPagination: vi.fn(),
        updateQualityDistChart: vi.fn(),
        updateScoreSourceChart: vi.fn(),
    });
    return harness.loadJournalPage(1, true).then(() => renderTable);
}

it('treats a 503 journal list as the expected first-install state', async () => {
    const renderTable = await loadJournalListPage(journalListResponse({
        status: 'error',
        message: JOURNAL_REFERENCE_DB_UNAVAILABLE,
    }));
    const error = document.getElementById('ldr-error');

    expect(error.style.display).toBe('none');
    expect(error.textContent).toBe('');
    expect(document.getElementById('ldr-loading').style.display).toBe('none');
    expect(renderTable).not.toHaveBeenCalled();
});

it('hides the list error when a reworded message says the reference database is not available', async () => {
    const renderTable = await loadJournalListPage(journalListResponse({
        status: 'error',
        message: 'The journal reference database is not available yet',
    }));
    const error = document.getElementById('ldr-error');

    expect(error.style.display).toBe('none');
    expect(error.textContent).toBe('');
    expect(document.getElementById('ldr-loading').style.display).toBe('none');
    expect(renderTable).not.toHaveBeenCalled();
});

it('hides the list error when that message uses a different case', async () => {
    const renderTable = await loadJournalListPage(journalListResponse({
        status: 'error',
        message: 'JOURNAL REFERENCE DATABASE NOT AVAILABLE.',
    }));
    const error = document.getElementById('ldr-error');

    expect(error.style.display).toBe('none');
    expect(error.textContent).toBe('');
    expect(document.getElementById('ldr-loading').style.display).toBe('none');
    expect(renderTable).not.toHaveBeenCalled();
});

it('shows a retryable banner when only detail says the reference database is not available', async () => {
    const renderTable = await loadJournalListPage(journalListResponse({
        detail: 'Journal reference database not available.',
    }));
    const error = document.getElementById('ldr-error');

    expect(error.style.display).toBe('block');
    expect(error.textContent).toContain('Journal reference database not available.');
    expect(error.textContent).toContain('Reload the page to retry.');
    expect(error.textContent).not.toContain('undefined');
    expect(renderTable).not.toHaveBeenCalled();
});

it('shows a retryable banner when the message does not say the reference database is missing', async () => {
    const message = 'The reference database finished downloading.';
    const renderTable = await loadJournalListPage(journalListResponse({
        status: 'error',
        message,
    }));
    const error = document.getElementById('ldr-error');

    expect(error.style.display).toBe('block');
    expect(error.textContent).toBe(`${message}${RETRY_RELOAD}`);
    expect(renderTable).not.toHaveBeenCalled();
});

it('shows a retryable banner for a generic 503 instead of the first-install state', async () => {
    const renderTable = await loadJournalListPage(journalListResponse(
        { detail: DATABASE_UNAVAILABLE_DETAIL },
        { retryAfter: '5' },
    ));
    const error = document.getElementById('ldr-error');

    expect.soft(error.style.display).toBe('block');
    expect.soft(error.textContent).toBe(
        `${DATABASE_UNAVAILABLE_DETAIL} Retry after 5 seconds.${RETRY_RELOAD}`,
    );
    expect.soft(error.textContent).not.toContain('undefined');
    expect.soft(error.textContent).not.toContain(JOURNAL_REFERENCE_DB_UNAVAILABLE);
    expect.soft(error.textContent).not.toContain('not installed');
    expect.soft(document.getElementById('ldr-loading').style.display).toBe('none');
    expect(renderTable).not.toHaveBeenCalled();
});

it('does not treat a bodyless 503 as the first-install state', async () => {
    const renderTable = await loadJournalListPage(journalListResponse());
    const error = document.getElementById('ldr-error');

    expect.soft(error.style.display).toBe('block');
    expect.soft(error.textContent).toBe(
        `Journal data is temporarily unavailable. Please try again in a moment.${RETRY_RELOAD}`,
    );
    expect.soft(error.textContent).not.toContain('undefined');
    expect.soft(error.textContent).not.toContain(JOURNAL_REFERENCE_DB_UNAVAILABLE);
    expect.soft(document.getElementById('ldr-loading').style.display).toBe('none');
    expect(renderTable).not.toHaveBeenCalled();
});

it('shows a non-sentinel 503 message instead of the first-install state', async () => {
    const renderTable = await loadJournalListPage(journalListResponse({
        status: 'error',
        message: 'Service temporarily overloaded.',
    }));
    const error = document.getElementById('ldr-error');

    expect.soft(error.style.display).toBe('block');
    expect.soft(error.textContent).toBe(
        `Service temporarily overloaded.${RETRY_RELOAD}`,
    );
    expect.soft(error.textContent).not.toContain('undefined');
    expect.soft(error.textContent).not.toContain(JOURNAL_REFERENCE_DB_UNAVAILABLE);
    expect(renderTable).not.toHaveBeenCalled();
});

it('renders a generic 503 detail as text rather than HTML', async () => {
    const detail = '<img src=x onerror=alert(1)>';
    const renderTable = await loadJournalListPage(journalListResponse({ detail }));
    const error = document.getElementById('ldr-error');

    expect.soft(error.style.display).toBe('block');
    expect.soft(error.textContent).toContain(detail);
    expect.soft(error.innerHTML).not.toContain('<img');
    expect.soft(error.textContent).not.toContain('undefined');
    expect(renderTable).not.toHaveBeenCalled();
});

it('keeps the journal-list missing-database wording aligned with the metrics route', () => {
    const metrics = readFileSync(resolve(
        __dirname,
        '../../../src/local_deep_research/web/routers/metrics.py',
    ), 'utf8');
    const messages = [...metrics.matchAll(/"message":\s*"([^"]*)"/g)]
        .map(match => match[1].toLowerCase());
    expect(messages.some(message =>
        message.includes('reference database') && message.includes('not available'),
    )).toBe(true);
});

it('renders journal rows, pagination, and summary from the success envelope', async () => {
    renderJournalFixture();
    const journals = [{ id: 'issn-1', name: 'Journal A' }];
    vi.stubGlobal('fetch', vi.fn().mockResolvedValue({
        status: 200,
        json: vi.fn().mockResolvedValue({
            status: 'success',
            journals,
            pagination: { page: 2, total_pages: 4, total_count: 151 },
            summary: {
                total: 151,
                avg_quality: 7.4,
                predatory_count: 3,
                doaj_count: 99,
                avg_h_index: null,
                quality_distribution: { 7: 10 },
                source_distribution: { doaj: 99 },
            },
        }),
    }));
    const renderTable = vi.fn();
    const renderPagination = vi.fn();
    const updateQualityDistChart = vi.fn();
    const updateScoreSourceChart = vi.fn();
    const harness = compileTemplateHarness({
        templatePath: TEMPLATE_PATH,
        functionNames: withJournalPayloadHelpers([
            'buildApiUrl',
            'loadJournalPage',
        ]),
        dependencies: {
            renderTable,
            renderPagination,
            updateQualityDistChart,
            updateScoreSourceChart,
        },
        preamble: `
            const PAGE_SIZE = 50;
            let sortField = 'quality';
            let sortDir = 'desc';
            let currentPage = 1;
            let totalPages = 1;
            let totalCount = 0;
            let summaryLoaded = false;
            let journalPageRequestId = 0;
        `,
        returnExpression: '({ loadJournalPage })',
    });

    await harness.loadJournalPage(2, true);

    expect(renderTable).toHaveBeenCalledWith(journals, 151);
    expect(renderPagination).toHaveBeenCalledOnce();
    expect(updateQualityDistChart).toHaveBeenCalledWith({ 7: 10 });
    expect(updateScoreSourceChart).toHaveBeenCalledWith({ doaj: 99 });
    expect(document.getElementById('stat-total').textContent).toBe('151');
    expect(document.getElementById('stat-h-index').textContent).toBe('—');
});

it('keeps a newer journal filter result over an older success', async () => {
    renderJournalFixture();
    const olderLoad = deferred();
    const newerLoad = deferred();
    vi.stubGlobal('fetch', vi.fn()
        .mockImplementationOnce(() => olderLoad.promise)
        .mockImplementationOnce(() => newerLoad.promise));
    const renderTable = vi.fn();
    const harness = compileJournalPageRuntime({
        renderTable,
        renderPagination: vi.fn(),
        updateQualityDistChart: vi.fn(),
        updateScoreSourceChart: vi.fn(),
    });

    const olderRequest = harness.loadJournalPage(1, false);
    document.getElementById('ldr-journal-search').value = 'new intent';
    const newerRequest = harness.loadJournalPage(2, false);
    const currentRows = [{ id: 'current-journal' }];
    newerLoad.resolve({
        status: 200,
        json: vi.fn().mockResolvedValue({
            status: 'success',
            journals: currentRows,
            pagination: { page: 2, total_pages: 3, total_count: 51 },
        }),
    });
    await newerRequest;
    olderLoad.resolve({
        status: 200,
        json: vi.fn().mockResolvedValue({
            status: 'success',
            journals: [{ id: 'stale-journal' }],
            pagination: { page: 1, total_pages: 1, total_count: 1 },
        }),
    });
    await olderRequest;

    expect(renderTable).toHaveBeenCalledOnce();
    expect(renderTable).toHaveBeenCalledWith(currentRows, 51);
    expect(harness.getPage()).toBe(2);
    expect(harness.getTotalCount()).toBe(51);
});

it('does not let an older journal failure replace a newer result', async () => {
    renderJournalFixture();
    const olderLoad = deferred();
    const newerLoad = deferred();
    vi.stubGlobal('fetch', vi.fn()
        .mockImplementationOnce(() => olderLoad.promise)
        .mockImplementationOnce(() => newerLoad.promise));
    const renderTable = vi.fn();
    const harness = compileJournalPageRuntime({
        renderTable,
        renderPagination: vi.fn(),
        updateQualityDistChart: vi.fn(),
        updateScoreSourceChart: vi.fn(),
    });

    const olderRequest = harness.loadJournalPage(1, false);
    document.getElementById('ldr-filter-tier').value = '';
    const newerRequest = harness.loadJournalPage(1, false);
    newerLoad.resolve({
        status: 200,
        json: vi.fn().mockResolvedValue({
            status: 'success',
            journals: [{ id: 'current-journal' }],
            pagination: { page: 1, total_pages: 1, total_count: 1 },
        }),
    });
    await newerRequest;
    olderLoad.reject(new Error('stale network failure'));
    await olderRequest;

    expect(renderTable).toHaveBeenCalledOnce();
    expect(document.getElementById('ldr-error').style.display).toBe('none');
    expect(document.getElementById('ldr-error').textContent).toBe('');
});

it('uses the research-scoped journal endpoint and renders its success shape', async () => {
    window.history.replaceState({}, '', '/metrics/journals?research_id=run%2Fa');
    document.body.innerHTML = `
        <div id="ldr-yr-loading" style="display: block"></div>
        <div id="ldr-yr-empty" style="display: none"></div>
        <div id="ldr-yr-content" style="display: none"></div>
        <span id="yr-stat-journals"></span>
        <span id="yr-stat-avg-quality"></span>
        <span id="yr-stat-papers"></span>
        <span id="yr-stat-predatory"></span>
        <span id="yr-table-count"></span>
    `;
    const journals = [{ name: 'Journal A' }];
    const fetchMock = vi.fn().mockResolvedValue({
        json: vi.fn().mockResolvedValue({
            status: 'success',
            summary: {
                total_journals: 1,
                avg_quality: 8.5,
                total_papers: 4,
                predatory_blocked: 2,
            },
            quality_distribution: { 8: 1 },
            journals,
        }),
    });
    vi.stubGlobal('fetch', fetchMock);
    const renderYrTable = vi.fn();
    const harness = compileTemplateHarness({
        templatePath: TEMPLATE_PATH,
        functionNames: ['loadUserResearchJournals'],
        dependencies: {
            renderYrQualityChart: vi.fn(),
            renderYrSourceChart: vi.fn(),
            renderYrTable,
        },
        returnExpression: '({ loadUserResearchJournals })',
    });

    await harness.loadUserResearchJournals();

    expect(fetchMock).toHaveBeenCalledWith(
        '/metrics/api/journals/research/run%2Fa',
    );
    expect(document.getElementById('ldr-yr-content').style.display)
        .toBe('block');
    expect(document.getElementById('yr-stat-predatory').textContent).toBe('2');
    expect(renderYrTable).toHaveBeenCalledWith(journals);
});

it('renders an em dash for the reference count when the reference DB was withheld', async () => {
    window.history.replaceState({}, '', '/metrics/journals?research_id=run%2Fa');
    document.body.innerHTML = `
        <div id="ldr-yr-loading" style="display: block"></div>
        <div id="ldr-yr-empty" style="display: none"></div>
        <div id="ldr-yr-content" style="display: none"></div>
        <span id="yr-stat-journals"></span>
        <span id="yr-stat-avg-quality"></span>
        <span id="yr-stat-papers"></span>
        <span id="yr-stat-predatory"></span>
        <span id="yr-table-count"></span>
    `;
    const journals = [{ name: 'Journal A' }];
    const fetchMock = vi.fn().mockResolvedValue({
        json: vi.fn().mockResolvedValue({
            status: 'success',
            // total_journals > 0 so the empty-state return above the
            // predatory-count line does not short-circuit rendering.
            summary: {
                total_journals: 1,
                avg_quality: 8.5,
                total_papers: 4,
                predatory_blocked: 0,
            },
            quality_distribution: { 8: 1 },
            journals,
            reference_data_available: false,
        }),
    });
    vi.stubGlobal('fetch', fetchMock);
    const harness = compileTemplateHarness({
        templatePath: TEMPLATE_PATH,
        functionNames: ['loadUserResearchJournals'],
        dependencies: {
            renderYrQualityChart: vi.fn(),
            renderYrSourceChart: vi.fn(),
            renderYrTable: vi.fn(),
        },
        returnExpression: '({ loadUserResearchJournals })',
    });

    await harness.loadUserResearchJournals();

    // `predatory_blocked: 0` must not read as "no predatory journals
    // found" when the reference DB was never actually consulted.
    expect(document.getElementById('yr-stat-predatory').textContent)
        .toBe('—');
});

it('renders the real count when the reference DB was explicitly available', async () => {
    window.history.replaceState({}, '', '/metrics/journals?research_id=run%2Fa');
    document.body.innerHTML = `
        <div id="ldr-yr-loading" style="display: block"></div>
        <div id="ldr-yr-empty" style="display: none"></div>
        <div id="ldr-yr-content" style="display: none"></div>
        <span id="yr-stat-journals"></span>
        <span id="yr-stat-avg-quality"></span>
        <span id="yr-stat-papers"></span>
        <span id="yr-stat-predatory"></span>
        <span id="yr-table-count"></span>
    `;
    const journals = [{ name: 'Journal A' }];
    const fetchMock = vi.fn().mockResolvedValue({
        json: vi.fn().mockResolvedValue({
            status: 'success',
            summary: {
                total_journals: 1,
                avg_quality: 8.5,
                total_papers: 4,
                predatory_blocked: 3,
            },
            quality_distribution: { 8: 1 },
            journals,
            // Explicit `true`, distinct from an older payload that omits
            // the field entirely (covered by the success-shape test
            // above) — both must render the real count, not the dash.
            reference_data_available: true,
        }),
    });
    vi.stubGlobal('fetch', fetchMock);
    const harness = compileTemplateHarness({
        templatePath: TEMPLATE_PATH,
        functionNames: ['loadUserResearchJournals'],
        dependencies: {
            renderYrQualityChart: vi.fn(),
            renderYrSourceChart: vi.fn(),
            renderYrTable: vi.fn(),
        },
        returnExpression: '({ loadUserResearchJournals })',
    });

    await harness.loadUserResearchJournals();

    expect(document.getElementById('yr-stat-predatory').textContent).toBe('3');
});

it('uses the aggregate journal endpoint and renders its empty success shape', async () => {
    window.history.replaceState({}, '', '/metrics/journals');
    document.body.innerHTML = `
        <div id="ldr-yr-loading" style="display: block"></div>
        <div id="ldr-yr-empty" style="display: none"></div>
        <div id="ldr-yr-content" style="display: none"></div>
    `;
    const fetchMock = vi.fn().mockResolvedValue({
        json: vi.fn().mockResolvedValue({
            status: 'success',
            summary: { total_journals: 0 },
            quality_distribution: {},
            journals: [],
        }),
    });
    vi.stubGlobal('fetch', fetchMock);
    const renderYrTable = vi.fn();
    const harness = compileTemplateHarness({
        templatePath: TEMPLATE_PATH,
        functionNames: ['loadUserResearchJournals'],
        dependencies: {
            renderYrQualityChart: vi.fn(),
            renderYrSourceChart: vi.fn(),
            renderYrTable,
        },
        returnExpression: '({ loadUserResearchJournals })',
    });

    await harness.loadUserResearchJournals();

    expect(fetchMock).toHaveBeenCalledWith(
        '/metrics/api/journals/user-research',
    );
    expect(document.getElementById('ldr-yr-loading').style.display)
        .toBe('none');
    expect(document.getElementById('ldr-yr-empty').style.display)
        .toBe('block');
    expect(renderYrTable).not.toHaveBeenCalled();
});