import { beforeEach, describe, expect, it } from "vitest";
import {
  getControllerMovement,
  resetControllerState,
  setExternalControllerMovement,
  subscribeControllerMovement,
} from "./controller";

describe("controller movement normalization", () => {
  beforeEach(() => resetControllerState());

  it("ignores dead-zone noise and quantizes analog jitter", () => {
    const updates: Array<{ moveX: number; moveY: number; lookX: number; lookY: number }> = [];
    const unsubscribe = subscribeControllerMovement((movement) => {
      updates.push({ ...movement });
    });

    setExternalControllerMovement({ moveX: 0.08, moveY: -0.1, lookX: 0.11, lookY: -0.02 });
    expect(updates).toHaveLength(0);

    setExternalControllerMovement({ moveX: 0.5001, moveY: -0.5001, lookX: 0.2501, lookY: -0.2501 });
    setExternalControllerMovement({ moveX: 0.5002, moveY: -0.5002, lookX: 0.2502, lookY: -0.2502 });
    expect(updates).toEqual([{ moveX: 0.5, moveY: -0.5, lookX: 0.25, lookY: -0.25 }]);
    expect(getControllerMovement()).toEqual({ moveX: 0.5, moveY: -0.5, lookX: 0.25, lookY: -0.25 });

    unsubscribe();
  });
});
