Files
summercms/modules/cabana/mail_unit_test.go
Jakub Zych 67f7164302 feat(cabana): mail templates list grouped by template with per-locale status
Gap G-35-1 (supersedes D-05; D-06 reset and D-07 drift per locale):
- the templates list keeps one row per template (the base row, or the
  lowest variant when only variants ship) with a Customized locales
  column (Default, then locale codes, at most three then +N);
- row states and the status filter aggregate over the template's
  locale versions; the locale filter is removed;
- a not-customized locale without a file shows the base row's current
  subject, body and layout; its info partial says so, its file version
  compares with the base file, and Reset returns it to the base file
  (postcard.ResetOverride accepts a fallback-locale code);
- copy: mail.template.customized_locales, mail.info_locale_default,
  mail.info_locale_fallback (en/pl);
- tests: TestMailScreensSmoke/locales_list, templates and drift updated,
  TestMailDrift on the grouped row, TestMailTemplateFilterScopes.
2026-11-13 20:39:45 +01:00

132 lines
4.1 KiB
Go

package cabana_test
import (
"strings"
"testing"
"testing/fstest"
"git.golem15.com/golem15/summercms/modules/cabana"
"git.golem15.com/golem15/summercms/modules/postcard"
)
func TestMailLocale(t *testing.T) {
got := postcard.TemplateLocale("acme.blog::mail.welcome-en", []string{"en", "pl"})
if got != "en" {
t.Fatalf("welcome-en = %q", got)
}
got = postcard.TemplateLocale("acme.blog::mail.welcome", []string{"en", "pl"})
if got != "" {
t.Fatalf("base = %q", got)
}
got = postcard.TemplateLocale("acme.blog::mail.welcome-de", []string{"en", "pl"})
if got != "" {
t.Fatalf("unknown suffix = %q", got)
}
}
func TestMailDiff(t *testing.T) {
identical := cabana.LineDiffForTest(
[]string{"a", "b", "c"},
[]string{"a", "b", "c"},
3,
)
if identical != "" {
t.Fatalf("identical = %q", identical)
}
got := cabana.LineDiffForTest(
[]string{"a", "b", "c", "d", "e", "f", "g", "h"},
[]string{"a", "b", "c", "d", "X", "f", "g", "h"},
3,
)
if got != " b| c| d|- e|+ X| f| g| h" {
t.Fatalf("context diff = %q", got)
}
long := make([]string, 2001)
for i := range long {
long[i] = "line"
}
if got := cabana.LineDiffForTest(long, []string{"x"}, 3); got != "" {
t.Fatalf("above the cap = %q", got)
}
}
func TestMailFieldSchema(t *testing.T) {
ctl := stubMailFieldController{}
okFS := fstest.MapFS{
"controllers/mail/config_form.yaml": {Data: []byte("name: mail\nform: ~/plugins/acme/mail/models/draft/fields.yaml\nmodelClass: Draft\n")},
"models/draft/fields.yaml": {Data: []byte(`fields:
html:
type: codeeditor
language: html
mailImages: true
css:
type: codeeditor
language: css
preview:
type: mailpreview
mode: template
brand_color:
type: colorpicker
logo:
type: mailimage
`)},
}
schema, err := cabana.CompileForm("acme.mail", ctl, okFS)
if err != nil {
t.Fatal(err)
}
byName := map[string]cabana.FormField{}
for _, f := range schema.Fields {
byName[f.Name] = f
}
if byName["html"].Language != "html" || byName["html"].MailImages == nil || !*byName["html"].MailImages {
t.Fatalf("html field = %+v", byName["html"])
}
if byName["preview"].Mode != "template" {
t.Fatalf("preview mode = %q", byName["preview"].Mode)
}
if byName["brand_color"].Type != "colorpicker" || byName["logo"].Type != "mailimage" {
t.Fatalf("settings types color=%q logo=%q", byName["brand_color"].Type, byName["logo"].Type)
}
cases := []struct {
name string
fields string
want string
}{
{"language_required", "fields:\n body:\n type: codeeditor\n", "language is required"},
{"language_on_text", "fields:\n body:\n type: text\n language: html\n", "language is only valid"},
{"mailImages_on_text", "fields:\n body:\n type: text\n mailImages: true\n", "mailImages is only valid"},
{"mode_required", "fields:\n preview:\n type: mailpreview\n", "mode is required"},
{"bad_mode", "fields:\n preview:\n type: mailpreview\n mode: other\n", "must be template"},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
fsys := fstest.MapFS{
"controllers/mail/config_form.yaml": {Data: []byte("name: mail\nform: ~/plugins/acme/mail/models/draft/fields.yaml\nmodelClass: Draft\n")},
"models/draft/fields.yaml": {Data: []byte(tc.fields)},
}
_, err := cabana.CompileForm("acme.mail", ctl, fsys)
if err == nil || !strings.Contains(err.Error(), tc.want) {
t.Fatalf("err = %v, want containing %q", err, tc.want)
}
})
}
}
type stubMailFieldController struct{}
func (stubMailFieldController) ID() string { return "acme.mail.drafts" }
func (stubMailFieldController) ModelName() string { return "Draft" }
func (stubMailFieldController) ConfigDir() string { return "controllers/mail" }
// TestMailTemplateFilterScopes: one row per template leaves no locale
// filter; the status filter is the only scope (G-35-1).
func TestMailTemplateFilterScopes(t *testing.T) {
if got := strings.Join(cabana.MailTemplate{}.FilterScopes(), ","); got != "filterStatus" {
t.Fatalf("MailTemplate filter scopes = %s", got)
}
}