import { describe, expect, it } from "vitest";
import { dictionaryLabel, dictionaryOptions } from "../dictionaryLabels";
import { isUiDictionaries } from "../dictionaryTypes";
import { dictionaryFixture } from "./fixtures";

describe("ui dictionary DTO and labels", () => {
  it("严格接受正式结构并显示后端中文标签", () => {
    const fixture = dictionaryFixture();
    expect(isUiDictionaries(fixture)).toBe(true);
    expect(dictionaryLabel(fixture, "securityLevels", "INTERNAL")).toBe("内部");
    expect(dictionaryLabel(fixture, "visibilityTypes", "ALL_AUTHENTICATED"))
      .toBe("全体已登录用户");
    expect(dictionaryLabel(fixture, "documentStatuses", "PUBLISHED")).toBe("已发布");
  });

  it("拒绝未知字段、错误类型和错误locale", () => {
    const fixture = dictionaryFixture();
    expect(isUiDictionaries({ ...fixture, extra: true })).toBe(false);
    expect(isUiDictionaries({ ...fixture, locale: "en-US" })).toBe(false);
    expect(isUiDictionaries({
      ...fixture,
      dictionaries: {
        ...fixture.dictionaries,
        securityLevels: [{ code: "INTERNAL", label: "内部", sortNo: "1" }],
      },
    })).toBe(false);
  });

  it("未知code不回显英文且选项按sortNo和code稳定排序", () => {
    const fixture = dictionaryFixture();
    fixture.dictionaries.securityLevels = [
      { code: "SECRET", label: "秘密", sortNo: 20 },
      { code: "PUBLIC", label: "公开", sortNo: 10 },
      { code: "INTERNAL", label: "内部", sortNo: 10 },
    ];
    expect(isUiDictionaries(fixture)).toBe(true);
    expect(dictionaryLabel(fixture, "securityLevels", "UNKNOWN")).toBe("未定义标签");
    expect(dictionaryOptions(fixture, "securityLevels").map((item) => item.code))
      .toEqual(["INTERNAL", "PUBLIC", "SECRET"]);
  });
});
