mod support;
use std::path::PathBuf;
use hird::db::Db;
use hird::model::Status;
use support::Sandbox;
fn fixture() -> PathBuf {
PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures/schema-v12.db")
}
fn upgraded_copy(sandbox: &Sandbox) -> PathBuf {
let path = sandbox.db();
std::fs::copy(fixture(), &path).expect("copy fixture");
path
}
#[test]
fn the_fixture_really_is_a_version_12_database() {
let conn = rusqlite::Connection::open(fixture()).unwrap();
let version: String = conn
.query_row(
"SELECT value FROM meta WHERE key = 'schema_version'",
[],
|row| row.get(0),
)
.unwrap();
assert_eq!(version, "12");
}
#[test]
fn a_version_12_database_upgrades_to_the_current_schema() {
let sandbox = Sandbox::new();
let path = upgraded_copy(&sandbox);
let db = Db::open(&path).expect("an old database opens");
assert_eq!(
db.schema_version().unwrap(),
Db::open_in_memory().unwrap().schema_version().unwrap()
);
drop(db);
Db::open(&path).expect("an upgraded database reopens");
}
#[test]
fn every_task_keeps_its_status_through_the_upgrade() {
let sandbox = Sandbox::new();
let db = Db::open(upgraded_copy(&sandbox)).unwrap();
let tasks = db.tasks();
let expect = [
(1, Status::Open),
(2, Status::Claimed),
(3, Status::InProgress),
(4, Status::Done),
(5, Status::Failed),
(6, Status::Cancelled),
(7, Status::Open),
(8, Status::Done),
(9, Status::Done),
(10, Status::Done),
(11, Status::Open),
(12, Status::Open),
];
for (seq, status) in expect {
assert_eq!(tasks.get(seq).unwrap().status, status, "task {seq}");
}
assert_eq!(
tasks.get(2).unwrap().claimed_by.as_deref(),
Some("claude-code:aa11")
);
assert!(tasks.get(8).unwrap().review);
}
#[test]
fn the_graph_the_verdicts_and_the_questions_survive_the_upgrade() {
let sandbox = Sandbox::new();
let db = Db::open(upgraded_copy(&sandbox)).unwrap();
let blockers: Vec<i64> = db
.deps()
.blockers(7)
.unwrap()
.iter()
.map(|b| b.seq)
.collect();
assert_eq!(blockers, vec![1, 4]);
let verdicts: Vec<String> = db
.verdicts()
.for_task(8)
.unwrap()
.iter()
.map(|v| v.verdict.as_str().to_string())
.collect();
assert_eq!(verdicts, vec!["sent_back", "upheld"]);
assert!(db
.tasks()
.get(8)
.unwrap()
.body
.contains("the error path drops the lock"));
assert!(db.questions().unanswered(11).unwrap().is_some());
assert!(db.questions().unanswered(12).unwrap().is_none());
assert_eq!(db.recusals().for_task(1).unwrap().len(), 1);
let events = db
.tasks()
.events(&db.tasks().get(8).unwrap().id, 100)
.unwrap();
assert!(
events.len() >= 6,
"the trail of task 8 survives: {events:?}"
);
}
#[test]
fn the_command_line_reads_an_upgraded_database() {
let sandbox = Sandbox::new();
upgraded_copy(&sandbox);
let listing = sandbox.run(&["ls", "--all-projects"]);
for title in ["open task", "claimed task", "reviewed work", "asking task"] {
assert!(listing.contains(title), "{title} missing from:\n{listing}");
}
let shown = sandbox.run(&["show", "8"]);
assert!(shown.contains("reviewed work"), "{shown}");
}
#[test]
fn events_written_before_the_audit_columns_read_as_unattributed() {
let sandbox = Sandbox::new();
let db = Db::open(upgraded_copy(&sandbox)).unwrap();
let trail = db.audit().for_task(2).unwrap();
assert!(!trail.is_empty());
for entry in trail {
assert_eq!(
(
entry.model.as_str(),
entry.role.as_str(),
entry.evidence.as_str()
),
("", "", ""),
"{entry:?}"
);
}
}
#[test]
fn work_sent_back_before_attempts_existed_keeps_its_rejection_on_the_record() {
let sandbox = Sandbox::new();
let db = Db::open(upgraded_copy(&sandbox)).unwrap();
let attempts = db.attempts().for_task(8).unwrap();
assert_eq!(attempts.len(), 1, "{attempts:?}");
let a = &attempts[0];
assert_eq!((a.n, a.outcome.as_str()), (1, "sent_back"));
assert_eq!(a.source, "review 9");
assert_eq!(a.actor, "codex:bb22");
assert_eq!(a.holder, "claude-code:aa11");
assert_eq!(a.output, "the error path drops the lock");
for seq in [1, 2, 3, 4, 6, 7, 9, 10, 11, 12] {
assert!(
db.attempts().for_task(seq).unwrap().is_empty(),
"task {seq}"
);
}
}
#[test]
fn tasks_from_before_links_existed_can_be_linked() {
let sandbox = Sandbox::new();
let db = Db::open(upgraded_copy(&sandbox)).unwrap();
for seq in 1..=12 {
assert_eq!(db.links().for_task(seq).unwrap(), None, "task {seq}");
}
let change = hird::repo::LinkChange {
branch: Some("feat/old".into()),
..Default::default()
};
db.links().set(4, "cli", &change).unwrap();
drop(db);
let shown = sandbox.run(&["show", "4"]);
assert!(shown.contains("branch feat/old"), "{shown}");
}