| 1 | // SPDX-License-Identifier: AGPL-3.0-or-later |
| 2 | |
| 3 | package avatars |
| 4 | |
| 5 | import ( |
| 6 | "strings" |
| 7 | "testing" |
| 8 | ) |
| 9 | |
| 10 | func TestIdenticon_Deterministic(t *testing.T) { |
| 11 | t.Parallel() |
| 12 | a := Identicon("alice", 80) |
| 13 | b := Identicon("alice", 80) |
| 14 | if a != b { |
| 15 | t.Fatal("Identicon must be deterministic for the same username") |
| 16 | } |
| 17 | } |
| 18 | |
| 19 | func TestIdenticon_DifferentUsers(t *testing.T) { |
| 20 | t.Parallel() |
| 21 | a := Identicon("alice", 80) |
| 22 | b := Identicon("bob", 80) |
| 23 | if a == b { |
| 24 | t.Fatal("different usernames should produce different SVGs") |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | func TestIdenticon_CaseInsensitive(t *testing.T) { |
| 29 | t.Parallel() |
| 30 | if Identicon("Alice", 80) != Identicon("alice", 80) { |
| 31 | t.Fatal("identicon should not change with username case") |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | func TestIdenticon_ContainsSVGSkeleton(t *testing.T) { |
| 36 | t.Parallel() |
| 37 | out := Identicon("alice", 64) |
| 38 | for _, want := range []string{ |
| 39 | `<svg `, `</svg>`, `viewBox="0 0 5 5"`, `<rect`, |
| 40 | } { |
| 41 | if !strings.Contains(out, want) { |
| 42 | t.Errorf("missing %q in svg", want) |
| 43 | } |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | func TestIdenticon_Symmetry(t *testing.T) { |
| 48 | t.Parallel() |
| 49 | out := Identicon("symmetry", 80) |
| 50 | // For each <rect x="0" ..> we expect a corresponding <rect x="4" ..>. |
| 51 | leftCount := strings.Count(out, `x="0"`) |
| 52 | rightCount := strings.Count(out, `x="4"`) |
| 53 | if leftCount != rightCount { |
| 54 | t.Fatalf("asymmetric: left col rects=%d, right col rects=%d", leftCount, rightCount) |
| 55 | } |
| 56 | } |
| 57 |