add all crates: domain, protocol, application, client, adapters, ESP32 firmware
Server: domain (entities, value objects, ports), protocol (postcard wire types), application (config service, data projection), adapters (config-memory, tcp-server), bootstrap (composition root with fake data). Client: client-domain (layout engine, render tree, HAL ports), client-application (message handling, repaint commands), adapters (tcp-client, display-terminal), client-desktop (end-to-end working). ESP32: client-esp32 firmware with ILI9341 display over SPI, WiFi networking. Display test verified on hardware — landscape orientation, text rendering works. 60 workspace tests, all passing.
This commit is contained in:
151
crates/application/tests/config_service_tests.rs
Normal file
151
crates/application/tests/config_service_tests.rs
Normal file
@@ -0,0 +1,151 @@
|
||||
mod support;
|
||||
|
||||
use std::time::Duration;
|
||||
use domain::{
|
||||
ConfigRepository, DisplayHint, DomainEvent, KeyMapping, WidgetConfig,
|
||||
DataSource, DataSourceConfig, DataSourceType,
|
||||
Layout, LayoutNode, ContainerNode, LayoutChild, Direction, Sizing,
|
||||
LayoutPreset,
|
||||
};
|
||||
use application::ConfigService;
|
||||
use support::{InMemoryConfigRepository, InMemoryEventPublisher};
|
||||
|
||||
#[tokio::test]
|
||||
async fn create_widget_persists_and_emits_event() {
|
||||
let repo = InMemoryConfigRepository::new();
|
||||
let events = InMemoryEventPublisher::new();
|
||||
let service = ConfigService::new(&repo, &events);
|
||||
|
||||
let config = WidgetConfig::new(
|
||||
1,
|
||||
"weather".into(),
|
||||
DisplayHint::IconValue,
|
||||
1,
|
||||
vec![
|
||||
KeyMapping { source_path: "$.temp".into(), target_key: "temperature".into() },
|
||||
],
|
||||
);
|
||||
|
||||
service.create_widget(config).await.unwrap();
|
||||
|
||||
let stored = repo.get_widget(1).await.unwrap();
|
||||
assert!(stored.is_some());
|
||||
|
||||
let emitted = events.emitted();
|
||||
assert_eq!(emitted.len(), 1);
|
||||
assert!(matches!(emitted[0], DomainEvent::WidgetCreated { id: 1 }));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn create_data_source_rejects_invalid() {
|
||||
let repo = InMemoryConfigRepository::new();
|
||||
let events = InMemoryEventPublisher::new();
|
||||
let service = ConfigService::new(&repo, &events);
|
||||
|
||||
let source = DataSource {
|
||||
id: 1,
|
||||
name: "bad".into(),
|
||||
source_type: DataSourceType::HttpJson,
|
||||
poll_interval: Duration::from_secs(60),
|
||||
config: DataSourceConfig {
|
||||
url: None,
|
||||
headers: vec![],
|
||||
api_key: None,
|
||||
},
|
||||
};
|
||||
|
||||
let result = service.create_data_source(source).await;
|
||||
assert!(result.is_err());
|
||||
assert!(events.emitted().is_empty());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn create_data_source_persists_valid_and_emits_event() {
|
||||
let repo = InMemoryConfigRepository::new();
|
||||
let events = InMemoryEventPublisher::new();
|
||||
let service = ConfigService::new(&repo, &events);
|
||||
|
||||
let source = DataSource {
|
||||
id: 1,
|
||||
name: "weather".into(),
|
||||
source_type: DataSourceType::Weather,
|
||||
poll_interval: Duration::from_secs(300),
|
||||
config: DataSourceConfig {
|
||||
url: Some("https://api.weather.com".into()),
|
||||
headers: vec![],
|
||||
api_key: None,
|
||||
},
|
||||
};
|
||||
|
||||
service.create_data_source(source).await.unwrap();
|
||||
|
||||
let stored = repo.get_data_source(1).await.unwrap();
|
||||
assert!(stored.is_some());
|
||||
|
||||
let emitted = events.emitted();
|
||||
assert_eq!(emitted.len(), 1);
|
||||
assert!(matches!(emitted[0], DomainEvent::DataSourceAdded { id: 1 }));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn update_layout_persists_and_emits_event() {
|
||||
let repo = InMemoryConfigRepository::new();
|
||||
let events = InMemoryEventPublisher::new();
|
||||
let service = ConfigService::new(&repo, &events);
|
||||
|
||||
let layout = Layout {
|
||||
root: LayoutNode::Container(ContainerNode {
|
||||
direction: Direction::Row,
|
||||
gap: 4,
|
||||
padding: 2,
|
||||
children: vec![
|
||||
LayoutChild { sizing: Sizing::Flex(1), node: LayoutNode::Leaf(1) },
|
||||
LayoutChild { sizing: Sizing::Flex(1), node: LayoutNode::Leaf(2) },
|
||||
],
|
||||
}),
|
||||
};
|
||||
|
||||
service.update_layout(layout.clone()).await.unwrap();
|
||||
|
||||
let stored = repo.get_layout().await.unwrap();
|
||||
assert_eq!(stored, Some(layout));
|
||||
|
||||
assert_eq!(events.emitted().len(), 1);
|
||||
assert!(matches!(events.emitted()[0], DomainEvent::LayoutChanged { .. }));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn load_preset_replaces_active_layout() {
|
||||
let repo = InMemoryConfigRepository::new();
|
||||
let events = InMemoryEventPublisher::new();
|
||||
let service = ConfigService::new(&repo, &events);
|
||||
|
||||
let preset_layout = Layout {
|
||||
root: LayoutNode::Container(ContainerNode {
|
||||
direction: Direction::Column,
|
||||
gap: 0,
|
||||
padding: 0,
|
||||
children: vec![
|
||||
LayoutChild { sizing: Sizing::Flex(1), node: LayoutNode::Leaf(5) },
|
||||
],
|
||||
}),
|
||||
};
|
||||
|
||||
let preset = LayoutPreset {
|
||||
id: 1,
|
||||
name: "vertical".into(),
|
||||
layout: preset_layout.clone(),
|
||||
};
|
||||
|
||||
repo.save_preset(&preset).await.unwrap();
|
||||
|
||||
service.load_preset(1).await.unwrap();
|
||||
|
||||
let stored = repo.get_layout().await.unwrap();
|
||||
assert_eq!(stored, Some(preset_layout));
|
||||
|
||||
let emitted = events.emitted();
|
||||
assert_eq!(emitted.len(), 2);
|
||||
assert!(matches!(emitted[0], DomainEvent::LayoutPresetLoaded { id: 1 }));
|
||||
assert!(matches!(emitted[1], DomainEvent::LayoutChanged { .. }));
|
||||
}
|
||||
82
crates/application/tests/data_projection_tests.rs
Normal file
82
crates/application/tests/data_projection_tests.rs
Normal file
@@ -0,0 +1,82 @@
|
||||
use std::collections::BTreeMap;
|
||||
use domain::{
|
||||
DisplayHint, KeyMapping, Value, WidgetConfig, WidgetId, WidgetState,
|
||||
};
|
||||
use application::DataProjection;
|
||||
|
||||
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())),
|
||||
]))
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn apply_poll_result_detects_new_widget_state() {
|
||||
let mut projection = DataProjection::new();
|
||||
let widgets = vec![weather_widget()];
|
||||
|
||||
let changed = projection.apply_poll_result(10, &weather_response(5.4), &widgets);
|
||||
|
||||
assert_eq!(changed.len(), 1);
|
||||
assert_eq!(changed[0].0, 1);
|
||||
assert_eq!(changed[0].1.data.get("temperature"), Some(&Value::Number(5.4)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn apply_poll_result_returns_empty_when_nothing_changed() {
|
||||
let mut projection = DataProjection::new();
|
||||
let widgets = vec![weather_widget()];
|
||||
|
||||
projection.apply_poll_result(10, &weather_response(5.4), &widgets);
|
||||
let changed = projection.apply_poll_result(10, &weather_response(5.4), &widgets);
|
||||
|
||||
assert!(changed.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn apply_poll_result_detects_changed_value() {
|
||||
let mut projection = DataProjection::new();
|
||||
let widgets = vec![weather_widget()];
|
||||
|
||||
projection.apply_poll_result(10, &weather_response(5.4), &widgets);
|
||||
let changed = projection.apply_poll_result(10, &weather_response(6.1), &widgets);
|
||||
|
||||
assert_eq!(changed.len(), 1);
|
||||
assert_eq!(changed[0].1.data.get("temperature"), Some(&Value::Number(6.1)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn apply_poll_result_only_updates_widgets_bound_to_source() {
|
||||
let mut 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);
|
||||
|
||||
assert_eq!(changed.len(), 1);
|
||||
assert_eq!(changed[0].0, 1);
|
||||
}
|
||||
126
crates/application/tests/support/mod.rs
Normal file
126
crates/application/tests/support/mod.rs
Normal file
@@ -0,0 +1,126 @@
|
||||
use std::cell::RefCell;
|
||||
use std::collections::HashMap;
|
||||
use domain::{
|
||||
ConfigRepository, EventPublisher,
|
||||
DataSource, DataSourceId, Layout, LayoutPreset, LayoutPresetId,
|
||||
WidgetConfig, WidgetId, DomainEvent,
|
||||
};
|
||||
|
||||
pub struct InMemoryConfigRepository {
|
||||
pub widgets: RefCell<HashMap<WidgetId, WidgetConfig>>,
|
||||
pub data_sources: RefCell<HashMap<DataSourceId, DataSource>>,
|
||||
pub layout: RefCell<Option<Layout>>,
|
||||
pub presets: RefCell<HashMap<LayoutPresetId, LayoutPreset>>,
|
||||
}
|
||||
|
||||
impl InMemoryConfigRepository {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
widgets: RefCell::new(HashMap::new()),
|
||||
data_sources: RefCell::new(HashMap::new()),
|
||||
layout: RefCell::new(None),
|
||||
presets: RefCell::new(HashMap::new()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Never;
|
||||
|
||||
impl std::fmt::Display for Never {
|
||||
fn fmt(&self, _: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
unreachable!()
|
||||
}
|
||||
}
|
||||
|
||||
impl ConfigRepository for InMemoryConfigRepository {
|
||||
type Error = Never;
|
||||
|
||||
async fn get_widget(&self, id: WidgetId) -> Result<Option<WidgetConfig>, Self::Error> {
|
||||
Ok(self.widgets.borrow().get(&id).cloned())
|
||||
}
|
||||
|
||||
async fn list_widgets(&self) -> Result<Vec<WidgetConfig>, Self::Error> {
|
||||
Ok(self.widgets.borrow().values().cloned().collect())
|
||||
}
|
||||
|
||||
async fn save_widget(&self, config: &WidgetConfig) -> Result<(), Self::Error> {
|
||||
self.widgets.borrow_mut().insert(config.id, config.clone());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn delete_widget(&self, id: WidgetId) -> Result<(), Self::Error> {
|
||||
self.widgets.borrow_mut().remove(&id);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn get_data_source(&self, id: DataSourceId) -> Result<Option<DataSource>, Self::Error> {
|
||||
Ok(self.data_sources.borrow().get(&id).cloned())
|
||||
}
|
||||
|
||||
async fn list_data_sources(&self) -> Result<Vec<DataSource>, Self::Error> {
|
||||
Ok(self.data_sources.borrow().values().cloned().collect())
|
||||
}
|
||||
|
||||
async fn save_data_source(&self, source: &DataSource) -> Result<(), Self::Error> {
|
||||
self.data_sources.borrow_mut().insert(source.id, source.clone());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn delete_data_source(&self, id: DataSourceId) -> Result<(), Self::Error> {
|
||||
self.data_sources.borrow_mut().remove(&id);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn get_layout(&self) -> Result<Option<Layout>, Self::Error> {
|
||||
Ok(self.layout.borrow().clone())
|
||||
}
|
||||
|
||||
async fn save_layout(&self, layout: &Layout) -> Result<(), Self::Error> {
|
||||
*self.layout.borrow_mut() = Some(layout.clone());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn get_preset(&self, id: LayoutPresetId) -> Result<Option<LayoutPreset>, Self::Error> {
|
||||
Ok(self.presets.borrow().get(&id).cloned())
|
||||
}
|
||||
|
||||
async fn list_presets(&self) -> Result<Vec<LayoutPreset>, Self::Error> {
|
||||
Ok(self.presets.borrow().values().cloned().collect())
|
||||
}
|
||||
|
||||
async fn save_preset(&self, preset: &LayoutPreset) -> Result<(), Self::Error> {
|
||||
self.presets.borrow_mut().insert(preset.id, preset.clone());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn delete_preset(&self, id: LayoutPresetId) -> Result<(), Self::Error> {
|
||||
self.presets.borrow_mut().remove(&id);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
pub struct InMemoryEventPublisher {
|
||||
pub events: RefCell<Vec<DomainEvent>>,
|
||||
}
|
||||
|
||||
impl InMemoryEventPublisher {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
events: RefCell::new(Vec::new()),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn emitted(&self) -> Vec<DomainEvent> {
|
||||
self.events.borrow().clone()
|
||||
}
|
||||
}
|
||||
|
||||
impl EventPublisher for InMemoryEventPublisher {
|
||||
type Error = Never;
|
||||
|
||||
async fn publish(&self, event: DomainEvent) -> Result<(), Self::Error> {
|
||||
self.events.borrow_mut().push(event);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user