use forge_engine::ForgeStore;
use tempfile::TempDir;
#[test]
fn a1_refuse_to_open_non_forge_db() {
let dir = TempDir::new().unwrap();
let db_path = dir.path().join("empty.db");
let conn = rusqlite::Connection::open(&db_path).unwrap();
conn.execute_batch("SELECT 1;").unwrap();
drop(conn);
let result = ForgeStore::open(&db_path);
assert!(result.is_err());
let err = result.unwrap_err();
let msg = format!("{err}");
assert!(
msg.contains("refuse to open"),
"Expected RefuseToOpenDb, got: {msg}"
);
}
#[test]
fn a2_refuse_to_open_missing_forge_meta() {
let dir = TempDir::new().unwrap();
let db_path = dir.path().join("no_meta.db");
let conn = rusqlite::Connection::open(&db_path).unwrap();
conn.execute_batch(
"PRAGMA user_version = 1;
CREATE TABLE users (id TEXT PRIMARY KEY, name TEXT);",
)
.unwrap();
drop(conn);
let result = ForgeStore::open(&db_path);
assert!(result.is_err());
let msg = format!("{}", result.unwrap_err());
assert!(
msg.contains("forge_meta") || msg.contains("refuse to open"),
"Expected forge_meta error, got: {msg}"
);
}
#[test]
fn a3_refuse_to_open_wrong_schema_hash() {
let dir = TempDir::new().unwrap();
let db_path = dir.path().join("wrong_hash.db");
let conn = rusqlite::Connection::open(&db_path).unwrap();
conn.execute_batch(
"PRAGMA user_version = 1;
CREATE TABLE forge_meta (key TEXT PRIMARY KEY, value TEXT NOT NULL);
INSERT INTO forge_meta VALUES ('schema_hash', 'wrong');
INSERT INTO forge_meta VALUES ('schema_version', '1');",
)
.unwrap();
drop(conn);
let result = ForgeStore::open(&db_path);
assert!(result.is_err());
let msg = format!("{}", result.unwrap_err());
assert!(
msg.contains("schema_hash") || msg.contains("refuse to open"),
"Expected schema_hash mismatch, got: {msg}"
);
}
#[test]
fn a4_refuse_to_open_semantic_memory_signature() {
let dir = TempDir::new().unwrap();
let db_path = dir.path().join("memory.db");
let conn = rusqlite::Connection::open(&db_path).unwrap();
conn.execute_batch(
"PRAGMA user_version = 1;
CREATE TABLE memory_entries (id TEXT PRIMARY KEY, content TEXT);",
)
.unwrap();
drop(conn);
let result = ForgeStore::open(&db_path);
assert!(result.is_err());
let msg = format!("{}", result.unwrap_err());
assert!(
msg.contains("refuse to open"),
"Expected RefuseToOpenDb, got: {msg}"
);
}
#[test]
fn a5_forge_writes_only_to_forge_db() {
let dir = TempDir::new().unwrap();
let db_path = dir.path().join("forge.db");
let store = ForgeStore::open(&db_path).unwrap();
assert!(db_path.exists());
store
.insert_candidate("test-candidate", "{}", "[]", "active")
.unwrap();
assert!(!dir.path().join("memory.db").exists());
let files: Vec<_> = std::fs::read_dir(dir.path())
.unwrap()
.filter_map(|e| e.ok())
.map(|e| e.file_name().to_string_lossy().to_string())
.collect();
for file in &files {
assert!(
file.starts_with("forge.db"),
"Unexpected file created: {file}"
);
}
}
#[test]
fn a6_refuse_to_open_reads_only_magic_bytes() {
let dir = TempDir::new().unwrap();
let db_path = dir.path().join("big_file.db");
let content = vec![b'X'; 1_000_000];
std::fs::write(&db_path, &content).unwrap();
let result = ForgeStore::open(&db_path);
assert!(result.is_err());
let msg = format!("{}", result.unwrap_err());
assert!(
msg.contains("not a valid SQLite database"),
"Expected magic bytes check failure, got: {msg}"
);
}
#[test]
fn transaction_commit_and_rollback() {
let dir = TempDir::new().unwrap();
let db_path = dir.path().join("forge.db");
let store = ForgeStore::open(&db_path).unwrap();
store
.with_transaction(|conn| {
conn.execute(
"INSERT INTO candidates (candidate_id, spec_json, parents_json, created_at, status) VALUES ('tx-ok', '{}', '[]', '2024-01-01', 'active')",
[],
)?;
Ok(())
})
.unwrap();
let spec = store.get_candidate_spec("tx-ok").unwrap();
assert_eq!(spec, "{}");
let result: Result<(), _> = store.with_transaction(|conn| {
conn.execute(
"INSERT INTO candidates (candidate_id, spec_json, parents_json, created_at, status) VALUES ('tx-fail', '{}', '[]', '2024-01-01', 'active')",
[],
)?;
Err(forge_engine::ForgeError::Other("rollback".into()))
});
assert!(result.is_err());
let not_found = store.get_candidate_spec("tx-fail");
assert!(not_found.is_err());
}
#[test]
fn forge_db_round_trip() {
let dir = TempDir::new().unwrap();
let db_path = dir.path().join("forge.db");
{
let store = ForgeStore::open(&db_path).unwrap();
store
.insert_candidate("c1", r#"{"k": 5.0}"#, "[]", "active")
.unwrap();
}
{
let store = ForgeStore::open(&db_path).unwrap();
let spec = store.get_candidate_spec("c1").unwrap();
assert!(spec.contains("5.0"));
}
}