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.

10.5 KB
Raw Blame
/** Endpoint and stale-response contracts for SubscriptionManager loading. */

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

let manager;

function response(payload, ok = true, status = ok ? 200 : 500) {
    return {
        ok,
        status,
        json: vi.fn().mockResolvedValue(payload),
    };
}

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

function mountSubscriptionUi() {
    document.body.innerHTML = `
        <span id="total-subscriptions"></span>
        <span id="active-subscriptions"></span>
        <span id="total-folders"></span>
        <span id="next-refresh-time"></span>
        <ul id="folderTabs">
            <li><button class="nav-link" data-folder="all">All</button></li>
            <li><button class="nav-link" data-folder="Unfiled">Unfiled</button></li>
            <li><button id="create-folder-btn">Create</button></li>
        </ul>
        <div id="subscriptions-list"></div>
    `;
    manager.subscriptions = {};
    manager.folders = [];
    manager.currentFolder = 'all';
}

function organizedSubscription(id, query, folder) {
    return {
        [folder]: [{
            id,
            query_or_topic: query,
            refresh_interval_minutes: 60,
            next_refresh: new Date(Date.now() + 60 * 60 * 1000).toISOString(),
            folder,
            status: 'active',
            notes: '',
        }],
    };
}

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

beforeEach(() => {
    mountSubscriptionUi();
});

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

it('loads stats, folders, and organized subscriptions in endpoint order', async () => {
    const stats = {
        total_subscriptions: 2,
        active_subscriptions: 1,
        total_folders: 1,
    };
    const folders = [{
        id: 'folder-1',
        name: 'Research',
        icon: '📚',
        item_count: 1,
    }];
    const subscriptions = organizedSubscription(
        'subscription-1',
        'FastAPI migration',
        'Research',
    );
    const fetchMock = vi.fn()
        .mockResolvedValueOnce(response(stats))
        .mockResolvedValueOnce(response(folders))
        .mockResolvedValueOnce(response(subscriptions));
    vi.stubGlobal('fetch', fetchMock);

    await manager.loadSubscriptionData();

    expect(fetchMock.mock.calls.map(([url]) => url)).toEqual([
        '/news/api/subscription/stats',
        '/news/api/subscription/folders',
        '/news/api/subscription/subscriptions/organized',
    ]);
    expect(manager.folders).toEqual(folders);
    expect(manager.subscriptions).toEqual(subscriptions);
    expect(document.getElementById('total-subscriptions').textContent).toBe('2');
    expect(document.getElementById('active-subscriptions').textContent).toBe('1');
    expect(document.getElementById('total-folders').textContent).toBe('1');
    expect(document.querySelector('[data-folder="Research"]').textContent)
        .toContain('Research');
    expect(document.querySelector('[data-subscription-id="subscription-1"]')
        .textContent).toContain('FastAPI migration');
});

it('does not let an older modal load overwrite a newer response', async () => {
    const firstStats = deferred();
    let statsCalls = 0;
    let folderCalls = 0;
    let organizedCalls = 0;
    const newestFolders = [{
        id: 'folder-new',
        name: 'Newest',
        icon: 'N',
        item_count: 1,
    }];
    const newestSubscriptions = organizedSubscription(
        'subscription-new',
        'Newest subscription',
        'Newest',
    );
    const fetchMock = vi.fn(url => {
        if (url === '/news/api/subscription/stats') {
            statsCalls += 1;
            if (statsCalls === 1) return firstStats.promise;
            return Promise.resolve(response({
                total_subscriptions: 9,
                active_subscriptions: 8,
                total_folders: 1,
            }));
        }
        if (url === '/news/api/subscription/folders') {
            folderCalls += 1;
            return Promise.resolve(response(folderCalls === 1
                ? newestFolders
                : [{ id: 'folder-old', name: 'Old', item_count: 1 }]));
        }
        if (url === '/news/api/subscription/subscriptions/organized') {
            organizedCalls += 1;
            return Promise.resolve(response(organizedCalls === 1
                ? newestSubscriptions
                : organizedSubscription(
                    'subscription-old',
                    'Old subscription',
                    'Old',
                )));
        }
        throw new Error(`Unexpected request: ${url}`);
    });
    vi.stubGlobal('fetch', fetchMock);

    const olderLoad = manager.loadSubscriptionData();
    const newerLoad = manager.loadSubscriptionData();
    await newerLoad;

    firstStats.resolve(response({
        total_subscriptions: 1,
        active_subscriptions: 1,
        total_folders: 1,
    }));
    await olderLoad;

    expect(manager.folders).toEqual(newestFolders);
    expect(manager.subscriptions).toEqual(newestSubscriptions);
    expect(document.getElementById('total-subscriptions').textContent).toBe('9');
    expect(document.querySelector('[data-subscription-id="subscription-new"]'))
        .not.toBeNull();
    expect(document.querySelector('[data-subscription-id="subscription-old"]'))
        .toBeNull();
    expect(fetchMock).toHaveBeenCalledTimes(4);
});

