import { describe, it, expect } from "vitest"
import { toSettingItem } from "./system-settings-utils"

describe("toSettingItem", () => {
  describe("string 类型", () => {
    it("将普通字符串转换为设置项", () => {
      const item = toSettingItem("school_info", "schoolName", "实验中学", "string")
      expect(item).toEqual({
        category: "school_info",
        key: "schoolName",
        value: "实验中学",
        valueType: "string",
      })
    })

    it("将 null 转换为空字符串", () => {
      const item = toSettingItem("school_info", "schoolCode", null, "string")
      expect(item.value).toBe("")
    })

    it("将 undefined 转换为空字符串", () => {
      const item = toSettingItem("school_info", "schoolCode", undefined, "string")
      expect(item.value).toBe("")
    })

    it("将数字转换为字符串", () => {
      const item = toSettingItem("school_info", "schoolCode", 12345, "string")
      expect(item.value).toBe("12345")
    })
  })

  describe("number 类型", () => {
    it("将数字转换为字符串", () => {
      const item = toSettingItem("security_policy", "passwordMinLength", 8, "number")
      expect(item.value).toBe("8")
    })

    it("将大数字转换为字符串", () => {
      const item = toSettingItem("security_policy", "sessionTimeout", 1440, "number")
      expect(item.value).toBe("1440")
    })

    it("将零转换为字符串", () => {
      const item = toSettingItem("security_policy", "sessionTimeout", 0, "number")
      expect(item.value).toBe("0")
    })
  })

  describe("boolean 类型", () => {
    it("将 true 转换为 'true'", () => {
      const item = toSettingItem("security_policy", "requireSpecialChar", true, "boolean")
      expect(item.value).toBe("true")
    })

    it("将 false 转换为 'false'", () => {
      const item = toSettingItem("security_policy", "requireUppercase", false, "boolean")
      expect(item.value).toBe("false")
    })

    it("将 truthy 值转换为 'true'", () => {
      const item = toSettingItem("security_policy", "forcePasswordChange", 1, "boolean")
      expect(item.value).toBe("true")
    })

    it("将 falsy 值转换为 'false'", () => {
      const item = toSettingItem("security_policy", "forcePasswordChange", 0, "boolean")
      expect(item.value).toBe("false")
    })
  })

  describe("json 类型", () => {
    it("将对象序列化为 JSON 字符串", () => {
      const item = toSettingItem("file_upload", "allowedTypes", ["jpg", "png"], "json")
      expect(item.value).toBe(JSON.stringify(["jpg", "png"]))
    })

    it("将嵌套对象序列化", () => {
      const data = { types: ["jpg", "png"], maxSize: 10 }
      const item = toSettingItem("file_upload", "config", data, "json")
      expect(item.value).toBe(JSON.stringify(data))
    })

    it("将 null 序列化为 'null'", () => {
      const item = toSettingItem("file_upload", "config", null, "json")
      expect(item.value).toBe("null")
    })

    it("将数组序列化", () => {
      const item = toSettingItem("notification_config", "channels", ["email", "sms"], "json")
      expect(item.value).toBe(JSON.stringify(["email", "sms"]))
    })
  })

  describe("返回结构", () => {
    it("返回包含所有字段的设置项", () => {
      const item = toSettingItem("school_info", "name", "测试", "string")
      expect(item).toHaveProperty("category")
      expect(item).toHaveProperty("key")
      expect(item).toHaveProperty("value")
      expect(item).toHaveProperty("valueType")
    })

    it("保留传入的 category 和 key", () => {
      const item = toSettingItem("notification_config", "notifyNewUser", true, "boolean")
      expect(item.category).toBe("notification_config")
      expect(item.key).toBe("notifyNewUser")
    })

    it("保留传入的 valueType", () => {
      const item = toSettingItem("security_policy", "min", 8, "number")
      expect(item.valueType).toBe("number")
    })
  })

  describe("边界情况", () => {
    it("处理空字符串", () => {
      const item = toSettingItem("school_info", "name", "", "string")
      expect(item.value).toBe("")
    })

    it("处理负数", () => {
      const item = toSettingItem("security_policy", "timeout", -1, "number")
      expect(item.value).toBe("-1")
    })

    it("处理浮点数", () => {
      const item = toSettingItem("file_upload", "maxSize", 10.5, "number")
      expect(item.value).toBe("10.5")
    })

    it("处理空数组 JSON", () => {
      const item = toSettingItem("file_upload", "types", [], "json")
      expect(item.value).toBe("[]")
    })

    it("处理空对象 JSON", () => {
      const item = toSettingItem("file_upload", "config", {}, "json")
      expect(item.value).toBe("{}")
    })
  })
})
