Skip to content
quarry Artifact [7929cee07b]
Login

Artifact [7929cee07b]

Artifact 7929cee07b60797fa4a3a005a22b70c59eda8b2c4457fd979eda44ab46a8fe4a:


package markup

import (
	"regexp"
	"strings"
	"testing"

	"craftdesign.group/quarry/internal/idn"
)

// The identicon is idn's face, cell for cell, drawn as a table: five
// rows of five, a cell coloured exactly where the grid is on and in the
// shade the grid says.
func TestIdenticonMarkMatchesTheGrid(t *testing.T) {
	const name = "dantest-sandbox"
	got := string(IdenticonMark(name))
	for _, banned := range []string{"<svg", "<img", "display:grid", "display:flex", "url("} {
		if strings.Contains(got, banned) {
			t.Errorf("mark carries %s", banned)
		}
	}
	inner := got[strings.Index(got, `align="center"`):]
	rows := strings.Split(inner, "<tr>")[1:]
	if len(rows) != 5 {
		t.Fatalf("%d rows", len(rows))
	}
	lit, deep := idn.Hex(name, markLit), idn.Hex(name, markDeep)
	for r, row := range rows {
		cells := regexp.MustCompile(`<td [^>]*>`).FindAllString(row, -1)
		if len(cells) != 5 {
			t.Fatalf("row %d: %d cells", r, len(cells))
		}
		for c, cell := range cells {
			want := ""
			switch idn.Grid(name)[r][c] {
			case 1:
				want = `bgcolor="` + lit + `"`
			case 2:
				want = `bgcolor="` + deep + `"`
			}
			if has := strings.Contains(cell, "bgcolor="); has != (want != "") || (want != "" && !strings.Contains(cell, want)) {
				t.Errorf("cell %d,%d = %s, want %q", r, c, cell, want)
			}
		}
	}
}

func TestAvatarMarkIsOneSizedImage(t *testing.T) {
	got := string(AvatarMark("https://forge.example/r/alpha/avatar", "alpha"))
	for _, want := range []string{`src="https://forge.example/r/alpha/avatar"`, `width="28"`, `height="28"`, `border-radius:6px`, `alt="alpha"`} {
		if !strings.Contains(got, want) {
			t.Errorf("missing %s", want)
		}
	}
}

// With nothing else to name, a card is the application's: no lowercase
// repository name, and the wordmark where the name would be.
func TestAccountCardIsTheApplicationsNotARepositorys(t *testing.T) {
	got, ok := EmailDocument(EmailDoc{Title: "t", Surface: "account", Body: "<p>x</p>", Why: "w"})
	if !ok || strings.Contains(got, ">quarry<") || !strings.Contains(got, ">QUARRY<") {
		t.Errorf("fallback header: %v", ok)
	}
	const wm = "https://forge.example/wordmark.png"
	got, _ = EmailDocument(EmailDoc{Title: "t", Surface: "account", Body: "<p>x</p>", Why: "w", WordmarkURL: wm, HomeURL: "https://forge.example", HomeName: "forge.example"})
	if n := strings.Count(got, `<img src="`+wm+`" alt="QUARRY"`); n != 2 {
		t.Errorf("%d wordmarks, want header and footer", n)
	}
	if strings.Contains(got, "Sent by") || strings.Contains(got, ">quarry<") {
		t.Error("lowercase quarry text or the old footer words")
	}
}

func TestLogoIsQuarrysMarkInAccountHeaderAndFooter(t *testing.T) {
	const logo = "https://forge.example/apple-touch-icon.png"
	got, _ := EmailDocument(EmailDoc{Title: "t", Surface: "account", Body: "<p>x</p>", Why: "w", LogoURL: logo, HomeURL: "https://forge.example", HomeName: "forge.example"})
	if n := strings.Count(got, `<img src="`+logo+`"`); n != 2 {
		t.Errorf("%d logos, want header and footer", n)
	}
	if strings.Contains(got, "&#9670;") {
		t.Error("diamond kept beside the logo")
	}
	if !strings.Contains(got, `width="16" height="16"`) || !strings.Contains(got, `width="28" height="28"`) {
		t.Error("logo sizes")
	}
	// A repo's own mark stays: only the footer carries the logo.
	got, _ = EmailDocument(EmailDoc{Title: "t", Name: "alpha", Mark: IdenticonMark("alpha"), Body: "<p>x</p>", LogoURL: logo})
	if n := strings.Count(got, "<img"); n != 1 {
		t.Errorf("%d images with a repo mark, want the footer's only", n)
	}
}