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.

12.6 KB
Raw Blame
/** Live delegated-action and edit-save contracts for SubscriptionManager. */

import '@js/security/xss-protection.js';
import '@js/components/subscription-manager.js';

let manager;

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

function subscription(id, overrides = {}) {
    return {
        id,
        query_or_topic: `Topic ${id}`,
        refresh_interval_minutes: 60,
        next_refresh: new Date(Date.now() + 60 * 60 * 1000).toISOString(),
        folder: 'Inbox',
        status: 'active',
        notes: 'Original notes',
        ...overrides,
    };
}

function setSubscriptions(...items) {
    manager.subscriptions = { Inbox: items };
    manager.folders = [
        { id: 'inbox', name: 'Inbox' },
        { id: 'archive', name: 'Archive' },
    ];
}

function installBootstrapModal() {
    const instances = [];
    class Modal {
        constructor(element) {
            this.element = element;
            this.show = vi.fn();
            this.hide = vi.fn();
            instances.push(this);
        }
    }
    vi.stubGlobal('bootstrap', { Modal });
    return instances;
}

beforeAll(() => {
    if (!window.subscriptionManager) {
        document.dispatchEvent(new Event('DOMContentLoaded'));
    }
    manager = window.subscriptionManager;
});

beforeEach(() => {
    document.body.innerHTML = '<div id="subscriptions-list"></div>';
    manager.subscriptions = {};
    manager.folders = [];
});

afterEach(() => {
    vi.restoreAllMocks();
    vi.unstubAllGlobals();
    document.body.replaceChildren();
});

it('routes nested action-icon clicks to the owning subscription buttons', () => {
    const item = subscription('nested-3299');
    setSubscriptions(item);
    // eslint-disable-next-line no-unsanitized/property -- checked-in renderer escapes the fixed fixture
    document.getElementById('subscriptions-list').innerHTML =
        manager.renderSubscriptionCard(item);
    const editSpy = vi.spyOn(manager, 'editSubscription').mockResolvedValue();
    const pauseSpy = vi.spyOn(manager, 'toggleSubscriptionStatus').mockResolvedValue();
    const deleteSpy = vi.spyOn(manager, 'deleteSubscription').mockResolvedValue();

    document.querySelector('.edit-subscription-btn i').click();
    document.querySelector('.pause-subscription-btn i').click();
    document.querySelector('.delete-subscription-btn i').click();

    expect(editSpy).toHaveBeenCalledOnce();
    expect(editSpy).toHaveBeenCalledWith('nested-3299');
    expect(pauseSpy).toHaveBeenCalledOnce();
    expect(pauseSpy).toHaveBeenCalledWith('nested-3299');
    expect(deleteSpy).toHaveBeenCalledOnce();
    expect(deleteSpy).toHaveBeenCalledWith('nested-3299');
});

it('keeps failed edits open and unchanged while guarding repeated saves', async () => {
    setSubscriptions(subscription('failed-save'));
    const modalInstances = installBootstrapModal();
    const pendingUpdate = deferred();
    const fetchMock = vi.fn(() => pendingUpdate.promise);
    vi.stubGlobal('fetch', fetchMock);
    vi.spyOn(manager, 'getCSRFToken').mockReturnValue('csrf-edit');
    vi.spyOn(manager, 'loadSubscriptionData').mockResolvedValue();
    const showSuccessSpy = vi.spyOn(manager, 'showSuccess').mockImplementation(() => {});
    const showErrorSpy = vi.spyOn(manager, 'showError').mockImplementation(() => {});

    await manager.editSubscription('failed-save');
    const modalElement = document.getElementById('editSubscriptionModal');
    const saveButton = document.getElementById('save-subscription-edit');
    document.getElementById('edit-frequency').value = '180';
    document.getElementById('edit-folder').value = 'Archive';
    document.getElementById('edit-notes').value = 'Keep these edits';

    saveButton.dispatchEvent(new MouseEvent('click', { bubbles: true }));
    saveButton.dispatchEvent(new MouseEvent('click', { bubbles: true }));

    expect(fetchMock).toHaveBeenCalledOnce();
    expect(saveButton.disabled).toBe(true);
    expect(modalInstances[0].hide).not.toHaveBeenCalled();
    const [url, options] = fetchMock.mock.calls[0];
    expect(url).toBe('/news/api/subscription/subscriptions/failed-save');
    expect(options.method).toBe('PUT');
    expect(JSON.parse(options.body)).toEqual({
        refresh_interval_minutes: 180,
        folder: 'Archive',
        notes: 'Keep these edits',
    });

    pendingUpdate.resolve({ ok: false, status: 422 });
    await vi.waitFor(() => {
        expect(saveButton.disabled).toBe(false);
    });

    expect(document.getElementById('editSubscriptionModal')).toBe(modalElement);
    expect(document.getElementById('edit-frequency').value).toBe('180');
    expect(document.getElementById('edit-folder').value).toBe('Archive');
    expect(document.getElementById('edit-notes').value).toBe('Keep these edits');
    expect(modalInstances[0].hide).not.toHaveBeenCalled();
    expect(showErrorSpy).toHaveBeenCalledWith('Failed to update subscription');
    expect(showSuccessSpy).not.toHaveBeenCalled();
    expect(manager.loadSubscriptionData).not.toHaveBeenCalled();
});