it('rejects an older organized payload after its final JSON await', async () => {
    const olderOrganizedJson = deferred();
    const newestFolders = [{
        id: 'folder-newest',
        name: 'Newest',
        icon: 'N',
        item_count: 1,
    }];
    const newestSubscriptions = organizedSubscription(
        'subscription-newest',
        'Newest final payload',
        'Newest',
    );
    const fetchMock = vi.fn()
        .mockResolvedValueOnce(response({
            total_subscriptions: 1,
            active_subscriptions: 1,
            total_folders: 1,
        }))
        .mockResolvedValueOnce(response([{
            id: 'folder-older',
            name: 'Older',
            item_count: 1,
        }]))
        .mockResolvedValueOnce({
            ok: true,
            json: vi.fn(() => olderOrganizedJson.promise),
        })
        .mockResolvedValueOnce(response({
            total_subscriptions: 9,
            active_subscriptions: 8,
            total_folders: 1,
        }))
        .mockResolvedValueOnce(response(newestFolders))
        .mockResolvedValueOnce(response(newestSubscriptions));
    vi.stubGlobal('fetch', fetchMock);

    const olderLoad = manager.loadSubscriptionData();
    await vi.waitFor(() => {
        expect(fetchMock).toHaveBeenCalledTimes(3);
    });

    const newerLoad = manager.loadSubscriptionData();
    await newerLoad;
    olderOrganizedJson.resolve(organizedSubscription(
        'subscription-older',
        'Stale final payload',
        'Older',
    ));
    await olderLoad;

    expect(fetchMock).toHaveBeenCalledTimes(6);
    expect(manager.folders).toEqual(newestFolders);
    expect(manager.subscriptions).toEqual(newestSubscriptions);
    expect(document.getElementById('total-subscriptions').textContent).toBe('9');
    expect(document.querySelector('[data-subscription-id="subscription-newest"]'))
        .not.toBeNull();
    expect(document.querySelector('[data-subscription-id="subscription-older"]'))
        .toBeNull();
});

it('suppresses an older load rejection after a newer load succeeds', async () => {
    const olderStats = deferred();
    const newestFolders = [{
        id: 'folder-current',
        name: 'Current',
        icon: 'C',
        item_count: 1,
    }];
    const newestSubscriptions = organizedSubscription(
        'subscription-current',
        'Current subscription',
        'Current',
    );
    const fetchMock = vi.fn()
        .mockImplementationOnce(() => olderStats.promise)
        .mockResolvedValueOnce(response({
            total_subscriptions: 4,
            active_subscriptions: 3,
            total_folders: 1,
        }))
        .mockResolvedValueOnce(response(newestFolders))
        .mockResolvedValueOnce(response(newestSubscriptions));
    vi.stubGlobal('fetch', fetchMock);
    const showErrorSpy = vi.spyOn(manager, 'showError');

    const olderLoad = manager.loadSubscriptionData();
    const newerLoad = manager.loadSubscriptionData();
    await newerLoad;

    olderStats.reject(new Error('older stats request failed'));
    await olderLoad;

    expect(showErrorSpy).not.toHaveBeenCalled();
    expect(manager.folders).toEqual(newestFolders);
    expect(manager.subscriptions).toEqual(newestSubscriptions);
    expect(document.getElementById('total-subscriptions').textContent).toBe('4');
    expect(document.querySelector('[data-subscription-id="subscription-current"]'))
        .not.toBeNull();
    expect(document.getElementById('subscriptions-list').textContent)
        .not.toContain('Failed to load subscriptions');
});

it('replaces the spinner when organized subscriptions returns non-ok', async () => {
    const fetchMock = vi.fn()
        .mockResolvedValueOnce(response({
            total_subscriptions: 2,
            active_subscriptions: 1,
            total_folders: 1,
        }))
        .mockResolvedValueOnce(response([{
            id: 'folder-recoverable',
            name: 'Recoverable',
            item_count: 0,
        }]))
        .mockResolvedValueOnce(response({
            detail: 'subscription backend unavailable',
        }, false, 503));
    vi.stubGlobal('fetch', fetchMock);
    const showAlertMock = vi.fn();
    vi.stubGlobal('showAlert', showAlertMock);

    await manager.loadSubscriptionData();

    expect(fetchMock.mock.calls.map(([url]) => url)).toEqual([
        '/news/api/subscription/stats',
        '/news/api/subscription/folders',
        '/news/api/subscription/subscriptions/organized',
    ]);
    expect(showAlertMock).toHaveBeenCalledWith(
        'Failed to load subscriptions',
        'error',
    );
    const list = document.getElementById('subscriptions-list');
    expect(list.querySelector('.spinner-border')).toBeNull();
    expect(list.querySelector('[role="alert"]')?.textContent)
        .toBe('Failed to load subscriptions. Please try again.');
    expect(manager.subscriptions).toEqual({});
});