use super::test_infrastructure::*;
use reqwest::StatusCode;
#[tokio::test]
async fn test_openapi_json_endpoint() {
let test_server = TestServer::start()
.await
.expect("Failed to start test server");
let client = WebTestClient::for_server(&test_server).expect("Failed to create test client");
let response = client
.get("/api-docs/openapi.json")
.await
.expect("Failed to send request");
assert_eq!(response.status(), StatusCode::OK);
let content_type = response
.headers()
.get("content-type")
.expect("Content-Type header should be present")
.to_str()
.expect("Content-Type should be valid string");
assert!(
content_type.contains("application/json"),
"Expected JSON content-type, got: {content_type}"
);
let openapi_spec: serde_json::Value = response
.json()
.await
.expect("Response should be valid JSON");
assert!(
openapi_spec.get("openapi").is_some(),
"Should have openapi version field"
);
assert!(
openapi_spec.get("info").is_some(),
"Should have info section"
);
assert!(
openapi_spec.get("paths").is_some(),
"Should have paths section"
);
let info = &openapi_spec["info"];
assert_eq!(info["title"], "Tasker Web API");
assert_eq!(info["version"], "1.0.0");
let paths = &openapi_spec["paths"];
assert!(
paths.get("/health").is_some(),
"Should have health endpoint"
);
assert!(
paths.get("/v1/tasks").is_some(),
"Should have tasks endpoint"
);
assert!(
paths.get("/v1/analytics/performance").is_some(),
"Should have analytics endpoint"
);
test_server
.shutdown()
.await
.expect("Failed to shutdown test server");
}
#[tokio::test]
async fn test_swagger_ui_endpoint() {
let test_server = TestServer::start()
.await
.expect("Failed to start test server");
let client = WebTestClient::for_server(&test_server).expect("Failed to create test client");
let response = client
.get("/api-docs/ui")
.await
.expect("Failed to send request");
let status = response.status();
assert!(
status == StatusCode::OK || status.is_redirection(),
"Expected 200 OK or redirection for Swagger UI, got {status}"
);
if status == StatusCode::OK {
let content_type = response
.headers()
.get("content-type")
.map(|ct| ct.to_str().unwrap_or(""))
.unwrap_or("");
assert!(
content_type.contains("text/html") || content_type.contains("application/json"),
"Expected HTML or JSON content for Swagger UI, got: {content_type}"
);
}
test_server
.shutdown()
.await
.expect("Failed to shutdown test server");
}
#[tokio::test]
async fn test_swagger_ui_redirect() {
let test_server = TestServer::start()
.await
.expect("Failed to start test server");
let client = WebTestClient::for_server(&test_server).expect("Failed to create test client");
let response = client
.get("/api-docs/ui/")
.await
.expect("Failed to send request");
let status = response.status();
assert!(
status.is_success() || status.is_redirection(),
"Expected success or redirection for Swagger UI, got {status}"
);
test_server
.shutdown()
.await
.expect("Failed to shutdown test server");
}
#[tokio::test]
async fn test_openapi_contains_expected_operations() {
let test_server = TestServer::start()
.await
.expect("Failed to start test server");
let client = WebTestClient::for_server(&test_server).expect("Failed to create test client");
let response = client
.get("/api-docs/openapi.json")
.await
.expect("Failed to send request");
let openapi_spec: serde_json::Value = response
.json()
.await
.expect("Response should be valid JSON");
let paths = &openapi_spec["paths"];
assert!(
paths.get("/health").is_some(),
"Should have basic health endpoint"
);
assert!(
paths.get("/ready").is_some(),
"Should have readiness probe endpoint"
);
assert!(
paths.get("/live").is_some(),
"Should have liveness probe endpoint"
);
assert!(
paths.get("/v1/tasks").is_some(),
"Should have tasks endpoint"
);
if let Some(tasks_path) = paths.get("/v1/tasks") {
assert!(
tasks_path.get("get").is_some(),
"Should have GET /v1/tasks operation"
);
assert!(
tasks_path.get("post").is_some(),
"Should have POST /v1/tasks operation"
);
}
assert!(
paths.get("/v1/analytics/performance").is_some(),
"Should have performance analytics endpoint"
);
assert!(
paths.get("/v1/analytics/bottlenecks").is_some(),
"Should have bottleneck analytics endpoint"
);
let components = &openapi_spec["components"];
assert!(
components.get("schemas").is_some(),
"Should have schemas section"
);
let schemas = &components["schemas"];
assert!(
schemas.get("TaskRequest").is_some(),
"Should have TaskRequest schema"
);
assert!(
schemas.get("HealthResponse").is_some(),
"Should have HealthResponse schema"
);
assert!(
schemas.get("ApiError").is_some(),
"Should have ApiError schema"
);
test_server
.shutdown()
.await
.expect("Failed to shutdown test server");
}