icon_catalog_test.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. // icon_catalog_test.go — the icon catalog is written in four places that have
  2. // to agree: ICON_OVERRIDES and ICON_CATEGORIES in DeviceRow.tsx, validateIcon
  3. // in this package, and devices.edit.iconNames in the four locale catalogs.
  4. // The app has no JS tests (pattern of alerts/frontend_defaults_test.go), so the
  5. // parity is asserted here over the source files. Without it an icon added to
  6. // one place and forgotten in another reaches the user as a tile with no name,
  7. // a name with no tile, or a selectable icon that no category ever renders.
  8. package httpapi
  9. import (
  10. "encoding/json"
  11. "os"
  12. "path/filepath"
  13. "regexp"
  14. "slices"
  15. "sort"
  16. "strings"
  17. "testing"
  18. )
  19. const iconCatalogSrc = "../../../app/src/components/DeviceRow.tsx"
  20. // hiddenIcon is in ICON_OVERRIDES but IconPicker keeps it out of the grid.
  21. const hiddenIcon = "help-circle"
  22. var (
  23. overrideKeyRe = regexp.MustCompile(`(?m)^\s*(?:'([a-z0-9-]+)'|([a-z0-9-]+)):\s*[A-Z]`)
  24. categoryRe = regexp.MustCompile(`id:\s*'([a-z-]+)'[\s\S]*?icons:\s*\[([^\]]*)\]`)
  25. categoryIconRe = regexp.MustCompile(`'([a-z0-9-]+)'`)
  26. whitelistKeyRe = regexp.MustCompile(`"([a-z0-9-]+)":\s*true`)
  27. )
  28. func readCatalogSource(t *testing.T, rel string) string {
  29. t.Helper()
  30. raw, err := os.ReadFile(rel)
  31. if err != nil {
  32. t.Fatalf("read %s: %v", rel, err)
  33. }
  34. return string(raw)
  35. }
  36. // catalogBlock cuts the source between two declarations, so a key added to a
  37. // neighbouring map cannot be counted as part of the catalog.
  38. func catalogBlock(t *testing.T, src, from, to string) string {
  39. t.Helper()
  40. start := strings.Index(src, from)
  41. if start < 0 {
  42. t.Fatalf("%s: %s not found", iconCatalogSrc, from)
  43. }
  44. rest := src[start:]
  45. end := strings.Index(rest, to)
  46. if end < 0 {
  47. t.Fatalf("%s: %s not found after %s", iconCatalogSrc, to, from)
  48. }
  49. return rest[:end]
  50. }
  51. func iconOverrideKeys(t *testing.T) []string {
  52. t.Helper()
  53. src := readCatalogSource(t, iconCatalogSrc)
  54. block := catalogBlock(t, src, "export const ICON_OVERRIDES", "export const ALLOWED_ICONS")
  55. var keys []string
  56. seen := map[string]bool{}
  57. for _, m := range overrideKeyRe.FindAllStringSubmatch(block, -1) {
  58. key := m[1]
  59. if key == "" {
  60. key = m[2]
  61. }
  62. if seen[key] {
  63. t.Errorf("ICON_OVERRIDES declares %q twice", key)
  64. }
  65. seen[key] = true
  66. keys = append(keys, key)
  67. }
  68. if len(keys) == 0 {
  69. t.Fatalf("%s: no ICON_OVERRIDES keys parsed", iconCatalogSrc)
  70. }
  71. return keys
  72. }
  73. func iconCategoryIDs(t *testing.T) []string {
  74. t.Helper()
  75. src := readCatalogSource(t, iconCatalogSrc)
  76. block := catalogBlock(t, src, "export const ICON_CATEGORIES", "export function deviceIcon")
  77. matches := categoryRe.FindAllStringSubmatch(block, -1)
  78. if len(matches) == 0 {
  79. t.Fatalf("%s: no ICON_CATEGORIES entries parsed", iconCatalogSrc)
  80. }
  81. ids := make([]string, 0, len(matches))
  82. for _, m := range matches {
  83. ids = append(ids, m[1])
  84. }
  85. return ids
  86. }
  87. // TestIconCategoriesCoverEverySelectableIcon: every icon the picker offers sits
  88. // in exactly one category, and no category lists an icon the picker cannot
  89. // render. An icon in two categories renders twice; an icon in none is invisible
  90. // outside search.
  91. func TestIconCategoriesCoverEverySelectableIcon(t *testing.T) {
  92. overrides := iconOverrideKeys(t)
  93. selectable := map[string]bool{}
  94. for _, key := range overrides {
  95. if key != hiddenIcon {
  96. selectable[key] = true
  97. }
  98. }
  99. src := readCatalogSource(t, iconCatalogSrc)
  100. block := catalogBlock(t, src, "export const ICON_CATEGORIES", "export function deviceIcon")
  101. count := map[string]int{}
  102. seenID := map[string]bool{}
  103. for _, m := range categoryRe.FindAllStringSubmatch(block, -1) {
  104. id := m[1]
  105. if seenID[id] {
  106. t.Errorf("category id %q is declared twice", id)
  107. }
  108. seenID[id] = true
  109. for _, im := range categoryIconRe.FindAllStringSubmatch(m[2], -1) {
  110. icon := im[1]
  111. count[icon]++
  112. if !selectable[icon] {
  113. t.Errorf("category %q lists %q, which ICON_OVERRIDES does not offer", id, icon)
  114. }
  115. }
  116. }
  117. var missing []string
  118. for icon := range selectable {
  119. switch count[icon] {
  120. case 0:
  121. missing = append(missing, icon)
  122. case 1:
  123. default:
  124. t.Errorf("icon %q sits in %d categories, want exactly 1", icon, count[icon])
  125. }
  126. }
  127. if len(missing) > 0 {
  128. sort.Strings(missing)
  129. t.Errorf("selectable icons in no category, so the grid never shows them: %s", strings.Join(missing, ", "))
  130. }
  131. }
  132. // TestIconWhitelistMatchesOverrides: the closed server list and the frontend
  133. // catalog are the same set in both directions, and validateIcon really is the
  134. // function enforcing it.
  135. func TestIconWhitelistMatchesOverrides(t *testing.T) {
  136. overrides := iconOverrideKeys(t)
  137. want := map[string]bool{}
  138. for _, key := range overrides {
  139. want[key] = true
  140. if !validateIcon(key) {
  141. t.Errorf("validateIcon rejects %q, which ICON_OVERRIDES offers", key)
  142. }
  143. }
  144. if validateIcon("zzz-not-an-icon") {
  145. t.Error("validateIcon accepts a key outside the catalog: the list is not closed")
  146. }
  147. src := readCatalogSource(t, "device_overrides.go")
  148. start := strings.Index(src, "allowed := map[string]bool{")
  149. if start < 0 {
  150. t.Fatal("device_overrides.go: the validateIcon whitelist was not found")
  151. }
  152. rest := src[start:]
  153. end := strings.Index(rest, "\n}")
  154. if end < 0 {
  155. t.Fatal("device_overrides.go: the validateIcon whitelist is not closed")
  156. }
  157. got := map[string]bool{}
  158. for _, m := range whitelistKeyRe.FindAllStringSubmatch(rest[:end], -1) {
  159. got[m[1]] = true
  160. }
  161. if len(got) == 0 {
  162. t.Fatal("device_overrides.go: no whitelist keys parsed")
  163. }
  164. for key := range got {
  165. if !want[key] {
  166. t.Errorf("validateIcon allows %q with no ICON_OVERRIDES entry", key)
  167. }
  168. }
  169. for key := range want {
  170. if !got[key] {
  171. t.Errorf("ICON_OVERRIDES offers %q but the validateIcon whitelist does not allow it", key)
  172. }
  173. }
  174. }
  175. // TestIconLocaleCatalogsMatchOverrides: every icon has a non-empty name and
  176. // every category a non-empty label in en, es, zh and ru, with no orphan keys on
  177. // either side. A missing name renders the raw key in the picker.
  178. func TestIconLocaleCatalogsMatchOverrides(t *testing.T) {
  179. overrides := iconOverrideKeys(t)
  180. want := map[string]bool{}
  181. for _, key := range overrides {
  182. want[key] = true
  183. }
  184. ids := iconCategoryIDs(t)
  185. for _, lang := range []string{"en", "es", "zh", "ru"} {
  186. rel := filepath.Join("..", "..", "..", "app", "public", "locales", lang, "translation.json")
  187. raw, err := os.ReadFile(rel)
  188. if err != nil {
  189. t.Fatalf("read %s: %v", rel, err)
  190. }
  191. var tree map[string]any
  192. if err := json.Unmarshal(raw, &tree); err != nil {
  193. t.Fatalf("%s: invalid JSON: %v", rel, err)
  194. }
  195. edit := catalogNode(t, tree, lang, "devices", "edit")
  196. names := catalogNode(t, edit, lang, "iconNames")
  197. for key := range want {
  198. value, ok := names[key]
  199. if !ok {
  200. t.Errorf("%s: iconNames has no entry for %q", lang, key)
  201. continue
  202. }
  203. if name, ok := value.(string); !ok || strings.TrimSpace(name) == "" {
  204. t.Errorf("%s: iconNames[%q] is not a non-empty string", lang, key)
  205. }
  206. }
  207. for key := range names {
  208. if !want[key] {
  209. t.Errorf("%s: iconNames[%q] is an orphan, ICON_OVERRIDES has no such icon", lang, key)
  210. }
  211. }
  212. labels := catalogNode(t, edit, lang, "iconCategories")
  213. for _, id := range ids {
  214. value, ok := labels[id]
  215. if !ok {
  216. t.Errorf("%s: iconCategories has no label for category %q", lang, id)
  217. continue
  218. }
  219. if label, ok := value.(string); !ok || strings.TrimSpace(label) == "" {
  220. t.Errorf("%s: iconCategories[%q] is not a non-empty string", lang, id)
  221. }
  222. }
  223. for id := range labels {
  224. if !slices.Contains(ids, id) {
  225. t.Errorf("%s: iconCategories[%q] is an orphan, no such category", lang, id)
  226. }
  227. }
  228. }
  229. }
  230. func catalogNode(t *testing.T, tree map[string]any, lang string, path ...string) map[string]any {
  231. t.Helper()
  232. current := tree
  233. for _, key := range path {
  234. next, ok := current[key].(map[string]any)
  235. if !ok {
  236. t.Fatalf("%s: %s is missing or not an object", lang, key)
  237. }
  238. current = next
  239. }
  240. return current
  241. }