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.

33.2 KB
Raw Blame
/**
 * Runtime contracts for mutating actions in the subscriptions page.
 * Its existing suite only exercises date formatting, while these handlers
 * consume migrated research/news APIs.
 */

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

// The handlers escape API text with the global escapeHtml before showing it
// in an alert. Production loads xss-protection.js on every page first; the
// extracted handlers below resolve escapeHtml from the same global.
import '@js/security/xss-protection.js';

const SOURCE_PATH = resolve(
    __dirname,
    '../../../src/local_deep_research/web/static/js/pages/subscriptions.js',
);
const source = readFileSync(SOURCE_PATH, 'utf8');

function sourceBlock(pattern, name) {
    const match = source.match(pattern);
    expect(match, `${name} source block not found`).toBeTruthy();
    return match[1];
}

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

function compileListMutationRuntime(initialSubscriptions = [{
    id: 'sub-list-ownership',
    name: 'Owned subscription',
    is_active: true,
}]) {
    const loadSubscriptionsSource = sourceBlock(
        /(async function loadSubscriptions\(\)\s*\{[\s\S]*?\n\})\n\n\/\/ Load folders/,
        'loadSubscriptions()',
    );
    const toggleSource = sourceBlock(
        /(async function toggleSubscription\(subscriptionId\)\s*\{[\s\S]*?\n\})\n\n\/\/ View subscription history/,
        'toggleSubscription()',
    );
    const deleteSource = sourceBlock(
        /(async function deleteSubscriptionDirect\(subscriptionId\)\s*\{[\s\S]*?\n\})\n\n\/\/ Update statistics/,
        'deleteSubscriptionDirect()',
    );
    const calls = {
        renderSubscriptions: vi.fn(),
        updateStats: vi.fn(),
        showAlert: vi.fn(),
    };
    // Repository-owned production source only; no user-controlled input.
    const runtime = new Function( // eslint-disable-line no-new-func
        'calls', 'initialSubscriptions', `
        let subscriptions = initialSubscriptions.map(subscription => ({
            ...subscription,
        }));
        let subscriptionsMutationGeneration = 0;
        let subscriptionsLoadRequestId = 0;
        const fetchOptions = { credentials: 'same-origin' };
        const subscriptionToggleIntents = new Map();
        const SafeLogger = { log: () => {}, error: () => {} };
        const renderSubscriptions = () => calls.renderSubscriptions();
        const updateStats = () => calls.updateStats();
        const showAlert = (...args) => calls.showAlert(...args);
        const getCSRFToken = () => window.api.getCsrfToken();
        ${loadSubscriptionsSource}
        ${toggleSource}
        ${deleteSource}
        return {
            loadSubscriptions,
            toggleSubscription,
            deleteSubscriptionDirect,
            getSubscriptions: () => subscriptions,
        };
    `)(calls, initialSubscriptions);
    return { calls, runtime };
}

afterEach(() => {
    vi.clearAllTimers();
    vi.useRealTimers();
    vi.restoreAllMocks();
    vi.unstubAllGlobals();
    localStorage.clear();
    delete window.api;
    delete window.RESEARCH_STATUS;
});

