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 tower::ServiceExt;

use std::sync::Arc;

use common::{app_in, app_with_store, get, json_body, post_json, upgrade_from_sync_key};
use second_brain_core::schema::{Memory, MemoryType};
use second_brain_core::sqlite_store::{SCHEMA_VERSION, SqliteStore};
use second_brain_core::store::{Store, StoreAdmin};
use second_brain_sync::crypto::seal_line;
use second_brain_sync::keyring::Keyring;
use second_brain_sync::protocol::SYNC_PROTOCOL_VERSION;
use tempfile::TempDir;

const SHARED_SYNC_KEY: [u8; 32] = [3; 32];

/// A daemon upgraded from the sync.key every test peer shares.
fn keyed(machine_id: &str) -> (axum::Router, Arc<SqliteStore>, TempDir) {
    let dir = tempfile::tempdir().unwrap();
    let store = SqliteStore::in_memory(machine_id.to_string()).unwrap();
    upgrade_from_sync_key(&store, dir.path(), SHARED_SYNC_KEY, &[]);
    let (app, store) = app_in(store, dir.path());
    (app, store, dir)
}

/// `peer_records`, sealed by a peer of the shared identity for `receiver`.
fn peer_lines(receiver: &str) -> Vec<String> {
    let dir = tempfile::tempdir().unwrap();
    let peer = SqliteStore::in_memory("sealing-peer".to_string()).unwrap();
    upgrade_from_sync_key(&peer, dir.path(), SHARED_SYNC_KEY, &[]);
    let sealer = Keyring::load(&peer, dir.path())
        .unwrap()
        .sealer_for(receiver, None)
        .unwrap();
    peer_records()
        .iter()
        .map(|record| seal_line(&sealer, &serde_json::to_vec(record).unwrap()).unwrap())
        .collect()
}

fn peer_records() -> Vec<serde_json::Value> {
    let peer = SqliteStore::in_memory("peer-machine".to_string()).unwrap();
    let mut m = Memory::new(
        "written on the peer".to_string(),
        MemoryType::Semantic,
        "test".to_string(),
        String::new(),
    );
    m.machine_id = "peer-machine".to_string();
    peer.store_memory(&m).unwrap();
    let mut buf = Vec::new();
    second_brain_sync::export::export_changes(&peer, 0, &mut buf).unwrap();
    String::from_utf8(buf)
        .unwrap()
        .lines()
        .map(|l| serde_json::from_str(l).unwrap())
        .collect()
}

#[tokio::test]
async fn version_reports_protocol_and_store_schema() {
    let (app, _) = app_with_store();
    let response = app.oneshot(get("/v1/sync/version")).await.unwrap();
    assert_eq!(response.status(), StatusCode::OK);
    let body = json_body(response).await;
    assert_eq!(body["sync_protocol"], SYNC_PROTOCOL_VERSION);
    assert_eq!(body["schema_version"], SCHEMA_VERSION);
}

#[tokio::test]
async fn export_without_a_protocol_is_refused() {
    let (app, _) = app_with_store();
    let response = app
        .oneshot(post_json(
            "/v1/sync/export",
            serde_json::json!({"after_seq": 0, "limit": 10}),
        ))
        .await
        .unwrap();
    assert_eq!(response.status(), StatusCode::CONFLICT);
    let error = json_body(response).await["error"]
        .as_str()
        .unwrap()
        .to_string();
    assert!(error.contains("protocol 1"), "{error}");
}

#[tokio::test]
async fn export_with_another_protocol_is_refused() {
    let (app, _) = app_with_store();
    let response = app
        .oneshot(post_json(
            "/v1/sync/export",
            serde_json::json!({"after_seq": 0, "limit": 10, "header": {"sync_protocol": SYNC_PROTOCOL_VERSION + 1, "schema_version": SCHEMA_VERSION}}),
        ))
        .await
        .unwrap();
    assert_eq!(response.status(), StatusCode::CONFLICT);
    let error = json_body(response).await["error"]
        .as_str()
        .unwrap()
        .to_string();
    assert!(error.contains("upgrade this daemon"), "{error}");
}

