import { describe, test, expect } from 'vitest';
import { selectReelsForViewFetch } from './viewfetch.js';
import type { ScrapedPost } from './types.js';

const NOW = '2026-06-29T00:00:00.000Z';
const reel = (over: Partial<ScrapedPost>): ScrapedPost => ({
  shortcode: 'ABC', type: 'reel', url: '', thumbnail: null, caption: null,
  uploadedAt: NOW, viewCount: null, likeCount: null, commentCount: 0, ...over,
});

describe('selectReelsForViewFetch', () => {
  test('includes a reel uploaded within the window', () => {
    const posts = [reel({ shortcode: 'fresh', uploadedAt: '2026-06-28T00:00:00.000Z' })];
    expect(selectReelsForViewFetch(posts, NOW, 3).map((p) => p.shortcode)).toEqual(['fresh']);
  });

  test('excludes a reel older than the window', () => {
    const posts = [reel({ shortcode: 'old', uploadedAt: '2026-06-20T00:00:00.000Z' })];
    expect(selectReelsForViewFetch(posts, NOW, 3)).toEqual([]);
  });

  test('includes a reel exactly windowDays old (boundary)', () => {
    const posts = [reel({ shortcode: 'edge', uploadedAt: '2026-06-26T00:00:00.000Z' })]; // exactly 3d
    expect(selectReelsForViewFetch(posts, NOW, 3).map((p) => p.shortcode)).toEqual(['edge']);
  });

  test('excludes non-reel posts and reels without a shortcode', () => {
    const posts = [
      reel({ type: 'post', shortcode: 'photo' }),
      reel({ shortcode: '' }),
    ];
    expect(selectReelsForViewFetch(posts, NOW, 3)).toEqual([]);
  });
});