it('loads the current subscriptions and folder-array response shapes', async () => {
    const loadSubscriptionsSource = sourceBlock(
        /(async function loadSubscriptions\(\)\s*\{[\s\S]*?\n\})\n\n\/\/ Load folders/,
        'loadSubscriptions()',
    );
    const loadFoldersSource = sourceBlock(
        /(async function loadFolders\(\)\s*\{[\s\S]*?\n\})\n\n\/\/ Render subscriptions grid/,
        'loadFolders()',
    );
    const calls = {
        renderSubscriptions: vi.fn(),
        updateStats: vi.fn(),
        renderFolders: vi.fn(),
        showAlert: vi.fn(),
    };
    // Repository-owned production source only; no user-controlled input.
    const runtime = new Function( // eslint-disable-line no-new-func
        'calls', `
        let subscriptions = [];
        let subscriptionsMutationGeneration = 0;
        let subscriptionsLoadRequestId = 0;
        let folders = [];
        let foldersLoadRequestId = 0;
        const renderSubscriptions = () => calls.renderSubscriptions();
        const updateStats = () => calls.updateStats();
        const renderFolders = () => calls.renderFolders();
        const showAlert = (...args) => calls.showAlert(...args);
        ${loadSubscriptionsSource}
        ${loadFoldersSource}
        return {
            loadSubscriptions,
            loadFolders,
            getSubscriptions: () => subscriptions,
            getFolders: () => folders,
        };
    `)(calls);
    const subscriptions = [{ id: 'sub-load', query: 'Migration' }];
    const folders = [{ id: 'folder-load', name: 'FastAPI' }];
    const fetchMock = vi.fn((url) => {
        if (url === '/news/api/subscriptions/current') {
            return Promise.resolve(new Response(JSON.stringify({
                subscriptions,
            }), { status: 200 }));
        }
        if (url === '/news/api/subscription/folders') {
            return Promise.resolve(new Response(JSON.stringify(folders), {
                status: 200,
            }));
        }
        throw new Error(`Unexpected request: ${url}`);
    });
    vi.stubGlobal('fetch', fetchMock);

    await Promise.all([
        runtime.loadSubscriptions(),
        runtime.loadFolders(),
    ]);

    expect(fetchMock).toHaveBeenCalledWith(
        '/news/api/subscriptions/current',
        { credentials: 'same-origin' },
    );
    expect(fetchMock).toHaveBeenCalledWith(
        '/news/api/subscription/folders',
        { credentials: 'same-origin' },
    );
    expect(runtime.getSubscriptions()).toEqual(subscriptions);
    expect(runtime.getFolders()).toEqual(folders);
    expect(calls.renderSubscriptions).toHaveBeenCalledOnce();
    expect(calls.updateStats).toHaveBeenCalledOnce();
    expect(calls.renderFolders).toHaveBeenCalledOnce();
    expect(calls.showAlert).not.toHaveBeenCalled();
});

it('renders the migrated scheduler status envelope', async () => {
    const schedulerSource = sourceBlock(
        /(async function checkSchedulerStatus\(\)\s*\{[\s\S]*?\n\})\n\n\/\/ Show scheduler information/,
        'checkSchedulerStatus()',
    );
    // Repository-owned production source only; no user-controlled input.
    const checkSchedulerStatus = new Function( // eslint-disable-line no-new-func
        `const fetchOptions = { credentials: 'same-origin' };
         ${schedulerSource}
         return checkSchedulerStatus;`,
    )();
    document.body.innerHTML = `
        <span id="status-indicator"></span>
        <span id="status-text"></span>
        <span id="scheduler-details"></span>
    `;
    const fetchMock = vi.fn().mockResolvedValue(new Response(JSON.stringify({
        scheduler_available: true,
        is_running: true,
        active_users: 2,
        scheduled_jobs: 5,
    }), { status: 200 }));
    vi.stubGlobal('fetch', fetchMock);

    await checkSchedulerStatus();

    expect(fetchMock).toHaveBeenCalledWith(
        '/news/api/scheduler/status',
        { credentials: 'same-origin' },
    );
    expect(document.getElementById('status-indicator').className)
        .toBe('ldr-status-indicator active');
    expect(document.getElementById('status-text').textContent).toBe('Active');
    expect(document.getElementById('scheduler-details').textContent)
        .toBe('2 active users, 5 scheduled jobs');
});

