import { describe, expect, it, vi } from 'vitest';

import { MarketDataAdapter } from '../packages/utils/src/marketDataAdapter';
import { ShadowBook } from '../packages/utils/src/shadowBook';
import type { OrderBook } from '../packages/domain/src/types';

describe('MarketDataAdapter', () => {
  it('updates shadow book and emits events for snapshot/delta', async () => {
    const shadow = new ShadowBook();
    const snapshot: OrderBook = {
      bids: [{ px: 100, sz: 1 }],
      asks: [{ px: 101, sz: 1 }],
      ts: Date.now()
    };

    const fetchSnapshot = vi.fn(async () => snapshot);
    const adapter = new MarketDataAdapter({
      symbols: ['BTC'],
      shadowBook: shadow,
      fetchSnapshot,
      pollIntervalMs: 10
    });

    const events: string[] = [];
    adapter.on('snapshot', payload => {
      events.push(`snapshot:${payload.symbol}`);
    });

    await adapter.start();
    expect(fetchSnapshot).toHaveBeenCalled();
    const stored = shadow.snapshot('BTC');
    expect(stored?.bids[0]?.px).toBe(100);

    adapter.ingestDelta('BTC', {
      bids: [{ price: 100.5, size: 2 }],
      seq: 1
    });
    const updated = shadow.snapshot('BTC');
    expect(updated?.bids[0]?.px).toBe(100.5);

    adapter.stop();
    expect(events).toContain('snapshot:BTC');
  });
});
