package postcard import ( "context" "io/fs" "strings" "testing" "testing/fstest" "git.golem15.com/golem15/summercms/modules/backpack" "git.golem15.com/golem15/summercms/modules/pact" ) // partialsPlugin is a fixture plugin that ships only mail partials. type partialsPlugin struct { id string fsys fs.FS partials []string } func (p partialsPlugin) ID() string { return p.id } func (p partialsPlugin) MailPartialsFS() fs.FS { return p.fsys } func (p partialsPlugin) MailPartials() []string { return p.partials } // mailPlugin is a fixture plugin that ships templates and, optionally, // partials. type mailPlugin struct { partialsPlugin templates []string layouts map[string]string } func (p mailPlugin) MailTemplatesFS() fs.FS { return p.fsys } func (p mailPlugin) MailTemplates() []string { return p.templates } func (p mailPlugin) MailLayouts() map[string]string { return p.layouts } // templatesOnly hides the partial capability of a mailPlugin. type templatesOnly struct{ p mailPlugin } func (t templatesOnly) ID() string { return t.p.id } func (t templatesOnly) MailTemplatesFS() fs.FS { return t.p.fsys } func (t templatesOnly) MailTemplates() []string { return t.p.templates } func (t templatesOnly) MailLayouts() map[string]string { return t.p.layouts } var ( _ pact.HasMailPartials = partialsPlugin{} _ pact.HasMailPartials = mailPlugin{} _ pact.HasMailTemplates = mailPlugin{} _ pact.HasMailTemplates = templatesOnly{} ) // fakeOverrides is an OverrideSource with a fixed answer. type fakeOverrides struct{ ov Overrides } func (f fakeOverrides) MailOverrides(context.Context, string) (Overrides, error) { return f.ov, nil } const noteHTM = `name = "Note" == NOTE: {{ .text }} ==
New: beta
`) { t.Errorf("HTML = %q, want the other plugin's partial inline", msg.HTML) } if !strings.Contains(msg.Text, "New: [beta]") { t.Errorf("text = %q", msg.Text) } }) t.Run("body", func(t *testing.T) { m, drv := partialsMailer(t, Options{}, nil) msg := sendTemplate(t, m, drv, "acme.blog::mail.welcome", map[string]any{"Name": "Ada", "Link": "https://example.com/go?a=1&b=2"}) if strings.Contains(msg.HTML, "raw HTML omitted") { t.Errorf("the partial was dropped by Markdown:\n%s", msg.HTML) } for _, want := range []string{ "Hi Ada
\nBye
", } { if !strings.Contains(msg.HTML, want) { t.Errorf("HTML lacks %q:\n%s", want, msg.HTML) } } if strings.Contains(msg.HTML, "MAILPARTIAL") || strings.Contains(msg.Text, "MAILPARTIAL") { t.Errorf("a token was left in the message:\n%s\n%s", msg.HTML, msg.Text) } if msg.Text != "Hi Ada\n\nGo| Item | ", "Pen | ", `
|---|