Files
foundry-core/internal/platform/content_test.go
T

42 lines
2.0 KiB
Go

// 内容 schema 的纯函数回归,不访问数据库或远程图片。
package platform
import (
"encoding/json"
"testing"
)
// TestContentValidation 按素材种类检查合法草稿、重复块 ID、未知字段/版本、图片 URL 和互斥内容,并检查规范化输出仍为合法 JSON。
func TestContentValidation(t *testing.T) {
cases := []struct {
name, kind, body string
valid bool
}{
{"plain text", "text", `{"text":""}`, true},
{"empty article draft", "article", `{"article":{"sections":[]}}`, true},
{"paragraph descriptions", "article", `{"article":{"sections":[{"id":"s","title":"Section","description":"Summary","paragraphs":[{"id":"p","text":"Body","description":"Notes"}]}]}}`, true},
{"duplicate paragraph IDs", "article", `{"article":{"sections":[{"id":"s","paragraphs":[{"id":"p","text":"One"},{"id":"p","text":"Two"}]}]}}`, false},
{"markdown field", "article", `{"markdown":"# Heading"}`, false},
{"unknown schema", "text", `{"schema_version":2,"text":"Text"}`, false},
{"null content", "board", `null`, false},
{"nested unknown field", "image", `{"image":{"url":"https://example.com/image.png","secret":"unexpected"}}`, false},
{"insecure image", "image", `{"image":{"url":"http://example.com/image.png"}}`, false},
{"URL credentials", "image", `{"image":{"url":"https://user:password@example.com/image.png"}}`, false},
{"card and text", "card", `{"parts":[{"kind":"text","text":"Caption"}]}`, true},
{"empty card", "card", `{"parts":[]}`, false},
{"mismatched part", "card", `{"parts":[{"kind":"text","image":{"url":"https://example.com/image.png"}}]}`, false},
{"board payload", "board", `{"text":"Wrong type"}`, false},
}
for _, test := range cases {
t.Run(test.name, func(t *testing.T) {
out, ok := normalizeContent(test.kind, json.RawMessage(test.body))
if ok != test.valid {
t.Fatalf("valid=%v, want %v", ok, test.valid)
}
if ok && !json.Valid(out) {
t.Fatal("normalized content is invalid JSON")
}
})
}
}