use std::collections::BTreeMap;
use std::path::PathBuf;
use fluxframe_core::EffectMetadata;
#[derive(Debug, Default, Clone)]
pub(crate) struct EffectInventory {
pub(crate) sections: BTreeMap<String, Vec<EffectMetadata>>,
#[allow(
dead_code,
reason = "surfaced by Stage 14 Step 7's 'About daemon' dialog"
)]
pub(crate) build_features: Vec<String>,
}
#[allow(
dead_code,
reason = "variants inspected by Step 7 'connection status' indicator"
)]
#[derive(Debug, Clone)]
pub(crate) enum ConnectionStatus {
Connecting,
Connected,
Disconnected(String),
}
#[derive(Debug)]
pub(crate) struct AppState {
pub(crate) socket_path: PathBuf,
pub(crate) status: ConnectionStatus,
pub(crate) presets: Vec<String>,
pub(crate) active_preset: Option<String>,
pub(crate) inventory: EffectInventory,
pub(crate) active_config: serde_json::Value,
pub(crate) baseline_config: serde_json::Value,
pub(crate) config_path: Option<PathBuf>,
}
impl AppState {
#[must_use]
pub(crate) fn new(socket_path: PathBuf) -> Self {
Self {
socket_path,
status: ConnectionStatus::Connecting,
presets: Vec::new(),
active_preset: None,
inventory: EffectInventory::default(),
active_config: serde_json::Value::Null,
baseline_config: serde_json::Value::Null,
config_path: None,
}
}
#[must_use]
pub(crate) fn is_dirty(&self) -> bool {
self.active_config != self.baseline_config
}
pub(crate) fn config_field(
&self,
section: &str,
effect: &str,
field: &str,
) -> &serde_json::Value {
const NULL: serde_json::Value = serde_json::Value::Null;
self.active_config
.get(section)
.and_then(|s| s.get("per_effect"))
.and_then(|p| p.get(effect))
.and_then(|e| e.get(field))
.unwrap_or(&NULL)
}
pub(crate) fn chain_for(&self, section: &str) -> Vec<String> {
self.active_config
.get(section)
.and_then(|s| s.get("chain"))
.and_then(|c| c.as_array())
.map(|arr| {
arr.iter()
.filter_map(|v| v.as_str().map(str::to_string))
.collect()
})
.unwrap_or_default()
}
}
#[cfg(test)]
mod tests {
use super::*;
fn fixture() -> AppState {
let mut s = AppState::new(PathBuf::from("/tmp/test.sock"));
s.active_config = serde_json::json!({
"background": {
"chain": ["blur", "vignette"],
"per_effect": {
"blur": { "radius": 30, "passes": 2 },
"vignette": { "strength": 0.4 }
}
},
"foreground": {
"chain": []
}
});
s
}
#[test]
fn config_field_returns_present_value() {
let s = fixture();
let v = s.config_field("background", "blur", "radius");
assert_eq!(v.as_i64(), Some(30));
}
#[test]
fn config_field_returns_null_for_missing_section() {
let s = fixture();
let v = s.config_field("post", "auto_frame", "threshold");
assert!(v.is_null());
}
#[test]
fn config_field_returns_null_for_missing_per_effect() {
let s = fixture();
let v = s.config_field("background", "unknown_effect", "field");
assert!(v.is_null());
}
#[test]
fn config_field_returns_null_for_missing_field() {
let s = fixture();
let v = s.config_field("background", "blur", "nonexistent");
assert!(v.is_null());
}
#[test]
fn chain_for_returns_names_in_order() {
let s = fixture();
assert_eq!(
s.chain_for("background"),
vec!["blur".to_string(), "vignette".to_string()]
);
}
#[test]
fn chain_for_empty_section_returns_empty_vec() {
let s = fixture();
assert!(s.chain_for("foreground").is_empty());
}
#[test]
fn chain_for_missing_section_returns_empty_vec() {
let s = fixture();
assert!(s.chain_for("nonexistent").is_empty());
}
#[test]
fn chain_for_handles_non_array_chain_gracefully() {
let mut s = AppState::new(PathBuf::from("/tmp/test.sock"));
s.active_config = serde_json::json!({
"mask": { "chain": "not-an-array" }
});
assert!(s.chain_for("mask").is_empty());
}
}