/**
 * Regression tests for the dialog focus trap's wrap-around.
 *
 * `Modal` declared `role="dialog"` and `aria-modal="true"` — which tell assistive
 * technology the rest of the page is inert — but nothing made it so: a keyboard
 * user could Tab out of any dialog into the page behind, and could not dismiss
 * one without finding the ✕ with a mouse. The trap added for that is only correct
 * if its wrap-around is, and the edge cases are where it goes wrong: both ends, a
 * dialog with a single focusable control, and focus that has already escaped.
 */
import { test, describe } from "node:test";
import assert from "node:assert/strict";
import { wrapFocusTarget } from "@/components/ui/useModalKeyboard";

/** Three focusable controls, as a confirmation dialog has: ✕, Confirm, Cancel. */
const three = ["close", "confirm", "cancel"];

describe("wrapFocusTarget — Tab forwards", () => {
  test("mid-list Tab is left to the browser", () => {
    // Returning null means "do not preventDefault" — native order is correct.
    assert.equal(
      wrapFocusTarget({ nodes: three, activeIndex: 0, shiftKey: false }),
      null
    );
    assert.equal(
      wrapFocusTarget({ nodes: three, activeIndex: 1, shiftKey: false }),
      null
    );
  });

  test("Tab from the last control wraps to the first", () => {
    assert.equal(
      wrapFocusTarget({ nodes: three, activeIndex: 2, shiftKey: false }),
      0
    );
  });
});

describe("wrapFocusTarget — Shift+Tab backwards", () => {
  test("mid-list Shift+Tab is left to the browser", () => {
    assert.equal(
      wrapFocusTarget({ nodes: three, activeIndex: 2, shiftKey: true }),
      null
    );
    assert.equal(
      wrapFocusTarget({ nodes: three, activeIndex: 1, shiftKey: true }),
      null
    );
  });

  test("Shift+Tab from the first control wraps to the last", () => {
    assert.equal(
      wrapFocusTarget({ nodes: three, activeIndex: 0, shiftKey: true }),
      2
    );
  });
});

describe("wrapFocusTarget — the edge cases that make it a trap", () => {
  test("focus that has escaped the dialog is pulled back in", () => {
    // activeIndex -1 means the focused element is not in the dialog at all —
    // which is the state the missing trap used to leave the page in.
    assert.equal(
      wrapFocusTarget({ nodes: three, activeIndex: -1, shiftKey: false }),
      0
    );
    assert.equal(
      wrapFocusTarget({ nodes: three, activeIndex: -1, shiftKey: true }),
      2
    );
  });

  test("a single focusable control keeps focus on itself, both directions", () => {
    // It is both the first and the last, so Tab must wrap to it rather than
    // falling through to the page behind.
    assert.equal(wrapFocusTarget({ nodes: ["only"], activeIndex: 0, shiftKey: false }), 0);
    assert.equal(wrapFocusTarget({ nodes: ["only"], activeIndex: 0, shiftKey: true }), 0);
  });

  test("a dialog with nothing focusable is left alone", () => {
    // No trap is possible, and preventing default would freeze Tab entirely.
    assert.equal(wrapFocusTarget({ nodes: [], activeIndex: -1, shiftKey: false }), null);
    assert.equal(wrapFocusTarget({ nodes: [], activeIndex: 0, shiftKey: true }), null);
  });

  test("two controls wrap in both directions", () => {
    const two = ["confirm", "cancel"];
    assert.equal(wrapFocusTarget({ nodes: two, activeIndex: 1, shiftKey: false }), 0);
    assert.equal(wrapFocusTarget({ nodes: two, activeIndex: 0, shiftKey: true }), 1);
    assert.equal(wrapFocusTarget({ nodes: two, activeIndex: 0, shiftKey: false }), null);
    assert.equal(wrapFocusTarget({ nodes: two, activeIndex: 1, shiftKey: true }), null);
  });
});
