import { describe, expect, it } from "vitest";
import {
  SIMPLE_MIND_MAP_BRIDGE_EVENTS,
  SIMPLE_MIND_MAP_REQUIRED_PLUGINS,
  buildSimpleMindMapOptions,
  createFallbackMindmapAdapterProjection,
  createSafeMindmapCommandExecutor,
  defaultSimpleMindMapPluginNames,
} from "./simple-mind-map-bridge";

describe("simple-mind-map bridge contract", () => {
  it("登记 Phase 6 第一阶段必需插件和事件", () => {
    expect(SIMPLE_MIND_MAP_REQUIRED_PLUGINS).toEqual([
      "Drag",
      "KeyboardNavigation",
      "Export",
      "Select",
      "AssociativeLine",
      "Search",
      "OuterFrame",
    ]);
    expect(defaultSimpleMindMapPluginNames()).toEqual(
      expect.arrayContaining(["Scrollbar", "MiniMap", "Painter", "Formula"]),
    );
    expect(defaultSimpleMindMapPluginNames()).not.toContain("RichText");
    expect(SIMPLE_MIND_MAP_BRIDGE_EVENTS).toEqual([
      "data_change",
      "view_data_change",
      "node_active",
      "back_forward",
      "scale",
      "translate",
    ]);
  });

  it("从 adapter projection 构建 simple-mind-map 初始化参数", () => {
    const projection = createFallbackMindmapAdapterProjection({
      mindmapId: "mind_1",
      root: { data: { text: "KMIND" }, children: [] },
      kernelRevision: 7,
    });
    const options = buildSimpleMindMapOptions({
      el: {} as HTMLElement,
      projection: {
        ...projection,
        theme: { template: "classic4" },
        themeConfig: { root: { fillColor: "#c62828" } },
        view: { state: { scale: 0.8, x: 1, y: 2 }, transform: { scaleX: 0.8, scaleY: 0.8 } },
      },
    });

    expect(options.data).toEqual({ data: { text: "KMIND" }, children: [] });
    expect(options.layout).toBe("logicalStructure");
    expect(options.theme).toBe("classic4");
    expect(options.themeConfig).toEqual({ root: { fillColor: "#c62828" } });
    expect(options.viewData).toEqual({ state: { scale: 0.8, x: 1, y: 2 }, transform: { scaleX: 0.8, scaleY: 0.8 } });
  });

  it("fallback projection 默认使用 KMind 风格主题与右向逻辑结构", () => {
    const projection = createFallbackMindmapAdapterProjection({
      mindmapId: "mind_1",
      root: { data: { text: "KMIND" }, children: [] },
    });

    expect(projection.layout).toBe("logicalStructure");
    expect(projection.theme).toBe("default");
    expect(projection.themeConfig).toMatchObject({
      lineStyle: "curve",
      root: {
        fillColor: "#e25563",
        color: "#ffffff",
        fontWeight: "bold",
      },
      second: {
        fillColor: "#4f7df3",
        color: "#ffffff",
      },
      node: {
        color: "#315aa9",
      },
      generalizationLineColor: "#ef6a5b",
    });
  });

  it("保留 runtimeOptions 中的 fit 设置，避免初始导图被裁切", () => {
    const projection = createFallbackMindmapAdapterProjection({
      mindmapId: "mind_1",
      root: { data: { text: "KMIND" }, children: [] },
      kernelRevision: 7,
    });

    expect(
      buildSimpleMindMapOptions({
        el: {} as HTMLElement,
        projection,
      }).fit,
    ).toBe(true);
    expect(
      buildSimpleMindMapOptions({
        el: {} as HTMLElement,
        projection,
        runtimeOptions: { fit: false },
      }).fit,
    ).toBe(false);
  });

  it("忽略无效 viewData，避免 simple-mind-map 读取 viewData.state 时崩溃", () => {
    const projection = createFallbackMindmapAdapterProjection({
      mindmapId: "mind_1",
      root: { data: { text: "KMIND" }, children: [] },
      kernelRevision: 7,
    });
    const options = buildSimpleMindMapOptions({
      el: {} as HTMLElement,
      projection: { ...projection, view: {} },
    });

    expect(options.viewData).toBeNull();
  });

  it("安全命令包装只允许受控 runtime command", () => {
    const calls: unknown[][] = [];
    const exec = createSafeMindmapCommandExecutor({
      execCommand: (...args) => calls.push(args),
    });

    expect(exec("INSERT_CHILD_NODE", "node_1")).toMatchObject({ ok: true });
    expect(exec("SAVE_FULL_BLOB", { data: {} })).toEqual({
      ok: false,
      command: "SAVE_FULL_BLOB",
      error: "unsupported_command",
    });
    expect(calls).toEqual([["INSERT_CHILD_NODE", "node_1"]]);
  });
});
