#[path = "common/harness.rs"]
mod harness;
use harness::TestHarness;
use macrame::prelude::*;
use std::sync::Arc;
const T1: &str = "2026-01-01T00:00:00.000000Z";
fn eager() -> SnapshotCadence {
SnapshotCadence {
every_entries: 1,
poll_interval: std::time::Duration::from_millis(5),
}
}
async fn write_one(db: &Database) {
db.write_concepts(vec![ConceptUpsert::new("a", "A").valid_from(T1)])
.await
.unwrap();
}
fn anchors(harness: &TestHarness) -> usize {
std::fs::read_dir(harness.db_path.with_file_name(format!(
"{}_snapshots",
harness.db_path.file_stem().unwrap().to_str().unwrap()
)))
.map(|d| d.count())
.unwrap_or(0)
}
#[test]
fn the_default_tuning_asks_for_the_default_cadence() {
let tuning = Tuning::default();
assert_eq!(
tuning.cadence,
CadencePolicy::Default,
"Tuning::default() must mean what Database::open() means. If this is \
Disabled, every caller who wrote `..Default::default()` has silently \
stopped writing snapshot anchors."
);
assert!(
tuning.clock.is_none(),
"the default clock is the SystemClock, chosen by absence"
);
}
#[tokio::test]
async fn a_disabled_cadence_leaves_the_snapshot_directory_to_close() {
let harness = TestHarness::new();
let db = Database::open_tuned(
&harness.db_path,
Tuning {
cadence: CadencePolicy::Disabled,
..Default::default()
},
)
.await
.unwrap();
write_one(&db).await;
assert_eq!(anchors(&harness), 0, "nothing but close() writes an anchor");
db.close().await.unwrap();
assert_eq!(anchors(&harness), 1, "close() still writes the final one");
}
#[tokio::test]
async fn an_explicit_cadence_anchors_without_being_closed() {
let harness = TestHarness::new();
let db = Database::open_tuned(
&harness.db_path,
Tuning {
cadence: CadencePolicy::Every(eager()),
..Default::default()
},
)
.await
.unwrap();
for _ in 0..3 {
write_one(&db).await;
}
let mut seen = 0;
for _ in 0..100 {
seen = anchors(&harness);
if seen > 0 {
break;
}
tokio::time::sleep(std::time::Duration::from_millis(20)).await;
}
assert!(seen > 0, "an eager cadence wrote no anchor in two seconds");
db.close().await.unwrap();
}
#[tokio::test]
async fn a_clock_injected_through_tuning_stamps_the_ledger() {
let harness = TestHarness::new();
let t0 = std::time::UNIX_EPOCH + std::time::Duration::from_secs(1_900_000_000);
let clock = Arc::new(FakeClock::new(t0));
let expected = clock.peek();
let db = Database::open_tuned(
&harness.db_path,
Tuning {
cadence: CadencePolicy::Disabled,
clock: Some(clock),
wal_autocheckpoint: WalCheckpointPolicy::Default,
writer_cache_size: None,
reader_cache_size: None,
},
)
.await
.unwrap();
write_one(&db).await;
let mut rows = db
.read_conn()
.query("SELECT recorded_at FROM concepts WHERE id = 'a'", ())
.await
.unwrap();
let stamp: String = rows.next().await.unwrap().unwrap().get(0).unwrap();
assert_eq!(
stamp, expected,
"the injected clock did not reach the actor through Tuning"
);
db.close().await.unwrap();
}
#[tokio::test]
async fn the_legacy_none_still_means_disabled() {
let harness = TestHarness::new();
let db = Database::open_with_cadence(&harness.db_path, None)
.await
.unwrap();
write_one(&db).await;
assert_eq!(
anchors(&harness),
0,
"open_with_cadence(path, None) acquired a cadence it declined"
);
db.close().await.unwrap();
}