parsimony/assets/assets_test.go
tgiachi 5f5deb3e30 feat(assets): the Parsimony logo
- Add the mark (an envelope in a rounded square) and the wordmark, drawn with monoline strokes so no font is needed
- Add light and dark versions, and the generator script
- Embed them in a Go package and ship the same files with the admin UI
- Test that the SVGs are well formed, need nothing from outside, stay in sync with the admin copies and have enough contrast
2026-11-10 12:13:17 +01:00

123 lines
3.6 KiB
Go

package assets
import (
"bytes"
"encoding/xml"
"io"
"os"
"strings"
"testing"
)
var files = map[string]string{"logo-mark.svg": Mark, "logo.svg": Logo, "logo-dark.svg": LogoDark}
func TestLogosAreWellFormedSVG(t *testing.T) {
for name, src := range files {
if src == "" {
t.Fatalf("%s is empty", name)
}
dec := xml.NewDecoder(strings.NewReader(src))
var root *xml.StartElement
for {
tok, err := dec.Token()
if err == io.EOF {
break
}
if err != nil {
t.Fatalf("%s: %v", name, err)
}
if se, ok := tok.(xml.StartElement); ok && root == nil {
se := se
root = &se
}
}
if root == nil || root.Name.Local != "svg" || root.Name.Space != "http://www.w3.org/2000/svg" {
t.Fatalf("%s: root element %+v", name, root)
}
attrs := map[string]string{}
for _, a := range root.Attr {
attrs[a.Name.Local] = a.Value
}
if attrs["viewBox"] == "" || attrs["role"] != "img" || attrs["aria-label"] != "Parsimony" {
t.Errorf("%s: it must scale (viewBox) and be named for screen readers: %v", name, attrs)
}
if attrs["width"] != "" || attrs["height"] != "" {
t.Errorf("%s: no fixed size: the page decides", name)
}
}
}
func TestLogosNeedNothingFromOutside(t *testing.T) {
// Inline in a page with a strict CSP, in a README, on a dark background: no fonts, scripts, images or links.
for name, src := range files {
for _, bad := range []string{"<text", "<script", "<image", "<use", "<style", "xlink:", "href=", "url(", "@import", "font-family", "<foreignObject"} {
if strings.Contains(src, bad) {
t.Errorf("%s contains %q", name, bad)
}
}
if n := strings.Count(src, "http"); n != 1 {
t.Errorf("%s: only the SVG namespace may mention http, found %d", name, n)
}
if len(src) > 4000 {
t.Errorf("%s is %d bytes: a logo should stay tiny", name, len(src))
}
}
}
func TestTheWordmarkLogosDifferOnlyInColour(t *testing.T) {
strip := func(s string) string {
for _, c := range []string{"#0f172a", "#ffffff", "#0f766e", "#2dd4bf"} {
s = strings.ReplaceAll(s, c, "#")
}
return s
}
if strip(Logo) != strip(LogoDark) {
t.Fatal("the light and dark logos must have the same shapes")
}
if Logo == LogoDark {
t.Fatal("the dark logo must use light colours")
}
if !strings.Contains(LogoDark, "#ffffff") || strings.Contains(LogoDark, "#0f172a") {
t.Fatal("the dark logo draws the letters in white, not in the dark ink")
}
if !strings.Contains(Logo, "#0f172a") {
t.Fatal("the light logo draws the letters in the dark ink")
}
}
func TestTheAdminUIShipsTheSameLogos(t *testing.T) {
for public, want := range map[string]string{"../admin/public/favicon.svg": Mark, "../admin/public/logo.svg": Logo} {
got, err := os.ReadFile(public)
if err != nil {
t.Fatalf("%s: %v (copy it from assets/)", public, err)
}
if !bytes.Equal(got, []byte(want)) {
t.Errorf("%s differs from the file in assets/: run `python3 assets/make-logo.py assets` and copy it", public)
}
}
}
// The mark is white on teal: that pair must stay readable (WCAG AA for graphics asks 3:1).
func TestTheMarkHasEnoughContrast(t *testing.T) {
lum := func(hex string) float64 {
var r, g, b int
if _, err := fmtSscanf(hex, &r, &g, &b); err != nil {
t.Fatal(err)
}
f := func(c int) float64 {
v := float64(c) / 255
if v <= 0.03928 {
return v / 12.92
}
return pow((v+0.055)/1.055, 2.4)
}
return 0.2126*f(r) + 0.7152*f(g) + 0.0722*f(b)
}
l1, l2 := lum("#ffffff"), lum("#0f766e")
if ratio := (l1 + 0.05) / (l2 + 0.05); ratio < 4.5 {
t.Fatalf("contrast %.2f:1", ratio)
}
if !strings.Contains(Mark, "#0f766e") || !strings.Contains(Mark, "#ffffff") {
t.Fatal("the contrast test checks the colours the mark really uses")
}
}