import { describe, expect, it } from 'vitest';
import { POLL_IDLE_MS, POLL_INPLAY_MS, POLL_NEAR_MS, fallbackAllowed, pollDelay } from './cadence';

describe('pollDelay', () => {
  it('polls fastest while a match is in play', () => {
    expect(pollDelay(true, true)).toBe(POLL_INPLAY_MS);
    expect(pollDelay(true, false)).toBe(POLL_INPLAY_MS);
  });

  it('polls at the near cadence inside a kickoff window', () => {
    expect(pollDelay(false, true)).toBe(POLL_NEAR_MS);
  });

  it('idles when nothing is on', () => {
    expect(pollDelay(false, false)).toBe(POLL_IDLE_MS);
  });

  it('tiers are ordered in-play < near < idle', () => {
    expect(POLL_INPLAY_MS).toBeLessThan(POLL_NEAR_MS);
    expect(POLL_NEAR_MS).toBeLessThan(POLL_IDLE_MS);
  });
});

describe('fallbackAllowed', () => {
  it('blocks attempts inside the throttle interval', () => {
    expect(fallbackAllowed(1_000_000, 1_010_000)).toBe(false);
  });

  it('allows attempts once the interval has passed', () => {
    expect(fallbackAllowed(1_000_000, 1_015_000)).toBe(true);
    expect(fallbackAllowed(0, 1_000_000)).toBe(true);
  });

  it('keeps the 8s in-play cadence under the 10 req/min football-data cap', () => {
    // one attempt per 15s ⇒ 4 req/min even if every tick fails over
    expect(fallbackAllowed(0, 15_000)).toBe(true);
    expect(fallbackAllowed(0, 14_999)).toBe(false);
  });
});
