/**
 * `clickable` is the keyboard path for elements that cannot be a `<button>`.
 *
 * The tests check the three things a reviewer would otherwise have to take on
 * trust: that the role and tab stop are actually present, that Enter and Space
 * both activate, and that nothing else does — a `div` that fired on every
 * keypress would be worse than one that fired on none.
 */
import { test, describe } from "node:test";
import assert from "node:assert/strict";

import { checkable, clickable, isActivationKey } from "@/lib/a11y";

/** The minimum of a React KeyboardEvent that `clickable` touches. */
const keyEvent = (key: string) => {
  let prevented = false;
  return {
    event: { key, preventDefault: () => (prevented = true) },
    get prevented() {
      return prevented;
    },
  };
};

describe("clickable", () => {
  test("supplies the role and a tab stop", () => {
    const props = clickable(() => {});
    assert.equal(props.role, "button");
    assert.equal(props.tabIndex, 0);
  });

  test("keeps the mouse path", () => {
    let calls = 0;
    clickable(() => calls++).onClick();
    assert.equal(calls, 1);
  });

  test("Enter activates", () => {
    let calls = 0;
    const k = keyEvent("Enter");
    clickable(() => calls++).onKeyDown(k.event as never);
    assert.equal(calls, 1);
  });

  test("Space activates, and its page scroll is prevented", () => {
    let calls = 0;
    const k = keyEvent(" ");
    clickable(() => calls++).onKeyDown(k.event as never);
    assert.equal(calls, 1);
    assert.equal(k.prevented, true, "Space must not also scroll the page");
  });

  test("Enter does not preventDefault anything it should not", () => {
    const k = keyEvent("Enter");
    clickable(() => {}).onKeyDown(k.event as never);
    assert.equal(k.prevented, true);
  });

  test("no other key activates", () => {
    for (const key of ["Tab", "Escape", "a", "ArrowDown", "Shift", "k"]) {
      let calls = 0;
      const k = keyEvent(key);
      clickable(() => calls++).onKeyDown(k.event as never);
      assert.equal(calls, 0, `${key} must not activate`);
      assert.equal(k.prevented, false, `${key} must keep its default`);
    }
  });
});

describe("isActivationKey", () => {
  test("exactly Enter and Space", () => {
    assert.equal(isActivationKey("Enter"), true);
    assert.equal(isActivationKey(" "), true);
    assert.equal(isActivationKey("Spacebar"), false);
    assert.equal(isActivationKey("Tab"), false);
    assert.equal(isActivationKey(""), false);
  });
});

describe("checkable", () => {
  test("announces the role and the checked state", () => {
    assert.equal(checkable(true, () => {}).role, "checkbox");
    assert.equal(checkable(true, () => {})["aria-checked"], true);
    assert.equal(checkable(false, () => {})["aria-checked"], false);
  });

  test("is reachable by keyboard", () => {
    assert.equal(checkable(false, () => {}).tabIndex, 0);
  });

  test("Space and Enter both toggle", () => {
    for (const key of [" ", "Enter"]) {
      let calls = 0;
      const k = keyEvent(key);
      checkable(false, () => calls++).onKeyDown(k.event as never);
      assert.equal(calls, 1, `${key} must toggle`);
      assert.equal(k.prevented, true);
    }
  });

  test("no other key toggles", () => {
    for (const key of ["Tab", "Escape", "x", "ArrowRight"]) {
      let calls = 0;
      checkable(false, () => calls++).onKeyDown(keyEvent(key).event as never);
      assert.equal(calls, 0, `${key} must not toggle`);
    }
  });
});
