import { describe, expect, it } from "vitest";
import { buildParentById, isAncestorOf } from "../../../convex/_utils/documentTree";

describe("document tree helper", () => {
  it("构建父链映射时保留页面与父页面关系，供树命令 legality 复用", () => {
    const parentById = buildParentById([
      { id: "root", parent_id: null },
      { id: "child", parent_id: "root" },
      { id: "leaf", parent_id: "child" },
    ]);

    expect(parentById.get("root")).toBeNull();
    expect(parentById.get("child")).toBe("root");
    expect(parentById.get("leaf")).toBe("child");
  });

  it("祖先判断应能识别多级后代，避免把页面移动到自己的子树下面", () => {
    const parentById = buildParentById([
      { id: "root", parent_id: null },
      { id: "child", parent_id: "root" },
      { id: "leaf", parent_id: "child" },
    ]);

    expect(isAncestorOf("root", "leaf", parentById)).toBe(true);
    expect(isAncestorOf("child", "leaf", parentById)).toBe(true);
    expect(isAncestorOf("leaf", "root", parentById)).toBe(false);
    expect(isAncestorOf("missing", "leaf", parentById)).toBe(false);
  });
});