it('starts queued subscription research through the saved-subscription route with CSRF', async () => {
    vi.useFakeTimers();
    const runSource = sourceBlock(
        /(async function runSubscriptionNow\(subscriptionId\)\s*\{[\s\S]*?\n\})\n\n\/\/ Toggle subscription/,
        'runSubscriptionNow()',
    );
    const calls = {
        showAlert: vi.fn(),
        renderSubscriptions: vi.fn(),
        loadSubscriptions: vi.fn(),
    };
    // Repository-owned production source only; no user-controlled input.
    const runtime = new Function( // eslint-disable-line no-new-func
        'calls', `
        let subscriptions = [{
            id: 'sub-1',
            query: 'FastAPI migration news',
            is_active: true,
        }];
        const fetchOptions = { credentials: 'same-origin' };
        const runningSubscriptionIds = new Set();
        const showAlert = (...args) => calls.showAlert(...args);
        const renderSubscriptions = (...args) => calls.renderSubscriptions(...args);
        const loadSubscriptions = (...args) => calls.loadSubscriptions(...args);
        const getCSRFToken = () => window.api.getCsrfToken();
        ${runSource}
        return { runSubscriptionNow, getSubscriptions: () => subscriptions };
    `)(calls);
    window.api = { getCsrfToken: vi.fn(() => 'csrf-subscriptions') };
    window.RESEARCH_STATUS = { QUEUED: 'queued' };
    const fetchMock = vi.fn().mockResolvedValue(new Response(JSON.stringify({
        status: 'queued',
        research_id: 'research-1<script>',
    }), { status: 200 }));
    vi.stubGlobal('fetch', fetchMock);

    await runtime.runSubscriptionNow('sub-1');

    expect(fetchMock).toHaveBeenCalledOnce();
    const [url, options] = fetchMock.mock.calls[0];
    expect(url).toBe('/news/api/subscriptions/sub-1/run');
    expect(options.method).toBe('POST');
    expect(options.credentials).toBe('same-origin');
    expect(options.headers).toEqual({
        'Content-Type': 'application/json',
        'X-CSRFToken': 'csrf-subscriptions',
    });
    expect(options.body).toBeUndefined();
    expect(calls.showAlert).toHaveBeenLastCalledWith(
        expect.stringContaining('/progress/research-1script'),
        'success',
    );
    expect(calls.renderSubscriptions).toHaveBeenCalledOnce();
    expect(runtime.getSubscriptions()[0].last_run).toBeTruthy();
    expect(JSON.parse(localStorage.getItem('active_news_research'))).toEqual(
        expect.objectContaining({
            researchId: 'research-1<script>',
            query: 'FastAPI migration news',
        }),
    );
});

it('updates active state through the migrated PUT contract', async () => {
    const toggleSource = sourceBlock(
        /(async function toggleSubscription\(subscriptionId\)\s*\{[\s\S]*?\n\})\n\n\/\/ View subscription history/,
        'toggleSubscription()',
    );
    const calls = { renderSubscriptions: vi.fn(), updateStats: vi.fn() };
    // Repository-owned production source only; no user-controlled input.
    const runtime = new Function( // eslint-disable-line no-new-func
        'calls', `
        let subscriptions = [{ id: 'sub-2', is_active: true }];
        let subscriptionsMutationGeneration = 0;
        const fetchOptions = { credentials: 'same-origin' };
        const subscriptionToggleIntents = new Map();
        const renderSubscriptions = () => calls.renderSubscriptions();
        const updateStats = () => calls.updateStats();
        const showAlert = () => {};
        const getCSRFToken = () => window.api.getCsrfToken();
        ${toggleSource}
        return { toggleSubscription, getSubscriptions: () => subscriptions };
    `)(calls);
    window.api = { getCsrfToken: vi.fn(() => 'csrf-subscriptions') };
    const fetchMock = vi.fn().mockResolvedValue(new Response(JSON.stringify({
        status: 'success',
        subscription: { id: 'sub-2', is_active: false },
    }), { status: 200 }));
    vi.stubGlobal('fetch', fetchMock);

    await runtime.toggleSubscription('sub-2');

    expect(fetchMock).toHaveBeenCalledWith('/news/api/subscriptions/sub-2', {
        credentials: 'same-origin',
        method: 'PUT',
        headers: {
            'Content-Type': 'application/json',
            'X-CSRFToken': 'csrf-subscriptions',
        },
        body: JSON.stringify({ is_active: false }),
    });
    expect(runtime.getSubscriptions()[0].is_active).toBe(false);
    expect(calls.renderSubscriptions).toHaveBeenCalledOnce();
    expect(calls.updateStats).toHaveBeenCalledOnce();
});

