import assert from "node:assert/strict";
import test from "node:test";
import { createSleepTimer } from "../../lib/client/player/sleep-timer.ts";

test("supports 15/30/45/60 minute deadlines and cancellation", () => {
  let now = 1_000;
  const timer = createSleepTimer(() => now);
  timer.setMinutes(15);
  assert.equal(timer.remainingMs(), 15 * 60_000);
  now += 5 * 60_000;
  assert.equal(timer.remainingMs(), 10 * 60_000);
  assert.equal(timer.shouldStop(), false);
  now += 10 * 60_000;
  assert.equal(timer.shouldStop(), true);
  timer.cancel();
  assert.equal(timer.remainingMs(), null);
});

test("stops at the end of the current chapter only when armed", () => {
  const timer = createSleepTimer(() => 0);
  timer.setChapterEnd("chapter-2");
  assert.equal(timer.shouldStopAtChapterBoundary("chapter-1", "chapter-2"), false);
  assert.equal(timer.shouldStopAtChapterBoundary("chapter-2", "chapter-3"), true);
  assert.equal(timer.shouldStopAtChapterBoundary("chapter-2", null), true);
});
