from pathlib import Path import pytest from tools import run_destructive_tests def test_reuses_initial_backup_across_test_attempts(tmp_path: Path) -> None: data_path = tmp_path / "data" backup_root = tmp_path / ".test-backups" data_path.mkdir() backup_root.mkdir() (data_path / "transcription.db").write_text("original", encoding="utf-8") initial_backup = run_destructive_tests.create_or_reuse_backup(data_path, backup_root) (data_path / "transcription.db").write_text("overwritten", encoding="utf-8") reused_backup = run_destructive_tests.create_or_reuse_backup(data_path, backup_root) assert reused_backup == initial_backup assert (reused_backup / "transcription.db").read_text(encoding="utf-8") == "original" assert [path for path in backup_root.iterdir() if path.is_dir()] == [initial_backup] def test_missing_active_backup_stops_instead_of_replacing_it(tmp_path: Path) -> None: data_path = tmp_path / "data" backup_root = tmp_path / ".test-backups" data_path.mkdir() backup_root.mkdir() (data_path / "transcription.db").write_text("post-test", encoding="utf-8") (backup_root / run_destructive_tests.ACTIVE_BACKUP_FILENAME).write_text("data-backup-missing", encoding="utf-8") with pytest.raises(FileNotFoundError, match="Active backup is missing"): run_destructive_tests.create_or_reuse_backup(data_path, backup_root) assert not any(path.is_dir() for path in backup_root.iterdir()) def test_closing_cycle_preserves_backup(tmp_path: Path) -> None: data_path = tmp_path / "data" backup_root = tmp_path / ".test-backups" data_path.mkdir() backup_root.mkdir() (data_path / "transcription.db").write_text("original", encoding="utf-8") backup_path = run_destructive_tests.create_or_reuse_backup(data_path, backup_root) closed_backup = run_destructive_tests.close_active_backup_cycle(backup_root) assert closed_backup == backup_path assert backup_path.is_dir() assert not (backup_root / run_destructive_tests.ACTIVE_BACKUP_FILENAME).exists()