it('coalesces repeated Run clicks until the owned research start settles', async () => {
    vi.useFakeTimers();
    const runSource = sourceBlock(
        /(async function runSubscriptionNow\(subscriptionId\)\s*\{[\s\S]*?\n\})\n\n\/\/ Toggle subscription/,
        'runSubscriptionNow()',
    );
    const calls = {
        showAlert: vi.fn(),
        renderSubscriptions: vi.fn(),
        loadSubscriptions: vi.fn(),
    };
    // Repository-owned production source only; no user-controlled input.
    const runtime = new Function( // eslint-disable-line no-new-func
        'calls', `
        let subscriptions = [{
            id: 'sub-run-guard',
            query: 'One research only',
            is_active: true,
        }];
        const fetchOptions = { credentials: 'same-origin' };
        const runningSubscriptionIds = new Set();
        const showAlert = (...args) => calls.showAlert(...args);
        const renderSubscriptions = (...args) => calls.renderSubscriptions(...args);
        const loadSubscriptions = (...args) => calls.loadSubscriptions(...args);
        const getCSRFToken = () => window.api.getCsrfToken();
        ${runSource}
        return { runSubscriptionNow };
    `)(calls);
    window.api = { getCsrfToken: vi.fn(() => 'csrf-run-guard') };
    window.RESEARCH_STATUS = { QUEUED: 'queued' };
    const firstResponse = deferred();
    const fetchMock = vi.fn(() => firstResponse.promise);
    vi.stubGlobal('fetch', fetchMock);

    const firstRun = runtime.runSubscriptionNow('sub-run-guard');
    const duplicateRun = runtime.runSubscriptionNow('sub-run-guard');
    await duplicateRun;

    expect(fetchMock).toHaveBeenCalledOnce();
    expect(calls.showAlert.mock.calls.filter(([, type]) => type === 'info'))
        .toHaveLength(1);

    firstResponse.resolve(new Response(JSON.stringify({
        status: 'queued',
        research_id: 'run-guard-1',
    }), { status: 200 }));
    await firstRun;

    fetchMock.mockResolvedValueOnce(new Response(JSON.stringify({
        status: 'queued',
        research_id: 'run-guard-2',
    }), { status: 200 }));
    await runtime.runSubscriptionNow('sub-run-guard');

    expect(fetchMock).toHaveBeenCalledTimes(2);
});

it('shows a FastAPI detail error and releases Run ownership so retry can recover', async () => {
    vi.useFakeTimers();
    const runSource = sourceBlock(
        /(async function runSubscriptionNow\(subscriptionId\)\s*\{[\s\S]*?\n\})\n\n\/\/ Toggle subscription/,
        'runSubscriptionNow()',
    );
    const calls = {
        showAlert: vi.fn(),
        renderSubscriptions: vi.fn(),
        loadSubscriptions: vi.fn(),
    };
    // Repository-owned production source only; no user-controlled input.
    const runSubscriptionNow = new Function( // eslint-disable-line no-new-func
        'calls', `
        const subscriptions = [{
            id: 'sub-run-retry',
            query: 'Recover the failed run',
            is_active: true,
        }];
        const fetchOptions = { credentials: 'same-origin' };
        const runningSubscriptionIds = new Set();
        const showAlert = (...args) => calls.showAlert(...args);
        const renderSubscriptions = (...args) => calls.renderSubscriptions(...args);
        const loadSubscriptions = (...args) => calls.loadSubscriptions(...args);
        const getCSRFToken = () => window.api.getCsrfToken();
        ${runSource}
        return runSubscriptionNow;
    `)(calls);
    window.api = { getCsrfToken: vi.fn(() => 'csrf-run-retry') };
    window.RESEARCH_STATUS = { QUEUED: 'queued' };
    const fetchMock = vi.fn()
        .mockResolvedValueOnce(new Response(JSON.stringify({
            detail: 'Research service is temporarily unavailable',
        }), {
            status: 503,
            statusText: 'Service Unavailable',
        }))
        .mockResolvedValueOnce(new Response(JSON.stringify({
            status: 'queued',
            research_id: 'run-recovered',
        }), { status: 200 }));
    vi.stubGlobal('fetch', fetchMock);

    await runSubscriptionNow('sub-run-retry');

    expect(fetchMock).toHaveBeenCalledOnce();
    expect(calls.showAlert).toHaveBeenLastCalledWith(
        'Research service is temporarily unavailable',
        'error',
    );
    expect(calls.renderSubscriptions).not.toHaveBeenCalled();

    await runSubscriptionNow('sub-run-retry');

    expect(fetchMock).toHaveBeenCalledTimes(2);
    expect(calls.showAlert).toHaveBeenLastCalledWith(
        expect.stringContaining('/progress/run-recovered'),
        'success',
    );
    expect(calls.renderSubscriptions).toHaveBeenCalledOnce();
    expect(vi.getTimerCount()).toBe(1);

    await vi.advanceTimersByTimeAsync(2000);

    expect(calls.loadSubscriptions).toHaveBeenCalledOnce();
});

