/**
 * 命名规则(D14)单测。
 */
import { describe, expect, it } from "vitest";
import { FileLibError, nameKey, normalizeNodeName, NODE_NAME_MAX_LENGTH } from "../../src/database/filelib/model.js";

describe("normalizeNodeName · D14", () => {
  it("trim 空白", () => {
    expect(normalizeNodeName("  物理必修一  ")).toBe("物理必修一");
  });

  it("NFC 归一化(分解形式 → 合成形式)", () => {
    expect(normalizeNodeName("é")).toBe("é");
  });

  it("CJK 与常见字符原样通过", () => {
    expect(normalizeNodeName("TH-141 表面张力 (上)")).toBe("TH-141 表面张力 (上)");
  });

  it("空名/全空白 → invalid_name", () => {
    expect(() => normalizeNodeName("")).toThrowError(FileLibError);
    expect(() => normalizeNodeName("   ")).toThrowError(FileLibError);
  });

  it("超长 → invalid_name", () => {
    expect(() => normalizeNodeName("a".repeat(NODE_NAME_MAX_LENGTH + 1))).toThrowError(FileLibError);
    expect(normalizeNodeName("a".repeat(NODE_NAME_MAX_LENGTH))).toHaveLength(NODE_NAME_MAX_LENGTH);
  });

  it("拒绝斜杠与反斜杠", () => {
    expect(() => normalizeNodeName("a/b")).toThrowError(FileLibError);
    expect(() => normalizeNodeName("a\\b")).toThrowError(FileLibError);
  });

  it("拒绝控制字符", () => {
    expect(() => normalizeNodeName("a\nb")).toThrowError(FileLibError);
    expect(() => normalizeNodeName("a\tb")).toThrowError(FileLibError);
    expect(() => normalizeNodeName("a\0b")).toThrowError(FileLibError);
  });
});

describe("nameKey · 大小写不敏感比较键", () => {
  it("ASCII 折叠", () => {
    expect(nameKey("Physics")).toBe("physics");
  });
  it("CJK 不变", () => {
    expect(nameKey("物理")).toBe("物理");
  });
});
