use crate::agents::Agent;
pub struct OhMyPiAgent;
impl Agent for OhMyPiAgent {
fn name(&self) -> &'static str {
"oh-my-pi"
}
fn extract_session_id(&self, stdin_json: &str) -> Option<String> {
let v: serde_json::Value = serde_json::from_str(stdin_json).ok()?;
let id = v.get("session_id")?.as_str()?;
if id.is_empty() {
None
} else {
Some(id.to_string())
}
}
fn extract_message(&self, stdin_json: &str) -> Option<String> {
let v: serde_json::Value = serde_json::from_str(stdin_json).ok()?;
let m = v.get("message")?.as_str()?;
if m.is_empty() {
None
} else {
Some(m.to_string())
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn name_is_oh_my_pi() {
assert_eq!(OhMyPiAgent.name(), "oh-my-pi");
}
#[test]
fn extract_session_id_returns_id() {
let json = r#"{"session_id":"0196fdb1-1111-7000-8000-aaaaaaaaaaaa","other":"stuff"}"#;
assert_eq!(
OhMyPiAgent.extract_session_id(json).as_deref(),
Some("0196fdb1-1111-7000-8000-aaaaaaaaaaaa")
);
}
#[test]
fn extract_session_id_returns_none_for_missing_field() {
assert_eq!(OhMyPiAgent.extract_session_id(r#"{"other":1}"#), None);
}
#[test]
fn extract_session_id_returns_none_for_empty_string() {
assert_eq!(OhMyPiAgent.extract_session_id(r#"{"session_id":""}"#), None);
}
#[test]
fn extract_session_id_returns_none_for_invalid_json() {
assert_eq!(OhMyPiAgent.extract_session_id("not json"), None);
}
#[test]
fn extract_message_returns_string_when_present() {
let json = r#"{"session_id":"x","message":"Running: cargo test"}"#;
assert_eq!(
OhMyPiAgent.extract_message(json).as_deref(),
Some("Running: cargo test")
);
}
#[test]
fn extract_message_returns_none_when_field_missing() {
assert!(OhMyPiAgent.extract_message(r#"{"session_id":"x"}"#).is_none());
}
#[test]
fn extract_message_returns_none_when_empty() {
assert!(OhMyPiAgent.extract_message(r#"{"session_id":"x","message":""}"#).is_none());
}
#[test]
fn extract_message_returns_none_for_non_string_value() {
assert!(OhMyPiAgent.extract_message(r#"{"session_id":"x","message":null}"#).is_none());
}
#[test]
fn extract_message_returns_none_for_invalid_json() {
assert!(OhMyPiAgent.extract_message("not json").is_none());
}
}