import { describe, expect, it } from "vitest";
import type { MdtodoTaskRecord } from "@/api";
import {
  taskParentRefs,
  treeKeyAction,
  visibleTasks,
} from "./mdtodoTaskTreeModel";

const tasks: MdtodoTaskRecord[] = [
  { taskRef: "root", taskId: "R1", depth: 0 },
  { taskRef: "child", taskId: "R1.1", parentTaskRef: "root", depth: 1 },
  { taskRef: "deep", taskId: "R1.1.1", parentTaskRef: "child", depth: 2 },
  { taskRef: "sibling", taskId: "R1.2", parentTaskRef: "root", depth: 1 },
];

describe("MDTODO task tree model", () => {
  it("uses parentTaskRef as the authority chain", () => {
    expect(taskParentRefs(tasks[2]!, tasks)).toEqual(["root", "child"]);
  });

  it("keeps only the selected deep-link path visible through a collapsed parent", () => {
    expect(
      visibleTasks(tasks, new Set(["root"]), "deep").map(
        (task) => task.taskRef,
      ),
    ).toEqual(["root", "child", "deep"]);
    expect(
      visibleTasks(tasks, new Set(["root"]), null).map((task) => task.taskRef),
    ).toEqual(["root"]);
  });

  it("maps directional keys to tree actions", () => {
    expect(treeKeyAction("ArrowDown", true, true)).toBe("next");
    expect(treeKeyAction("ArrowUp", true, true)).toBe("previous");
    expect(treeKeyAction("ArrowRight", false, true)).toBe("expand");
    expect(treeKeyAction("ArrowLeft", true, true)).toBe("collapse");
    expect(treeKeyAction("ArrowLeft", false, false)).toBe("parent");
  });
});
