use pe_core::node::HumanInput;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
#[non_exhaustive]
pub enum Command {
Resume {
human_input: HumanInput,
},
Goto {
node: String,
},
Update {
update: serde_json::Value,
},
}
impl Command {
pub fn resume(input: HumanInput) -> Self {
Self::Resume { human_input: input }
}
pub fn goto(node: impl Into<String>) -> Self {
Self::Goto { node: node.into() }
}
pub fn update(value: serde_json::Value) -> Self {
Self::Update { update: value }
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_resume_construction() {
let cmd = Command::resume(HumanInput {
approved: true,
feedback: Some("ok".into()),
data: None,
});
match cmd {
Command::Resume { human_input } => {
assert!(human_input.approved);
assert_eq!(human_input.feedback.as_deref(), Some("ok"));
}
_ => panic!("expected Resume"),
}
}
#[test]
fn test_goto_construction() {
let cmd = Command::goto("my_node");
match cmd {
Command::Goto { node } => assert_eq!(node, "my_node"),
_ => panic!("expected Goto"),
}
}
#[test]
fn test_update_construction() {
let cmd = Command::update(serde_json::json!({"key": "value"}));
match cmd {
Command::Update { update } => {
assert_eq!(update["key"], "value");
}
_ => panic!("expected Update"),
}
}
#[test]
fn test_command_serialization_round_trip() {
let cmd = Command::resume(HumanInput {
approved: false,
feedback: None,
data: Some(serde_json::json!(42)),
});
let json = serde_json::to_string(&cmd).unwrap();
let restored: Command = serde_json::from_str(&json).unwrap();
match restored {
Command::Resume { human_input } => {
assert!(!human_input.approved);
assert_eq!(human_input.data, Some(serde_json::json!(42)));
}
_ => panic!("expected Resume"),
}
}
}