Skip to main content

max / balanced_breakfast

4.0 KB · 137 lines History Blame Raw
1 //! Theme TOML parsing integration tests (unit-level, no AppHandle needed).
2
3 // ── Theme Parsing ────────────────────────────────────────────────────
4
5 #[test]
6 fn theme_toml_parse_full() {
7 let toml_str = r##"
8 [meta]
9 name = "Ocean Dark"
10 variant = "dark"
11
12 [background]
13 primary = "#1a1a2e"
14 secondary = "#16213e"
15
16 [foreground]
17 primary = "#e0e0e0"
18 secondary = "#a0a0a0"
19
20 [accent]
21 primary = "#0f3460"
22 highlight = "#e94560"
23
24 [border]
25 default = "#2a2a4a"
26 "##;
27
28 let table: toml::Table = toml_str.parse().unwrap();
29
30 // Verify meta parsing
31 let meta = table.get("meta").unwrap().as_table().unwrap();
32 assert_eq!(meta.get("name").unwrap().as_str().unwrap(), "Ocean Dark");
33 assert_eq!(meta.get("variant").unwrap().as_str().unwrap(), "dark");
34
35 // Verify color sections can be extracted (replicates get_theme logic)
36 let mut colors = std::collections::HashMap::new();
37 for section in &["background", "foreground", "accent", "border"] {
38 if let Some(sect) = table.get(*section).and_then(|s| s.as_table()) {
39 for (key, val) in sect {
40 if let Some(color) = val.as_str() {
41 colors.insert(format!("{}.{}", section, key), color.to_string());
42 }
43 }
44 }
45 }
46
47 assert_eq!(colors.get("background.primary").unwrap(), "#1a1a2e");
48 assert_eq!(colors.get("foreground.primary").unwrap(), "#e0e0e0");
49 assert_eq!(colors.get("accent.highlight").unwrap(), "#e94560");
50 assert_eq!(colors.get("border.default").unwrap(), "#2a2a4a");
51 assert_eq!(colors.len(), 7);
52 }
53
54 #[test]
55 fn theme_toml_parse_minimal() {
56 // A theme with only meta and one color section
57 let toml_str = r##"
58 [meta]
59 name = "Minimal"
60
61 [background]
62 primary = "#fff"
63 "##;
64
65 let table: toml::Table = toml_str.parse().unwrap();
66
67 let meta = table.get("meta").unwrap().as_table().unwrap();
68 assert_eq!(meta.get("name").unwrap().as_str().unwrap(), "Minimal");
69 // Variant not specified — consumer should default to "dark"
70 assert!(meta.get("variant").is_none());
71
72 let mut colors = std::collections::HashMap::new();
73 for section in &["background", "foreground", "accent", "border"] {
74 if let Some(sect) = table.get(*section).and_then(|s| s.as_table()) {
75 for (key, val) in sect {
76 if let Some(color) = val.as_str() {
77 colors.insert(format!("{}.{}", section, key), color.to_string());
78 }
79 }
80 }
81 }
82
83 assert_eq!(colors.len(), 1);
84 assert_eq!(colors.get("background.primary").unwrap(), "#fff");
85 }
86
87 #[test]
88 fn theme_toml_parse_empty_is_valid() {
89 // An empty TOML should parse without error
90 let table: toml::Table = "".parse().unwrap();
91
92 let mut colors = std::collections::HashMap::new();
93 for section in &["background", "foreground", "accent", "border"] {
94 if let Some(sect) = table.get(*section).and_then(|s| s.as_table()) {
95 for (key, val) in sect {
96 if let Some(color) = val.as_str() {
97 colors.insert(format!("{}.{}", section, key), color.to_string());
98 }
99 }
100 }
101 }
102
103 assert!(colors.is_empty());
104 }
105
106 #[test]
107 fn theme_toml_ignores_unknown_sections() {
108 let toml_str = r##"
109 [meta]
110 name = "Custom"
111
112 [background]
113 primary = "#000"
114
115 [custom_section]
116 foo = "bar"
117 "##;
118
119 let table: toml::Table = toml_str.parse().unwrap();
120
121 // The standard extraction logic only reads known sections
122 let mut colors = std::collections::HashMap::new();
123 for section in &["background", "foreground", "accent", "border"] {
124 if let Some(sect) = table.get(*section).and_then(|s| s.as_table()) {
125 for (key, val) in sect {
126 if let Some(color) = val.as_str() {
127 colors.insert(format!("{}.{}", section, key), color.to_string());
128 }
129 }
130 }
131 }
132
133 // custom_section should not appear in colors
134 assert_eq!(colors.len(), 1);
135 assert!(!colors.contains_key("custom_section.foo"));
136 }
137