second-brain-api 0.13.5

HTTP API server for second-brain: REST endpoints for recall, remember, and ingest
Documentation
mod common;

use axum::http::StatusCode;
use chrono::{DateTime, Utc};
use tower::ServiceExt;
use uuid::Uuid;

use common::{app_with_store, json_body, post_json};
use second_brain_core::schema::{Memory, MemoryType};
use second_brain_core::store::Store;

fn at(s: &str) -> DateTime<Utc> {
    s.parse().unwrap()
}

fn raw(content: &str, session_at: Option<&str>) -> Memory {
    let mut m = Memory::new(
        content.to_string(),
        MemoryType::Semantic,
        "claude-code".to_string(),
        "/t.jsonl".to_string(),
    );
    m.session_at = session_at.map(at);
    m
}

async fn post(app: &axum::Router, uri: &str, body: serde_json::Value) -> serde_json::Value {
    let response = app.clone().oneshot(post_json(uri, body)).await.unwrap();
    assert_eq!(response.status(), StatusCode::OK, "{uri}");
    json_body(response).await
}

async fn consolidate(app: &axum::Router, raws: &[&Memory]) -> Uuid {
    let body = post(
        app,
        "/v1/consolidate/store",
        serde_json::json!({
            "raw_ids": raws.iter().map(|m| m.id.to_string()).collect::<Vec<_>>(),
            "source_id": "/t.jsonl",
            "distilled_content": "the distilled fact",
            "model": "qwen",
        }),
    )
    .await;
    Uuid::parse_str(body["distilled_id"].as_str().unwrap()).unwrap()
}

#[tokio::test]
async fn a_distillate_takes_the_latest_session_of_its_raws() {
    let (app, store) = app_with_store();
    let july = raw("raw from july", Some("2026-07-23T09:00:00Z"));
    let august = raw("raw from august", Some("2026-08-01T09:00:00Z"));
    let undated = raw("raw without a date", None);
    for m in [&july, &august, &undated] {
        store.store_memory(m).unwrap();
    }

    let id = consolidate(&app, &[&july, &august, &undated]).await;

    assert_eq!(
        store.get_memory(id).unwrap().unwrap().session_at,
        Some(at("2026-08-01T09:00:00Z"))
    );
}

#[tokio::test]
async fn a_distillate_of_undated_raws_has_no_date() {
    let (app, store) = app_with_store();
    let a = raw("undated one", None);
    let b = raw("undated two", None);
    store.store_memory(&a).unwrap();
    store.store_memory(&b).unwrap();

    let id = consolidate(&app, &[&a, &b]).await;

    assert_eq!(store.get_memory(id).unwrap().unwrap().session_at, None);
}

#[tokio::test]
async fn remember_dates_a_note_by_when_it_was_written() {
    let (app, store) = app_with_store();
    let before = Utc::now();
    let body = post(
        &app,
        "/v1/remember",
        serde_json::json!({"content": "the pi runs the consolidate job"}),
    )
    .await;
    let after = Utc::now();

    let id = Uuid::parse_str(body["id"].as_str().unwrap()).unwrap();
    let session_at = store.get_memory(id).unwrap().unwrap().session_at.unwrap();
    assert!(before <= session_at && session_at <= after);
}

#[tokio::test]
async fn recall_and_context_carry_the_session_date_or_null() {
    let (app, store) = app_with_store();
    let dated = raw("sync sunset dated memory", Some("2026-07-23T09:00:00Z"))
        .with_project_path(Some("sb".to_string()));
    let undated = raw("sync sunset undated memory", None).with_project_path(Some("sb".to_string()));
    store.store_memory(&dated).unwrap();
    store.store_memory(&undated).unwrap();

    let recall = post(
        &app,
        "/v1/recall",
        serde_json::json!({"query": "sync sunset"}),
    )
    .await;
    let context = post(&app, "/v1/context", serde_json::json!({"project": "sb"})).await;

    for body in [&recall, &context] {
        let items = body["memories"].as_array().unwrap();
        assert_eq!(items.len(), 2, "{body}");
        for item in items {
            let expected = if item["id"] == dated.id.to_string() {
                serde_json::json!(at("2026-07-23T09:00:00Z").to_rfc3339())
            } else {
                serde_json::Value::Null
            };
            assert_eq!(item["session_at"], expected, "{item}");
        }
    }
}

#[tokio::test]
async fn the_backfill_route_reports_on_a_dry_run_and_writes_on_a_real_one() {
    let (app, store) = app_with_store();
    let note = Memory::new(
        "saved during a session".to_string(),
        MemoryType::Semantic,
        "mcp-direct".to_string(),
        String::new(),
    );
    store.store_memory(&note).unwrap();

    let dry = post(
        &app,
        "/v1/backfill-session-at",
        serde_json::json!({"dry_run": true}),
    )
    .await;
    assert_eq!(dry["dry_run"], true);
    assert_eq!(dry["all"]["remembered_at"], 1);
    assert_eq!(dry["written"], 0);
    assert_eq!(store.get_memory(note.id).unwrap().unwrap().session_at, None);

    let real = post(&app, "/v1/backfill-session-at", serde_json::json!({})).await;
    assert_eq!(real["written"], 1);
    assert_eq!(
        store.get_memory(note.id).unwrap().unwrap().session_at,
        Some(note.created_at)
    );
}