mod common;
use std::sync::{Arc, RwLock};
use tower::ServiceExt;
use common::{app_with_store, get, json_body, post_json};
use second_brain_api::{AppState, routes};
use second_brain_core::index::Indexes;
use second_brain_core::machine::MachineIdentity;
use second_brain_core::retrieval::RetrievalTunables;
use second_brain_core::schema::{Memory, MemoryType};
use second_brain_core::sqlite_store::SqliteStore;
use second_brain_core::store::Store;
async fn status(app: &axum::Router) -> serde_json::Value {
json_body(app.clone().oneshot(get("/v1/status")).await.unwrap()).await
}
#[tokio::test]
async fn status_counts_live_memories_apart_from_retired_and_superseded() {
let (app, store) = app_with_store();
let mut ids = Vec::new();
for (content, memory_type) in [
("a live fact", MemoryType::Semantic),
("a live decision", MemoryType::Decision),
("a retired fact", MemoryType::Semantic),
("a superseded fact", MemoryType::Semantic),
] {
let m = Memory::new(
content.to_string(),
memory_type,
"api".to_string(),
String::new(),
);
store.store_memory(&m).unwrap();
ids.push(m.id);
}
store.retire_memory(ids[2], "wrong").unwrap();
store.supersede_memory(ids[3], ids[0]).unwrap();
let body = status(&app).await;
assert_eq!(body["memory_count"], 2);
assert_eq!(body["stored_total"], 4);
assert_eq!(body["archived"], 1);
assert_eq!(body["superseded"], 1);
assert_eq!(body["by_type"]["semantic"], 1);
assert_eq!(body["by_type"]["decision"], 1);
assert_eq!(body["index"]["verdict"], "unavailable");
}
struct TempIndexDir(std::path::PathBuf);
impl Drop for TempIndexDir {
fn drop(&mut self) {
std::fs::remove_dir_all(&self.0).ok();
}
}
#[tokio::test]
async fn status_index_verdict_follows_the_outbox_and_the_index_counts() {
let machine = MachineIdentity {
id: "status-machine".to_string(),
name: "status".to_string(),
};
let sqlite = Arc::new(SqliteStore::in_memory(machine.id.clone()).unwrap());
let dir =
TempIndexDir(std::env::temp_dir().join(format!("sb-status-{}", uuid::Uuid::new_v4())));
let mut writer = Indexes::open(&dir.0).unwrap();
writer.persist().unwrap();
let reader = Arc::new(RwLock::new(Indexes::open_readonly(&dir.0).unwrap()));
let state = Arc::new(AppState {
store: sqlite.clone(),
sqlite: sqlite.clone(),
indexes: Some(reader.clone()),
reranker: None,
tunables: RetrievalTunables::default(),
embedder: None,
machine,
maintenance: Arc::new(RwLock::new(None)),
repair_tx: None,
});
let app = routes::router().with_state(state);
assert_eq!(status(&app).await["index"]["verdict"], "consistent");
app.clone()
.oneshot(post_json(
"/v1/remember",
serde_json::json!({"content": "status probe"}),
))
.await
.unwrap();
let body = status(&app).await;
assert_eq!(body["index"]["verdict"], "draining");
assert_eq!(body["index"]["outbox_pending"], 1);
writer.drain(&sqlite).unwrap();
reader.write().unwrap().reload_view().unwrap();
let body = status(&app).await;
assert_eq!(body["index"]["verdict"], "consistent");
assert_eq!(body["index"]["documents"], 1);
}