Skip to content

armgit / kanzo

Public Imported from GitHub
0

Self-hosted, sharable kanban for humans and agents. One 4.7 MB binary, 5 MB of RAM, 10 to 60 tokens per agent reply, starts in 3 ms, zero setup.

Test sync's locking and recovery

emanuele-emdcf133a81f35
diff --git a/src/home.rs b/src/home.rs
index 2266760..ebf57ac 100644
--- a/src/home.rs
+++ b/src/home.rs
@@ -43,7 +43,7 @@ kanzo.lock
";
/// How long an operation waits for `kanzo sync` before giving up with "busy".
-const LOCK_WAIT: Duration = Duration::from_secs(10);
+const LOCK_WAIT: Duration = Duration::from_secs(if cfg!(test) { 1 } else { 10 });
pub const NO_LOCKS: &str = "This folder doesn't support file locks: don't sync while agents are working.";
@@ -227,4 +227,30 @@ mod tests {
let home = Home::open(tmp.path().to_path_buf()).unwrap();
assert_eq!(home.store().unwrap().statuses(), ["todo", "doing", "review", "done"]);
}
+
+ #[test]
+ fn waits_for_sync_then_reports_busy() {
+ let tmp = tempfile::tempdir().unwrap();
+ let home = Home::open(tmp.path().to_path_buf()).unwrap();
+ let lock = home.lock_exclusive().unwrap();
+ assert!(matches!(home.store(), Err(store::Error::Busy(_))));
+
+ // An operation started during a replacement waits, then goes on once it's done.
+ let waiting = std::thread::spawn({
+ let home = home.clone();
+ move || home.store().map(|s| s.statuses().len())
+ });
+ std::thread::sleep(Duration::from_millis(200));
+ assert!(!waiting.is_finished());
+ drop(lock);
+ assert_eq!(waiting.join().unwrap().unwrap(), 4);
+ }
+
+ #[test]
+ fn no_lock_only_when_unsupported() {
+ assert!(unsupported(&io::Error::from(io::ErrorKind::Unsupported)));
+ #[cfg(unix)]
+ assert!(unsupported(&io::Error::from_raw_os_error(libc::ENOLCK)));
+ assert!(!unsupported(&io::Error::from(io::ErrorKind::PermissionDenied)));
+ }
}
diff --git a/src/sync.rs b/src/sync.rs
index d679270..942088d 100644
--- a/src/sync.rs
+++ b/src/sync.rs
@@ -158,7 +158,6 @@ pub fn sync(home: &Home, keep: Option<Keep>, prompt: bool) -> Result<String> {
git(&["add", "--all"])?;
if lock.is_some() { String::new() } else { format!("\n{NO_LOCKS}") }
};
-
if !git_ok(&["diff", "--cached", "--quiet"]) {
let message = format!("kanzo sync from {}", hostname());
// A machine where git doesn't know the user yet still syncs.
@@ -305,6 +304,24 @@ fn hostname() -> String {
mod tests {
use super::*;
+ /// A machine's data folder, syncing with the bare repo `remote`.
+ fn machine(dir: &Path, remote: &Path) -> Home {
+ let home = Home::open(dir.to_path_buf()).unwrap();
+ set_remote(&home, remote.to_str().unwrap(), false).unwrap();
+ home
+ }
+
+ fn two_machines(tmp: &Path) -> (Home, Home) {
+ let remote = tmp.join("remote.git");
+ git(tmp, false, &["init", "--quiet", "--bare", remote.to_str().unwrap()]).unwrap();
+ let a = machine(&tmp.join("a"), &remote);
+ sync(&a, None, false).unwrap();
+ let b = machine(&tmp.join("b"), &remote);
+ b.store().unwrap().create_project("local", "", None).unwrap();
+ sync(&b, Some(Keep::Remote), false).unwrap();
+ (a, b)
+ }
+
#[test]
fn refuses_options_as_urls() {
let tmp = tempfile::tempdir().unwrap();
@@ -314,6 +331,36 @@ mod tests {
assert!(!tmp.path().join("pwned").exists());
}
+ #[test]
+ fn keep_remote_keeps_the_old_board() {
+ let tmp = tempfile::tempdir().unwrap();
+ let (_, b) = two_machines(tmp.path());
+ assert!(b.store().unwrap().list_projects().unwrap().is_empty());
+ // The old board is in kanzo.db.bak, and in the commit sync made before resetting.
+ let bak = Store::open(&b.dir.join("kanzo.db.bak"), None).unwrap();
+ assert_eq!(bak.list_projects().unwrap()[0].slug, "local");
+ let old = git(&b.dir, false, &["log", "-1", "--format=%s", "HEAD@{1}"]).unwrap();
+ assert!(old.starts_with("kanzo sync from"));
+ }
+
+ #[test]
+ fn a_failed_migration_leaves_the_pulled_board() {
+ let tmp = tempfile::tempdir().unwrap();
+ let (a, b) = two_machines(tmp.path());
+ let db = rusqlite::Connection::open(a.db_path()).unwrap();
+ db.execute("UPDATE meta SET value = '999' WHERE key IN ('schema_version', 'compatible')", []).unwrap();
+ drop(db);
+ sync(&a, None, false).unwrap();
+
+ let newer = "saved by a newer version of Kanzo";
+ assert!(sync(&b, None, false).unwrap_err().to_string().contains(newer));
+ let db = rusqlite::Connection::open(b.db_path()).unwrap();
+ let version: String =
+ db.query_row("SELECT value FROM meta WHERE key = 'schema_version'", [], |r| r.get(0)).unwrap();
+ assert_eq!(version, "999");
+ assert!(format!("{:#}", Home::open(b.dir.clone()).unwrap_err()).contains(newer));
+ }
+
#[test]
fn urls() {
let ssh = "[email protected]:you/kanzo-data.git";