it('closes the owned edit modal only after a successful PUT', async () => {
    setSubscriptions(subscription('successful-save'));
    const modalInstances = installBootstrapModal();
    const pendingUpdate = deferred();
    vi.stubGlobal('fetch', vi.fn(() => pendingUpdate.promise));
    vi.spyOn(manager, 'getCSRFToken').mockReturnValue('csrf-edit');
    vi.spyOn(manager, 'loadSubscriptionData').mockResolvedValue();
    vi.spyOn(manager, 'showSuccess').mockImplementation(() => {});
    vi.spyOn(manager, 'showError').mockImplementation(() => {});

    await manager.editSubscription('successful-save');
    const saveButton = document.getElementById('save-subscription-edit');
    saveButton.click();

    expect(saveButton.disabled).toBe(true);
    expect(modalInstances[0].hide).not.toHaveBeenCalled();

    pendingUpdate.resolve({ ok: true, status: 200 });
    await vi.waitFor(() => {
        expect(modalInstances[0].hide).toHaveBeenCalledOnce();
    });

    expect(manager.showSuccess).toHaveBeenCalledWith('Subscription updated');
    expect(manager.loadSubscriptionData).toHaveBeenCalledOnce();
    expect(manager.showError).not.toHaveBeenCalled();
});

it('does not let an older save completion hide a newly opened edit modal', async () => {
    setSubscriptions(
        subscription('older-edit'),
        subscription('newer-edit', { notes: 'New modal notes' }),
    );
    const modalInstances = installBootstrapModal();
    const olderUpdate = deferred();
    vi.stubGlobal('fetch', vi.fn(() => olderUpdate.promise));
    vi.spyOn(manager, 'getCSRFToken').mockReturnValue('csrf-edit');
    vi.spyOn(manager, 'loadSubscriptionData').mockResolvedValue();
    vi.spyOn(manager, 'showSuccess').mockImplementation(() => {});

    await manager.editSubscription('older-edit');
    document.getElementById('save-subscription-edit').click();
    await manager.editSubscription('newer-edit');
    const newerModal = document.getElementById('editSubscriptionModal');

    olderUpdate.resolve({ ok: true, status: 200 });
    await vi.waitFor(() => {
        expect(manager.loadSubscriptionData).toHaveBeenCalledOnce();
    });

    expect(modalInstances).toHaveLength(2);
    expect(modalInstances[0].hide).not.toHaveBeenCalled();
    expect(modalInstances[1].hide).not.toHaveBeenCalled();
    expect(document.getElementById('editSubscriptionModal')).toBe(newerModal);
    expect(document.getElementById('edit-notes').value).toBe('New modal notes');
});

async function openIntervalEditModal(interval, fetchImplementation) {
    setSubscriptions(
        subscription('interval-edit', { refresh_interval_minutes: interval }),
    );
    installBootstrapModal();
    const fetchMock = vi.fn(
        fetchImplementation ||
            (() => Promise.resolve({ ok: true, status: 200 })),
    );
    vi.stubGlobal('fetch', fetchMock);
    vi.spyOn(manager, 'getCSRFToken').mockReturnValue('csrf-edit');
    vi.spyOn(manager, 'loadSubscriptionData').mockResolvedValue();
    vi.spyOn(manager, 'showSuccess').mockImplementation(() => {});
    vi.spyOn(manager, 'showError').mockImplementation(() => {});

    await manager.editSubscription('interval-edit');
    const select = document.getElementById('edit-frequency');
    return {
        fetchMock,
        select,
        // Read from the markup: happy-dom resolves `selected` on options after
        // the second one to the wrong option and reports selectedIndex -1 for
        // a first option that carries it, so only a first-position selection
        // is checked through select.value below.
        selectedOptions: Array.from(select.querySelectorAll('option[selected]')),
        saveButton: document.getElementById('save-subscription-edit'),
    };
}

