// Session 44 — Phase 3: stale-game filtering.

const fs = require('fs');
const path = require('path');
const adapter = require('../../web/src/lib/slateAdapter');

const NOW = new Date('2026-06-18T20:00:00Z').getTime();
const hoursAgo = (h) => new Date(NOW - h * 3_600_000).toISOString();

describe('isRelevantGame', () => {
  it('keeps upcoming and live games regardless of date', () => {
    expect(adapter.isRelevantGame({ status: 'pre', gameTime: hoursAgo(-2) }, NOW)).toBe(true);
    expect(adapter.isRelevantGame({ status: 'in', gameTime: hoursAgo(1) }, NOW)).toBe(true);
  });
  it('drops a completed game older than 24h', () => {
    expect(adapter.isRelevantGame({ status: 'post', gameTime: hoursAgo(120) }, NOW)).toBe(false); // 5 days
    expect(adapter.isRelevantGame({ state: 'final', date: hoursAgo(30) }, NOW)).toBe(false);
  });
  it('keeps a recently completed game (<24h)', () => {
    expect(adapter.isRelevantGame({ status: 'post', gameTime: hoursAgo(3) }, NOW)).toBe(true);
  });
  it('keeps games with an unparseable/missing date (degrade open)', () => {
    expect(adapter.isRelevantGame({ status: 'post' }, NOW)).toBe(true);
    expect(adapter.isRelevantGame({ status: 'post', gameTime: 'nonsense' }, NOW)).toBe(true);
  });
});

describe('Slate applies the freshness filter', () => {
  it('filters games through isRelevantGame', () => {
    const src = fs.readFileSync(path.join(__dirname, '..', '..', 'web', 'src', 'components', 'Slate.tsx'), 'utf8');
    expect(src).toContain('isRelevantGame');
    expect(src).toContain('games.filter((g) => isRelevantGame(g))');
  });
});
