refactor(cli): batched purge in cmd_purge and cmd_trash_empty (Plan B Phase 6)
Renames purge_item to purge_item_filesystem — body becomes filesystem-only (remove item.enc, remove attachments/<id>/, manifest.remove). Returns the relative paths it removed. cmd_purge and cmd_trash_empty accumulate the paths and fire ONE git rm + ONE git add + ONE git commit per invocation. A 50-item trash empty now produces 3 git subprocesses regardless of N (was N+2). New regression test trash_empty_batches_into_one_commit asserts the one-commit invariant via git rev-list --count.
This commit is contained in:
@@ -109,6 +109,72 @@ fn rm_restore_purge_cycle() {
|
||||
assert!(!String::from_utf8(out.stdout).unwrap().contains("target"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn trash_empty_batches_into_one_commit() {
|
||||
let v = TestVault::init();
|
||||
|
||||
// Add 3 items.
|
||||
for title in ["alpha", "bravo", "charlie"] {
|
||||
let out = v.run(&[
|
||||
"add", "login",
|
||||
"--title", title,
|
||||
"--username", "u",
|
||||
"--password", "p",
|
||||
]);
|
||||
assert!(out.status.success(), "add {title} failed");
|
||||
}
|
||||
|
||||
// Soft-delete all 3.
|
||||
for title in ["alpha", "bravo", "charlie"] {
|
||||
let out = v.run(&["rm", title]);
|
||||
assert!(out.status.success(), "rm {title} failed");
|
||||
}
|
||||
|
||||
// Set retention to 0 days so the recently-trashed items become purgeable
|
||||
// (should_purge: now - trashed_at > 0 * 86400 = 0).
|
||||
let out = v.run(&["settings", "trash-retention", "--days", "0"]);
|
||||
assert!(out.status.success(), "settings trash-retention failed");
|
||||
|
||||
// Brief sleep to ensure now > trashed_at by at least 1 second
|
||||
// (otherwise should_purge returns false at strict-greater-than equality).
|
||||
std::thread::sleep(std::time::Duration::from_secs(1));
|
||||
|
||||
// Count commits before.
|
||||
let before = std::process::Command::new("git")
|
||||
.args(["rev-list", "--count", "HEAD"])
|
||||
.current_dir(v.path())
|
||||
.output()
|
||||
.unwrap();
|
||||
let before_count: u32 = String::from_utf8(before.stdout).unwrap().trim().parse().unwrap();
|
||||
|
||||
// Run trash empty.
|
||||
let out = v.run(&["trash", "empty"]);
|
||||
assert!(out.status.success(), "trash empty failed: stderr={}",
|
||||
String::from_utf8_lossy(&out.stderr));
|
||||
|
||||
// Count commits after.
|
||||
let after = std::process::Command::new("git")
|
||||
.args(["rev-list", "--count", "HEAD"])
|
||||
.current_dir(v.path())
|
||||
.output()
|
||||
.unwrap();
|
||||
let after_count: u32 = String::from_utf8(after.stdout).unwrap().trim().parse().unwrap();
|
||||
|
||||
assert_eq!(
|
||||
after_count - before_count, 1,
|
||||
"trash empty should fire exactly one commit; before={before_count} after={after_count}"
|
||||
);
|
||||
|
||||
// The remaining `list --trashed` should be empty.
|
||||
let out = v.run(&["list", "--trashed"]);
|
||||
let stdout = String::from_utf8(out.stdout).unwrap();
|
||||
let stderr = String::from_utf8(out.stderr).unwrap();
|
||||
assert!(
|
||||
!stdout.contains("alpha") && !stdout.contains("bravo") && !stdout.contains("charlie"),
|
||||
"items still in trashed list: stdout={stdout} stderr={stderr}"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn generate_random_and_bip39() {
|
||||
let dir = tempfile::TempDir::new().unwrap();
|
||||
|
||||
Reference in New Issue
Block a user