| 1 | use serde_json::{Value, json}; |
| 2 | use teams_core::{ |
| 3 | AdaptiveCard, BarDisplayMode, CardChart, CardElement, ChartColor, ChartKind, GaugeValueFormat, |
| 4 | format_chart_value, label_stride, nice_scale, series_categories, series_maximum, slice_angles, |
| 5 | unit_fraction, |
| 6 | }; |
| 7 | |
| 8 | fn parse_chart(element: Value) -> CardChart { |
| 9 | let card = json!({"type": "AdaptiveCard", "version": "1.5", "body": [element]}); |
| 10 | let card = AdaptiveCard::parse(&card.to_string()).expect("card parses"); |
| 11 | match &card.items[0].element { |
| 12 | CardElement::Chart(chart) => chart.clone(), |
| 13 | other => panic!("expected chart, got {other:?}"), |
| 14 | } |
| 15 | } |
| 16 | |
| 17 | fn fruit_slices() -> Value { |
| 18 | json!([ |
| 19 | {"legend": "Pear", "value": 59}, |
| 20 | {"legend": "Banana", "value": 292}, |
| 21 | {"legend": "Apple", "value": 143, "color": "good"} |
| 22 | ]) |
| 23 | } |
| 24 | |
| 25 | fn days_series() -> Value { |
| 26 | json!([ |
| 27 | {"legend": "Outlook", "values": [ |
| 28 | {"x": "2023-05-01", "y": 24}, {"x": "2023-05-02", "y": 27}, {"x": "2023-05-03", "y": 18} |
| 29 | ]}, |
| 30 | {"legend": "Teams", "values": [ |
| 31 | {"x": "2023-05-01", "y": 9}, {"x": "2023-05-02", "y": 100}, {"x": "2023-05-03", "y": 22} |
| 32 | ]} |
| 33 | ]) |
| 34 | } |
| 35 | |
| 36 | #[test] |
| 37 | fn donut_parses_slices_and_explicit_color() { |
| 38 | let chart = |
| 39 | parse_chart(json!({"type": "Chart.Donut", "title": "Fruit", "data": fruit_slices()})); |
| 40 | assert_eq!(chart.title.as_deref(), Some("Fruit")); |
| 41 | let ChartKind::Donut(slices) = &chart.kind else { |
| 42 | panic!("expected donut"); |
| 43 | }; |
| 44 | assert_eq!(slices.len(), 3); |
| 45 | assert_eq!(slices[1].label, "Banana"); |
| 46 | assert_eq!(slices[1].value, 292.); |
| 47 | assert_eq!(slices[2].color, ChartColor::Good); |
| 48 | assert_ne!(slices[0].color, slices[1].color); |
| 49 | assert!(chart.show_legend); |
| 50 | } |
| 51 | |
| 52 | #[test] |
| 53 | fn pie_parses_slices() { |
| 54 | let chart = parse_chart( |
| 55 | json!({"type": "Chart.Pie", "colorSet": "categorical", "data": fruit_slices()}), |
| 56 | ); |
| 57 | let ChartKind::Pie(slices) = &chart.kind else { |
| 58 | panic!("expected pie"); |
| 59 | }; |
| 60 | assert_eq!(slices.len(), 3); |
| 61 | assert_eq!(chart.legend_entries().len(), 3); |
| 62 | } |
| 63 | |
| 64 | #[test] |
| 65 | fn gauge_parses_segments_and_defaults() { |
| 66 | let chart = parse_chart(json!({ |
| 67 | "type": "Chart.Gauge", "value": 50, "subLabel": "CPU", |
| 68 | "segments": [ |
| 69 | {"legend": "Low risk", "size": 33, "color": "good"}, |
| 70 | {"legend": "Medium risk", "size": 34, "color": "warning"}, |
| 71 | {"legend": "High risk", "size": 33, "color": "attention"} |
| 72 | ] |
| 73 | })); |
| 74 | let ChartKind::Gauge(gauge) = &chart.kind else { |
| 75 | panic!("expected gauge"); |
| 76 | }; |
| 77 | assert_eq!(gauge.value, 50.); |
| 78 | assert_eq!(gauge.minimum, 0.); |
| 79 | assert_eq!(gauge.maximum, 100.); |
| 80 | assert_eq!(gauge.format, GaugeValueFormat::Percentage); |
| 81 | assert_eq!(gauge.sub_label.as_deref(), Some("CPU")); |
| 82 | assert!(gauge.show_min_max); |
| 83 | assert_eq!(gauge.segments[2].color, ChartColor::Attention); |
| 84 | } |
| 85 | |
| 86 | #[test] |
| 87 | fn gauge_reads_fraction_format_and_value_key() { |
| 88 | let chart = parse_chart(json!({ |
| 89 | "type": "Chart.Gauge", "value": 35, "valueFormat": "fraction", "min": 10, "showLegend": false, |
| 90 | "segments": [{"legend": "Used", "value": 35}, {"legend": "Unused", "value": 65, "color": "neutral"}] |
| 91 | })); |
| 92 | let ChartKind::Gauge(gauge) = &chart.kind else { |
| 93 | panic!("expected gauge"); |
| 94 | }; |
| 95 | assert_eq!(gauge.format, GaugeValueFormat::Fraction); |
| 96 | assert_eq!(gauge.maximum, 110.); |
| 97 | assert_eq!(gauge.segments[0].size, 35.); |
| 98 | assert_eq!(gauge.segments[1].color, ChartColor::Neutral); |
| 99 | assert!(!chart.show_legend); |
| 100 | } |
| 101 | |
| 102 | #[test] |
| 103 | fn line_parses_series_with_axis_titles() { |
| 104 | let chart = parse_chart(json!({ |
| 105 | "type": "Chart.Line", "title": "Sample", "xAxisTitle": "Days", "yAxisTitle": "Sales", |
| 106 | "colorSet": "categorical", "data": days_series() |
| 107 | })); |
| 108 | assert_eq!(chart.x_axis_title.as_deref(), Some("Days")); |
| 109 | assert_eq!(chart.y_axis_title.as_deref(), Some("Sales")); |
| 110 | let ChartKind::Line(series) = &chart.kind else { |
| 111 | panic!("expected line"); |
| 112 | }; |
| 113 | assert_eq!(series.len(), 2); |
| 114 | assert_eq!(series[1].legend, "Teams"); |
| 115 | assert_eq!(series[1].points[1].value, 100.); |
| 116 | assert_eq!(series[0].points[0].color, series[0].color); |
| 117 | } |
| 118 | |
| 119 | #[test] |
| 120 | fn line_accepts_numeric_x_values() { |
| 121 | let chart = parse_chart(json!({ |
| 122 | "type": "Chart.Line", |
| 123 | "data": [{"legend": "A", "values": [{"x": 1, "y": 2}, {"x": 2.5, "y": 3}]}] |
| 124 | })); |
| 125 | let ChartKind::Line(series) = &chart.kind else { |
| 126 | panic!("expected line"); |
| 127 | }; |
| 128 | assert_eq!(series[0].points[0].label, "1"); |
| 129 | assert_eq!(series[0].points[1].label, "2.5"); |
| 130 | } |
| 131 | |
| 132 | #[test] |
| 133 | fn vertical_bar_parses_points_and_show_values() { |
| 134 | let chart = parse_chart(json!({ |
| 135 | "type": "Chart.VerticalBar", "showBarValues": true, "color": "categoricalPurple", |
| 136 | "data": [{"x": "Pear", "y": 59}, {"x": "Banana", "y": 292}] |
| 137 | })); |
| 138 | assert!(chart.show_values); |
| 139 | let ChartKind::VerticalBar(bars) = &chart.kind else { |
| 140 | panic!("expected vertical bar"); |
| 141 | }; |
| 142 | assert_eq!(bars[1].label, "Banana"); |
| 143 | assert_eq!(bars[1].color, ChartColor::CategoricalPurple); |
| 144 | } |
| 145 | |
| 146 | #[test] |
| 147 | fn grouped_bar_parses_stacked_flag() { |
| 148 | let grouped = parse_chart(json!({"type": "Chart.VerticalBar.Grouped", "data": days_series()})); |
| 149 | let stacked = parse_chart( |
| 150 | json!({"type": "Chart.VerticalBar.Grouped", "stacked": true, "colorSet": "diverging", "data": days_series()}), |
| 151 | ); |
| 152 | assert!(matches!( |
| 153 | grouped.kind, |
| 154 | ChartKind::GroupedBar { stacked: false, .. } |
| 155 | )); |
| 156 | let ChartKind::GroupedBar { |
| 157 | series, |
| 158 | stacked: true, |
| 159 | } = &stacked.kind |
| 160 | else { |
| 161 | panic!("expected stacked grouped bar"); |
| 162 | }; |
| 163 | assert_eq!(series.len(), 2); |
| 164 | assert_eq!(stacked.legend_entries().len(), 2); |
| 165 | } |
| 166 | |
| 167 | #[test] |
| 168 | fn horizontal_bar_parses_display_modes() { |
| 169 | let data = json!([{"x": "Pear", "y": 59}]); |
| 170 | for (name, expected) in [ |
| 171 | ("AbsoluteWithAxis", BarDisplayMode::AbsoluteWithAxis), |
| 172 | ("AbsoluteNoAxis", BarDisplayMode::AbsoluteNoAxis), |
| 173 | ("PartToWhole", BarDisplayMode::PartToWhole), |
| 174 | ] { |
| 175 | let chart = |
| 176 | parse_chart(json!({"type": "Chart.HorizontalBar", "displayMode": name, "data": data})); |
| 177 | let ChartKind::HorizontalBar { mode, .. } = chart.kind else { |
| 178 | panic!("expected horizontal bar"); |
| 179 | }; |
| 180 | assert_eq!(mode, expected); |
| 181 | } |
| 182 | let chart = parse_chart(json!({"type": "Chart.HorizontalBar", "data": data})); |
| 183 | assert!(matches!( |
| 184 | chart.kind, |
| 185 | ChartKind::HorizontalBar { |
| 186 | mode: BarDisplayMode::AbsoluteWithAxis, |
| 187 | .. |
| 188 | } |
| 189 | )); |
| 190 | } |
| 191 | |
| 192 | #[test] |
| 193 | fn stacked_horizontal_bar_parses_rows_and_legend() { |
| 194 | let chart = parse_chart(json!({ |
| 195 | "type": "Chart.HorizontalBar.Stacked", "title": "Sample", |
| 196 | "data": [ |
| 197 | {"title": "Outlook", "data": [ |
| 198 | {"legend": "2023-05-01", "value": 24, "color": "good"}, |
| 199 | {"legend": "2023-05-02", "value": 27, "color": "warning"} |
| 200 | ]}, |
| 201 | {"title": "Teams", "data": [ |
| 202 | {"legend": "2023-05-01", "value": 9, "color": "good"}, |
| 203 | {"legend": "2023-05-02", "value": 100, "color": "warning"} |
| 204 | ]} |
| 205 | ] |
| 206 | })); |
| 207 | let ChartKind::StackedHorizontalBar(rows) = &chart.kind else { |
| 208 | panic!("expected stacked horizontal bar"); |
| 209 | }; |
| 210 | assert_eq!(rows.len(), 2); |
| 211 | assert_eq!(rows[1].legend, "Teams"); |
| 212 | assert_eq!(rows[1].points[1].value, 100.); |
| 213 | assert_eq!(rows[1].points[1].color, ChartColor::Warning); |
| 214 | assert_eq!(chart.legend_entries().len(), 2); |
| 215 | } |
| 216 | |
| 217 | #[test] |
| 218 | fn unknown_chart_type_falls_back_to_nothing() { |
| 219 | let card = json!({"type": "AdaptiveCard", "body": [{"type": "Chart.Radar", "data": []}]}); |
| 220 | assert!(AdaptiveCard::parse(&card.to_string()).is_none()); |
| 221 | } |
| 222 | |
| 223 | #[test] |
| 224 | fn slice_angles_cover_full_circle_in_order() { |
| 225 | let angles = slice_angles(&[59., 292., 143.]); |
| 226 | let (first_start, _) = angles[0]; |
| 227 | let (_, last_end) = angles[2]; |
| 228 | assert!((first_start + std::f32::consts::FRAC_PI_2).abs() < 1e-6); |
| 229 | assert!((last_end - first_start - std::f32::consts::TAU).abs() < 1e-4); |
| 230 | assert_eq!(angles[0].1, angles[1].0); |
| 231 | } |
| 232 | |
| 233 | #[test] |
| 234 | fn slice_angles_ignore_negative_and_empty_totals() { |
| 235 | let angles = slice_angles(&[-5., 10.]); |
| 236 | assert_eq!(angles[0].0, angles[0].1); |
| 237 | assert!((angles[1].1 - angles[1].0 - std::f32::consts::TAU).abs() < 1e-4); |
| 238 | assert!( |
| 239 | slice_angles(&[0., 0.]) |
| 240 | .iter() |
| 241 | .all(|(start, end)| start == end) |
| 242 | ); |
| 243 | } |
| 244 | |
| 245 | #[test] |
| 246 | fn unit_fraction_clamps_gauge_values() { |
| 247 | assert_eq!(unit_fraction(50., 0., 100.), 0.5); |
| 248 | assert_eq!(unit_fraction(-20., 0., 100.), 0.); |
| 249 | assert_eq!(unit_fraction(250., 0., 100.), 1.); |
| 250 | assert_eq!(unit_fraction(60., 20., 100.), 0.5); |
| 251 | assert_eq!(unit_fraction(5., 10., 10.), 0.); |
| 252 | } |
| 253 | |
| 254 | #[test] |
| 255 | fn nice_scale_rounds_up_to_round_steps() { |
| 256 | let scale = nice_scale(292., 4); |
| 257 | assert_eq!(scale.step, 100.); |
| 258 | assert_eq!(scale.maximum, 300.); |
| 259 | assert_eq!(scale.ticks(), vec![0., 100., 200., 300.]); |
| 260 | let small = nice_scale(0.9, 4); |
| 261 | assert!(small.maximum >= 0.9); |
| 262 | assert_eq!(nice_scale(0., 4).maximum, 1.); |
| 263 | assert_eq!(nice_scale(100., 4).maximum, 100.); |
| 264 | } |
| 265 | |
| 266 | #[test] |
| 267 | fn bar_scaling_is_proportional_to_scale_maximum() { |
| 268 | let scale = nice_scale(292., 4); |
| 269 | assert!((unit_fraction(150., 0., scale.maximum) - 0.5).abs() < 1e-6); |
| 270 | assert!(unit_fraction(292., 0., scale.maximum) < 1.); |
| 271 | } |
| 272 | |
| 273 | #[test] |
| 274 | fn label_stride_skips_labels_that_do_not_fit() { |
| 275 | assert_eq!(label_stride(8, 60., 40.), 1); |
| 276 | assert_eq!(label_stride(8, 30., 62.), 3); |
| 277 | assert_eq!(label_stride(0, 30., 62.), 1); |
| 278 | } |
| 279 | |
| 280 | #[test] |
| 281 | fn series_helpers_union_categories_and_stack_totals() { |
| 282 | let chart = parse_chart(json!({"type": "Chart.VerticalBar.Grouped", "data": days_series()})); |
| 283 | let ChartKind::GroupedBar { series, .. } = &chart.kind else { |
| 284 | panic!("expected grouped bar"); |
| 285 | }; |
| 286 | assert_eq!(series_categories(series).len(), 3); |
| 287 | assert_eq!(series_maximum(series, false), 100.); |
| 288 | assert_eq!(series_maximum(series, true), 127.); |
| 289 | } |
| 290 | |
| 291 | #[test] |
| 292 | fn chart_values_format_without_trailing_zeros() { |
| 293 | assert_eq!(format_chart_value(292.), "292"); |
| 294 | assert_eq!(format_chart_value(2.5), "2.5"); |
| 295 | assert_eq!(format_chart_value(0.333333), "0.33"); |
| 296 | } |