it('guards rapid toggles, applies the captured intent once, and then unlocks', async () => {
    const toggleSource = sourceBlock(
        /(async function toggleSubscription\(subscriptionId\)\s*\{[\s\S]*?\n\})\n\n\/\/ View subscription history/,
        'toggleSubscription()',
    );
    const calls = {
        renderSubscriptions: vi.fn(),
        updateStats: vi.fn(),
        showAlert: vi.fn(),
    };
    // Repository-owned production source only; no user-controlled input.
    const runtime = new Function( // eslint-disable-line no-new-func
        'calls', `
        let subscriptions = [{ id: 'sub-toggle-guard', is_active: true }];
        let subscriptionsMutationGeneration = 0;
        const fetchOptions = { credentials: 'same-origin' };
        const subscriptionToggleIntents = new Map();
        const renderSubscriptions = () => calls.renderSubscriptions();
        const updateStats = () => calls.updateStats();
        const showAlert = (...args) => calls.showAlert(...args);
        const getCSRFToken = () => window.api.getCsrfToken();
        ${toggleSource}
        return { toggleSubscription, getSubscriptions: () => subscriptions };
    `)(calls);
    window.api = { getCsrfToken: vi.fn(() => 'csrf-toggle-guard') };
    const firstResponse = deferred();
    const fetchMock = vi.fn(() => firstResponse.promise);
    vi.stubGlobal('fetch', fetchMock);

    const firstToggle = runtime.toggleSubscription('sub-toggle-guard');
    const duplicateToggle = runtime.toggleSubscription('sub-toggle-guard');
    await duplicateToggle;

    expect(fetchMock).toHaveBeenCalledOnce();
    expect(runtime.getSubscriptions()[0].is_active).toBe(true);
    expect(JSON.parse(fetchMock.mock.calls[0][1].body))
        .toEqual({ is_active: false });
    expect(calls.renderSubscriptions).not.toHaveBeenCalled();

    firstResponse.resolve({ ok: true, status: 200 });
    await firstToggle;

    expect(runtime.getSubscriptions()[0].is_active).toBe(false);
    expect(calls.renderSubscriptions).toHaveBeenCalledOnce();
    expect(calls.updateStats).toHaveBeenCalledOnce();

    fetchMock.mockResolvedValueOnce({ ok: true, status: 200 });
    await runtime.toggleSubscription('sub-toggle-guard');

    expect(fetchMock).toHaveBeenCalledTimes(2);
    expect(JSON.parse(fetchMock.mock.calls[1][1].body))
        .toEqual({ is_active: true });
    expect(runtime.getSubscriptions()[0].is_active).toBe(true);
    expect(calls.renderSubscriptions).toHaveBeenCalledTimes(2);
    expect(calls.showAlert).not.toHaveBeenCalled();
});

it('applies a pending toggle intent to the subscription object from a newer list load', async () => {
    const toggleSource = sourceBlock(
        /(async function toggleSubscription\(subscriptionId\)\s*\{[\s\S]*?\n\})\n\n\/\/ View subscription history/,
        'toggleSubscription()',
    );
    const calls = {
        renderSubscriptions: vi.fn(),
        updateStats: vi.fn(),
        showAlert: vi.fn(),
    };
    // Repository-owned production source only; no user-controlled input.
    const runtime = new Function( // eslint-disable-line no-new-func
        'calls', `
        let subscriptions = [{ id: 'sub-toggle-reload', is_active: true }];
        let subscriptionsMutationGeneration = 0;
        const fetchOptions = { credentials: 'same-origin' };
        const subscriptionToggleIntents = new Map();
        const renderSubscriptions = () => calls.renderSubscriptions();
        const updateStats = () => calls.updateStats();
        const showAlert = (...args) => calls.showAlert(...args);
        const getCSRFToken = () => window.api.getCsrfToken();
        ${toggleSource}
        return {
            toggleSubscription,
            getSubscriptions: () => subscriptions,
            replaceSubscriptions: value => { subscriptions = value; },
        };
    `)(calls);
    window.api = { getCsrfToken: vi.fn(() => 'csrf-toggle-reload') };
    const response = deferred();
    vi.stubGlobal('fetch', vi.fn(() => response.promise));
    const staleSubscription = runtime.getSubscriptions()[0];

    const toggle = runtime.toggleSubscription('sub-toggle-reload');
    const refreshedSubscription = {
        id: 'sub-toggle-reload',
        is_active: true,
        marker: 'fresh-list-object',
    };
    runtime.replaceSubscriptions([refreshedSubscription]);
    response.resolve({ ok: true, status: 200 });
    await toggle;

    expect(staleSubscription.is_active).toBe(true);
    expect(runtime.getSubscriptions()[0]).toBe(refreshedSubscription);
    expect(refreshedSubscription.is_active).toBe(false);
    expect(calls.renderSubscriptions).toHaveBeenCalledOnce();
    expect(calls.updateStats).toHaveBeenCalledOnce();
    expect(calls.showAlert).not.toHaveBeenCalled();
});

