#[path = "common/harness.rs"]
mod harness;
use harness::TestHarness;
use macrame::prelude::*;
const T1: &str = "2026-01-01T00:00:00.000000Z";
fn wal_bytes(harness: &TestHarness) -> u64 {
let mut wal = harness.db_path.clone().into_os_string();
wal.push("-wal");
std::fs::metadata(std::path::PathBuf::from(wal))
.map(|m| m.len())
.unwrap_or(0)
}
async fn db(harness: &TestHarness) -> Database {
Database::open_tuned(
&harness.db_path,
Tuning {
cadence: CadencePolicy::Disabled,
..Default::default()
},
)
.await
.unwrap()
}
async fn write_some(db: &Database) {
for i in 0..40 {
db.upsert_concept(
ConceptUpsert::new(format!("c{i}"), format!("Concept {i}")).valid_from(T1),
)
.await
.unwrap();
}
}
#[tokio::test]
async fn a_checkpoint_reclaims_the_wal_and_reports_the_frames_it_moved() {
let harness = TestHarness::new();
let db = db(&harness).await;
write_some(&db).await;
let before = wal_bytes(&harness);
assert!(before > 0, "the fixture wrote nothing to the WAL");
let report = db.checkpoint().await.unwrap();
assert!(
!report.busy,
"nothing else holds this database; a busy checkpoint here means the \
write connection is blocking itself"
);
assert!(
report.checkpointed_frames > 0,
"the WAL was {before} bytes and the report claims 0 frames moved — \
the report is not being read from SQLite"
);
assert_eq!(
report.log_frames, 0,
"TRUNCATE leaves no frames behind; a non-zero count means a weaker \
mode is being run than the one documented"
);
assert!(report.is_complete());
assert_eq!(
wal_bytes(&harness),
0,
"the report says truncated and the -wal file disagrees"
);
db.close().await.unwrap();
}
#[tokio::test]
async fn a_second_checkpoint_moves_nothing_and_is_still_ok() {
let harness = TestHarness::new();
let db = db(&harness).await;
write_some(&db).await;
db.checkpoint().await.unwrap();
let second = db.checkpoint().await.unwrap();
assert!(second.is_complete());
assert_eq!(
second.checkpointed_frames, 0,
"there was nothing left to move, so a non-zero count is invented"
);
db.close().await.unwrap();
}
#[tokio::test]
async fn the_rows_written_before_a_checkpoint_are_still_there_after_it() {
let harness = TestHarness::new();
let db = db(&harness).await;
write_some(&db).await;
db.checkpoint().await.unwrap();
let mut rows = db
.read_conn()
.query("SELECT COUNT(*) FROM concepts", ())
.await
.unwrap();
let n: i64 = rows.next().await.unwrap().unwrap().get(0).unwrap();
assert_eq!(n, 40, "a checkpoint lost committed rows");
db.close().await.unwrap();
}
#[cfg(feature = "metrics")]
#[tokio::test]
async fn a_checkpoint_is_counted_as_a_checkpoint_and_exempt_from_the_budget() {
use macrame::metrics::{CommandKind, MetricsSnapshot};
fn turns(snap: &MetricsSnapshot, kind: CommandKind) -> u64 {
snap.kinds.iter().find(|k| k.kind == kind).unwrap().turns
}
assert!(
CommandKind::Checkpoint.exempt_from_budget(),
"a checkpoint's hold is not the caller's to bound"
);
let harness = TestHarness::new();
let db = db(&harness).await;
write_some(&db).await;
let before = turns(&db.metrics(), CommandKind::Checkpoint);
db.checkpoint().await.unwrap();
let after = turns(&db.metrics(), CommandKind::Checkpoint);
assert_eq!(
after,
before + 1,
"the checkpoint turn was attributed to some other kind"
);
db.close().await.unwrap();
}