use std::error::Error;
use super::startup::{FOREIGN_SHARD_COUNT, inject_foreign_config_next_create};
use super::{Database, DatabaseConfig, DatabaseError};
type TestResult = Result<(), Box<dyn Error>>;
fn config(data_dir: &std::path::Path, shard_count: usize) -> DatabaseConfig {
DatabaseConfig {
data_dir: data_dir.to_path_buf(),
shard_count,
distributed: None,
}
}
#[test]
fn ownership_transfer_refusal_never_deletes_the_winners_database() -> TestResult {
let temp = tempfile::tempdir()?;
let data_dir = temp.path().join("db");
assert!(
!data_dir.exists(),
"the directory must be absent so created_dir is true"
);
inject_foreign_config_next_create();
let refused = Database::create(config(&data_dir, 1));
assert!(
matches!(
refused,
Err(DatabaseError::DataDirAlreadyInitialised { .. })
),
"the created-the-dir call must be refused at ensure_uninitialised, got {refused:?}"
);
assert!(
data_dir.exists(),
"F4: the refused creator must NOT delete the winner's directory"
);
let opened = Database::open(&data_dir)?;
assert_eq!(
opened.shard_count(),
FOREIGN_SHARD_COUNT,
"the winner's config must survive byte-identically (foreign shard_count intact)"
);
Ok(())
}