Files
marqdo/plugins/web/internal/form/source_test.go
T
cflmy 9331af78e2 release(2.8.0): 合入 CHANGELOG/README/发版说明/skills prove 任务;CI 链接指向 doc/releases
- CHANGELOG.md:v2.8.0 条目(中英双语);
- README.md:版本行 2.7.1→2.8.0 + 愿景第 6 条(知识可证明);
- doc/releases/v2.8.0.md:双语发版说明(亮点/用法/已知限制);
- skills/marqdo-dev/index.mq.md:新增 prove 任务(路径/目录模式/
  require_lean/json;参数用 + 行声明升参,check 与 narrative-ratio 通过);
- 设计文档 §12.5 标记已合入;发版链接统一指向 doc/releases/v2.8.0.md。
2026-11-18 10:29:26 +08:00

84 lines
2.8 KiB
Go
Executable File

package form
import "testing"
func TestApplySourcesOverwritesClient(t *testing.T) {
frm := SetFields(New("comments", "insert", "c"), []any{
map[string]any{"字段": "body", "标签": "body", "类型": "textarea", "必填": true, "来源": "client"},
map[string]any{"字段": "author", "标签": "a", "类型": "text", "必填": true, "来源": "session.username"},
map[string]any{"字段": "post_slug", "标签": "s", "类型": "text", "必填": true, "来源": "route.slug"},
map[string]any{"字段": "created_at", "标签": "t", "类型": "text", "来源": "now"},
})
data := map[string]any{
"body": "hi",
"author": "forged",
"post_slug": "evil",
}
ApplySources(frm, data, &RequestContext{
Username: "alice",
Params: map[string]any{"slug": "hello"},
})
if data["author"] != "alice" {
t.Fatalf("author=%v", data["author"])
}
if data["post_slug"] != "hello" {
t.Fatalf("slug=%v", data["post_slug"])
}
if data["body"] != "hi" {
t.Fatalf("body=%v", data["body"])
}
if data["created_at"] == nil || data["created_at"] == "" {
t.Fatalf("created_at missing")
}
}
func TestApplySourcesNowIfEmpty(t *testing.T) {
frm := SetFields(New("news", "insert", "n"), []any{
map[string]any{"字段": "title", "标签": "t", "类型": "text", "必填": true},
map[string]any{"字段": "published_at", "标签": "d", "类型": "text", "来源": "空则现在"},
})
empty := map[string]any{"title": "x", "published_at": ""}
ApplySources(frm, empty, &RequestContext{})
if empty["published_at"] == nil || empty["published_at"] == "" {
t.Fatalf("expected stamp, got %#v", empty["published_at"])
}
kept := map[string]any{"title": "x", "published_at": "2020-01-02"}
ApplySources(frm, kept, &RequestContext{})
if kept["published_at"] != "2020-01-02" {
t.Fatalf("should keep client date, got %#v", kept["published_at"])
}
}
func TestRenderSkipsServerSources(t *testing.T) {
frm := SetFields(New("comments", "insert", "c"), []any{
map[string]any{"字段": "body", "标签": "Body", "类型": "textarea", "必填": true},
map[string]any{"字段": "author", "标签": "Author", "类型": "text", "来源": "session.username"},
})
html := RenderBodyCtx(frm, "c", nil, nil, "", &RequestContext{
Username: "alice",
Params: map[string]any{"slug": "x"},
ReturnPath: "/post/x",
})
if stringsContains(html, `name="author"`) {
t.Fatalf("author field should not render: %s", html)
}
if !stringsContains(html, `name="body"`) {
t.Fatalf("body missing")
}
if !stringsContains(html, `_mq_params`) || !stringsContains(html, `_mq_return`) {
t.Fatalf("missing mq context fields: %s", html)
}
}
func stringsContains(s, sub string) bool {
return len(s) >= len(sub) && (s == sub || len(sub) == 0 ||
(func() bool {
for i := 0; i+len(sub) <= len(s); i++ {
if s[i:i+len(sub)] == sub {
return true
}
}
return false
})())
}