#![allow(
clippy::unwrap_used,
clippy::expect_used,
reason = "test code — panics are acceptable failures"
)]
mod support;
use axum::{body::to_bytes, http::StatusCode, response::IntoResponse};
use cognee_http_server::error::{ApiError, PipelineErrorSource};
use uuid::Uuid;
#[tokio::test]
async fn improve_pipeline_errored_returns_420_with_raw_run_info() {
let run_id = Uuid::new_v4();
let dataset_id = Uuid::new_v4();
let run_info_dto = serde_json::json!({
"status": "PipelineRunErrored",
"pipeline_run_id": run_id,
"dataset_id": dataset_id,
"dataset_name": "test_improve_dataset",
"error": "simulated improve failure"
});
let resp = ApiError::PipelineErrored {
pipeline_source: PipelineErrorSource::Improve,
run_info: run_info_dto.clone(),
}
.into_response();
assert_eq!(
resp.status().as_u16(),
420,
"PipelineRunErrored from /improve must return HTTP 420, not 500"
);
let body_bytes = to_bytes(resp.into_body(), usize::MAX)
.await
.expect("body bytes");
let body: serde_json::Value = serde_json::from_slice(&body_bytes).expect("body is valid JSON");
assert_eq!(
body["status"], "PipelineRunErrored",
"body.status must be 'PipelineRunErrored'"
);
assert_eq!(
body["error"], "simulated improve failure",
"body.error must carry the pipeline error message"
);
assert_eq!(
body["pipeline_run_id"].as_str().unwrap(),
run_id.to_string().as_str(),
"body must include pipeline_run_id"
);
assert_ne!(
body.get("error").and_then(|e| e.as_str()),
Some("Pipeline run errored"),
"body must NOT be the canonical error envelope"
);
assert!(
body.get("detail").is_none(),
"body must NOT have a 'detail' field (not the canonical envelope)"
);
}
#[tokio::test]
async fn cognify_pipeline_errored_returns_500_with_canonical_envelope() {
let resp = ApiError::PipelineErrored {
pipeline_source: PipelineErrorSource::Cognify,
run_info: serde_json::json!({
"error": "Pipeline run errored",
"detail": "cognify internal error"
}),
}
.into_response();
assert_eq!(resp.status(), StatusCode::INTERNAL_SERVER_ERROR);
let body_bytes = to_bytes(resp.into_body(), usize::MAX)
.await
.expect("body bytes");
let body: serde_json::Value = serde_json::from_slice(&body_bytes).expect("json");
assert_eq!(body["error"], "Pipeline run errored");
assert_eq!(body["detail"], "cognify internal error");
}