diff --git a/bin/compress_archives/test_compress_archives.py b/bin/compress_archives/test_compress_archives.py index 6ee1470a..34823f6c 100644 --- a/bin/compress_archives/test_compress_archives.py +++ b/bin/compress_archives/test_compress_archives.py @@ -15,10 +15,12 @@ from failure import Failure NOW = 1790000000 DAY = 24 * 60 * 60 +OLD = 30 * DAY + 60 +NEW = 30 * DAY - 60 class Main(unittest.TestCase): - """A home whose archive and review folders each hold a file last written 31 days ago and one written 29 days ago.""" + """A home whose archive and review folders each hold files last written a minute over 30 days ago, and one a minute under.""" def setUp(self): self.home = pathlib.Path(self.enterContext(tempfile.TemporaryDirectory())) @@ -26,23 +28,23 @@ class Main(unittest.TestCase): self.reviews = self.home / ".local" / "state" / "larv-review" / "code" / "proj-item-1" for folder in (self.archive / "session" / "s" / "subagents", self.reviews / "probe"): folder.mkdir(parents=True) - self.write(self.archive / "a.jsonl", 31) - self.write(self.archive / "a.outcomes.md", 31) - self.write(self.archive / "session" / "s" / "subagents" / "agent-a.jsonl", 31) - self.write(self.archive / "session" / "s" / "subagents" / "agent-a.meta.json", 31) - self.write(self.archive / "b.jsonl", 29) - self.write(self.reviews / "stability-round-1.md", 31) - self.write(self.reviews / "probe" / "out.js", 31) - self.write(self.reviews / "branch-1.diff", 29) + self.write(self.archive / "a.jsonl", OLD) + self.write(self.archive / "a.outcomes.md", OLD) + self.write(self.archive / "session" / "s" / "subagents" / "agent-a.jsonl", OLD) + self.write(self.archive / "session" / "s" / "subagents" / "agent-a.meta.json", OLD) + self.write(self.archive / "b.jsonl", NEW) + self.write(self.reviews / "stability-round-1.md", OLD) + self.write(self.reviews / "probe" / "out.js", OLD) + self.write(self.reviews / "branch-1.diff", NEW) - def write(self, path, days_ago): + def write(self, path, seconds_ago): path.write_bytes(f"{path.name}\n".encode()) - os.utime(path, (NOW - days_ago * DAY, NOW - days_ago * DAY)) + os.utime(path, (NOW - seconds_ago, NOW - seconds_ago)) - def run_main(self, argv=(), env=None): + def run_main(self, argv=(), env=None, now=NOW): out, err = io.StringIO(), io.StringIO() with contextlib.redirect_stdout(out), contextlib.redirect_stderr(err): - code = main.main(list(argv), env or {}, self.home, NOW) + code = main.main(list(argv), env or {}, self.home, now) return code, out.getvalue(), err.getvalue() def kept(self): @@ -65,6 +67,21 @@ class Main(unittest.TestCase): self.assertEqual(out, "Compressed 4 files.\n") self.assertEqual(self.run_main(), (0, "Compressed 0 files.\n", "")) + def test_keeps_a_file_last_written_exactly_30_days_ago_plain(self): + self.write(self.archive / "b.jsonl", 30 * DAY) + self.run_main() + self.assertTrue((self.archive / "b.jsonl").is_file()) + + def test_measures_the_30_days_from_the_clock_by_default(self): + with mock.patch.object(main.time, "time", return_value=NOW): + self.assertEqual(self.run_main(now=None), (0, "Compressed 4 files.\n", "")) + + def test_skips_a_file_removed_while_it_walks(self): + gone = mock.Mock(**{"name": "gone.jsonl", "is_file.return_value": True, "is_symlink.return_value": False, "stat.side_effect": FileNotFoundError}) + kept = self.archive / "a.jsonl" + root = mock.Mock(**{"rglob.return_value": [gone, kept]}) + self.assertEqual(main.old_files(root, "*.jsonl", NOW), [kept]) + def test_reads_the_archive_where_loop_claude_transcripts_puts_it(self): moved = self.home / "elsewhere" (self.home / ".claude" / "loop-claude-transcripts").rename(moved) @@ -84,10 +101,11 @@ class Main(unittest.TestCase): self.assertTrue((self.archive / "a.jsonl").is_file()) def test_prints_its_help_and_refuses_other_arguments(self): - code, out, _ = self.run_main(["--help"]) - self.assertEqual(code, 0) - self.assertTrue(out.startswith("Compress every file"), out) - self.assertEqual(self.run_main(["x"])[0], 1) + for flag in ("--help", "-h"): + code, out, _ = self.run_main([flag]) + self.assertEqual(code, 0) + self.assertTrue(out.startswith("Compress every file"), out) + self.assertEqual(self.run_main(["x", "y"]), (1, "", "unknown arguments x y; usage: compress-archives\n")) if __name__ == "__main__": diff --git a/bin/review_replay/test_review_replay.py b/bin/review_replay/test_review_replay.py index d4a74e8e..665b525b 100644 --- a/bin/review_replay/test_review_replay.py +++ b/bin/review_replay/test_review_replay.py @@ -366,14 +366,16 @@ class Main(unittest.TestCase): self.assertIn("claude-opus-5-5 at low effort found 0, 0%, and missed 1 such 🔴: it doesn't qualify.", out) def review_folder_with_round_1(self): - """The chunk's review folder, holding round 1's report from before the replayed round began at 10:00, that round's own and a later pass's reports, written after, and an earlier attempt's report, from before the chunk began at 09:15.""" + """The chunk's review folder, holding round 1's report, written the second the chunk began at 09:15; that round's own report, written after the replayed round began at 10:00, and a later pass's, written the second it began; and an earlier attempt's report, from before the chunk began.""" folder = self.home / ".local" / "state" / "larv-review" / "code" / "proj-x" folder.mkdir(parents=True) (folder / "stability-round-1.md").write_text("round 1\n") - before = stamp_seconds("2026-10-14T09:50:00Z") - os.utime(folder / "stability-round-1.md", (before, before)) + began = stamp_seconds("2026-10-14T09:15:00Z") + os.utime(folder / "stability-round-1.md", (began, began)) (folder / "stability-round-2.md").write_text("🔴 a.txt:1 wrong\n") (folder / "prose-round-1.md").write_text("later\n") + round_began = stamp_seconds("2026-10-14T10:00:00Z") + os.utime(folder / "prose-round-1.md", (round_began, round_began)) (folder / "stability-round-3.md").write_text("🔴 a.txt:1 wrong\n") attempt = stamp_seconds("2026-10-14T09:00:00Z") os.utime(folder / "stability-round-3.md", (attempt, attempt)) diff --git a/bin/text_files/test_compressed.py b/bin/text_files/test_compressed.py index d6acd69f..b1f0703d 100644 --- a/bin/text_files/test_compressed.py +++ b/bin/text_files/test_compressed.py @@ -1,3 +1,4 @@ +import errno import gzip import os import pathlib @@ -21,9 +22,25 @@ class Compress(unittest.TestCase): self.assertIsNone(compressed.compress(plain)) self.assertFalse(plain.exists()) packed = pathlib.Path(root, "t.jsonl.gz") - self.assertEqual(gzip.decompress(packed.read_bytes()), b'{"a": 1}\n') + # A gzip header's own time is 0, so compressing the same bytes twice writes the same file. + self.assertEqual(packed.read_bytes(), gzip.compress(b'{"a": 1}\n', mtime=0)) self.assertEqual(packed.stat().st_mtime, 2000) + def test_a_gzip_it_cannot_write_keeps_the_original_and_says_why(self): + with tempfile.TemporaryDirectory() as root: + plain = pathlib.Path(root, "t.jsonl") + plain.write_bytes(b"kept\n") + with mock.patch.object(compressed, "replace_file", return_value=Failure("disk full")): + self.assertEqual(compressed.compress(plain), Failure(f"could not compress {plain}: disk full")) + self.assertEqual(plain.read_bytes(), b"kept\n") + + def test_a_file_it_cannot_read_fails_naming_it(self): + with tempfile.TemporaryDirectory() as root: + folder = pathlib.Path(root, "t.jsonl") + folder.mkdir() + failed = compressed.compress(folder) + self.assertTrue(failed.reason.startswith(f"could not compress {folder}: [Errno {errno.EISDIR}]"), failed.reason) + def test_a_copy_that_reads_back_other_bytes_keeps_the_original_and_fails(self): with tempfile.TemporaryDirectory() as root: plain = pathlib.Path(root, "t.jsonl") @@ -61,8 +78,9 @@ class Read(unittest.TestCase): self.assertEqual(compressed.read_stored(pathlib.Path(root, "b.jsonl")), b"b\n") self.assertEqual(compressed.stored(pathlib.Path(root, "b.jsonl")), pathlib.Path(root, "b.jsonl.gz")) self.assertIsNone(compressed.stored(pathlib.Path(root, "c.jsonl"))) - with self.assertRaises(FileNotFoundError): + with self.assertRaises(FileNotFoundError) as raised: compressed.read_stored(pathlib.Path(root, "c.jsonl")) + self.assertEqual((raised.exception.errno, raised.exception.strerror, raised.exception.filename), (errno.ENOENT, "No such file or directory", str(pathlib.Path(root, "c.jsonl")))) def test_the_plain_file_wins_over_an_older_gzip_beside_it(self): with tempfile.TemporaryDirectory() as root: @@ -73,8 +91,10 @@ class Read(unittest.TestCase): def test_a_truncated_gzip_fails_as_an_os_error(self): with tempfile.TemporaryDirectory() as root: pathlib.Path(root, "a.jsonl.gz").write_bytes(gzip.compress(b"a\n" * 100)[:20]) - with self.assertRaises(OSError): + with self.assertRaises(OSError) as raised: compressed.read_stored(pathlib.Path(root, "a.jsonl")) + self.assertEqual((raised.exception.errno, raised.exception.filename), (errno.EIO, str(pathlib.Path(root, "a.jsonl.gz")))) + self.assertTrue(raised.exception.strerror.startswith("not a whole gzip: "), raised.exception.strerror) def test_globs_each_file_once_by_its_plain_name(self): with tempfile.TemporaryDirectory() as root: