Skip to content
quarry Artifact [8d49df7918]
Login

Artifact [8d49df7918]

Artifact 8d49df7918f1a3533b32b9ea527f35b80c0667041d6f8b3465ebfc2fea3a752f:


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: its Q logo and
// the word QUARRY as styled text (never an image, so a client that
// recolours dark mail recolours it too), again in the footer, small.
func TestAccountCardIsTheApplicationsNotARepositorys(t *testing.T) {
	const logo = "https://forge.example/apple-touch-icon.png"
	got, ok := EmailDocument(EmailDoc{Title: "t", Surface: "account", Body: "<p>x</p>", Why: "w", LogoURL: logo,
		HomeURL: "https://forge.example", HomeName: "forge.example"})
	if !ok {
		t.Fatal("fell back")
	}
	if n := strings.Count(got, `<img src="`+logo+`"`); n != 2 {
		t.Errorf("%d logo images, want header and footer", n)
	}
	if n := strings.Count(got, ">QUARRY<"); n != 2 {
		t.Errorf("%d QUARRY words, want header and footer", n)
	}
	for _, want := range []string{
		"font-family:'Iosevka Fixed','Iosevka','Arial Narrow','Roboto Condensed','Helvetica Neue Condensed',sans-serif",
		"font-weight:600;text-transform:uppercase;letter-spacing:", "color:#282828;",
		`.q-word { color:#f0ece4 !important; }`,
		`href="https://cdn.jsdelivr.net/npm/@fontsource/iosevka/600.css"`,
	} {
		if !strings.Contains(got, want) {
			t.Errorf("missing %s", want)
		}
	}
	for _, banned := range []string{"lockup", "wordmark", "&#9670;", "Sent by", ">quarry<", "b9541a"} {
		if strings.Contains(got, banned) {
			t.Errorf("carries %q", banned)
		}
	}
	if strings.Contains(got, "zeitforge.app/") {
		t.Error("a font or image is fetched from the forge: a self-hosted fetch is a read signal")
	}
}

// A repository's header keeps its own mark and name; the footer carries
// the small logo and the word.
func TestRepoCardKeepsItsMarkAndFooterHasTheWord(t *testing.T) {
	const logo = "https://forge.example/apple-touch-icon.png"
	got, _ := EmailDocument(EmailDoc{Title: "t", Name: "alpha", Mark: IdenticonMark("alpha"), Body: "<p>x</p>", LogoURL: logo})
	if strings.Count(got, "<img") != 1 || strings.Count(got, ">QUARRY<") != 1 || strings.Contains(got, "lockup") {
		t.Errorf("repo card: %d images, %d words", strings.Count(got, "<img"), strings.Count(got, ">QUARRY<"))
	}
}

// The wordmark is three copies of one word and a client shows one: the
// dark-ink image by default, the light-ink image under a dark scheme (and
// Outlook.com's [data-ogsc]), the text only in Gmail, which forces text
// dark but never images. The hidden copies are display:none, so one
// reading of QUARRY reaches a screen reader.
func TestWordmarkIsThreeCopiesAndAClientShowsOne(t *testing.T) {
	const base = "https://forge.example/wordmark-"
	got, ok := EmailDocument(EmailDoc{Title: "t", Surface: "account", Body: "<p>x</p>", Why: "w", LogoURL: "https://forge.example/apple-touch-icon.png", WordmarkBase: base})
	if !ok {
		t.Fatal("fell back")
	}
	for _, want := range []string{
		`class="q-wm-d" src="` + base + `dark-ink.png" alt="QUARRY" width="120" height="29" style="display:block;`,
		`class="q-wm-l" src="` + base + `light-ink.png" alt="QUARRY" width="120" height="29" style="display:none;max-height:0;overflow:hidden;mso-hide:all;`,
		`class="q-word q-wm-t" style="display:none;max-height:0;overflow:hidden;mso-hide:all;`,
		`src="` + base + `dark-ink-sm.png"`, `src="` + base + `light-ink-sm.png"`,
		`.q-wm-d { display:none !important; }`, `.q-wm-l { display:block !important;`,
		`[data-ogsc] .q-wm-d`, `[data-ogsc] .q-wm-l`,
		`u + .body .q-wm-d, u + .body .q-wm-l { display:none !important; }`,
		`u + .body .q-wm-t { display:inline !important;`,
		`<body class="body"`, `<!doctype html>`,
	} {
		if !strings.Contains(got, want) {
			t.Errorf("missing %s", want)
		}
	}
	if strings.Contains(got, "wordmark.png") || strings.Contains(strings.ToLower(got), "b9541a") {
		t.Error("the orange wordmark is back")
	}
	// Header and footer: three copies each.
	if n := strings.Count(got, `alt="QUARRY"`); n != 4 {
		t.Errorf("%d images alt QUARRY, want 2 inks x header and footer", n)
	}
	if n := strings.Count(got, ">QUARRY<"); n != 2 {
		t.Errorf("%d text copies, want header and footer", n)
	}
}