it('does not let a stale list response overwrite a completed toggle', async () => {
    const { calls, runtime } = compileListMutationRuntime();
    window.api = { getCsrfToken: vi.fn(() => 'csrf-stale-load') };
    const staleLoad = deferred();
    const fetchMock = vi.fn((url, options = {}) => {
        if (url === '/news/api/subscriptions/current') {
            return staleLoad.promise;
        }
        if (
            url === '/news/api/subscriptions/sub-list-ownership'
            && options.method === 'PUT'
        ) {
            return Promise.resolve({ ok: true, status: 200 });
        }
        throw new Error(`Unexpected request: ${url}`);
    });
    vi.stubGlobal('fetch', fetchMock);

    const load = runtime.loadSubscriptions();
    await vi.waitFor(() => expect(fetchMock).toHaveBeenCalledOnce());

    await runtime.toggleSubscription('sub-list-ownership');
    expect(runtime.getSubscriptions()[0].is_active).toBe(false);

    staleLoad.resolve(new Response(JSON.stringify({
        subscriptions: [{ id: 'sub-list-ownership', is_active: true }],
    }), { status: 200 }));
    await load;

    expect(runtime.getSubscriptions()[0].is_active).toBe(false);
    expect(calls.renderSubscriptions).toHaveBeenCalledOnce();
    expect(calls.updateStats).toHaveBeenCalledOnce();
    expect(calls.showAlert).not.toHaveBeenCalled();
});

it('does not let a stale list body overwrite a completed toggle', async () => {
    const { calls, runtime } = compileListMutationRuntime();
    window.api = { getCsrfToken: vi.fn(() => 'csrf-stale-body') };
    const staleBody = deferred();
    const staleJson = vi.fn(() => staleBody.promise);
    const fetchMock = vi.fn((url, options = {}) => {
        if (url === '/news/api/subscriptions/current') {
            return Promise.resolve({ ok: true, json: staleJson });
        }
        if (
            url === '/news/api/subscriptions/sub-list-ownership'
            && options.method === 'PUT'
        ) {
            return Promise.resolve({ ok: true, status: 200 });
        }
        throw new Error(`Unexpected request: ${url}`);
    });
    vi.stubGlobal('fetch', fetchMock);

    const load = runtime.loadSubscriptions();
    await vi.waitFor(() => expect(staleJson).toHaveBeenCalledOnce());

    await runtime.toggleSubscription('sub-list-ownership');
    staleBody.resolve({
        subscriptions: [{ id: 'sub-list-ownership', is_active: true }],
    });
    await load;

    expect(runtime.getSubscriptions()[0].is_active).toBe(false);
    expect(calls.renderSubscriptions).toHaveBeenCalledOnce();
    expect(calls.updateStats).toHaveBeenCalledOnce();
    expect(calls.showAlert).not.toHaveBeenCalled();
});

it('lets only the newest same-generation list request render', async () => {
    const { calls, runtime } = compileListMutationRuntime();
    window.api = { getCsrfToken: vi.fn(() => 'csrf-latest-list') };
    const olderBody = deferred();
    const olderJson = vi.fn(() => olderBody.promise);
    const fetchMock = vi.fn()
        .mockResolvedValueOnce({ ok: true, json: olderJson })
        .mockResolvedValueOnce(new Response(JSON.stringify({
            subscriptions: [{
                id: 'sub-list-ownership',
                is_active: false,
                marker: 'newest-list',
            }],
        }), { status: 200 }));
    vi.stubGlobal('fetch', fetchMock);

    const olderLoad = runtime.loadSubscriptions();
    await vi.waitFor(() => expect(olderJson).toHaveBeenCalledOnce());
    await runtime.loadSubscriptions();

    olderBody.resolve({
        subscriptions: [{
            id: 'sub-list-ownership',
            is_active: true,
            marker: 'older-list',
        }],
    });
    await olderLoad;

    expect(runtime.getSubscriptions()).toEqual([{
        id: 'sub-list-ownership',
        is_active: false,
        marker: 'newest-list',
    }]);
    expect(calls.renderSubscriptions).toHaveBeenCalledOnce();
    expect(calls.updateStats).toHaveBeenCalledOnce();
    expect(calls.showAlert).not.toHaveBeenCalled();
});

