|
| 1 | +"""Integration tests for checkpoint store: stderr capture + large file skip.""" |
| 2 | +from __future__ import annotations |
| 3 | + |
| 4 | +import pytest |
| 5 | + |
| 6 | +import checkpoint.store as store |
| 7 | + |
| 8 | + |
| 9 | +@pytest.fixture(autouse=True) |
| 10 | +def isolate_store(tmp_path, monkeypatch): |
| 11 | + """Redirect checkpoint root to tmp_path and reset global state.""" |
| 12 | + monkeypatch.setattr(store, "_checkpoints_root", lambda: tmp_path / "checkpoints") |
| 13 | + store.reset_file_versions() |
| 14 | + |
| 15 | + |
| 16 | +def test_large_file_skipped_and_logged_to_stderr(tmp_path, monkeypatch, capsys): |
| 17 | + monkeypatch.setattr(store, "_MAX_FILE_SIZE", 50) |
| 18 | + big_file = tmp_path / "big.txt" |
| 19 | + big_file.write_bytes(b"x" * 100) |
| 20 | + |
| 21 | + result = store.track_file_edit("test-session", str(big_file)) |
| 22 | + |
| 23 | + assert result is None |
| 24 | + captured = capsys.readouterr() |
| 25 | + assert "[checkpoint] skipping large file" in captured.err |
| 26 | + assert "100 bytes" in captured.err |
| 27 | + assert captured.out == "" |
| 28 | + |
| 29 | + |
| 30 | +def test_normal_file_backed_up(tmp_path, capsys): |
| 31 | + small_file = tmp_path / "small.txt" |
| 32 | + content = b"hello world" |
| 33 | + small_file.write_bytes(content) |
| 34 | + |
| 35 | + result = store.track_file_edit("test-session", str(small_file)) |
| 36 | + |
| 37 | + assert result is not None |
| 38 | + backup_dir = tmp_path / "checkpoints" / "test-session" / "backups" |
| 39 | + backup_path = backup_dir / result |
| 40 | + assert backup_path.exists() |
| 41 | + assert backup_path.read_bytes() == content |
| 42 | + captured = capsys.readouterr() |
| 43 | + assert captured.err == "" |
| 44 | + |
| 45 | + |
| 46 | +def test_backup_failure_logged_to_stderr(tmp_path, monkeypatch, capsys): |
| 47 | + normal_file = tmp_path / "normal.txt" |
| 48 | + normal_file.write_bytes(b"some data") |
| 49 | + |
| 50 | + def failing_copy(*args, **kwargs): |
| 51 | + raise PermissionError("access denied") |
| 52 | + |
| 53 | + monkeypatch.setattr(store.shutil, "copy2", failing_copy) |
| 54 | + |
| 55 | + result = store.track_file_edit("test-session", str(normal_file)) |
| 56 | + |
| 57 | + assert result is None |
| 58 | + captured = capsys.readouterr() |
| 59 | + assert "[checkpoint] backup failed" in captured.err |
| 60 | + assert "access denied" in captured.err |
| 61 | + assert captured.out == "" |
0 commit comments