lmjtfy.git / packages / card / src / pixel_tests.rs
1use super::*;
2
3fn icons() -> [(&'static str, Icon); 3] {
4    [("claude", CLAUDE), ("chatgpt", CHATGPT), ("gemini", GEMINI)].map(|(name, svg)| (name, Icon::parse(svg).unwrap_or_else(|why| panic!("{name}: {why}"))))
5}
6
7#[test]
8fn the_three_vendored_icons_are_read_whole() {
9    for (name, icon) in icons() {
10        assert_eq!(icon.size, 9, "{name}");
11        assert!(icon.cells.len() > 15, "{name}");
12        assert!(icon.cells.iter().all(|cell| cell.x < 9 && cell.y < 9), "{name}");
13    }
14}
15
16#[test]
17fn they_are_distinct_and_in_their_upstream_colours() {
18    let all = icons();
19    assert_ne!(all[0].1, all[1].1);
20    assert_ne!(all[0].1, all[2].1);
21    assert_ne!(all[1].1, all[2].1);
22    assert_eq!(all[0].1.colours(), vec![[0xD9, 0x77, 0x57]]);
23    assert_eq!(all[1].1.colours(), vec![[0x34, 0x33, 0x41]]);
24    assert_eq!(all[2].1.colours().len(), 5);
25}
26
27#[test]
28fn a_path_with_a_hole_leaves_it_open() {
29    // ChatGPT's knot: `M5,4V5H4V4z` is wound the other way, and is a hole.
30    let chatgpt = Icon::parse(CHATGPT).unwrap();
31    assert!(!chatgpt.cells.iter().any(|cell| (cell.x, cell.y) == (4, 4)));
32    assert!(chatgpt.cells.iter().any(|cell| (cell.x, cell.y) == (3, 3)));
33}
34
35#[test]
36fn claudes_sparkle_is_the_shape_upstream_draws() {
37    let claude = Icon::parse(CLAUDE).unwrap();
38    let mut rows = vec![String::new(); 9];
39    for y in 0..9 {
40        for x in 0..9 {
41            rows[y].push(if claude.cells.iter().any(|cell| (cell.x, cell.y) == (x, y)) { '#' } else { '.' });
42        }
43    }
44    assert_eq!(rows, [".........", "....#....", "..#.#.#..", "...###...", ".#######.", "...###...", "..#.#.#..", "....#....", "........."]);
45}
46
47#[test]
48fn what_it_cannot_read_is_refused() {
49    let svg = |shape: &str| format!(r#"<svg viewBox="0 0 9 9">{shape}</svg>"#);
50    for bad in [
51        svg(r##"<path fill="#000000" d="M0,0L5,5z"/>"##),
52        svg(r##"<path fill="#000000" d="M0,0H4V4z"/>"##),
53        svg(r##"<rect fill="red" x="0" y="0" width="1" height="1"/>"##),
54        svg(r##"<rect fill="#000000" x="0" y="0" width="1"/>"##),
55        svg(""),
56        "<svg></svg>".to_owned(),
57    ] {
58        assert!(Icon::parse(&bad).is_err(), "{bad}");
59    }
60}