diff --git a/modules/postcard/inline_test.go b/modules/postcard/inline_test.go new file mode 100644 index 00000000..e148a514 --- /dev/null +++ b/modules/postcard/inline_test.go @@ -0,0 +1,245 @@ +package postcard + +import ( + "bytes" + "context" + "errors" + "log/slog" + "strings" + "testing" + "testing/fstest" + "time" +) + +const ( + inlineBrandColour = "#445566" + inlineMedia = "@media only screen and (max-width: 600px) { .inner-body { width: 100% !important; } }" +) + +var inlineVars = map[string]any{"Name": "Ada", "Link": "https://example.com/go"} + +// defaultLayoutWithCSS is the built-in default layout as a database layout +// whose CSS is css. +func defaultLayoutWithCSS(css string, optOut bool) map[string]LayoutOverride { + def, _ := NewCatalog().Layout(defaultLayoutName) + return map[string]LayoutOverride{defaultLayoutName: {HTML: def.HTML, Text: def.Text, CSS: css, DisableAutoInlineCSS: optOut}} +} + +func TestInlineCSS(t *testing.T) { + t.Parallel() + brand := &Brand{ButtonPrimaryBg: inlineBrandColour} + ov := &Overrides{Brand: brand, Layouts: defaultLayoutWithCSS(inlineMedia, false)} + + m, drv := partialsMailer(t, Options{}, ov) + inlined := sendTemplate(t, m, drv, "acme.blog::mail.welcome", inlineVars) + + // The same message with inlining off: the mailer's no-op inliner seam. + plainMailer, plainDrv := partialsMailer(t, Options{}, ov) + plainMailer.(*mailer).inline = noInline + plain := sendTemplate(t, plainMailer, plainDrv, "acme.blog::mail.welcome", inlineVars) + + t.Run("button_colour", func(t *testing.T) { + if style := buttonStyle(t, inlined.HTML); !strings.Contains(style, "background-color:"+inlineBrandColour) { + t.Errorf("button style = %q, want the brand primary colour inline", style) + } + if style := buttonStyle(t, plain.HTML); style != "" { + t.Errorf("without inlining the button has style %q", style) + } + }) + t.Run("doctype", func(t *testing.T) { + const doctype = `` + if !strings.HasPrefix(inlined.HTML, doctype) { + t.Errorf("DOCTYPE lost:\n%.200s", inlined.HTML) + } + if strings.Count(inlined.HTML, "