#[cfg(test)]
mod tests {
use crate::intelligent_behavior::config::{Persona, PersonasConfig};
use mockforge_openapi::openapi_routes::OpenApiRouteRegistry;
use mockforge_openapi::response::ResponseGenerator;
use mockforge_openapi::spec::OpenApiSpec;
use openapiv3::ReferenceOr;
use serde_json::Value;
use std::collections::HashMap;
use std::sync::Arc;
fn create_test_persona() -> Persona {
let mut traits = HashMap::new();
traits.insert("hive_count".to_string(), "50-100".to_string());
traits.insert("apiary_count".to_string(), "3-5".to_string());
Persona {
name: "test_persona".to_string(),
traits,
}
}
fn create_test_spec() -> OpenApiSpec {
let yaml = r#"
openapi: 3.0.0
info:
title: Test API
version: 1.0.0
paths:
/api/apiaries/{apiaryId}/hives:
get:
operationId: listHives
parameters:
- name: apiaryId
in: path
required: true
schema:
type: string
responses:
'200':
description: List of hives
content:
application/json:
schema:
type: object
properties:
items:
type: array
items:
type: object
properties:
id:
type: string
name:
type: string
total:
type: integer
page:
type: integer
limit:
type: integer
components:
schemas:
Apiary:
type: object
properties:
id:
type: string
name:
type: string
hive_count:
type: integer
example: 75
"#;
OpenApiSpec::from_string(yaml, Some("yaml")).expect("Failed to parse test spec")
}
#[test]
fn test_persona_loading_from_config() {
let mut config = PersonasConfig::default();
let persona = create_test_persona();
config.personas.push(persona.clone());
let active = config.get_active_persona();
assert!(active.is_some(), "Should return first persona when no active specified");
assert_eq!(active.unwrap().name, "test_persona");
config.active_persona = Some("test_persona".to_string());
let active = config.get_active_persona();
assert!(active.is_some(), "Should return active persona when specified");
assert_eq!(active.unwrap().name, "test_persona");
}
#[test]
fn test_persona_numeric_trait_parsing() {
let persona = create_test_persona();
let hive_count = persona.get_numeric_trait("hive_count");
assert_eq!(hive_count, Some(75), "Should parse range and return midpoint");
let apiary_count = persona.get_numeric_trait("apiary_count");
assert_eq!(apiary_count, Some(4), "Should parse range and return midpoint");
assert_eq!(persona.get_numeric_trait("nonexistent"), None);
}
#[test]
fn test_route_generation_with_persona() {
let spec = create_test_spec();
let persona = Arc::new(create_test_persona());
let registry = OpenApiRouteRegistry::new_with_env_and_persona(spec, Some(persona.clone()));
assert!(!registry.routes().is_empty(), "Should generate routes");
let hives_route = registry.routes().iter().find(|r| r.path.contains("hives"));
assert!(hives_route.is_some(), "Should find hives route");
let route = hives_route.unwrap();
assert!(route.persona.is_some(), "Route should have persona attached");
assert_eq!(route.persona.as_ref().unwrap().name, "test_persona");
}
#[test]
fn test_response_generation_with_persona_count_inference() {
let spec = Arc::new(create_test_spec());
let persona = create_test_persona();
let paths = &spec.spec.paths.paths;
let path_item = paths.get("/api/apiaries/{apiaryId}/hives");
assert!(path_item.is_some(), "Should find hives path");
let path_item = path_item.unwrap();
let operation = match path_item {
ReferenceOr::Item(item) => item.get.as_ref(),
_ => None,
};
assert!(operation.is_some(), "Should find GET operation");
let operation = operation.unwrap();
let result = ResponseGenerator::generate_response_with_expansion_and_mode_and_persona(
&spec,
operation,
200,
Some("application/json"),
false,
None,
None,
Some(&persona),
);
assert!(result.is_ok(), "Should generate response successfully");
let response: Value = result.unwrap();
assert!(response.get("items").is_some(), "Response should have items array");
let items = response.get("items").unwrap().as_array();
assert!(items.is_some(), "Items should be an array");
let items_array = items.unwrap();
println!("Generated {} items", items_array.len());
println!("Response: {}", serde_json::to_string_pretty(&response).unwrap());
assert!(
response.get("total").is_some() || !items_array.is_empty(),
"Response should have total or items"
);
}
#[test]
fn test_persona_trait_used_when_no_explicit_total() {
let yaml = r#"
openapi: 3.0.0
info:
title: Test API
version: 1.0.0
paths:
/api/apiaries/{apiaryId}/hives:
get:
operationId: listHives
responses:
'200':
description: List of hives
content:
application/json:
schema:
type: object
properties:
items:
type: array
items:
type: object
properties:
id:
type: string
name:
type: string
components:
schemas:
Apiary:
type: object
properties:
id:
type: string
name:
type: string
hive_count:
type: integer
example: 75
"#;
let spec = Arc::new(OpenApiSpec::from_string(yaml, Some("yaml")).expect("Failed to parse"));
let persona = create_test_persona();
let paths = &spec.spec.paths.paths;
let path_item = paths.get("/api/apiaries/{apiaryId}/hives");
let operation = match path_item {
Some(ReferenceOr::Item(item)) => item.get.as_ref(),
_ => None,
}
.unwrap();
let result = ResponseGenerator::generate_response_with_expansion_and_mode_and_persona(
&spec,
operation,
200,
Some("application/json"),
false,
None,
None,
Some(&persona),
);
assert!(result.is_ok());
let response: Value = result.unwrap();
println!(
"Response without explicit total: {}",
serde_json::to_string_pretty(&response).unwrap()
);
if let Some(items) = response.get("items").and_then(|v| v.as_array()) {
println!("Generated {} items (persona trait: 75)", items.len());
}
}
}