import { describe, expect, it } from "vitest";
import {
  isCategoryNode,
  isCategoryTree,
  type CategoryNode,
} from "../categoryTypes";

const child: CategoryNode = {
  id: "2",
  categoryCode: "STYLE_A1",
  categoryName: "XXXX样式A1",
  categoryType: "STYLE",
  parentId: "1",
  categoryPath: "/XXXX场景A/XXXX样式A1",
  sortNo: 10,
  documentCount: 0,
  rowVersion: 0,
  children: [],
};

const root: CategoryNode = {
  id: "1",
  categoryCode: "SCENE_A",
  categoryName: "XXXX场景A",
  categoryType: "SCENE",
  parentId: null,
  categoryPath: "/XXXX场景A",
  sortNo: 10,
  documentCount: 0,
  rowVersion: 1,
  children: [child],
};

describe("CategoryNode contract", () => {
  it("递归接受完整camelCase分类树", () => {
    expect(isCategoryNode(root)).toBe(true);
    expect(isCategoryTree([root])).toBe(true);
  });

  it("拒绝根节点number ID", () => {
    expect(isCategoryNode({ ...root, id: 1 })).toBe(false);
  });

  it("拒绝递归子节点number ID", () => {
    expect(isCategoryNode({
      ...root,
      children: [{ ...child, id: 2 }],
    })).toBe(false);
  });

  it("拒绝未知CategoryType", () => {
    expect(isCategoryNode({ ...root, categoryType: "UNKNOWN" })).toBe(false);
  });

  it("拒绝snake_case字段替代契约字段", () => {
    const { categoryName: _, ...rest } = root;
    expect(isCategoryNode({ ...rest, category_name: "错误字段" })).toBe(false);
  });
});

