package postcard import ( "strings" "testing" "text/template/parse" ) func TestSample(t *testing.T) { t.Parallel() t.Run("case_kept_after_prefix", func(t *testing.T) { fields, samples, err := parseHeader("subject = \"S\"\nsample.User.Name = \"Ada\"\nsample.order_id = \"1\"\n") if err != nil { t.Fatal(err) } if fields["subject"] != "S" { t.Fatalf("fields = %v", fields) } if samples["User.Name"] != "Ada" || samples["order_id"] != "1" { t.Fatalf("samples = %v", samples) } }) t.Run("old_headers_unchanged", func(t *testing.T) { fields, samples, err := parseHeader("subject = \"S\"\ndescription = \"D\"\nlayout = \"blog\"\n") if err != nil { t.Fatal(err) } if fields["subject"] != "S" || fields["description"] != "D" || fields["layout"] != "blog" { t.Fatalf("fields = %v", fields) } if len(samples) != 0 { t.Fatalf("samples = %v", samples) } }) t.Run("walker", func(t *testing.T) { src := strings.Join([]string{ "Hello {{ .User.Name }}", "{{ if .Flag }}yes{{ else }}no{{ end }}", "{{ with .Cart }}{{ .Total }}{{ end }}", "{{ range .Items }}{{ .SKU }}{{ end }}", "{{ range .Tags }}{{ . }}{{ end }}", "{{ .Order.Customer.Email }}", "{{ $x := .User }}{{ $x.Name }}", }, "\n") samples := map[string]string{ "User.Name": "Ada", "Flag": "true", "Cart.Total": "10", "Items.SKU": "A1", "Tags": "red", "Order.Customer.Email": "ada@example.com", } vars := placeholderVars(samples, src) if vars["User"].(map[string]any)["Name"] != "Ada" { t.Fatalf("nested User = %#v", vars["User"]) } if vars["Order"].(map[string]any)["Customer"].(map[string]any)["Email"] != "ada@example.com" { t.Fatalf("chain = %#v", vars["Order"]) } if _, ok := vars["Items"].([]any); !ok { t.Fatalf("Items = %#v", vars["Items"]) } if _, ok := vars["Tags"].([]any); !ok { t.Fatalf("Tags = %#v", vars["Tags"]) } // Ensure the walker actually visited the parse tree (not a no-op). tree, err := parse.Parse("t", src, "{{", "}}") if err != nil { t.Fatal(err) } if tree["t"] == nil || tree["t"].Root == nil { t.Fatal("parse tree empty") } }) t.Run("consumer_shapes_validate", func(t *testing.T) { // Shapes mirrored from sm-user-plugin and sm-paymentgateway-plugin // mail templates, with neutral acme.* IDs and no consuming-app names. shapes := []string{ "subject = \"Activate your account\"\nsample.code = \"abc123\"\nsample.url = \"https://example.test/a\"\n==\nUse code {{ .code }} or open {{ .url }}.\n", "subject = \"Reset password\"\nsample.url = \"https://example.test/r\"\nsample.user.name = \"Ada\"\n==\nHello {{ .user.name }}, {{ .url }}\n", "subject = \"Receipt for order {{ .order.id }}\"\nsample.order.id = \"42\"\nsample.order.total = \"19.99\"\nsample.order.currency = \"PLN\"\n==\nOrder {{ .order.id }} total {{ .order.total }} {{ .order.currency }}\n", "subject = \"Invoice {{ .number }}\"\nsample.number = \"INV-1\"\nsample.lines.name = \"Item\"\nsample.lines.amount = \"10\"\n==\n{{ range .lines }}{{ .name }}: {{ .amount }}\n{{ end }}\n", "subject = \"Payment failed\"\nsample.reason = \"card\"\n==\nReason: {{ .reason }}\n", "subject = \"Subscription {{ .plan }}\"\nsample.plan = \"pro\"\nsample.next = \"2026-12-01\"\n==\nPlan {{ .plan }}, next {{ .next }}\n", } for i, src := range shapes { header, body, ok := strings.Cut(src, "\n==\n") if !ok { t.Fatalf("shape %d: missing separator", i) } fields, samples, err := parseHeader(header) if err != nil { t.Fatalf("shape %d header: %v", i, err) } if fields["subject"] == "" { t.Fatalf("shape %d: empty subject", i) } vars := placeholderVars(samples, fields["subject"], body) if len(samples) > 0 && len(vars) == 0 { t.Fatalf("shape %d: samples not walked into vars", i) } // Subject and body must parse as templates with the samples filled. name := "acme.shape" + string(rune('a'+i)) if _, err := parseTemplateFile(name+"::mail.t", "acme.shape", src); err != nil { t.Fatalf("shape %d parseTemplateFile: %v", i, err) } } }) }