function sentIntervals(fetchMock) {
    return fetchMock.mock.calls.map(
        ([, options]) => JSON.parse(options.body).refresh_interval_minutes,
    );
}

function choose(select, value) {
    select.value = value;
    select.dispatchEvent(new Event('change', { bubbles: true }));
}

it('requires an explicit choice before replacing a saved interval above the one-week limit', async () => {
    const { fetchMock, select, selectedOptions, saveButton } =
        await openIntervalEditModal(43200);

    // Saving without a choice must not send the first preset, which is what
    // the browser selects when the list holds no option for the saved value.
    saveButton.click();
    expect(sentIntervals(fetchMock)).toEqual([]);

    expect(selectedOptions).toHaveLength(1);
    expect(selectedOptions[0].disabled).toBe(true);
    expect(selectedOptions[0].textContent).toBe(
        'Current: 43200 minutes (above the 1-week limit)',
    );
    expect(select.value).toBe('');
    expect(saveButton.disabled).toBe(true);

    choose(select, '10080');
    expect(saveButton.disabled).toBe(false);
    saveButton.click();
    await vi.waitFor(() => {
        expect(sentIntervals(fetchMock)).toEqual([10080]);
    });
});

it('does not re-enable a pending save when the frequency changes again', async () => {
    const pendingUpdate = deferred();
    const { fetchMock, select, saveButton } = await openIntervalEditModal(
        43200,
        () => pendingUpdate.promise,
    );

    choose(select, '1440');
    saveButton.click();
    expect(sentIntervals(fetchMock)).toEqual([1440]);
    expect(saveButton.disabled).toBe(true);

    choose(select, '720');
    saveButton.click();

    expect(saveButton.disabled).toBe(true);
    expect(sentIntervals(fetchMock)).toEqual([1440]);
    pendingUpdate.resolve({ ok: true, status: 200 });
});

it.each([0, -5, 90.5, null, 'soon'])(
    'requires an explicit choice when the saved interval %s is not a whole number of minutes in range',
    async (interval) => {
        const { fetchMock, select, selectedOptions, saveButton } =
            await openIntervalEditModal(interval);

        saveButton.click();
        expect(sentIntervals(fetchMock)).toEqual([]);

        expect(selectedOptions).toHaveLength(1);
        expect(selectedOptions[0].disabled).toBe(true);
        expect(selectedOptions[0].textContent).toBe(
            'Current interval is not valid',
        );
        expect(select.value).toBe('');
        expect(saveButton.disabled).toBe(true);

        choose(select, '60');
        saveButton.click();
        await vi.waitFor(() => {
            expect(sentIntervals(fetchMock)).toEqual([60]);
        });
    },
);

it.each([90, 2880])(
    'keeps a valid custom interval of %i minutes selected and saves it unchanged',
    async (minutes) => {
        const { fetchMock, select, selectedOptions, saveButton } =
            await openIntervalEditModal(minutes);

        expect(selectedOptions).toHaveLength(1);
        expect(selectedOptions[0].disabled).toBe(false);
        expect(selectedOptions[0].textContent).toBe(`Current: ${minutes} minutes`);
        expect(select.value).toBe(String(minutes));
        expect(saveButton.disabled).toBe(false);

        saveButton.click();
        await vi.waitFor(() => {
            expect(sentIntervals(fetchMock)).toEqual([minutes]);
        });
    },
);

it.each([60, 180, 360, 720, 1440, 10080])(
    'selects the %i-minute preset without adding a current-interval option',
    async (minutes) => {
        const { select, selectedOptions, saveButton } =
            await openIntervalEditModal(minutes);

        expect(select.options).toHaveLength(6);
        expect(selectedOptions.map((option) => option.value)).toEqual([
            String(minutes),
        ]);
        expect(saveButton.disabled).toBe(false);
    },
);