mod common;
use axum::http::StatusCode;
use tower::ServiceExt;
use uuid::Uuid;
use common::{app_with_store, json_body, post_json};
use second_brain_core::schema::RelationType;
use second_brain_core::store::Store;
async fn remember(app: &axum::Router, body: serde_json::Value) -> serde_json::Value {
let response = app
.clone()
.oneshot(post_json("/v1/remember", body))
.await
.unwrap();
assert_eq!(response.status(), StatusCode::OK);
json_body(response).await
}
fn id_of(body: &serde_json::Value) -> Uuid {
Uuid::parse_str(body["id"].as_str().unwrap()).unwrap()
}
#[tokio::test]
async fn a_duplicate_with_a_project_is_refiled_under_it() {
let (app, store) = app_with_store();
let first = remember(
&app,
serde_json::json!({"content": "deploys go through argo"}),
)
.await;
let again = remember(
&app,
serde_json::json!({"content": "Deploys go through Argo", "project": "infrastructure"}),
)
.await;
assert_eq!(again["duplicate"], true);
assert_eq!(again["id"], first["id"]);
assert_eq!(again["project_updated"], true);
let stored = store.get_memory(id_of(&first)).unwrap().unwrap();
assert_eq!(stored.project_path.as_deref(), Some("infrastructure"));
let same = remember(
&app,
serde_json::json!({"content": "deploys go through argo", "project": "infrastructure"}),
)
.await;
assert_eq!(same["project_updated"], false);
}
#[tokio::test]
async fn a_duplicate_without_a_project_keeps_the_stored_one() {
let (app, store) = app_with_store();
let first = remember(
&app,
serde_json::json!({"content": "the nas runs tos 6", "project": "haurka"}),
)
.await;
let again = remember(&app, serde_json::json!({"content": "the nas runs tos 6"})).await;
assert_eq!(again["duplicate"], true);
assert_eq!(again["project_updated"], false);
let stored = store.get_memory(id_of(&first)).unwrap().unwrap();
assert_eq!(stored.project_path.as_deref(), Some("haurka"));
}
#[tokio::test]
async fn remember_links_extracted_and_named_entities() {
let (app, store) = app_with_store();
let body = remember(
&app,
serde_json::json!({
"content": "billing moved from postgres to sqlite",
"related_to": ["billing-v2"],
}),
)
.await;
let id = id_of(&body);
assert_eq!(body["entities"], 3);
let mentioned: Vec<String> = store
.get_relations(id, Some(RelationType::Mentions))
.unwrap()
.into_iter()
.map(|r| store.get_entity(r.to_id).unwrap().unwrap().name)
.collect();
for name in ["postgres", "sqlite", "billing-v2"] {
assert!(mentioned.iter().any(|n| n == name), "{name}: {mentioned:?}");
}
let second = remember(
&app,
serde_json::json!({"content": "sqlite needs WAL mode", "related_to": ["billing-v2"]}),
)
.await;
let reused = store.find_entity_by_name("billing-v2").unwrap().unwrap();
assert!(
store
.get_relations(id_of(&second), Some(RelationType::Mentions))
.unwrap()
.iter()
.any(|r| r.to_id == reused.id),
"a named entity that already exists is reused, not duplicated"
);
}
#[tokio::test]
async fn remember_only_accepts_sources_consolidation_leaves_alone() {
let (app, store) = app_with_store();
let cli = remember(
&app,
serde_json::json!({"content": "noted from the terminal", "source": "cli"}),
)
.await;
assert_eq!(
store.get_memory(id_of(&cli)).unwrap().unwrap().source,
"cli"
);
let response = app
.clone()
.oneshot(post_json(
"/v1/remember",
serde_json::json!({"content": "posing as a transcript", "source": "claude-code"}),
))
.await
.unwrap();
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
let body = json_body(response).await;
assert!(
body["error"].as_str().unwrap().contains("mcp-direct"),
"{body}"
);
}