import { describe, expect, it } from 'bun:test';
import { renderToStaticMarkup } from 'react-dom/server';
import { NewsPanel } from './NewsPanel';
import type { NewsArticle } from '../../types/news';

const sampleNews: NewsArticle[] = [
  {
    id: '1',
    sourceId: 'source-1',
    source: 'Source 1',
    headline: 'Breaking macro item',
    summary: 'Summary 1',
    url: 'https://example.com/1',
    publishedAt: '2026-04-08T11:00:00Z',
    publishedTs: 400,
    fetchedAt: '2026-04-08T11:01:00Z',
    sentiment: 'BULL',
    sentimentScore: 0.7,
    highlightReason: 'macro_event',
    tickers: ['AAPL'],
    isRead: false,
    isSaved: false,
  },
  {
    id: '2',
    sourceId: 'source-2',
    source: 'Source 2',
    headline: 'Recent market item',
    summary: 'Summary 2',
    publishedAt: '2026-04-08T10:00:00Z',
    publishedTs: 300,
    fetchedAt: '2026-04-08T10:01:00Z',
    sentiment: 'NEUTRAL',
    sentimentScore: 0,
    tickers: ['MSFT'],
    isRead: false,
    isSaved: false,
  },
];

describe('NewsPanel', () => {
  it('renders highlights and recent sections for generic /news', () => {
    const html = renderToStaticMarkup(<NewsPanel news={sampleNews} />);

    expect(html).toContain('Market News');
    expect(html).toContain('Highlights');
    expect(html).toContain('Recent');
  });

  it('hides highlights when there are no highlighted articles', () => {
    const html = renderToStaticMarkup(
      <NewsPanel
        news={sampleNews.map((article) => ({
          ...article,
          highlightReason: undefined,
        }))}
      />,
    );

    expect(html).not.toContain('Highlights');
    expect(html).toContain('Recent');
  });

  it('renders ticker mode as a single chronological view', () => {
    const html = renderToStaticMarkup(<NewsPanel news={sampleNews} ticker="NVDA" />);

    expect(html).toContain('News: NVDA');
    expect(html).not.toContain('>Highlights<');
    expect(html).not.toContain('>Recent<');
  });

  it('includes the ticker symbol in the empty state for ticker mode', () => {
    const html = renderToStaticMarkup(<NewsPanel news={[]} ticker="NVDA" />);

    expect(html).toContain('No news articles found for NVDA.');
  });
});
