|
| 1 | +//! Port of tests/gc-keep-expiry.test.ts: `keep=true` buys a DEAD session a |
| 2 | +//! bounded retention window against `pty gc`, not immortality. Agents tag a |
| 3 | +//! session they are debugging right now and never come back to untag it, so |
| 4 | +//! an unbounded exemption turns the registry into an append-only log. |
| 5 | +//! |
| 6 | +//! The policy pinned here: exempt while the session has been dead for less |
| 7 | +//! than `--keep-max-age` (default 7d), swept and reported separately once |
| 8 | +//! past it, `0` sweeps the whole backlog, and a RUNNING keep session is never |
| 9 | +//! a candidate no matter what the flag says. |
| 10 | +//! |
| 11 | +//! Records are written straight into the root rather than spawned: the policy |
| 12 | +//! is a comparison against `exitedAt`/`createdAt`, so a fabricated record is |
| 13 | +//! both exact about age and free of daemon-startup waits. |
| 14 | +
|
| 15 | +use pty_conformance::*; |
| 16 | + |
| 17 | +const DAY: i64 = 24 * 60 * 60; |
| 18 | + |
| 19 | +/// An exited session, dead for `dead_for_secs`, tagged `keep=true`. |
| 20 | +fn write_exited_keep(rig: &Rig, name: &str, dead_for_secs: i64) { |
| 21 | + write_fake_metadata( |
| 22 | + rig.root(), |
| 23 | + name, |
| 24 | + FakeMeta::created(-dead_for_secs) |
| 25 | + .exited(-dead_for_secs, 0) |
| 26 | + .tag("keep", "true"), |
| 27 | + ); |
| 28 | +} |
| 29 | + |
| 30 | +/// node: tests/gc-keep-expiry.test.ts:101 |
| 31 | +#[test] |
| 32 | +fn keeps_an_exited_keep_session_younger_than_the_default_window() { |
| 33 | + let rig = Rig::new(); |
| 34 | + let name = unique_id("gke"); |
| 35 | + write_exited_keep(&rig, &name, 2 * DAY); |
| 36 | + |
| 37 | + let out = rig.pty(&["gc"]); |
| 38 | + expect_status(&out, 0); |
| 39 | + let stdout = out.stdout(); |
| 40 | + expect_contains(&stdout, &format!("Kept (keep tag): {name}")); |
| 41 | + expect_not_contains(&stdout, "keep expired"); |
| 42 | + assert!(rig.meta_path(&name).exists()); |
| 43 | +} |
| 44 | + |
| 45 | +/// node: tests/gc-keep-expiry.test.ts:113 |
| 46 | +#[test] |
| 47 | +fn sweeps_an_expired_keep_session_apart_from_the_plain_sweep() { |
| 48 | + let rig = Rig::new(); |
| 49 | + let expired = unique_id("gke"); |
| 50 | + let stale = unique_id("gke"); |
| 51 | + write_exited_keep(&rig, &expired, 30 * DAY); |
| 52 | + // A same-age session WITHOUT the tag: proves the two buckets stay |
| 53 | + // distinct rather than one absorbing the other. |
| 54 | + write_fake_metadata( |
| 55 | + rig.root(), |
| 56 | + &stale, |
| 57 | + FakeMeta::created(-30 * DAY).exited(-30 * DAY, 0), |
| 58 | + ); |
| 59 | + |
| 60 | + let out = rig.pty(&["gc"]); |
| 61 | + expect_status(&out, 0); |
| 62 | + let stdout = out.stdout(); |
| 63 | + expect_contains( |
| 64 | + &stdout, |
| 65 | + &format!("Removed (keep expired after 7d): {expired}"), |
| 66 | + ); |
| 67 | + expect_contains(&stdout, &format!("Removed: {stale}")); |
| 68 | + expect_contains(&stdout, "1 stale session"); |
| 69 | + expect_contains(&stdout, "1 keep-expired session"); |
| 70 | + assert!(!rig.meta_path(&expired).exists()); |
| 71 | + assert!(!rig.meta_path(&stale).exists()); |
| 72 | +} |
| 73 | + |
| 74 | +/// node: tests/gc-keep-expiry.test.ts:132 |
| 75 | +#[test] |
| 76 | +fn honours_a_custom_window_in_both_flag_spellings() { |
| 77 | + let rig = Rig::new(); |
| 78 | + let spaced = unique_id("gke"); |
| 79 | + let equals = unique_id("gke"); |
| 80 | + write_exited_keep(&rig, &spaced, 2 * 3600); |
| 81 | + write_exited_keep(&rig, &equals, 2 * 3600); |
| 82 | + |
| 83 | + // 3h window: both sessions are 2h dead, so both survive. |
| 84 | + let kept = rig.pty(&["gc", "--keep-max-age", "3h"]); |
| 85 | + expect_status(&kept, 0); |
| 86 | + let stdout = kept.stdout(); |
| 87 | + expect_contains(&stdout, &format!("Kept (keep tag): {spaced}")); |
| 88 | + expect_contains(&stdout, &format!("Kept (keep tag): {equals}")); |
| 89 | + assert!(rig.meta_path(&spaced).exists()); |
| 90 | + |
| 91 | + // 1h window: both are past it. |
| 92 | + let swept = rig.pty(&["gc", "--keep-max-age=1h"]); |
| 93 | + expect_status(&swept, 0); |
| 94 | + let stdout = swept.stdout(); |
| 95 | + expect_contains( |
| 96 | + &stdout, |
| 97 | + &format!("Removed (keep expired after 1h): {spaced}"), |
| 98 | + ); |
| 99 | + expect_contains( |
| 100 | + &stdout, |
| 101 | + &format!("Removed (keep expired after 1h): {equals}"), |
| 102 | + ); |
| 103 | + assert!(!rig.meta_path(&spaced).exists()); |
| 104 | + assert!(!rig.meta_path(&equals).exists()); |
| 105 | +} |
| 106 | + |
| 107 | +/// node: tests/gc-keep-expiry.test.ts:155 |
| 108 | +#[test] |
| 109 | +fn a_zero_window_sweeps_a_keep_session_that_just_exited() { |
| 110 | + let rig = Rig::new(); |
| 111 | + let name = unique_id("gke"); |
| 112 | + write_exited_keep(&rig, &name, 0); |
| 113 | + |
| 114 | + let out = rig.pty(&["gc", "--keep-max-age", "0"]); |
| 115 | + expect_status(&out, 0); |
| 116 | + expect_contains( |
| 117 | + &out.stdout(), |
| 118 | + &format!("Removed (keep expired after 0s): {name}"), |
| 119 | + ); |
| 120 | + assert!(!rig.meta_path(&name).exists()); |
| 121 | +} |
| 122 | + |
| 123 | +/// node: tests/gc-keep-expiry.test.ts:166 |
| 124 | +#[test] |
| 125 | +fn anchors_on_created_at_when_there_is_no_exit_record() { |
| 126 | + let rig = Rig::new(); |
| 127 | + let name = unique_id("gke"); |
| 128 | + // A vanished session (SIGKILLed daemon) never wrote `exitedAt`, so its |
| 129 | + // age comes from `createdAt` — the same anchor precedence `pty list |
| 130 | + // --older-than` uses. |
| 131 | + write_fake_metadata( |
| 132 | + rig.root(), |
| 133 | + &name, |
| 134 | + FakeMeta::created(-30 * DAY).tag("keep", "true"), |
| 135 | + ); |
| 136 | + |
| 137 | + let out = rig.pty(&["gc"]); |
| 138 | + expect_status(&out, 0); |
| 139 | + expect_contains( |
| 140 | + &out.stdout(), |
| 141 | + &format!("Removed (keep expired after 7d): {name}"), |
| 142 | + ); |
| 143 | + assert!(!rig.meta_path(&name).exists()); |
| 144 | +} |
| 145 | + |
| 146 | +/// node: tests/gc-keep-expiry.test.ts:188 |
| 147 | +#[test] |
| 148 | +fn never_sweeps_a_running_keep_session_even_at_zero() { |
| 149 | + let rig = Rig::new(); |
| 150 | + let name = unique_id("gke"); |
| 151 | + // The test process itself stands in for a live daemon (the same device |
| 152 | + // list_filters.rs uses): an alive pid with no exit record reads as |
| 153 | + // status=running. Aged well past the window, so the only thing keeping |
| 154 | + // it out of the sweep is that it is still running. |
| 155 | + std::fs::write(rig.pid_path(&name), std::process::id().to_string()).unwrap(); |
| 156 | + write_fake_metadata( |
| 157 | + rig.root(), |
| 158 | + &name, |
| 159 | + FakeMeta::created(-30 * DAY).tag("keep", "true"), |
| 160 | + ); |
| 161 | + assert_eq!(rig.list_entry(&name).expect("listed")["status"], "running"); |
| 162 | + |
| 163 | + let out = rig.pty(&["gc", "--keep-max-age", "0"]); |
| 164 | + expect_status(&out, 0); |
| 165 | + expect_not_contains(&out.stdout(), &name); |
| 166 | + assert!(rig.meta_path(&name).exists()); |
| 167 | + assert_eq!(rig.list_entry(&name).expect("listed")["status"], "running"); |
| 168 | +} |
| 169 | + |
| 170 | +/// node: tests/gc-keep-expiry.test.ts:214 |
| 171 | +#[test] |
| 172 | +fn dry_run_previews_keep_expiry_without_removing_anything() { |
| 173 | + let rig = Rig::new(); |
| 174 | + let name = unique_id("gke"); |
| 175 | + write_exited_keep(&rig, &name, 30 * DAY); |
| 176 | + |
| 177 | + let dry = rig.pty(&["gc", "--dry-run"]); |
| 178 | + expect_status(&dry, 0); |
| 179 | + let stdout = dry.stdout(); |
| 180 | + expect_contains( |
| 181 | + &stdout, |
| 182 | + &format!("Would remove (keep expired after 7d): {name}"), |
| 183 | + ); |
| 184 | + expect_contains(&stdout, "1 keep-expired session"); |
| 185 | + expect_contains(&stdout, "Dry run"); |
| 186 | + assert!(rig.meta_path(&name).exists()); |
| 187 | + |
| 188 | + // A zero-window dry run is equally non-mutating. |
| 189 | + let dry_zero = rig.pty(&["gc", "-n", "--keep-max-age", "0"]); |
| 190 | + expect_status(&dry_zero, 0); |
| 191 | + expect_contains( |
| 192 | + &dry_zero.stdout(), |
| 193 | + &format!("Would remove (keep expired after 0s): {name}"), |
| 194 | + ); |
| 195 | + assert!(rig.meta_path(&name).exists()); |
| 196 | + |
| 197 | + // And the real pass then actually removes it. |
| 198 | + let real = rig.pty(&["gc"]); |
| 199 | + expect_status(&real, 0); |
| 200 | + expect_contains( |
| 201 | + &real.stdout(), |
| 202 | + &format!("Removed (keep expired after 7d): {name}"), |
| 203 | + ); |
| 204 | + assert!(!rig.meta_path(&name).exists()); |
| 205 | +} |
| 206 | + |
| 207 | +/// node: tests/gc-keep-expiry.test.ts:239 |
| 208 | +#[test] |
| 209 | +fn rejects_a_unit_less_non_zero_window() { |
| 210 | + let rig = Rig::new(); |
| 211 | + let bare = rig.pty(&["gc", "--keep-max-age", "7"]); |
| 212 | + expect_failure(&bare); |
| 213 | + expect_contains( |
| 214 | + &bare.stderr(), |
| 215 | + "--keep-max-age expects a duration like 12h, 7d, or 0", |
| 216 | + ); |
| 217 | + |
| 218 | + let junk = rig.pty(&["gc", "--keep-max-age=soon"]); |
| 219 | + expect_failure(&junk); |
| 220 | + expect_contains( |
| 221 | + &junk.stderr(), |
| 222 | + "--keep-max-age expects a duration like 12h, 7d, or 0", |
| 223 | + ); |
| 224 | +} |
0 commit comments