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)
}
}
}
func TestAccountCardKeepsQuarrysMark(t *testing.T) {
got, ok := EmailDocument(EmailDoc{Title: "t", Surface: "account", Body: "<p>x</p>", Why: "w"})
if !ok || !strings.Contains(got, "◆") || strings.Contains(got, "<img") || !strings.Contains(got, ">quarry</td>") {
t.Errorf("no quarry mark: %v", ok)
}
}
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, "◆") {
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)
}
}