use super::*; use std::path::{Path, PathBuf}; use crate::ci::ledger::{PassReport, Reason}; fn claim(path: &str, rule: &str) -> Claim { Claim { path: PathBuf::from(path), kind: ClaimKind::Evidence, lane: "lane-a".into(), session: Some("s-1".into()), rule: rule.into(), created_at: "2026-10-10T00:00:00Z".into(), adopted: false, pid: None, bytes: Some(12_400_000_000), measured_at: Some("2026-10-10T00:00:00Z".into()), touched_at: Some("2026-10-09T00:00:00Z".into()), drop_asked_at: None, } } /// RULE: a released claim on a plain file (one left loose in ~) is removed as a file; a folder is removed whole. /// Owner, 10 Oct 2026: a loose FILE at the top of ~ is a claim too, so the remover must take a file. #[test] fn a_released_file_is_removed_as_a_file() { let dir = tempfile::tempdir().unwrap(); let file = dir.path().join("scratch.txt"); std::fs::write(&file, "scratch").unwrap(); let folder = dir.path().join("session-out"); std::fs::create_dir_all(folder.join("inner")).unwrap(); for path in [&file, &folder] { let mut c = claim(path.to_str().unwrap(), "idle_hours:72"); c.kind = ClaimKind::Scratch; super::remove(&c).unwrap(); assert!(!path.exists(), "{}", path.display()); } } /// RULE: a running job's working folders are read from `lsof -F n` — each `n` line once — and nothing else in its output is a folder. /// Architect's read, 10 Oct 2026: the pass holds only the claims a running job works in. #[test] fn a_jobs_working_folders_are_the_n_lines_of_lsof() { let out = "p4242\nfcwd\nn/Users/me/.cache/cottondb/walk/worktree/crates\np4343\nfcwd\nn/Users/me/.cache/cottondb/walk/worktree/crates\n"; assert_eq!(super::working_dirs(out), vec![PathBuf::from("/Users/me/.cache/cottondb/walk/worktree/crates")]); assert!(super::working_dirs("").is_empty()); } /// RULE: before every pass its caller reads what is alive — each checkout's registered worktrees, every live process's working folder, every tmux pane's — and a pass whose process list did not answer, or whose checkout git would not list, holds everything. /// Incident, 10 Oct 2026: a lane's worktree went while its session sat in it, because a process working in a folder counted for nothing. #[test] fn what_is_alive_is_read_from_the_processes_the_worktrees_and_the_panes() { let lsof = "p50538\nfcwd\nn/Users/me/.cache/cottondb/statuses/worktree\np1\nfcwd\nn/\n"; let list = "worktree /Users/me/Code/cottondb\nHEAD a\nbranch refs/heads/main\n\nworktree /Users/me/.cache/cottondb/board/worktree\nHEAD b\nbranch refs/heads/board\n\nworktree /Users/me/Code/cottondb/.worktrees/gone\nHEAD c\ndetached\nprunable gitdir file points to non-existent location\n"; let live = super::liveness_of(Some(lsof), &[Some(list.to_string())], "/Users/me/.cache/cottondb/media/worktree\n"); assert!(!live.unread); for lane in ["statuses", "board", "media"] { assert!(live.holds(Path::new(&format!("/Users/me/.cache/cottondb/{lane}"))).is_some(), "{lane} is alive"); } assert_eq!(live.holds(Path::new("/Users/me/Code/cottondb/.worktrees/gone")), None, "a prunable worktree is not alive"); assert_eq!(live.holds(Path::new("/Users/me/.cache/cottondb/idle-lane")), None); assert!(super::liveness_of(None, &[Some(list.to_string())], "").unread, "no process list"); assert!(super::liveness_of(Some(""), &[], "").unread, "a process list naming no folder"); assert!(super::liveness_of(Some(lsof), &[None], "").unread, "a checkout git would not list"); assert!(!super::liveness_of(Some(lsof), &[], "").unread, "a Mac with no checkout has no worktrees to read"); } /// RULE: every removal a pass performs is said in its caller's log with its path, claim, rule and reason, ahead of the pass's one sentence. #[test] fn a_pass_says_each_removal_in_its_callers_log() { let gone = claim("/Users/me/.cache/cottondb/old-lane", "session_ends"); let report = PassReport { released: vec![(gone.clone(), Reason::SessionEnded)], ..PassReport::default() }; let lines = super::said(&report); assert_eq!( lines[0], format!("ledger: removed /Users/me/.cache/cottondb/old-lane claim={} lane={} rule=session_ends reason=\"{}\"", gone.id(), gone.lane, Reason::SessionEnded.words()) ); assert_eq!(lines.len(), 2, "then the one sentence: {lines:?}"); assert!(super::said(&PassReport::default()).is_empty()); }