Files
k-frame/crates/domain/tests/widget_tests.rs

135 lines
3.9 KiB
Rust

use domain::{DisplayHint, DisplayHintKind, KeyMapping, Value, WidgetConfig};
use std::collections::BTreeMap;
#[test]
fn extract_applies_all_mappings_to_produce_widget_state() {
let config = WidgetConfig {
id: 1,
name: "weather".into(),
display_hint: DisplayHint::new(DisplayHintKind::IconValue),
data_source_id: 1,
mappings: vec![
KeyMapping {
source_path: "$.main.temp".into(),
target_key: "temperature".into(),
},
KeyMapping {
source_path: "$.weather[0].icon".into(),
target_key: "icon".into(),
},
],
max_data_size: 2048,
};
let raw = Value::Object(BTreeMap::from([
(
"main".into(),
Value::Object(BTreeMap::from([("temp".into(), Value::Number(5.4))])),
),
(
"weather".into(),
Value::Array(vec![Value::Object(BTreeMap::from([(
"icon".into(),
Value::String("cloud_rain".into()),
)]))]),
),
]));
let state = config.extract(&raw);
assert_eq!(state.data.get("temperature"), Some(&Value::Number(5.4)));
assert_eq!(
state.data.get("icon"),
Some(&Value::String("cloud_rain".into()))
);
assert_eq!(state.error, None);
}
#[test]
fn extract_truncates_string_values_exceeding_max_data_size() {
let long_text = "a".repeat(3000);
let config = WidgetConfig {
id: 1,
name: "news".into(),
display_hint: DisplayHint::new(DisplayHintKind::TextBlock),
data_source_id: 1,
mappings: vec![KeyMapping {
source_path: "$.text".into(),
target_key: "body".into(),
}],
max_data_size: 100,
};
let raw = Value::Object(BTreeMap::from([("text".into(), Value::String(long_text))]));
let state = config.extract(&raw);
match state.data.get("body") {
Some(Value::String(s)) => assert!(s.len() <= 100),
other => panic!("expected truncated string, got {:?}", other),
}
}
#[test]
fn extract_respects_max_data_size_across_total_state() {
let config = WidgetConfig {
id: 1,
name: "big".into(),
display_hint: DisplayHint::new(DisplayHintKind::TextBlock),
data_source_id: 1,
mappings: vec![
KeyMapping {
source_path: "$.a".into(),
target_key: "a".into(),
},
KeyMapping {
source_path: "$.b".into(),
target_key: "b".into(),
},
KeyMapping {
source_path: "$.c".into(),
target_key: "c".into(),
},
],
max_data_size: 50,
};
let raw = Value::Object(BTreeMap::from([
("a".into(), Value::String("x".repeat(20))),
("b".into(), Value::String("y".repeat(20))),
("c".into(), Value::String("z".repeat(20))),
]));
let state = config.extract(&raw);
let total: usize = state.data.values().map(|v| v.estimated_size()).sum();
assert!(total <= 50, "total size {total} exceeds max 50");
}
#[test]
fn extract_skips_mappings_that_dont_match() {
let config = WidgetConfig {
id: 1,
name: "weather".into(),
display_hint: DisplayHint::new(DisplayHintKind::IconValue),
data_source_id: 1,
mappings: vec![
KeyMapping {
source_path: "$.temp".into(),
target_key: "temperature".into(),
},
KeyMapping {
source_path: "$.missing".into(),
target_key: "gone".into(),
},
],
max_data_size: 2048,
};
let raw = Value::Object(BTreeMap::from([("temp".into(), Value::Number(5.4))]));
let state = config.extract(&raw);
assert_eq!(state.data.len(), 1);
assert_eq!(state.data.get("temperature"), Some(&Value::Number(5.4)));
assert_eq!(state.data.get("gone"), None);
}