import os import tempfile import unittest from pathlib import Path from core.approval_store import ApprovalStore from core.operation_guard import OperationGuard, OperationBlocked class OperationGuardTests(unittest.TestCase): def setUp(self): self.temp = tempfile.TemporaryDirectory() self.addCleanup(self.temp.cleanup) self.root = Path(self.temp.name) self.store = ApprovalStore(self.root) self.guard = OperationGuard(self.store) self.identifier = "a" * 32 def marker(self, suffix): path = self.root / (self.identifier + suffix) path.touch(mode=0o600) return path def test_empty_store_clear(self): self.assertEqual(self.guard.inspect()["status"], "CLEAR") def test_empty_store_permits_operations(self): self.guard.require_clear() def test_unused_approval_permitted(self): import test_approval_store fixture = test_approval_store.ApprovalStoreTests() self.addCleanup(fixture.doCleanups) fixture.setUp() fixture.save() OperationGuard(fixture.store).require_clear() def test_migration_intent_blocks(self): self.marker(".migration-intent.json") with self.assertRaises(OperationBlocked): self.guard.require_clear() def test_consumed_approval_blocks(self): self.marker(".consumed.json") with self.assertRaises(OperationBlocked): self.guard.require_clear() def test_migration_result_blocks_until_archived(self): self.marker(".migration-result.json") with self.assertRaises(OperationBlocked): self.guard.require_clear() def test_activation_intent_blocks(self): self.marker(".activation-intent.json") with self.assertRaises(OperationBlocked): self.guard.require_clear() def test_activation_result_blocks_until_archived(self): self.marker(".activation-result.json") with self.assertRaises(OperationBlocked): self.guard.require_clear() def test_successful_result_does_not_bypass_guard(self): path = self.marker(".activation-result.json") path.write_text('{"status":"VERIFIED"}') with self.assertRaises(OperationBlocked): self.guard.require_clear() def test_corrupted_record_blocks(self): path = self.marker(".migration-intent.json") path.write_text("{invalid") with self.assertRaises(OperationBlocked): self.guard.require_clear() def test_dangling_symlink_blocks(self): path = self.root / ( self.identifier + ".migration-intent.json" ) path.symlink_to(self.root / "missing") with self.assertRaises(OperationBlocked): self.guard.require_clear() def test_directory_marker_blocks(self): path = self.root / ( self.identifier + ".migration-result.json" ) path.mkdir() with self.assertRaises(OperationBlocked): self.guard.require_clear() def test_invalid_identifier_blocks(self): (self.root / "invalid.migration-intent.json").touch() with self.assertRaises(OperationBlocked): self.guard.require_clear() def test_temporary_publication_record_blocks(self): (self.root / ".approval-tmp-interrupted").touch() with self.assertRaises(OperationBlocked): self.guard.require_clear() def test_unknown_json_record_blocks(self): (self.root / "unexpected.json").touch() with self.assertRaises(OperationBlocked): self.guard.require_clear() def test_multiple_operations_reported(self): self.marker(".migration-intent.json") self.marker(".consumed.json") result = self.guard.inspect() self.assertEqual(result["status"], "BLOCKED") self.assertEqual(len(result["blockers"]), 2) def test_deterministic_report(self): self.marker(".migration-result.json") self.marker(".consumed.json") self.assertEqual( self.guard.inspect(), self.guard.inspect(), ) def test_read_only_inspection(self): path = self.marker(".migration-intent.json") path.write_text("original") before = path.read_bytes() self.guard.inspect() self.assertEqual(path.read_bytes(), before) def test_lock_file_ignored(self): (self.root / ".approval.lock").touch(mode=0o600) self.guard.require_clear() def test_insecure_directory_blocks(self): self.root.chmod(0o755) with self.assertRaises(OperationBlocked): self.guard.require_clear() def test_missing_directory_blocks(self): self.root.rename(self.root.with_name( self.root.name + "-moved" )) moved = self.root.with_name( self.root.name + "-moved" ) self.addCleanup(moved.rmdir) with self.assertRaises(OperationBlocked): self.guard.require_clear() if __name__ == "__main__": unittest.main()