import { ListingFeaturedExpiredHandler } from '../event-handlers/listing-featured-expired.handler';

describe('ListingFeaturedExpiredHandler', () => {
  let handler: ListingFeaturedExpiredHandler;
  let mockIndexer: { indexListing: ReturnType<typeof vi.fn> };
  let mockCache: {
    invalidate: ReturnType<typeof vi.fn>;
    invalidateByPrefix: ReturnType<typeof vi.fn>;
  };
  let mockLogger: { log: ReturnType<typeof vi.fn> };

  beforeEach(() => {
    mockIndexer = { indexListing: vi.fn().mockResolvedValue(undefined) };
    mockCache = {
      invalidate: vi.fn().mockResolvedValue(undefined),
      invalidateByPrefix: vi.fn().mockResolvedValue(undefined),
    };
    // Provide static buildKey on the mock
    (mockCache as any).constructor = { buildKey: (prefix: string, id: string) => `${prefix}:${id}` };
    mockLogger = { log: vi.fn() };

    handler = new ListingFeaturedExpiredHandler(
      mockIndexer as any,
      mockCache as any,
      mockLogger as any,
    );
  });

  it('re-indexes listing and invalidates caches on featured expiry', async () => {
    const event = {
      aggregateId: 'listing-1',
      expiredAt: new Date(),
      eventName: 'listing.featured_expired',
      occurredAt: new Date(),
    };

    await handler.handle(event as any);

    expect(mockIndexer.indexListing).toHaveBeenCalledWith('listing-1');
    expect(mockCache.invalidateByPrefix).toHaveBeenCalled();
  });
});
