use serde::{Deserialize, Serialize};
use super::types::HookResult;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct AskQuestionOption {
pub label: String,
pub value: String,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct AskQuestionEntry {
pub question: String,
pub options: Vec<AskQuestionOption>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct AskQuestionInteractionSpec {
pub entries: Vec<AskQuestionEntry>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct QuestionResponse {
pub answers: Vec<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct QuestionHookResult {
pub hook_result: HookResult,
pub response: Option<QuestionResponse>,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn ask_question_option_serde_roundtrip() {
let opt = AskQuestionOption {
label: "Yes".into(),
value: "y".into(),
};
let json = serde_json::to_string(&opt).unwrap();
let parsed: AskQuestionOption = serde_json::from_str(&json).unwrap();
assert_eq!(parsed, opt);
}
#[test]
fn ask_question_entry_serde_roundtrip() {
let entry = AskQuestionEntry {
question: "Continue?".into(),
options: vec![
AskQuestionOption {
label: "Yes".into(),
value: "y".into(),
},
AskQuestionOption {
label: "No".into(),
value: "n".into(),
},
],
};
let json = serde_json::to_string(&entry).unwrap();
let parsed: AskQuestionEntry = serde_json::from_str(&json).unwrap();
assert_eq!(parsed, entry);
}
#[test]
fn ask_question_interaction_spec_serde_roundtrip() {
let spec = AskQuestionInteractionSpec {
entries: vec![
AskQuestionEntry {
question: "Q1?".into(),
options: vec![AskQuestionOption {
label: "A".into(),
value: "a".into(),
}],
},
AskQuestionEntry {
question: "Q2?".into(),
options: vec![],
},
],
};
let json = serde_json::to_string(&spec).unwrap();
let parsed: AskQuestionInteractionSpec = serde_json::from_str(&json).unwrap();
assert_eq!(parsed, spec);
}
#[test]
fn question_response_serde_roundtrip() {
let resp = QuestionResponse {
answers: vec!["yes".into(), "fast".into()],
};
let json = serde_json::to_string(&resp).unwrap();
let parsed: QuestionResponse = serde_json::from_str(&json).unwrap();
assert_eq!(parsed, resp);
}
#[test]
fn question_hook_result_with_response() {
let result = QuestionHookResult {
hook_result: HookResult::allow_with_message("confirmed"),
response: Some(QuestionResponse {
answers: vec!["answer1".into()],
}),
};
let json = serde_json::to_string(&result).unwrap();
let parsed: QuestionHookResult = serde_json::from_str(&json).unwrap();
assert_eq!(parsed.hook_result, result.hook_result);
assert!(parsed.response.is_some());
}
#[test]
fn question_hook_result_without_response() {
let result = QuestionHookResult {
hook_result: HookResult::deny("cancelled"),
response: None,
};
let json = serde_json::to_string(&result).unwrap();
let parsed: QuestionHookResult = serde_json::from_str(&json).unwrap();
assert!(!parsed.hook_result.allow);
assert!(parsed.response.is_none());
}
}