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.

4.9 KB
Raw Blame
/** Direct contracts for the optional News design-enhancement runtime. */

let voteButton;
let topicPill;
let refreshButton;
let refreshListener;
let inlineRefresh;
let initialImpactWidth;

beforeAll(async () => {
    vi.useFakeTimers();
    document.body.innerHTML = `
        <article class="ldr-news-item" id="news-a"></article>
        <article class="ldr-news-item" id="news-b"></article>
        <button class="ldr-vote-btn" id="vote"></button>
        <button class="ldr-topic-pill" id="topic"></button>
        <div class="ldr-news-cards-container"></div>
        <button id="refresh-feed-btn"></button>
        <label id="search-wrapper"><input id="news-search"></label>
        <div class="ldr-impact-fill" style="width: 72%"></div>
        <header class="ldr-feed-header-section"></header>
    `;

    voteButton = document.getElementById('vote');
    topicPill = document.getElementById('topic');
    refreshButton = document.getElementById('refresh-feed-btn');
    vi.spyOn(voteButton, 'getBoundingClientRect').mockReturnValue({
        width: 40,
        height: 20,
        left: 10,
        top: 20,
        right: 50,
        bottom: 40,
        x: 10,
        y: 20,
        toJSON: () => ({}),
    });

    // news.js owns refresh through addEventListener; some deployments may
    // also provide a legacy inline callback. The enhancement must preserve
    // both ownership paths while adding only visual state.
    refreshListener = vi.fn();
    inlineRefresh = vi.fn();
    refreshButton.addEventListener('click', refreshListener);
    refreshButton.onclick = inlineRefresh;

    await import('@js/news-enhancements.js');
    document.dispatchEvent(new Event('DOMContentLoaded'));
    initialImpactWidth = document.querySelector('.ldr-impact-fill').style.width;
});

afterAll(() => {
    vi.clearAllTimers();
    vi.useRealTimers();
    vi.restoreAllMocks();
    document.body.innerHTML = '';
    document.head.querySelectorAll('style').forEach(style => {
        if (style.textContent.includes('ripple-animation')) style.remove();
    });
});

it('initializes stagger, hover, search, impact, and parallax behavior', async () => {
    const [firstItem, secondItem] = document.querySelectorAll('.ldr-news-item');
    expect(firstItem.style.animationDelay).toBe('0s');
    expect(secondItem.style.animationDelay).toBe('0.1s');

    firstItem.dispatchEvent(new MouseEvent('mouseenter'));
    expect(firstItem.style.transform).toBe('translateY(-4px)');
    firstItem.dispatchEvent(new MouseEvent('mouseleave'));
    expect(firstItem.style.transform).toBe('translateY(0)');

    const search = document.getElementById('news-search');
    search.dispatchEvent(new Event('focus'));
    expect(document.getElementById('search-wrapper').style.transform)
        .toBe('scale(1.02)');
    search.dispatchEvent(new Event('blur'));
    expect(document.getElementById('search-wrapper').style.transform)
        .toBe('scale(1)');

    const impact = document.querySelector('.ldr-impact-fill');
    // Capture the one-time bootstrap state before another shuffled test can
    // advance the suite's shared fake clock past this animation timeout.
    expect(initialImpactWidth).toBe('0px');
    await vi.advanceTimersByTimeAsync(500);
    expect(impact.style.width).toBe('72%');

    Object.defineProperty(window, 'pageYOffset', {
        configurable: true,
        value: 250,
    });
    window.dispatchEvent(new Event('scroll'));
    expect(document.querySelector('.ldr-feed-header-section').style.transform)
        .toBe('translateY(-25px)');
});

it('owns temporary click effects and removes them on schedule', async () => {
    voteButton.dispatchEvent(new MouseEvent('click', {
        bubbles: true,
        clientX: 30,
        clientY: 30,
    }));
    const ripple = voteButton.querySelector('.ldr-ripple');
    expect(ripple).not.toBeNull();
    expect(ripple.style.width).toBe('40px');
    expect(ripple.style.height).toBe('40px');
    expect(ripple.style.left).toBe('0px');
    expect(ripple.style.top).toBe('-10px');

    topicPill.click();
    expect(topicPill.style.transform).toBe('scale(0.95)');
    await vi.advanceTimersByTimeAsync(150);
    expect(topicPill.style.transform).toBe('scale(1)');
    await vi.advanceTimersByTimeAsync(450);
    expect(voteButton.querySelector('.ldr-ripple')).toBeNull();

    expect(document.head.querySelector('style').textContent)
        .toContain('pointer-events: none');
});

it('adds and releases refresh skeleton state without stealing refresh ownership', async () => {
    const container = document.querySelector('.ldr-news-cards-container');

    refreshButton.click();

    expect(refreshListener).toHaveBeenCalledOnce();
    expect(inlineRefresh).toHaveBeenCalledOnce();
    expect(container.style.opacity).toBe('0.5');
    expect(container.classList.contains('ldr-skeleton-loader')).toBe(true);

    await vi.advanceTimersByTimeAsync(1000);
    expect(container.style.opacity).toBe('1');
    expect(container.classList.contains('ldr-skeleton-loader')).toBe(false);
});