import { describe, expect, it } from "vitest";

import { trapTarget } from "./focusTrap";

// P1-7：焦点陷阱决策核（纯函数，node 可测；DOM 包装 handleTabTrap 留浏览器）。
describe("trapTarget（focus trap 决策）", () => {
  it("Tab 在尾元素时环回到首", () => {
    expect(
      trapTarget({
        focusableCount: 3,
        activeIndex: 2,
        containsActive: true,
        shiftKey: false,
      }),
    ).toBe("first");
  });

  it("Shift+Tab 在首元素时环回到尾", () => {
    expect(
      trapTarget({
        focusableCount: 3,
        activeIndex: 0,
        containsActive: true,
        shiftKey: true,
      }),
    ).toBe("last");
  });

  it("Tab 在中间元素不接管（放行原生顺序）", () => {
    expect(
      trapTarget({
        focusableCount: 3,
        activeIndex: 1,
        containsActive: true,
        shiftKey: false,
      }),
    ).toBe("none");
  });

  it("焦点已逃出容器时 Tab 拉回首元素", () => {
    expect(
      trapTarget({
        focusableCount: 3,
        activeIndex: -1,
        containsActive: false,
        shiftKey: false,
      }),
    ).toBe("first");
  });

  it("焦点已逃出容器时 Shift+Tab 拉回尾元素", () => {
    expect(
      trapTarget({
        focusableCount: 3,
        activeIndex: -1,
        containsActive: false,
        shiftKey: true,
      }),
    ).toBe("last");
  });

  it("无可聚焦子元素时把焦点钉在容器上", () => {
    expect(
      trapTarget({
        focusableCount: 0,
        activeIndex: -1,
        containsActive: true,
        shiftKey: false,
      }),
    ).toBe("container");
  });
});
