use super::helpers::*;
use crate::provider::{Response, ResponseFormat, TokenUsage};
use serde_json::json;
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_structured_response_captures_story_emotional_analysis() {
let story_analysis_data = json!({
"emotional_intensity": 0.85,
"primary_emotion": "nostalgia",
"themes": ["family", "childhood", "home"],
"story_elements": {
"characters": [{"name": "grandmother", "relationship": "family"}],
"locations": [{"name": "old house", "type": "residential"}]
}
});
let response = Response {
content: "The story shows strong nostalgic themes about family and childhood memories in the old house with grandmother.".to_string(),
structured_response: Some(story_analysis_data.clone()),
tool_calls: vec![],
usage: Some(TokenUsage {
prompt_tokens: 50,
completion_tokens: 30,
total_tokens: 80,
}),
model: Some("test-model".to_string()),
raw_body: None,
};
assert_eq!(
response.structured_response,
Some(story_analysis_data.clone())
);
assert!(response.content.contains("nostalgic"));
let emotional_intensity = story_analysis_data["emotional_intensity"].as_f64().unwrap();
assert!(emotional_intensity >= 0.0 && emotional_intensity <= 1.0);
assert_eq!(story_analysis_data["primary_emotion"], "nostalgia");
assert!(story_analysis_data["themes"].is_array());
}
#[test]
fn test_response_format_defines_narrative_analysis_schema() {
let emotional_analysis_schema = json!({
"type": "object",
"properties": {
"emotional_intensity": {
"type": "number",
"minimum": 0,
"maximum": 1,
"description": "Intensity of emotional content in user's story"
},
"primary_emotion": {
"type": "string",
"enum": ["joy", "sadness", "nostalgia", "fear", "anger", "love"],
"description": "Dominant emotion in narrative segment"
},
"story_themes": {
"type": "array",
"items": {"type": "string"},
"description": "Key themes extracted from user's narrative"
}
},
"required": ["emotional_intensity", "primary_emotion"]
});
let format = ResponseFormat {
name: "story_emotional_analysis".to_string(),
schema: emotional_analysis_schema.clone(),
};
assert_eq!(format.name, "story_emotional_analysis");
assert_eq!(format.schema, emotional_analysis_schema);
assert!(format.schema["properties"]["emotional_intensity"].is_object());
assert!(format.schema["properties"]["primary_emotion"].is_object());
assert_eq!(
format.schema["properties"]["emotional_intensity"]["minimum"],
0
);
assert_eq!(
format.schema["properties"]["emotional_intensity"]["maximum"],
1
);
}
#[test]
fn test_story_schema_supports_character_and_location_extraction() {
let story_schema = create_test_complex_story_schema();
let schema_properties = &story_schema.schema["properties"]["analysis"]["properties"];
assert!(schema_properties["story_elements"]["properties"]["characters"].is_object());
assert!(schema_properties["story_elements"]["properties"]["locations"].is_object());
assert!(
schema_properties["emotional_content"]["properties"]["primary_emotion"].is_object()
);
assert!(schema_properties["emotional_content"]["properties"]["intensity"].is_object());
let required_fields = schema_properties["story_elements"]["required"]
.as_array()
.unwrap();
assert!(required_fields.contains(&json!("characters")));
assert!(required_fields.contains(&json!("locations")));
}
}