#[tokio::test]
async fn export_with_the_current_protocol_returns_sealed_lines_for_the_named_peer() {
    let (app, store, _dir) = keyed("smoke-machine");
    for record in peer_records() {
        store
            .apply_record(&serde_json::from_value(record).unwrap())
            .unwrap();
    }
    let response = app
        .oneshot(post_json(
            "/v1/sync/export",
            serde_json::json!({"after_seq": 0, "limit": 10, "peer_id": "peer-machine", "header": {"sync_protocol": SYNC_PROTOCOL_VERSION, "schema_version": SCHEMA_VERSION}}),
        ))
        .await
        .unwrap();
    assert_eq!(response.status(), StatusCode::OK);
    let body = json_body(response).await;
    let lines = body["lines"].as_array().unwrap();
    assert!(!lines.is_empty(), "{body}");
    assert!(
        lines
            .iter()
            .all(|l| !l.as_str().unwrap().contains("written on the peer")),
        "{body}"
    );
    assert!(body.get("records").is_none(), "{body}");
}

#[tokio::test]
async fn import_from_an_unversioned_cli_applies_nothing() {
    let (app, store, _dir) = keyed("smoke-machine");
    let response = app
        .oneshot(post_json(
            "/v1/sync/import",
            serde_json::json!({"lines": peer_lines("smoke-machine")}),
        ))
        .await
        .unwrap();
    assert_eq!(response.status(), StatusCode::CONFLICT);
    assert_eq!(store.memory_count().unwrap(), 0);
}

#[tokio::test]
async fn import_with_a_mismatched_protocol_applies_nothing() {
    let (app, store, _dir) = keyed("smoke-machine");
    let response = app
        .oneshot(post_json(
            "/v1/sync/import",
            serde_json::json!({
                "header": {"sync_protocol": SYNC_PROTOCOL_VERSION + 1, "schema_version": SCHEMA_VERSION},
                "lines": peer_lines("smoke-machine"),
            }),
        ))
        .await
        .unwrap();
    assert_eq!(response.status(), StatusCode::CONFLICT);
    assert_eq!(store.memory_count().unwrap(), 0);
}

#[tokio::test]
async fn import_with_a_mismatched_schema_applies_nothing() {
    let (app, store, _dir) = keyed("smoke-machine");
    let response = app
        .oneshot(post_json(
            "/v1/sync/import",
            serde_json::json!({
                "header": {"sync_protocol": SYNC_PROTOCOL_VERSION, "schema_version": SCHEMA_VERSION - 1},
                "lines": peer_lines("smoke-machine"),
            }),
        ))
        .await
        .unwrap();
    assert_eq!(response.status(), StatusCode::CONFLICT);
    let error = json_body(response).await["error"]
        .as_str()
        .unwrap()
        .to_string();
    assert!(
        error.contains(&format!("schema {}", SCHEMA_VERSION - 1)),
        "{error}"
    );
    assert!(error.contains("upgrade the sender"), "{error}");
    assert_eq!(store.memory_count().unwrap(), 0);
}

#[tokio::test]
async fn import_with_a_matching_header_applies_the_records() {
    let (app, store, _dir) = keyed("smoke-machine");
    let response = app
        .oneshot(post_json(
            "/v1/sync/import",
            serde_json::json!({
                "header": {"sync_protocol": SYNC_PROTOCOL_VERSION, "schema_version": SCHEMA_VERSION},
                "lines": peer_lines("smoke-machine"),
            }),
        ))
        .await
        .unwrap();
    assert_eq!(response.status(), StatusCode::OK);
    assert_eq!(json_body(response).await["created"], 1);
    assert_eq!(store.memory_count().unwrap(), 1);
}

