Skip to content
quarry Artifact [bc1df16c83]
Login

Artifact [bc1df16c83]

Artifact bc1df16c834ea2cf10f1c925d1afdf49eac752598903b9a97d8ca6f2977f3ae7:


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, "&#9670;") || strings.Contains(got, "<img") || !strings.Contains(got, ">quarry</td>") {
		t.Errorf("no quarry mark: %v", ok)
	}
}