sdk-ci / test (push) Failing after 8s
- 自主仓 git.wxccs.org/EntitleForge/EntitleForge 拆分(协议实现单一归属: license/crypto/ulid 随 SDK 迁出,服务端反向依赖本仓) - sdk:Client 三模式(文件/远程/代理)、GRACE 状态机、查询 API(6.6ns/0 alloc) - license:三段式编解码与严格解析;crypto:Ed25519 信任链 + Proxy 受约束证书 - 零第三方依赖(CI 有依赖闭包断言红线);go.mod 零 require - CI:Gitea Actions(test + 零依赖断言 + 三平台交叉编译冒烟)
97 lines
2.5 KiB
Go
97 lines
2.5 KiB
Go
package license
|
|
|
|
import (
|
|
"encoding/json"
|
|
"testing"
|
|
)
|
|
|
|
func TestEntitlement_JSONRoundTrip(t *testing.T) {
|
|
cases := []struct {
|
|
name string
|
|
ent Entitlement
|
|
want string
|
|
}{
|
|
{"boolean", Bool(true), `{"t":"b","v":true}`},
|
|
{"boolean false", Bool(false), `{"t":"b","v":false}`},
|
|
{"quantity", Quantity(1000), `{"t":"q","v":1000}`},
|
|
{"quantity 大数无科学计数法", Quantity(9007199254740993), `{"t":"q","v":9007199254740993}`},
|
|
{"tier", TierValue("enterprise"), `{"t":"t","v":"enterprise"}`},
|
|
{"constraint", Constraint([]string{"cn-north", "cn-east"}), `{"t":"c","v":["cn-north","cn-east"]}`},
|
|
}
|
|
for _, tc := range cases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
b, err := json.Marshal(tc.ent)
|
|
if err != nil {
|
|
t.Fatalf("marshal: %v", err)
|
|
}
|
|
if string(b) != tc.want {
|
|
t.Errorf("marshal\ngot %s\nwant %s", b, tc.want)
|
|
}
|
|
var got Entitlement
|
|
if err := json.Unmarshal(b, &got); err != nil {
|
|
t.Fatalf("unmarshal: %v", err)
|
|
}
|
|
if !got.Equal(tc.ent) {
|
|
t.Errorf("往返不一致: got %+v want %+v", got, tc.ent)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestEntitlement_RejectsBadValues(t *testing.T) {
|
|
cases := []struct {
|
|
name string
|
|
raw string
|
|
}{
|
|
{"未知类型", `{"t":"x","v":1}`},
|
|
{"缺 t", `{"v":1}`},
|
|
{"缺 v", `{"t":"b"}`},
|
|
{"v 为 null", `{"t":"b","v":null}`},
|
|
{"bool 配数字", `{"t":"b","v":1}`},
|
|
{"quantity 配字符串", `{"t":"q","v":"50"}`},
|
|
{"quantity 配小数", `{"t":"q","v":1.5}`},
|
|
{"quantity 配布尔", `{"t":"q","v":true}`},
|
|
{"tier 配数字", `{"t":"t","v":3}`},
|
|
{"constraint 配字符串", `{"t":"c","v":"cn-north"}`},
|
|
{"constraint 元素非字符串", `{"t":"c","v":["a",1]}`},
|
|
{"整体非对象", `"b"`},
|
|
}
|
|
for _, tc := range cases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
var e Entitlement
|
|
if err := json.Unmarshal([]byte(tc.raw), &e); err == nil {
|
|
t.Errorf("应报错, got %+v", e)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestEntitlement_Accessors(t *testing.T) {
|
|
if !Bool(true).AsBool() {
|
|
t.Error("AsBool")
|
|
}
|
|
if Quantity(42).AsInt() != 42 {
|
|
t.Error("AsInt")
|
|
}
|
|
if TierValue("pro").AsString() != "pro" {
|
|
t.Error("AsString")
|
|
}
|
|
got := Constraint([]string{"a", "b"}).AsList()
|
|
if len(got) != 2 || got[0] != "a" || got[1] != "b" {
|
|
t.Errorf("AsList: %v", got)
|
|
}
|
|
// 类型不匹配时返回零值
|
|
if Quantity(42).AsBool() {
|
|
t.Error("quantity AsBool 应为 false")
|
|
}
|
|
if Bool(true).AsInt() != 0 {
|
|
t.Error("bool AsInt 应为 0")
|
|
}
|
|
if Bool(true).AsString() != "" {
|
|
t.Error("bool AsString 应为空")
|
|
}
|
|
if Bool(true).AsList() != nil {
|
|
t.Error("bool AsList 应为 nil")
|
|
}
|
|
}
|