import assert from "node:assert/strict";
import { describe, it } from "node:test";
import { PointerStick } from "../input/pointerStick.ts";

const BOUNDS = { left: 10, top: 20, width: 120, height: 120 };

describe("analogue pointer stick", () => {
  it("normalizes the disc and preserves the vertical controller convention", () => {
    const stick = new PointerStick();
    assert.deepEqual(stick.begin(7, 70, 20, BOUNDS), { moveX: 0, moveY: 1 });
    const corner = stick.move(7, 130, 20);
    assert.ok(corner);
    assert.ok(Math.abs(Math.hypot(corner.moveX, corner.moveY) - 1) < 1e-12);
  });

  it("does not let another finger steal or release the active gesture", () => {
    const stick = new PointerStick();
    stick.begin(7, 70, 20, BOUNDS);
    assert.equal(stick.begin(8, 10, 80, BOUNDS), null);
    assert.equal(stick.end(8), false);
    assert.equal(stick.activePointer(), 7);
    assert.ok(stick.move(7, 10, 80));
  });

  it("returns to neutral ownership after release or cancellation", () => {
    const stick = new PointerStick();
    stick.begin(3, 70, 20, BOUNDS);
    assert.equal(stick.end(3), true);
    assert.equal(stick.activePointer(), null);
    assert.deepEqual(stick.begin(4, 70, 80, BOUNDS), { moveX: 0, moveY: 0 });
    assert.equal(stick.cancel(4), true);
    assert.equal(stick.activePointer(), null);
  });
});
