package domain import ( "encoding/json" "strings" "testing" ) // 判别联合的核心契约:每种 kind 的 payload 都能校验、能反序列化。 // 这些测试的作用是「加新 kind 时忘了写 case 会在 CI 就挂」, // 而不是等线上存储了脏数据才发现读不出来。 func TestSectionValidate_AllKinds(t *testing.T) { cases := []struct { name string section Section wantErr string // 空表示应通过 }{ { name: "text 正常", section: mkSection(KindText, `{"plain_text":"你好世界"}`), }, { name: "text 空 payload", section: Section{Kind: KindText, Payload: json.RawMessage(`{}`)}, }, { name: "subtitle 空文本被拒", section: mkSection(KindSubtitle, `{"text":""}`), wantErr: "小标题内容不能为空", }, { name: "subtitle 正常", section: mkSection(KindSubtitle, `{"text":"第一节"}`), }, { name: "image 无图被拒", section: mkSection(KindImage, `{"images":[]}`), wantErr: "至少需要一张图", }, { name: "image 缺 url 被拒", section: mkSection(KindImage, `{"images":[{"caption":"无地址"}]}`), wantErr: "缺少 url", }, { name: "image 正常", section: mkSection(KindImage, `{"images":[{"url":"https://x/a.jpg","width":800,"height":600}],"layout":"single"}`), }, { name: "video 缺 url 被拒", section: mkSection(KindVideo, `{"video_url":""}`), wantErr: "视频 url 不能为空", }, { name: "audio 缺 url 被拒", section: mkSection(KindAudio, `{"audio_url":""}`), wantErr: "音频 url 不能为空", }, { name: "vote 单选项被拒", section: mkSection(KindVote, `{"question":"选一个","options":[{"id":"1","text":"A"}]}`), wantErr: "至少需要 2 个选项", }, { name: "vote 正常", section: mkSection(KindVote, `{"question":"选一个","options":[{"id":"1","text":"A"},{"id":"2","text":"B"}]}`), }, { name: "vote 50 个以上选项被拒", section: mkSection(KindVote, voteWithOptions(51)), wantErr: "最多 50 个选项", }, { name: "location 空名称被拒", section: mkSection(KindLocation, `{"name":""}`), wantErr: "地点名称不能为空", }, { name: "goods 空标题被拒", section: mkSection(KindGoods, `{"title":""}`), wantErr: "商品标题不能为空", }, { name: "未知 kind 被拒", section: mkSection(Kind("brand_new"), `{}`), wantErr: "未知区块类型", }, { name: "payload 非对象被拒", section: Section{Kind: KindText, Payload: json.RawMessage(`[1,2,3]`)}, wantErr: "不是合法 JSON 对象", }, } for _, tc := range cases { t.Run(tc.name, func(t *testing.T) { err := tc.section.Validate() if tc.wantErr == "" { if err != nil { t.Fatalf("期望通过,却报错: %v", err) } // 通过校验的必须能反序列化 —— 这是「存得进就读得出」的保证 if _, derr := tc.section.Decoded(); derr != nil { t.Fatalf("校验通过但反序列化失败: %v", derr) } return } if err == nil { t.Fatalf("期望报错 %q,实际通过", tc.wantErr) } if !strings.Contains(err.Error(), tc.wantErr) { t.Fatalf("错误信息不符\n期望包含: %s\n实际: %s", tc.wantErr, err.Error()) } }) } } func TestKindValid(t *testing.T) { for _, k := range AllKinds { if !k.Valid() { t.Errorf("%s 应为合法 kind", k) } } for _, k := range []Kind{"", "unknown", "Text", "TEXT"} { if k.Valid() { t.Errorf("%q 不应为合法 kind", k) } } } // AllKinds 与实际 Decoded 分支必须一一对应。 // 这条测试拦住「加了常量但忘了写反序列化分支」的常见遗漏。 func TestAllKindsHaveDecoder(t *testing.T) { samples := map[Kind]string{ KindText: `{"plain_text":"a"}`, KindSubtitle: `{"text":"a"}`, KindImage: `{"images":[{"url":"u"}]}`, KindVideo: `{"video_url":"u"}`, KindAudio: `{"audio_url":"u"}`, KindVote: `{"question":"q","options":[{"id":"1","text":"A"},{"id":"2","text":"B"}]}`, KindLocation: `{"name":"n"}`, KindGoods: `{"title":"t"}`, } for _, k := range AllKinds { payload, ok := samples[k] if !ok { t.Errorf("kind %s 缺少测试样本", k) continue } s := mkSection(k, payload) if _, err := s.Decoded(); err != nil { t.Errorf("kind %s 反序列化失败: %v", k, err) } } if len(AllKinds) != len(samples) { t.Errorf("AllKinds 有 %d 项,样本有 %d 项,可能漏了分支", len(AllKinds), len(samples)) } } func mkSection(k Kind, payload string) Section { return Section{Kind: k, Payload: json.RawMessage(payload)} } func voteWithOptions(n int) string { opts := make([]string, n) for i := range opts { opts[i] = `{"id":"` + string(rune('a'+i%26)) + `","text":"x"}` } return `{"question":"q","options":[` + strings.Join(opts, ",") + `]}` }