mod common;
use axum::http::StatusCode;
use tower::ServiceExt;
use common::{app_with_store, get, json_body, post_json};
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::protocol::SYNC_PROTOCOL_VERSION;
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_records() {
let (app, store) = app_with_store();
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, "header": {"sync_protocol": SYNC_PROTOCOL_VERSION, "schema_version": SCHEMA_VERSION}}),
))
.await
.unwrap();
assert_eq!(response.status(), StatusCode::OK);
assert!(
!json_body(response).await["records"]
.as_array()
.unwrap()
.is_empty()
);
}
#[tokio::test]
async fn import_from_an_unversioned_cli_applies_nothing() {
let (app, store) = app_with_store();
let response = app
.oneshot(post_json(
"/v1/sync/import",
serde_json::json!({"records": peer_records()}),
))
.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) = app_with_store();
let response = app
.oneshot(post_json(
"/v1/sync/import",
serde_json::json!({
"header": {"sync_protocol": SYNC_PROTOCOL_VERSION + 1, "schema_version": SCHEMA_VERSION},
"records": peer_records(),
}),
))
.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) = app_with_store();
let response = app
.oneshot(post_json(
"/v1/sync/import",
serde_json::json!({
"header": {"sync_protocol": SYNC_PROTOCOL_VERSION, "schema_version": SCHEMA_VERSION - 1},
"records": peer_records(),
}),
))
.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) = app_with_store();
let response = app
.oneshot(post_json(
"/v1/sync/import",
serde_json::json!({
"header": {"sync_protocol": SYNC_PROTOCOL_VERSION, "schema_version": SCHEMA_VERSION},
"records": peer_records(),
}),
))
.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},
"records": peer_records(),
}),
))
.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) = app_with_store();
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}),
))
.await
.unwrap(),
)
.await;
let (target, target_store) = app_with_store();
let response = target
.oneshot(post_json(
"/v1/sync/import",
serde_json::json!({"header": header, "records": export["records"]}),
))
.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);
}