#[tokio::test]
async fn a_daemon_older_than_its_database_refuses_to_sync() {
    let dir = tempfile::tempdir().unwrap();
    let path = dir.path().join("future.db");
    drop(SqliteStore::open(&path, "m".to_string()).unwrap());
    {
        let conn = rusqlite::Connection::open(&path).unwrap();
        conn.pragma_update(None, "user_version", SCHEMA_VERSION + 1)
            .unwrap();
    }
    let (app, _) = common::app_with_sqlite(SqliteStore::open(&path, "m".to_string()).unwrap());
    let version = app.clone().oneshot(get("/v1/sync/version")).await.unwrap();
    assert_eq!(version.status(), StatusCode::CONFLICT);
    let response = app
        .oneshot(post_json(
            "/v1/sync/import",
            serde_json::json!({
                "header": {"sync_protocol": SYNC_PROTOCOL_VERSION, "schema_version": SCHEMA_VERSION + 1},
                "lines": peer_lines("smoke-machine"),
            }),
        ))
        .await
        .unwrap();
    assert_eq!(response.status(), StatusCode::CONFLICT);
    let error = json_body(response).await["error"]
        .as_str()
        .unwrap()
        .to_string();
    assert!(error.contains("upgrade this daemon"), "{error}");
}

#[tokio::test]
async fn matching_versions_round_trip_without_shifting_sequence_numbers() {
    let header = serde_json::json!({"sync_protocol": SYNC_PROTOCOL_VERSION, "schema_version": SCHEMA_VERSION});
    let (source, source_store, _source_dir) = keyed("source-machine");
    for record in peer_records() {
        source_store
            .apply_record(&serde_json::from_value(record).unwrap())
            .unwrap();
    }
    let export = json_body(
        source
            .oneshot(post_json(
                "/v1/sync/export",
                serde_json::json!({"after_seq": 0, "limit": 10, "header": header, "peer_id": "target-machine"}),
            ))
            .await
            .unwrap(),
    )
    .await;

    let (target, target_store, _target_dir) = keyed("target-machine");
    let response = target
        .oneshot(post_json(
            "/v1/sync/import",
            serde_json::json!({"header": header, "lines": export["lines"]}),
        ))
        .await
        .unwrap();
    assert_eq!(response.status(), StatusCode::OK);
    let import = json_body(response).await;
    assert_eq!(import["created"], 1);
    assert_eq!(import["max_seq"], export["max_seq"]);
    assert_eq!(target_store.memory_count().unwrap(), 1);
}

#[tokio::test]
async fn export_answers_a_pullers_hello_with_its_own_and_raises_its_clock_to_the_cursor() {
    let header = serde_json::json!({"sync_protocol": SYNC_PROTOCOL_VERSION, "schema_version": SCHEMA_VERSION});
    let (app, store, dir) = keyed("smoke-machine");
    let cursor = store.clock_state().unwrap().last + 1_000;
    let keys = Keyring::load(&store, dir.path())
        .unwrap()
        .handshake_for("puller-machine", None)
        .unwrap();
    let hello = serde_json::json!({
        "machine_id": "puller-machine",
        "wall_micros": chrono::Utc::now().timestamp_micros(),
        "synced": false,
        "cursor": cursor,
        "held_origin_seq": null,
        "keys": {"identity_tag": keys.identity_tag, "epochs": keys.epochs},
    });

    let plain = json_body(
        app.clone()
            .oneshot(post_json(
                "/v1/sync/export",
                serde_json::json!({"after_seq": 0, "limit": 10, "header": header, "peer_id": "puller-machine"}),
            ))
            .await
            .unwrap(),
    )
    .await;
    assert!(plain.get("hello").is_none(), "{plain}");

    let response = app
        .oneshot(post_json(
            "/v1/sync/export",
            serde_json::json!({"after_seq": 0, "limit": 10, "header": header, "hello": hello}),
        ))
        .await
        .unwrap();
    assert_eq!(response.status(), StatusCode::OK);
    let answer = json_body(response).await["hello"].clone();
    assert_eq!(answer["machine_id"], "smoke-machine");
    assert!(answer["wall_micros"].is_i64(), "{answer}");
    assert!(store.clock_state().unwrap().last >= cursor);
}