it('does not let a pre-delete list response resurrect a deleted subscription', async () => {
    const { calls, runtime } = compileListMutationRuntime();
    window.api = { getCsrfToken: vi.fn(() => 'csrf-delete-owned') };
    vi.stubGlobal('confirm', vi.fn(() => true));
    const staleLoad = deferred();
    let listCalls = 0;
    const fetchMock = vi.fn((url, options = {}) => {
        if (url === '/news/api/subscriptions/current') {
            listCalls += 1;
            if (listCalls === 1) return staleLoad.promise;
            return Promise.resolve(new Response(JSON.stringify({
                subscriptions: [],
            }), { status: 200 }));
        }
        if (
            url === '/news/api/subscriptions/sub-list-ownership'
            && options.method === 'DELETE'
        ) {
            return Promise.resolve({ ok: true, status: 204 });
        }
        throw new Error(`Unexpected request: ${url}`);
    });
    vi.stubGlobal('fetch', fetchMock);

    const olderLoad = runtime.loadSubscriptions();
    await vi.waitFor(() => expect(fetchMock).toHaveBeenCalledOnce());
    await runtime.deleteSubscriptionDirect('sub-list-ownership');

    expect(runtime.getSubscriptions()).toEqual([]);
    staleLoad.resolve(new Response(JSON.stringify({
        subscriptions: [{ id: 'sub-list-ownership', is_active: true }],
    }), { status: 200 }));
    await olderLoad;

    expect(runtime.getSubscriptions()).toEqual([]);
    expect(calls.renderSubscriptions).toHaveBeenCalledTimes(2);
    expect(calls.updateStats).toHaveBeenCalledTimes(2);
    expect(calls.showAlert).toHaveBeenCalledWith(
        'Subscription deleted successfully',
        'success',
    );
    expect(fetchMock).toHaveBeenCalledWith(
        '/news/api/subscriptions/sub-list-ownership',
        expect.objectContaining({
            method: 'DELETE',
            headers: { 'X-CSRFToken': 'csrf-delete-owned' },
        }),
    );
});

it('keeps a deleted subscription absent when a later toggle retires its reload', async () => {
    const { calls, runtime } = compileListMutationRuntime([
        { id: 'delete-a', name: 'Delete A', is_active: true },
        { id: 'toggle-b', name: 'Toggle B', is_active: true },
    ]);
    window.api = { getCsrfToken: vi.fn(() => 'csrf-delete-toggle') };
    vi.stubGlobal('confirm', vi.fn(() => true));
    const deleteReloadBody = deferred();
    const deleteReloadJson = vi.fn(() => deleteReloadBody.promise);
    const fetchMock = vi.fn((url, options = {}) => {
        if (
            url === '/news/api/subscriptions/delete-a'
            && options.method === 'DELETE'
        ) {
            return Promise.resolve({ ok: true, status: 204 });
        }
        if (url === '/news/api/subscriptions/current') {
            return Promise.resolve({ ok: true, json: deleteReloadJson });
        }
        if (
            url === '/news/api/subscriptions/toggle-b'
            && options.method === 'PUT'
        ) {
            return Promise.resolve({ ok: true, status: 200 });
        }
        throw new Error(`Unexpected request: ${url}`);
    });
    vi.stubGlobal('fetch', fetchMock);

    const deleting = runtime.deleteSubscriptionDirect('delete-a');
    await vi.waitFor(() => expect(deleteReloadJson).toHaveBeenCalledOnce());
    await runtime.toggleSubscription('toggle-b');

    deleteReloadBody.resolve({
        subscriptions: [
            { id: 'delete-a', name: 'Delete A', is_active: true },
            { id: 'toggle-b', name: 'Toggle B', is_active: true },
        ],
    });
    await deleting;

    expect(runtime.getSubscriptions()).toEqual([
        { id: 'toggle-b', name: 'Toggle B', is_active: false },
    ]);
    expect(calls.renderSubscriptions).toHaveBeenCalledTimes(2);
    expect(calls.updateStats).toHaveBeenCalledTimes(2);
    expect(calls.showAlert).toHaveBeenCalledWith(
        'Subscription deleted successfully',
        'success',
    );
});

