feat(mailer): emails follow a light system theme, and stay dark otherwise
release / go (push) Successful in 2m0s
release / web (push) Successful in 1m26s
release / govulncheck (push) Successful in 20s
release / integration (push) Successful in 4m16s
release / android (push) Successful in 4m23s
release / Build signed APK (push) Successful in 4m52s
release / Attach APK to the Release (tag releases only) (push) Skipped
release / Build + push container image (push) Successful in 17s
release / Verify release artifacts (tag releases only) (push) Skipped

Every email is still drawn dark inline, so a client that can't tell the
reader's theme shows the app's own look. A stylesheet gives each coloured
element the design system's light colours when the reader's system is
light. A test checks that every element drawn dark carries the class that
lightens it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-28 16:15:48 -05:00
co-authored by Claude Opus 5.5
parent c662bc1f90
commit 7c6c47cdb2
5 changed files with 155 additions and 24 deletions
+60 -1
View File
@@ -1,6 +1,7 @@
package mailer
import (
"regexp"
"strings"
"testing"
"testing/fstest"
@@ -23,7 +24,7 @@ func TestEveryEmailIsFramedByTheLayout(t *testing.T) {
}
for _, want := range []string{
`">Minstrel</td>`,
"<h1 style=",
`<h1 class="m-heading" style=`,
"Sent by the Minstrel server at https://music.example because ",
"— Minstrel",
} {
@@ -63,3 +64,61 @@ func TestUnknownStyleFailsTheRender(t *testing.T) {
t.Fatal("want an error for an unknown style")
}
}
// An email is dark unless the reader's system is light. Every element drawn
// in a dark colour carries the class that gives it the light one, and every
// style with a colour has light colours to give.
func TestEveryColouredElementFollowsALightTheme(t *testing.T) {
for name, s := range styles {
if strings.Contains(s, "#") && name != "button" {
if _, ok := lightStyles[name]; !ok {
t.Errorf("style %q has a colour and no light colours", name)
}
}
}
reset, err := func() (string, error) {
_, html, err := RenderResetEmail(ResetEmailVars{
Username: "alice", ResetURL: "https://music.example/reset/abc", ExpiryHours: 1, Server: "https://music.example",
})
return html, err
}()
if err != nil {
t.Fatal(err)
}
_, smtpTest, err := RenderSMTPTestEmail("https://music.example/")
if err != nil {
t.Fatal(err)
}
for name, html := range map[string]string{"reset": reset, "smtp test": smtpTest} {
if !strings.Contains(html, "@media (prefers-color-scheme: light)") ||
!strings.Contains(html, ".m-card { background:"+lightCard+" !important;") {
t.Errorf("%s html has no light theme", name)
}
for _, tag := range unthemed(html) {
t.Errorf("%s: %s is drawn dark with no light class", name, tag)
}
}
if got := unthemed(`<p style="color:` + colourText + `;">x</p>`); len(got) != 1 {
t.Errorf("an element without its class went unnoticed: %v", got)
}
}
// unthemed lists the tags in html whose inline style uses a dark colour that
// has a light one, but which carry no m- class. A button is the action
// colour with its own text colour, the same in both themes.
func unthemed(html string) []string {
dark := []string{colourPage, colourCard, colourBorder, colourText, colourSecondary, colourTertiary, colourLink}
var out []string
for _, tag := range regexp.MustCompile(`<[a-z0-9]+\s[^>]*style="[^"]*"[^>]*>`).FindAllString(html, -1) {
if strings.Contains(tag, colourAction) {
continue
}
for _, c := range dark {
if strings.Contains(tag, c) && !strings.Contains(tag, `class="m-`) {
out = append(out, tag)
break
}
}
}
return out
}