import { describe, expect, it, vi } from "vitest";
import { attachDocumentMouseDrag } from "./timelineDocumentDrag.js";

describe("timelineDocumentDrag", () => {
  it("registers move and up listeners and removes both during cleanup", () => {
    const addEventListener = vi.fn();
    const removeEventListener = vi.fn();
    const onMove = vi.fn();
    const onUp = vi.fn();

    const cleanup = attachDocumentMouseDrag({ addEventListener, removeEventListener }, onMove, onUp);

    expect(addEventListener).toHaveBeenCalledTimes(2);
    expect(addEventListener).toHaveBeenNthCalledWith(1, "mousemove", onMove);
    expect(addEventListener).toHaveBeenNthCalledWith(2, "mouseup", onUp);

    cleanup();

    expect(removeEventListener).toHaveBeenCalledTimes(2);
    expect(removeEventListener).toHaveBeenNthCalledWith(1, "mousemove", onMove);
    expect(removeEventListener).toHaveBeenNthCalledWith(2, "mouseup", onUp);
  });

  it("keeps cleanup idempotent from the helper perspective", () => {
    const removeEventListener = vi.fn();

    const cleanup = attachDocumentMouseDrag(
      {
        addEventListener: vi.fn(),
        removeEventListener,
      },
      vi.fn(),
      vi.fn(),
    );

    cleanup();
    cleanup();

    expect(removeEventListener).toHaveBeenCalledTimes(4);
  });
});
