import { ScraperService } from '../../src/scrapers/scraper-service';
import { VendorListing } from '../../src/scrapers/types';

describe('ScraperService', () => {
  let service: ScraperService;

  beforeEach(() => {
    service = new ScraperService();
  });

  afterEach(async () => {
    await service.close();
  });

  describe('calculatePriceIndex', () => {
    it('should return null for empty listings', () => {
      const result = service.calculatePriceIndex([]);
      expect(result).toBeNull();
    });

    it('should calculate median for odd number of listings', () => {
      const listings: VendorListing[] = [
        { pricePerMillion: 10 } as VendorListing,
        { pricePerMillion: 20 } as VendorListing,
        { pricePerMillion: 30 } as VendorListing,
      ];
      
      const result = service.calculatePriceIndex(listings);
      expect(result).toBe(20);
    });

    it('should calculate median for even number of listings', () => {
      const listings: VendorListing[] = [
        { pricePerMillion: 10 } as VendorListing,
        { pricePerMillion: 20 } as VendorListing,
        { pricePerMillion: 30 } as VendorListing,
        { pricePerMillion: 40 } as VendorListing,
      ];
      
      const result = service.calculatePriceIndex(listings);
      expect(result).toBe(25);
    });

    it('should handle unsorted listings', () => {
      const listings: VendorListing[] = [
        { pricePerMillion: 30 } as VendorListing,
        { pricePerMillion: 10 } as VendorListing,
        { pricePerMillion: 20 } as VendorListing,
      ];
      
      const result = service.calculatePriceIndex(listings);
      expect(result).toBe(20);
    });
  });
});
