hird 0.3.1

Cross-harness agent work queue and shared assertion memory, backed by one local SQLite database
Documentation
//! Upgrading a database an older hird wrote.
//!
//! `tests/fixtures/schema-v12.db` was written by the hird that shipped schema
//! version 12, with a task in every status, a dependency, a review sent back and
//! then upheld, an open and an answered question, a recusal, a requirement, an
//! assertion and a recess. Every later migration has to carry all of it forward.

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")
}

/// A private copy, because opening a database migrates it in place.
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()
    );
    // Opening twice is a no-op, as it is for a fresh file.
    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();
    // The sandbox already points HIRD_DB at the copy.
    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();
    // Task 8 was sent back once by review 9, then upheld by review 10.
    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");
    // Nothing else was ever rejected.
    for seq in [1, 2, 3, 4, 6, 7, 9, 10, 11, 12] {
        assert!(
            db.attempts().for_task(seq).unwrap().is_empty(),
            "task {seq}"
        );
    }
}

/// Tasks from before links existed have none, and take them like any other.
#[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}");
}