#![allow(
clippy::unwrap_used,
clippy::expect_used,
reason = "test code — panics are acceptable failures"
)]
mod support;
use axum::{body::Body, http::Request};
use cognee_database::IngestDb;
use cognee_graph::GraphDBTrait;
use cognee_graph::mock::MockGraphDB;
use std::sync::Arc;
use tower::ServiceExt;
use support::{body_json, build_p4_state};
#[tokio::test]
async fn missing_dataset_id_returns_422_or_400() {
let graph: Arc<dyn GraphDBTrait> = Arc::new(MockGraphDB::new());
let state = build_p4_state(None, None, Some(graph)).await;
let app = cognee_http_server::build_router(state)
.await
.expect("router");
let req = Request::builder()
.method("GET")
.uri("/api/v1/visualize")
.body(Body::empty())
.unwrap();
let resp = app.oneshot(req).await.expect("resp");
let status = resp.status().as_u16();
assert!(status == 400 || status == 422, "got {status}");
}
#[tokio::test]
async fn unknown_dataset_id_collapses_to_409() {
let graph: Arc<dyn GraphDBTrait> = Arc::new(MockGraphDB::new());
let state = build_p4_state(None, None, Some(graph)).await;
let app = cognee_http_server::build_router(state)
.await
.expect("router");
let req = Request::builder()
.method("GET")
.uri(format!(
"/api/v1/visualize?dataset_id={}",
uuid::Uuid::new_v4()
))
.body(Body::empty())
.unwrap();
let resp = app.oneshot(req).await.expect("resp");
assert_eq!(resp.status(), 409);
let body = body_json(resp).await;
assert!(body["error"].is_string());
assert!(body.get("detail").is_none());
}
#[tokio::test]
async fn happy_path_returns_html_content_type() {
use cognee_models::Dataset;
use uuid::Uuid;
let default_user_id = Uuid::new_v5(&Uuid::NAMESPACE_OID, "default_user@example.com".as_bytes());
let dataset_id = Uuid::new_v4();
let graph_db = MockGraphDB::new();
graph_db
.add_node_raw(serde_json::json!({"id": "n1", "type": "Entity"}))
.await
.expect("seed node");
let graph: Arc<dyn GraphDBTrait> = Arc::new(graph_db);
let state = build_p4_state(None, None, Some(graph)).await;
let components = state.components().expect("components wired");
let dataset = Dataset::new(
"test-dataset".to_string(),
default_user_id,
None,
dataset_id,
);
IngestDb::create_dataset(components.database.as_ref(), dataset)
.await
.expect("create dataset");
let app = cognee_http_server::build_router(state)
.await
.expect("router");
let req = Request::builder()
.method("GET")
.uri(format!("/api/v1/visualize?dataset_id={dataset_id}"))
.body(Body::empty())
.unwrap();
let resp = app.oneshot(req).await.expect("resp");
assert_eq!(
resp.status(),
200,
"expected 200 for authorized read, got {}",
resp.status()
);
let ct = resp
.headers()
.get("content-type")
.and_then(|v| v.to_str().ok())
.unwrap_or("");
assert!(
ct.starts_with("text/html"),
"expected text/html content-type, got {ct:?}"
);
}
#[ignore = "F10: pre-existing stale assertion since F2 require_authentication=false default"]
#[tokio::test]
async fn dataset_belonging_to_other_user_collapses_to_409() {
use cognee_models::Dataset;
use uuid::Uuid;
let other_user_id = Uuid::new_v4();
let dataset_id = Uuid::new_v4();
let graph: Arc<dyn GraphDBTrait> = Arc::new(MockGraphDB::new());
let state = build_p4_state(None, None, Some(graph)).await;
let components = state.components().expect("components");
let dataset = Dataset::new(
"other-user-dataset".to_string(),
other_user_id,
None,
dataset_id,
);
IngestDb::create_dataset(components.database.as_ref(), dataset)
.await
.expect("create dataset");
let app = cognee_http_server::build_router(state)
.await
.expect("router");
let req = Request::builder()
.method("GET")
.uri(format!("/api/v1/visualize?dataset_id={dataset_id}"))
.body(Body::empty())
.unwrap();
let resp = app.oneshot(req).await.expect("resp");
assert_eq!(resp.status(), 409);
let body = body_json(resp).await;
assert!(body["error"].is_string());
assert!(body.get("detail").is_none());
}