import { describe, expect, it } from "vitest";
import {
  getMindmapActionMapping,
  listMindmapActionMappings,
  mapMindmapActionToCommand,
} from "./mindmap-action-map";

describe("mindmap action map", () => {
  it("把核心节点动作映射到 runtime command 和 kernel command", () => {
    expect(mapMindmapActionToCommand({ actionId: "insertChild", mindmapId: "mind_1", activeNodeId: "node_1" })).toEqual({
      runtimeCommand: "INSERT_CHILD_NODE",
      command: {
        type: "insertChild",
        mindmapId: "mind_1",
        parentNodeId: "node_1",
        node: { text: "新节点" },
      },
    });
    expect(
      mapMindmapActionToCommand({ actionId: "insertSiblingAfter", mindmapId: "mind_1", activeNodeId: "node_1" }),
    ).toEqual({
      runtimeCommand: "INSERT_NODE",
      command: {
        type: "insertSiblingAfter",
        mindmapId: "mind_1",
        targetNodeId: "node_1",
        node: { text: "新节点" },
      },
    });
    expect(mapMindmapActionToCommand({ actionId: "deleteNode", mindmapId: "mind_1", activeNodeId: "node_1" })).toEqual({
      runtimeCommand: "REMOVE_NODE",
      command: { type: "deleteNode", mindmapId: "mind_1", nodeId: "node_1" },
    });
  });

  it("把视图动作映射到 localView，不依赖 active node", () => {
    expect(getMindmapActionMapping("centerRoot")).toMatchObject({ target: "localView", runtimeMethod: "centerRoot" });
    expect(getMindmapActionMapping("zoomIn")).toMatchObject({ target: "localView", runtimeMethod: "zoomIn" });
    expect(getMindmapActionMapping("zoomOut")).toMatchObject({ target: "localView", runtimeMethod: "zoomOut" });
    expect(getMindmapActionMapping("fitView")).toMatchObject({ target: "localView", runtimeMethod: "fitView" });
    expect(getMindmapActionMapping("fullscreenCanvas")).toMatchObject({ target: "localView", requiresActiveNode: false });
    expect(getMindmapActionMapping("fullscreenPage")).toMatchObject({ target: "localView", requiresActiveNode: false });
    expect(getMindmapActionMapping("exitFullscreen")).toMatchObject({ target: "localView", requiresActiveNode: false });
    expect(getMindmapActionMapping("showMenu")).toMatchObject({ target: "localView", requiresActiveNode: false });
  });

  it("全屏动作不产生 kernel command，readonly 下仍可使用", () => {
    expect(mapMindmapActionToCommand({ actionId: "fullscreenCanvas", mindmapId: "mind_1" })).toEqual({});
    expect(mapMindmapActionToCommand({ actionId: "fullscreenPage", mindmapId: "mind_1" })).toEqual({});
    expect(mapMindmapActionToCommand({ actionId: "exitFullscreen", mindmapId: "mind_1" })).toEqual({});
    expect(mapMindmapActionToCommand({ actionId: "showMenu", mindmapId: "mind_1" })).toEqual({});
    expect(getMindmapActionMapping("fullscreenCanvas")).toMatchObject({ readonlyAllowed: true });
    expect(getMindmapActionMapping("fullscreenPage")).toMatchObject({ readonlyAllowed: true });
    expect(getMindmapActionMapping("exitFullscreen")).toMatchObject({ readonlyAllowed: true });
    expect(getMindmapActionMapping("showMenu")).toMatchObject({ readonlyAllowed: true });
  });

  it("导出能力暂缓时不下发未受控 runtime command", () => {
    expect(getMindmapActionMapping("export")).toMatchObject({ target: "localView", readonlyAllowed: true });
    expect(getMindmapActionMapping("export")?.runtimeCommand).toBeUndefined();
    expect(mapMindmapActionToCommand({ actionId: "export", mindmapId: "mind_1" })).toEqual({});
  });

  it("把主题、结构和扩展字段映射到 kernel/compat", () => {
    expect(mapMindmapActionToCommand({ actionId: "setTheme", mindmapId: "mind_1", value: "classic4" })).toEqual({
      command: { type: "setTheme", mindmapId: "mind_1", theme: "classic4" },
    });
    expect(mapMindmapActionToCommand({ actionId: "setLayout", mindmapId: "mind_1", value: "logicalStructure" })).toEqual({
      command: { type: "setLayout", mindmapId: "mind_1", layout: "logicalStructure" },
    });
    expect(mapMindmapActionToCommand({ actionId: "note", mindmapId: "mind_1", activeNodeId: "node_1", value: "备注" })).toEqual({
      command: {
        type: "compatPayloadPatch",
        mindmapId: "mind_1",
        path: "nodes.node_1.data.note",
        value: "备注",
        source: "toolbar",
        actionId: "note",
      },
    });
    expect(
      mapMindmapActionToCommand({
        actionId: "painter",
        mindmapId: "mind_1",
        activeNodeId: "node_1",
        value: { fillColor: "#dbeafe" },
        source: "sidebar",
      }),
    ).toEqual({
      command: {
        type: "compatPayloadPatch",
        mindmapId: "mind_1",
        path: "nodes.node_1.data.style",
        value: { fillColor: "#dbeafe" },
        source: "sidebar",
        actionId: "painter",
      },
    });
  });

  it("所有 action id 映射唯一", () => {
    const mappings = listMindmapActionMappings();
    const ids = mappings.map((mapping) => mapping.actionId);
    expect(new Set(ids).size).toBe(ids.length);
  });
});
