// Session 45 — in-process snapshot scheduler (gated, fires once per slot).

const { startSnapshotScheduler, HOURS_UTC } = require('../../src/snapshotScheduler');

afterEach(() => { delete process.env.SNAPSHOT_CRON; });

describe('startSnapshotScheduler', () => {
  it('no-ops unless SNAPSHOT_CRON=1', () => {
    delete process.env.SNAPSHOT_CRON;
    expect(startSnapshotScheduler()).toBeNull();
  });

  it('defaults to the sports-cycle UTC hours', () => {
    expect(HOURS_UTC).toEqual([14, 19, 22, 1, 3]);
  });

  it('logs an armed startup message including SNAPSHOT_CRON when started', () => {
    process.env.SNAPSHOT_CRON = '1';
    const spy = jest.spyOn(console, 'log').mockImplementation(() => {});
    const sched = startSnapshotScheduler({ runAllSnapshots: jest.fn(), now: () => new Date(Date.UTC(2026, 5, 18, 12, 30, 0)) });
    expect(spy).toHaveBeenCalledWith(expect.stringContaining('[snapshotScheduler] armed'));
    expect(spy).toHaveBeenCalledWith(expect.stringContaining('SNAPSHOT_CRON=1'));
    spy.mockRestore();
    if (sched && sched.interval && sched.interval.unref) clearInterval(sched.interval);
  });

  it('fires runAllSnapshots at a scheduled hour, once per slot', async () => {
    process.env.SNAPSHOT_CRON = '1';
    const runAllSnapshots = jest.fn(async () => [{ sport: 'mlb', status: 'ok' }]);
    let d = new Date(Date.UTC(2026, 5, 18, 14, 0, 0)); // 14:00 UTC — scheduled
    const sched = startSnapshotScheduler({ runAllSnapshots, now: () => d });
    await sched.tick();
    await sched.tick(); // same slot — must NOT double-fire
    expect(runAllSnapshots).toHaveBeenCalledTimes(1);
    if (sched.interval && sched.interval.unref) clearInterval(sched.interval);
  });

  it('does not fire off-schedule (wrong hour / non-zero minute)', async () => {
    process.env.SNAPSHOT_CRON = '1';
    const runAllSnapshots = jest.fn();
    const sched = startSnapshotScheduler({ runAllSnapshots, now: () => new Date(Date.UTC(2026, 5, 18, 15, 0, 0)) });
    await sched.tick();
    const sched2 = startSnapshotScheduler({ runAllSnapshots, now: () => new Date(Date.UTC(2026, 5, 18, 14, 30, 0)) });
    await sched2.tick();
    expect(runAllSnapshots).not.toHaveBeenCalled();
    [sched, sched2].forEach((s) => s.interval && clearInterval(s.interval));
  });
});