it('surfaces a non-ok toggle and unlocks the subscription for recovery', async () => {
    const toggleSource = sourceBlock(
        /(async function toggleSubscription\(subscriptionId\)\s*\{[\s\S]*?\n\})\n\n\/\/ View subscription history/,
        'toggleSubscription()',
    );
    const calls = {
        renderSubscriptions: vi.fn(),
        updateStats: vi.fn(),
        showAlert: vi.fn(),
    };
    // Repository-owned production source only; no user-controlled input.
    const runtime = new Function( // eslint-disable-line no-new-func
        'calls', `
        let subscriptions = [{ id: 'sub-toggle-retry', is_active: true }];
        let subscriptionsMutationGeneration = 0;
        const fetchOptions = { credentials: 'same-origin' };
        const subscriptionToggleIntents = new Map();
        const renderSubscriptions = () => calls.renderSubscriptions();
        const updateStats = () => calls.updateStats();
        const showAlert = (...args) => calls.showAlert(...args);
        const getCSRFToken = () => window.api.getCsrfToken();
        ${toggleSource}
        return { toggleSubscription, getSubscriptions: () => subscriptions };
    `)(calls);
    window.api = { getCsrfToken: vi.fn(() => 'csrf-toggle-retry') };
    const failedResponse = deferred();
    const fetchMock = vi.fn(() => failedResponse.promise);
    vi.stubGlobal('fetch', fetchMock);

    const failedToggle = runtime.toggleSubscription('sub-toggle-retry');
    const duplicateToggle = runtime.toggleSubscription('sub-toggle-retry');
    await duplicateToggle;
    failedResponse.resolve({
        ok: false,
        status: 503,
        statusText: 'Service Unavailable',
    });
    await failedToggle;

    expect(fetchMock).toHaveBeenCalledOnce();
    expect(runtime.getSubscriptions()[0].is_active).toBe(true);
    expect(calls.renderSubscriptions).not.toHaveBeenCalled();
    expect(calls.updateStats).not.toHaveBeenCalled();
    expect(calls.showAlert).toHaveBeenCalledWith(
        'Failed to update subscription',
        'error',
    );

    fetchMock.mockResolvedValueOnce({ ok: true, status: 200 });
    await runtime.toggleSubscription('sub-toggle-retry');

    expect(fetchMock).toHaveBeenCalledTimes(2);
    expect(runtime.getSubscriptions()[0].is_active).toBe(false);
    expect(calls.renderSubscriptions).toHaveBeenCalledOnce();
    expect(calls.updateStats).toHaveBeenCalledOnce();
});

it('creates a folder with CSRF and consumes the created folder envelope', async () => {
    const createSource = sourceBlock(
        /(async function createNewFolder\(\)\s*\{[\s\S]*?\n\})\n\n\/\/ Utility functions/,
        'createNewFolder()',
    );
    const calls = {
        showAlert: vi.fn(),
        loadFolders: vi.fn().mockResolvedValue(undefined),
        renderSubscriptions: vi.fn(),
    };
    // Repository-owned production source only; no user-controlled input.
    const createNewFolder = new Function( // eslint-disable-line no-new-func
        'calls', `
        const fetchOptions = { credentials: 'same-origin' };
        const showAlert = (...args) => calls.showAlert(...args);
        const loadFolders = (...args) => calls.loadFolders(...args);
        const renderSubscriptions = (...args) => calls.renderSubscriptions(...args);
        const getCSRFToken = () => window.api.getCsrfToken();
        ${createSource}
        return createNewFolder;
    `)(calls);
    window.api = { getCsrfToken: vi.fn(() => 'csrf-subscriptions') };
    vi.stubGlobal('prompt', vi.fn(() => '  Migration Sources  '));
    const fetchMock = vi.fn().mockResolvedValue(new Response(JSON.stringify({
        id: 'folder-1',
        name: 'Migration Sources',
    }), { status: 201 }));
    vi.stubGlobal('fetch', fetchMock);

    await createNewFolder();

    expect(fetchMock).toHaveBeenCalledWith('/news/api/subscription/folders', {
        credentials: 'same-origin',
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
            'X-CSRFToken': 'csrf-subscriptions',
        },
        body: JSON.stringify({ name: 'Migration Sources' }),
    });
    expect(calls.showAlert).toHaveBeenCalledWith(
        'Folder "Migration Sources" created successfully!',
        'success',
    );
    expect(calls.loadFolders).toHaveBeenCalledOnce();
    expect(calls.renderSubscriptions).toHaveBeenCalledOnce();
});