import { describe, expect, it } from "vitest";
import {
  buildPageTreeProjectionItems,
  buildPickerTreeItems,
  filterVisiblePageTreeProjectionItems,
} from "./tree-projection";

describe("buildPageTreeProjectionItems", () => {
  it("按当前树结构重建 parent/depth，而不是信任陈旧节点字段", () => {
    const rows = buildPageTreeProjectionItems([
      {
        access_scope: "private",
        id: "root",
        workspace_id: "w",
        title: "Root",
        parent_id: null,
        sort_order: 0,
        is_starred: false,
        is_template: false,
        created_at: "",
        updated_at: null,
        kernel: {
          nodeType: "page",
          depth: 0,
          position: 0,
          childCount: 1,
          expandedByDefault: true,
        },
        children: [
          {
            access_scope: "private",
            id: "child",
            workspace_id: "w",
            title: "Child",
            parent_id: null,
            sort_order: 9,
            is_starred: false,
            is_template: false,
            created_at: "",
            updated_at: null,
            kernel: {
              nodeType: "page",
              depth: 0,
              position: 9,
              childCount: 0,
              expandedByDefault: true,
            },
            children: [],
          },
        ],
      },
    ]);

    expect(rows.map((item) => ({
      nodeId: item.nodeId,
      parentNodeId: item.parentNodeId,
      depth: item.depth,
    }))).toEqual([
      { nodeId: "root", parentNodeId: null, depth: 0 },
      { nodeId: "child", parentNodeId: "root", depth: 1 },
    ]);
    expect(rows[1]?.node.parent_id).toBe("root");
    expect(rows[1]?.node.kernel?.depth).toBe(1);
  });
});

describe("filterVisiblePageTreeProjectionItems", () => {
  it("只返回当前展开路径上的可见页面", () => {
    const items = buildPageTreeProjectionItems([
      {
        access_scope: "private",
        id: "root",
        workspace_id: "w",
        title: "Root",
        parent_id: null,
        sort_order: 0,
        is_starred: false,
        is_template: false,
        created_at: "",
        updated_at: null,
        children: [
          {
            access_scope: "private",
            id: "child",
            workspace_id: "w",
            title: "Child",
            parent_id: "root",
            sort_order: 0,
            is_starred: false,
            is_template: false,
            created_at: "",
            updated_at: null,
            children: [
              {
                access_scope: "private",
                id: "leaf",
                workspace_id: "w",
                title: "Leaf",
                parent_id: "child",
                sort_order: 0,
                is_starred: false,
                is_template: false,
                created_at: "",
                updated_at: null,
                children: [],
              },
            ],
          },
        ],
      },
    ]);

    expect(filterVisiblePageTreeProjectionItems(items, new Set()).map((item) => item.nodeId)).toEqual(["root"]);
    expect(filterVisiblePageTreeProjectionItems(items, new Set(["root"])).map((item) => item.nodeId)).toEqual([
      "root",
      "child",
    ]);
    expect(
      filterVisiblePageTreeProjectionItems(items, new Set(["root", "child"])).map(
        (item) => item.nodeId,
      ),
    ).toEqual(["root", "child", "leaf"]);
  });
});

describe("buildPickerTreeItems", () => {
  it("复用 page_tree projection 生成 picker 列表", () => {
    const items = buildPageTreeProjectionItems([
      {
        access_scope: "private",
        id: "a",
        workspace_id: "w",
        title: "A",
        parent_id: null,
        sort_order: 0,
        is_starred: false,
        is_template: false,
        created_at: "",
        updated_at: null,
        children: [
          {
            access_scope: "private",
            id: "b",
            workspace_id: "w",
            title: null,
            parent_id: "a",
            sort_order: 0,
            is_starred: false,
            is_template: false,
            created_at: "",
            updated_at: null,
            children: [],
          },
        ],
      },
    ]);

    expect(buildPickerTreeItems(items, ["a"])).toEqual([
      { id: "b", title: "无标题", depth: 1 },
    ]);
  });
});
