import {collectBeepTimes, countBeeps, slotOfBeep} from './stopwatch.component';

const MINUTE = 60_000;

describe('stopwatch interval scheduling', () => {
  const firstMs = 1 * MINUTE;
  const secondMs = 2 * MINUTE;

  it('counts no beeps without a configured first interval', () => {
    expect(countBeeps(10 * MINUTE, 0, secondMs)).toBe(0);
  });

  it('falls back to a single repeating interval when the second one is off', () => {
    expect(countBeeps(0.9 * MINUTE, firstMs, 0)).toBe(0);
    expect(countBeeps(1 * MINUTE, firstMs, 0)).toBe(1);
    expect(countBeeps(3 * MINUTE, firstMs, 0)).toBe(3);
  });

  it('alternates both intervals so beeps fall on 1, 3, 4, 6, 7 minutes', () => {
    const expectedBeepCountPerMinute = [0, 1, 1, 2, 3, 3, 4, 5, 5, 6];

    expectedBeepCountPerMinute.forEach((expected, minute) => {
      expect(countBeeps(minute * MINUTE, firstMs, secondMs)).toBe(expected);
    });
  });

  it('reports the alternating beep times', () => {
    const times = collectBeepTimes(firstMs, secondMs, 5);

    expect(times).toEqual([1 * MINUTE, 3 * MINUTE, 4 * MINUTE, 6 * MINUTE, 7 * MINUTE]);
  });

  it('assigns odd beeps to the first interval and even ones to the second', () => {
    expect(slotOfBeep(1, secondMs)).toBe('first');
    expect(slotOfBeep(2, secondMs)).toBe('second');
    expect(slotOfBeep(3, secondMs)).toBe('first');
    expect(slotOfBeep(2, 0)).toBe('first');
  });
});
