use super::*;
fn write(path: &Path, bytes: usize) {
std::fs::create_dir_all(path.parent().unwrap()).unwrap();
std::fs::write(path, "x".repeat(bytes)).unwrap();
}
fn age(path: &Path, seconds_ago: u64) {
let when = std::time::SystemTime::now() - std::time::Duration::from_secs(seconds_ago);
std::fs::File::options()
.write(true)
.open(path)
.unwrap()
.set_modified(when)
.unwrap();
}
#[test]
fn test8990_the_oldest_go_first_and_it_stops_when_it_is_under() {
let dir = tempfile::tempdir().unwrap();
let root = dir.path();
let ops = stream_dir(root, Stream::Ops).join("2026-08-22");
for (n, name) in ["a", "b", "c", "d"].iter().enumerate() {
let path = ops.join(format!("{name}.jsonl"));
write(&path, 30 * 1024 * 1024);
age(&path, (4 - n as u64) * 3600);
}
let report = sweep(root, MIN_MAX_BYTES, &[], false);
assert_eq!(report.removed.len(), 2, "{report:?}");
assert!(report.removed[0].path.ends_with("a.jsonl"));
assert!(report.removed[1].path.ends_with("b.jsonl"));
assert!(!ops.join("a.jsonl").exists());
assert!(ops.join("c.jsonl").exists(), "a sweep must stop at the limit");
assert!(ops.join("d.jsonl").exists());
assert!(!report.still_over);
}
#[test]
fn test8991_a_running_operations_log_is_protected() {
let dir = tempfile::tempdir().unwrap();
let root = dir.path();
let ops = stream_dir(root, Stream::Ops).join("2026-08-22");
let running = ops.join("running.jsonl");
write(&running, 90 * 1024 * 1024);
age(&running, 99_999); let other = ops.join("other.jsonl");
write(&other, 30 * 1024 * 1024);
age(&other, 1);
let report = sweep(root, MIN_MAX_BYTES, &[running.clone()], false);
assert!(running.exists(), "the protected entry must survive");
assert_eq!(report.protected, 1);
assert!(!other.exists(), "everything else is still fair game");
assert!(
report.still_over,
"it must SAY it could not get under the limit rather than pretending it did"
);
}
#[test]
fn test8992_a_session_is_one_entry() {
let dir = tempfile::tempdir().unwrap();
let root = dir.path();
let session = stream_dir(root, Stream::Test).join("2026-08-22_10-00-00");
write(&session.join("runner.log"), 40 * 1024 * 1024);
write(&session.join("suite-one.log"), 40 * 1024 * 1024);
age(&session.join("runner.log"), 7200);
age(&session.join("suite-one.log"), 7200);
let newer = stream_dir(root, Stream::Daemon).join("wsb-now.log");
write(&newer, 10 * 1024 * 1024);
age(&newer, 1);
let found = entries(root);
assert_eq!(found.len(), 2, "one session and one daemon log: {found:?}");
let report = sweep(root, MIN_MAX_BYTES, &[], false);
assert_eq!(report.removed.len(), 1);
assert_eq!(report.removed[0].group, Stream::Test.dir_name());
assert!(!session.exists(), "the session goes whole");
assert!(newer.exists());
}
#[test]
fn test8993_a_dry_run_touches_nothing() {
let dir = tempfile::tempdir().unwrap();
let root = dir.path();
let ops = stream_dir(root, Stream::Ops).join("2026-08-22");
let old = ops.join("old.jsonl");
write(&old, 100 * 1024 * 1024);
age(&old, 7200);
let report = sweep(root, MIN_MAX_BYTES, &[], true);
assert_eq!(report.removed.len(), 1, "it must still SAY what it would take");
assert!(old.exists(), "a dry run removes nothing");
}
#[test]
fn test8994_the_limit_has_a_floor() {
let dir = tempfile::tempdir().unwrap();
let root = dir.path();
let path = stream_dir(root, Stream::Ops)
.join("2026-08-22")
.join("one.jsonl");
write(&path, 32 * 1024 * 1024);
let report = sweep(root, 1, &[], false);
assert!(report.removed.is_empty(), "{report:?}");
assert!(path.exists());
}
#[test]
fn test8995_op_log_paths_are_dated_and_ordered() {
let root = Path::new("/w/.wsb/disposable/logs");
let id = uuid::Uuid::parse_str("3f9a2c7d-0000-4000-8000-000000000000").unwrap();
let at: DateTime<Utc> = "2026-08-22T14:30:12Z".parse().unwrap();
let path = op_log_path(root, "TestRun", &id, at);
let name = path.file_name().unwrap().to_string_lossy().to_string();
assert!(name.ends_with("-testrun-3f9a2c7d.jsonl"), "{name}");
assert_eq!(
path.parent().unwrap().parent().unwrap(),
root.join("ops"),
"one directory per day, under the stream"
);
let local = at.with_timezone(&chrono::Local);
assert_eq!(
path.parent().unwrap().file_name().unwrap().to_string_lossy(),
local.format("%Y-%m-%d").to_string()
);
assert!(name.starts_with(&local.format("%H%M%S").to_string()));
}
#[test]
fn test8996_usage_names_every_stream() {
let dir = tempfile::tempdir().unwrap();
write(
&stream_dir(dir.path(), Stream::Ops)
.join("2026-08-22")
.join("one.jsonl"),
2048,
);
std::fs::write(dir.path().join("a-screenshot.png"), "x".repeat(512)).unwrap();
let usage = usage(dir.path());
assert_eq!(usage.managed_bytes, 2048);
assert_eq!(usage.unmanaged_bytes, 512);
assert_eq!(usage.total_bytes, 2560, "the total is what `du` would say");
assert_eq!(usage.entries, 1);
for stream in Stream::ALL {
assert!(
usage.by_group.contains_key(stream.dir_name()),
"{} is missing from {:?}",
stream.dir_name(),
usage.by_group
);
}
}
#[test]
fn test11402_unmanaged_bytes_over_the_limit_are_reported_not_hidden() {
let dir = tempfile::tempdir().unwrap();
let root = dir.path();
write(&root.join("2026-07-12_17-44-53").join("runner.log"), 90 * 1024 * 1024);
let reachable = stream_dir(root, Stream::Ops)
.join("2026-08-22")
.join("a.jsonl");
write(&reachable, 10 * 1024 * 1024);
age(&reachable, 3600);
let report = sweep(root, MIN_MAX_BYTES, &[], false);
assert!(
report.still_over,
"the root holds {} against a {} limit and the sweep reported it settled: {report:?}",
human_bytes(usage(root).total_bytes),
human_bytes(MIN_MAX_BYTES)
);
assert_eq!(
report.unreachable_bytes,
90 * 1024 * 1024,
"the bytes no sweep can reach are named, so an operator knows a smaller \
limit will not help: {report:?}"
);
assert!(!reachable.exists(), "the reachable log was not swept: {report:?}");
}
mod find_op_log {
use super::super::*;
use uuid::Uuid;
fn logged(id: Uuid, kind: &str) -> (tempfile::TempDir, std::path::PathBuf) {
let dir = tempfile::TempDir::new().expect("temp dir");
let root = dir.path().join("logs");
let writer = op_log::OpLogWriter::create(
&root,
id,
kind,
"running the declared suite",
&["wsb".to_string(), "test".to_string()],
chrono::Utc::now(),
)
.expect("a log is created");
let path = writer.path().to_path_buf();
writer.finish(op_log::Status::Completed, None);
(dir, path)
}
#[test]
fn test10244_a_finished_log_is_found_by_its_id() {
let id = Uuid::new_v4();
let (dir, path) = logged(id, "test_run");
assert_eq!(
find_op_log(&dir.path().join("logs"), &id),
Some(path),
"the log is found from the id alone"
);
}
#[test]
fn test10245_an_unknown_id_finds_nothing() {
let (dir, _) = logged(Uuid::new_v4(), "test_run");
assert_eq!(
find_op_log(&dir.path().join("logs"), &Uuid::new_v4()),
None,
"an id that was never logged has no file"
);
}
#[test]
fn test10246_a_short_id_collision_resolves_to_the_right_log() {
let first = Uuid::parse_str("aabbccdd-0000-4000-8000-000000000001").expect("uuid");
let second = Uuid::parse_str("aabbccdd-0000-4000-8000-000000000002").expect("uuid");
let dir = tempfile::TempDir::new().expect("temp dir");
let logs = dir.path().join("logs");
let mut paths = Vec::new();
for (id, kind) in [(first, "build"), (second, "test-run")] {
let writer = op_log::OpLogWriter::create(
&logs,
id,
kind,
"label",
&["wsb".to_string()],
chrono::Utc::now(),
)
.expect("a log is created");
paths.push(writer.path().to_path_buf());
writer.finish(op_log::Status::Completed, None);
}
assert!(
paths[0] != paths[1],
"the two logs are different files: {paths:?}"
);
assert_eq!(find_op_log(&logs, &first), Some(paths[0].clone()));
assert_eq!(find_op_log(&logs, &second), Some(paths[1].clone()));
}
#[test]
fn test10247_a_log_root_that_was_never_used_finds_nothing() {
let dir = tempfile::TempDir::new().expect("temp dir");
assert_eq!(find_op_log(&dir.path().join("logs"), &Uuid::new_v4()), None);
}
}