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.

5.2 KB
Raw Blame
/** Isolated terminal lifecycle coverage for the real news page runtime. */

const NOW = new Date('2026-09-01T12:00:00.000Z');

function response(payload) {
    return {
        ok: true,
        status: 200,
        headers: {},
        json: vi.fn().mockResolvedValue(payload),
        text: vi.fn().mockResolvedValue(JSON.stringify(payload)),
    };
}

function installDom() {
    document.body.innerHTML = `
        <header class="ldr-feed-header"><h2>News</h2></header>
        <section id="news-feed-content"></section>
        <input id="table-view-toggle" type="checkbox">
        <input id="news-search" type="search">
        <button id="search-btn"></button>
        <button id="create-subscription-btn"></button>
        <button id="run-template-btn"></button>
        <input id="impact-filter" type="range" min="0" max="10" value="0">
        <span class="ldr-impact-value">0+</span>
        <section id="news-semantic-results"></section>
        <input id="news-query">
        <div id="newsSubscriptionModal"></div>
        <textarea id="news-subscription-query"></textarea>
        <div class="ldr-time-filter-group">
            <button class="ldr-filter-btn active" data-time="all"></button>
        </div>
        <input id="auto-refresh" type="checkbox">
        <button id="refresh-feed-btn"></button>
        <div id="recent-searches"></div>
        <div id="trending-topics"></div>
        <div id="priority-status" style="display: none">
            <span id="priority-message"></span>
        </div>
        <div id="news-alert"></div>
    `;
}

let fetchMock;

beforeAll(async () => {
    vi.useFakeTimers();
    vi.setSystemTime(NOW);
    localStorage.clear();
    sessionStorage.clear();
    installDom();

    const purify = {
        isSupported: true,
        addHook: vi.fn(),
        sanitize: vi.fn(dirty => {
            const template = document.createElement('template');
            // eslint-disable-next-line no-unsanitized/property -- controlled test-only HTML fixture.
            template.innerHTML = String(dirty);
            return template.content.cloneNode(true);
        }),
    };
    globalThis.DOMPurify = purify;
    window.DOMPurify = purify;
    window.RESEARCH_STATUS = {
        IN_PROGRESS: 'in_progress',
        QUEUED: 'queued',
        PENDING: 'pending',
        COMPLETED: 'completed',
        FAILED: 'failed',
        ERROR: 'error',
        CANCELLED: 'cancelled',
        SUSPENDED: 'suspended',
    };
    window.RESEARCH_TERMINAL_STATES = new Set([
        'completed', 'failed', 'error', 'cancelled', 'suspended',
    ]);

    const feedItems = [{
        id: 'news-one',
        research_id: 'research-one',
        headline: 'FastAPI migration reaches production',
        impact_score: 9,
        created_at: NOW.toISOString(),
        topics: [],
        links: [],
    }, {
        id: 'news-two',
        research_id: 'research-two',
        headline: 'Realtime migration follow-up',
        impact_score: 5,
        created_at: NOW.toISOString(),
        topics: [],
        links: [],
    }];

    fetchMock = vi.fn(async input => {
        const url = String(input);
        if (url === '/news/api/subscriptions/current') {
            return response({ subscriptions: [] });
        }
        if (url.startsWith('/news/api/feed?')) {
            return response({ news_items: feedItems });
        }
        if (url === '/news/api/feedback/batch') {
            return response({ votes: {} });
        }
        if (url === '/news/api/search-history') {
            return response({ search_history: [] });
        }
        if (url === '/history/api') return response({ items: [] });
        return response({});
    });
    vi.stubGlobal('fetch', fetchMock);

    await import('@js/config/constants.js');
    await import('@js/config/urls.js');
    await import('@js/security/url-validator.js');
    await import('@js/security/xss-protection.js');
    await import('@js/pages/news.js');
    document.dispatchEvent(new Event('DOMContentLoaded'));

    await vi.waitFor(() => {
        expect(document.querySelectorAll('.ldr-news-item')).toHaveLength(2);
        expect(fetchMock).toHaveBeenCalledWith('/history/api');
    });
});

afterAll(() => {
    window.dispatchEvent(new Event('pagehide'));
    vi.clearAllTimers();
    vi.useRealTimers();
    vi.restoreAllMocks();
    vi.unstubAllGlobals();
    localStorage.clear();
    sessionStorage.clear();
    document.body.replaceChildren();
    delete window.DOMPurify;
});

it('persists visit/read state and retires scheduled work on page exit', async () => {
    window.markAllAsRead();
    localStorage.removeItem('news_read_ids');

    const unloadTime = new Date().toISOString();
    window.dispatchEvent(new Event('beforeunload'));
    expect(localStorage.getItem('news_last_visit')).toBe(unloadTime);
    expect(JSON.parse(localStorage.getItem('news_read_ids')))
        .toEqual(expect.arrayContaining(['news-one', 'news-two']));

    const priorityCalls = fetchMock.mock.calls.filter(([url]) => (
        url === '/history/api'
    )).length;
    window.dispatchEvent(new Event('pagehide'));
    await vi.advanceTimersByTimeAsync(30000);
    expect(fetchMock.mock.calls.filter(([url]) => (
        url === '/history/api'
    ))).toHaveLength(priorityCalls);
});