ce3babcc33
Closes HIF-3 Stage 1 完整闭环 PR C:用户 API + 后台 CRUD + 抽奖事务服务 + 审计 + QA curl。合并后 Stage 1 可交测试。 架构师 review R1(rulecaps depth≤8/nodes≤64/bytes≤8KB)+ R2(InviteHook source_ref 加 order: 前缀)已全部落地。 - 迁移 02158_admin_action_log:后台写操作审计 - xerr 100xxx 段:抽奖错误码(NotEligible/NoChances/ActivityEnded/RateLimited/NotClaimable/InternalError/RuleTooDeep/RuleTooMany/RuleTooLarge) - feature flag config.Lottery.Enable 默认 false,合并后线上零副作用 - draw service:feature flag → rate limit → pre-tx reads → nonce dedupe → Consume → Pick → 乐观扣库存 → 双快照 → Dispatch → finalize - 用户 API 4 个:GET /config、POST /draw、GET /records、POST /claim(Stage 1 返回 100010) - 后台 CRUD:活动 / 奖品 / rules PUT(rulecaps gate)/ chances/grant - audit.WriteAdminAction:与调用方 tx 同生共死,SHA1 body 摘要 - QA 脚本:qa/lottery/stage1_curl.sh 全链路 curl 测试覆盖:model 76.5% / draw 69% / handler 68.3% / hook 91.9% / rulecaps 87.8% / audit 100% 100 并发抢库存 + 10000 次概率分布 e2e 推 QA 环境(sqlmock 无法忠实模拟 InnoDB 行锁) CI 全绿:构建/Vet/测试 + golangci-lint
128 lines
3.7 KiB
Go
128 lines
3.7 KiB
Go
package rulecaps
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/perfect-panel/server/internal/model/lottery"
|
|
)
|
|
|
|
func TestValidateEligibilityJSON_AcceptsEmpty(t *testing.T) {
|
|
cases := [][]byte{
|
|
nil,
|
|
[]byte(""),
|
|
[]byte("{}"),
|
|
[]byte("null"),
|
|
[]byte(" \n \t "),
|
|
}
|
|
for _, c := range cases {
|
|
if err := ValidateEligibilityJSON(c); err != nil {
|
|
t.Fatalf("expected accept for %q, got %v", string(c), err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestValidateEligibilityJSON_RejectsOversizePayload(t *testing.T) {
|
|
blob := make([]byte, MaxBytes+1)
|
|
for i := range blob {
|
|
blob[i] = 'a'
|
|
}
|
|
err := ValidateEligibilityJSON(blob)
|
|
if !errors.Is(err, ErrRuleTreeTooLarge) {
|
|
t.Fatalf("expected ErrRuleTreeTooLarge, got %v", err)
|
|
}
|
|
// user-facing message should name the limit
|
|
if !strings.Contains(err.Error(), "8192") {
|
|
t.Fatalf("expected message to mention 8192 byte limit, got %q", err.Error())
|
|
}
|
|
}
|
|
|
|
func TestValidateEligibilityJSON_RejectsBadJSON(t *testing.T) {
|
|
err := ValidateEligibilityJSON([]byte(`{bad`))
|
|
if err == nil {
|
|
t.Fatalf("expected error on invalid JSON")
|
|
}
|
|
}
|
|
|
|
// buildDeepTree 构造 depth 层单链嵌套(每层一个 OR 聚合)。root 为第 1 层。
|
|
func buildDeepTree(depth int) *lottery.EligibilityRule {
|
|
root := &lottery.EligibilityRule{Op: "OR", Children: []*lottery.EligibilityRule{{Type: "has_subscription"}}}
|
|
current := root
|
|
for i := 2; i < depth; i++ {
|
|
next := &lottery.EligibilityRule{Op: "OR", Children: []*lottery.EligibilityRule{{Type: "has_subscription"}}}
|
|
current.Children = []*lottery.EligibilityRule{next}
|
|
current = next
|
|
}
|
|
return root
|
|
}
|
|
|
|
func TestValidateEligibilityJSON_RejectsDepthOverLimit(t *testing.T) {
|
|
tree := buildDeepTree(MaxDepth + 1) // depth 9 with defaults
|
|
raw, err := json.Marshal(tree)
|
|
if err != nil {
|
|
t.Fatalf("marshal: %v", err)
|
|
}
|
|
err = ValidateEligibilityJSON(raw)
|
|
if !errors.Is(err, ErrRuleTreeTooDeep) {
|
|
t.Fatalf("expected ErrRuleTreeTooDeep, got %v", err)
|
|
}
|
|
}
|
|
|
|
func TestValidateEligibilityJSON_AcceptsMaxDepth(t *testing.T) {
|
|
tree := buildDeepTree(MaxDepth)
|
|
raw, _ := json.Marshal(tree)
|
|
if err := ValidateEligibilityJSON(raw); err != nil {
|
|
t.Fatalf("depth=MaxDepth must be accepted, got %v", err)
|
|
}
|
|
}
|
|
|
|
// buildWideTree 构造根节点 + N 个叶子,总节点数 = 1 + N。
|
|
func buildWideTree(leaves int) *lottery.EligibilityRule {
|
|
root := &lottery.EligibilityRule{Op: "AND"}
|
|
for i := 0; i < leaves; i++ {
|
|
root.Children = append(root.Children, &lottery.EligibilityRule{Type: "has_subscription"})
|
|
}
|
|
return root
|
|
}
|
|
|
|
func TestValidateEligibilityJSON_RejectsNodeCountOverLimit(t *testing.T) {
|
|
// 65 nodes total = 1 root + 64 leaves > MaxNodes
|
|
tree := buildWideTree(MaxNodes)
|
|
raw, _ := json.Marshal(tree)
|
|
err := ValidateEligibilityJSON(raw)
|
|
if !errors.Is(err, ErrRuleTreeTooManyNodes) {
|
|
t.Fatalf("expected ErrRuleTreeTooManyNodes, got %v", err)
|
|
}
|
|
}
|
|
|
|
func TestValidateEligibilityJSON_AcceptsAtNodeLimit(t *testing.T) {
|
|
// 64 nodes = 1 root + 63 leaves
|
|
tree := buildWideTree(MaxNodes - 1)
|
|
raw, _ := json.Marshal(tree)
|
|
if err := ValidateEligibilityJSON(raw); err != nil {
|
|
t.Fatalf("nodes=MaxNodes must be accepted, got %v", err)
|
|
}
|
|
}
|
|
|
|
func TestValidateEligibilityJSON_AcceptsRealisticTree(t *testing.T) {
|
|
// Typical activity: (has_subscription AND invite_count>=3) OR user_tag in {vip}
|
|
raw := []byte(`{
|
|
"op": "OR",
|
|
"children": [
|
|
{
|
|
"op": "AND",
|
|
"children": [
|
|
{"type": "has_subscription", "params": {"min_days_remaining": 7}},
|
|
{"type": "invite_count", "params": {"min": 3}}
|
|
]
|
|
},
|
|
{"type": "user_tag", "params": {"tags": ["vip"]}}
|
|
]
|
|
}`)
|
|
if err := ValidateEligibilityJSON(raw); err != nil {
|
|
t.Fatalf("realistic tree should be accepted, got %v", err)
|
|
}
|
|
}
|