import test from "node:test";
import assert from "node:assert/strict";
import { scheduleAuthenticationStartup } from "./authentication-startup.js";

test("authentication startup stays hidden until 600ms has elapsed", () => {
  let scheduled;
  let revealed = false;

  const cancel = scheduleAuthenticationStartup(
    () => { revealed = true; },
    {
      setTimer(callback, delay) {
        scheduled = { callback, delay };
        return 17;
      },
      clearTimer() {}
    }
  );

  assert.equal(revealed, false);
  assert.equal(scheduled.delay, 600);

  scheduled.callback();
  assert.equal(revealed, true);
  cancel();
});

test("authentication startup cancellation prevents a pending reveal", () => {
  let scheduled;
  let revealed = false;

  const cancel = scheduleAuthenticationStartup(
    () => { revealed = true; },
    {
      setTimer(callback) {
        scheduled = { callback, canceled: false };
        return 23;
      },
      clearTimer(timerId) {
        assert.equal(timerId, 23);
        scheduled.canceled = true;
      }
    }
  );

  cancel();
  if (!scheduled.canceled) scheduled.callback();

  assert.equal(revealed, false);
});
