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.4 KB
Raw Blame
/**
 * Behavior coverage for the non-news research banner on the news page.
 *
 * checkPriorityStatus is intentionally private, so these tests enter through
 * the real DOMContentLoaded initialization and retain the polling callback
 * that initializeNewsPage registers for subsequent checks.
 */

import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest';

let fetchMock;
let historyPayload = { items: [] };
let originalFetch;
let priorityPoll;
let setIntervalSpy;

function response(body) {
    return {
        ok: true,
        status: 200,
        headers: {},
        json: () => Promise.resolve(body),
        text: () => Promise.resolve(''),
    };
}

async function checkPriorityWith(items) {
    historyPayload = { items };
    fetchMock.mockClear();
    await priorityPoll();
}

beforeAll(async () => {
    originalFetch = globalThis.fetch;
    localStorage.clear();
    sessionStorage.clear();

    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',
    ]);
    fetchMock = vi.fn((input) => {
        const url = String(input);
        if (url === '/history/api') return Promise.resolve(response(historyPayload));
        if (url.includes('/news/api/subscriptions/current')) {
            return Promise.resolve(response({ subscriptions: [] }));
        }
        if (url.includes('/news/api/feed')) {
            return Promise.resolve(response({ news_items: [] }));
        }
        if (url.includes('/news/api/search-history')) {
            return Promise.resolve(response({ search_history: [] }));
        }
        return Promise.resolve(response({}));
    });
    globalThis.fetch = fetchMock;

    setIntervalSpy = vi.spyOn(globalThis, 'setInterval').mockImplementation((callback, delay) => {
        if (delay === 30000) priorityPoll = callback;
        return 1;
    });

    document.body.innerHTML = `
        <div class="ldr-feed-header"><h2>News</h2></div>
        <div id="news-feed-content"></div>
        <input id="table-view-toggle" type="checkbox">
        <table><tbody id="news-table-body"></tbody></table>
        <input id="news-search" type="text">
        <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"></span>
        <div id="news-semantic-results"></div>
        <div id="news-query"></div>
        <div id="newsSubscriptionModal"></div>
        <div id="news-subscription-query"></div>
        <div class="ldr-time-filter-group"><button class="ldr-filter-btn"></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>
    `;

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

    await vi.waitFor(() => {
        expect(priorityPoll).toEqual(expect.any(Function));
        expect(fetchMock).toHaveBeenCalledWith('/history/api');
    });
});

afterAll(() => {
    window.dispatchEvent(new Event('pagehide'));
    setIntervalSpy.mockRestore();
    globalThis.fetch = originalFetch;
    document.body.replaceChildren();
});

describe('pages/news.js priority status', () => {
    it('uses the canonical history endpoint and shows an active non-news item from the items envelope', async () => {
        await checkPriorityWith([{
            id: 'research-42',
            query: 'Investigate the migration behavior',
            status: 'in_progress',
            metadata: { is_news_search: false },
        }]);

        const requestedUrls = fetchMock.mock.calls.map(([url]) => String(url));
        expect(requestedUrls).toEqual(['/history/api']);
        expect(requestedUrls).not.toContain('/api/history');

        const status = document.getElementById('priority-status');
        const message = document.getElementById('priority-message');
        const link = message.querySelector('a');
        expect(status.style.display).toBe('block');
        expect(message.textContent).toContain('Investigate the migration behavior');
        expect(link?.getAttribute('href')).toBe('/progress/research-42');
    });

    it('hides the banner when the only active item is a news search', async () => {
        const status = document.getElementById('priority-status');
        status.style.display = 'block';

        await checkPriorityWith([{
            id: 'news-17',
            query: 'Today in technology',
            status: 'queued',
            metadata: { is_news_search: true },
        }]);

        expect(status.style.display).toBe('none');
    });

    it('hides the banner when the items envelope is empty', async () => {
        const status = document.getElementById('priority-status');
        status.style.display = 'block';

        await checkPriorityWith([]);

        expect(status.style.display).toBe('none');
    });
});