Files
k-frame/crates/application/tests/data_projection_tests.rs
Gabriel Kaszewski 1d7b5324d6 per-source polling, initial client state, webhook, preview, client tracking
- per-source poll intervals: spawn task per source with own interval,
  manager re-checks sources every 30s for add/remove
- initial screen update on TCP connect: send layout + widget states
- client tracking: ClientRegistry port, GET /api/clients, dashboard list
- webhook adapter: POST /api/webhook/{source_id} feeds data into projection
- widget preview: GET /api/widgets/{id}/preview returns current state
- serve SPA from Axum: ServeDir + index.html fallback via KFRAME_SPA_DIR
- layout builder delete confirmation with AlertDialog
- form validation: required fields disable save button
- guide page at /guide
- fix architecture: ClientDto to api-types, ClientRegistry + WidgetStateReader
  ports in domain, DataProjection has internal Mutex, no adapter cross-deps
- ESP32: full screen clear on layout change (stale pixel fix)
2026-06-19 00:42:31 +02:00

106 lines
2.7 KiB
Rust

use application::DataProjection;
use domain::{DisplayHint, KeyMapping, Value, WidgetConfig};
use std::collections::BTreeMap;
fn weather_widget() -> WidgetConfig {
WidgetConfig::new(
1,
"weather".into(),
DisplayHint::IconValue,
10,
vec![
KeyMapping {
source_path: "$.temp".into(),
target_key: "temperature".into(),
},
KeyMapping {
source_path: "$.icon".into(),
target_key: "icon".into(),
},
],
)
}
fn weather_response(temp: f64) -> Value {
Value::Object(BTreeMap::from([
("temp".into(), Value::Number(temp)),
("icon".into(), Value::String("sunny".into())),
]))
}
#[tokio::test]
async fn apply_poll_result_detects_new_widget_state() {
let projection = DataProjection::new();
let widgets = vec![weather_widget()];
let changed = projection
.apply_poll_result(10, &weather_response(5.4), &widgets)
.await;
assert_eq!(changed.len(), 1);
assert_eq!(changed[0].0, 1);
assert_eq!(
changed[0].1.data.get("temperature"),
Some(&Value::Number(5.4))
);
}
#[tokio::test]
async fn apply_poll_result_returns_empty_when_nothing_changed() {
let projection = DataProjection::new();
let widgets = vec![weather_widget()];
projection
.apply_poll_result(10, &weather_response(5.4), &widgets)
.await;
let changed = projection
.apply_poll_result(10, &weather_response(5.4), &widgets)
.await;
assert!(changed.is_empty());
}
#[tokio::test]
async fn apply_poll_result_detects_changed_value() {
let projection = DataProjection::new();
let widgets = vec![weather_widget()];
projection
.apply_poll_result(10, &weather_response(5.4), &widgets)
.await;
let changed = projection
.apply_poll_result(10, &weather_response(6.1), &widgets)
.await;
assert_eq!(changed.len(), 1);
assert_eq!(
changed[0].1.data.get("temperature"),
Some(&Value::Number(6.1))
);
}
#[tokio::test]
async fn apply_poll_result_only_updates_widgets_bound_to_source() {
let projection = DataProjection::new();
let widgets = vec![
weather_widget(),
WidgetConfig::new(
2,
"portfolio".into(),
DisplayHint::KeyValue,
20,
vec![KeyMapping {
source_path: "$.value".into(),
target_key: "amount".into(),
}],
),
];
let changed = projection
.apply_poll_result(10, &weather_response(5.4), &widgets)
.await;
assert_eq!(changed.len(), 1);
assert_eq!(changed[0].0, 1);
}