import { ReindexAllHandler } from '../commands/reindex-all/reindex-all.handler';

describe('ReindexAllHandler', () => {
  let handler: ReindexAllHandler;
  let mockIndexer: { reindexAll: ReturnType<typeof vi.fn>; indexListing: ReturnType<typeof vi.fn>; removeListing: ReturnType<typeof vi.fn> };

  beforeEach(() => {
    mockIndexer = {
      reindexAll: vi.fn(),
      indexListing: vi.fn(),
      removeListing: vi.fn(),
    };
    handler = new ReindexAllHandler(mockIndexer as any);
  });

  it('delegates to indexer.reindexAll and returns result', async () => {
    mockIndexer.reindexAll.mockResolvedValue({ indexed: 500, total: 500 });

    const result = await handler.execute();

    expect(result.indexed).toBe(500);
    expect(result.total).toBe(500);
    expect(mockIndexer.reindexAll).toHaveBeenCalledTimes(1);
  });

  it('returns zero counts when no listings exist', async () => {
    mockIndexer.reindexAll.mockResolvedValue({ indexed: 0, total: 0 });

    const result = await handler.execute();

    expect(result.indexed).toBe(0);
    expect(result.total).toBe(0);
  });
});
