use crate::providers::openai_shared::{OpenAIJsonSchema, OpenAIRequest, OpenAIResponseFormat};
use serde_json::json;
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_openai_format_conversion_enables_cross_provider_story_analysis() {
let story_schema = OpenAIJsonSchema {
name: "story_emotional_analysis".to_string(),
schema: json!({
"type": "object",
"properties": {
"emotional_intensity": {
"type": "number",
"minimum": 0,
"maximum": 1,
"description": "Emotional intensity of story content for narrative arc analysis"
},
"primary_emotion": {
"type": "string",
"enum": ["joy", "sadness", "nostalgia", "fear", "anger", "love"],
"description": "Dominant emotion for story theme categorization"
},
"story_elements": {
"type": "object",
"properties": {
"characters": {"type": "array"},
"locations": {"type": "array"}
}
}
},
"required": ["emotional_intensity", "primary_emotion"]
}),
strict: Some(true),
};
let response_format = OpenAIResponseFormat {
format_type: "json_schema".to_string(),
json_schema: Some(story_schema.clone()),
};
assert_eq!(response_format.format_type, "json_schema");
assert_eq!(
response_format.json_schema.unwrap().name,
"story_emotional_analysis"
);
assert_eq!(story_schema.strict, Some(true));
}
#[test]
fn test_openai_request_configures_structured_story_response() {
let story_analysis_schema = OpenAIJsonSchema {
name: "narrative_analysis".to_string(),
schema: json!({
"type": "object",
"properties": {
"themes": {"type": "array", "items": {"type": "string"}},
"emotional_arc": {"type": "string"},
"key_characters": {"type": "array"}
},
"required": ["themes", "emotional_arc"]
}),
strict: Some(true),
};
let response_format = OpenAIResponseFormat {
format_type: "json_schema".to_string(),
json_schema: Some(story_analysis_schema),
};
let request = OpenAIRequest {
model: "gpt-4".to_string(),
messages: vec![],
temperature: Some(0.1), max_tokens: Some(2000),
top_p: None,
presence_penalty: None,
stream: Some(false),
tools: Some(vec![]),
tool_choice: None,
response_format: Some(response_format),
};
assert!(request.response_format.is_some());
let format = request.response_format.unwrap();
assert_eq!(format.format_type, "json_schema");
let schema = format.json_schema.unwrap();
assert_eq!(schema.name, "narrative_analysis");
assert_eq!(request.temperature, Some(0.1));
}
#[test]
fn test_openai_schema_strict_mode_enforces_story_structure() {
let strict_story_schema = OpenAIJsonSchema {
name: "strict_story_analysis".to_string(),
schema: json!({"type": "object", "properties": {"emotion": {"type": "string"}}}),
strict: Some(true),
};
let flexible_story_schema = OpenAIJsonSchema {
name: "flexible_story_analysis".to_string(),
schema: json!({"type": "object", "properties": {"emotion": {"type": "string"}}}),
strict: Some(false),
};
let default_story_schema = OpenAIJsonSchema {
name: "default_story_analysis".to_string(),
schema: json!({"type": "object", "properties": {"emotion": {"type": "string"}}}),
strict: None,
};
assert_eq!(strict_story_schema.strict, Some(true));
assert_eq!(flexible_story_schema.strict, Some(false));
assert_eq!(default_story_schema.strict, None);
assert!(strict_story_schema.schema["properties"]["emotion"].is_object());
assert!(flexible_story_schema.schema["properties"]["emotion"].is_object());
assert!(default_story_schema.schema["properties"]["emotion"].is_object());
}
}