import tempfile import unittest from datetime import UTC, datetime, timedelta from pathlib import Path from scheduling_core.gmail_validation import GmailValidationError, SanitizedGmailMetadata, validate_intake, validate_reply_lineage, validate_owner_forward_boundary from scheduling_core.ledger import Ledger, OperationState from scheduling_core.policy import PolicyConfig, PolicyViolation, evaluate_intent, load_policy_config, validate_offer_slots from scheduling_core.postmark import PostmarkHttpsClient, ProviderDisabled NOW = datetime(2030, 12, 31, 16, tzinfo=UTC) CONFIG = PolicyConfig(revision="test", fixed_sender="petra@example.test") def intent(number="1", **overrides): data = {"transaction_id": f"tx-{number}", "requester": "requester@example.test", "sender": "petra@example.test", "start_at": f"2031-01-0{number}T18:00:00Z", "end_at": f"2031-01-0{number}T19:00:00Z", "expires_at": "2031-01-02T00:00:00Z", "authentication_evidence_id": "auth-id", "availability_evidence_id": "busy-id"} data.update(overrides) return data class PolicyAndRecoveryTests(unittest.TestCase): def test_duration_horizon_workweek_and_offer_count(self): with self.assertRaises(PolicyViolation): evaluate_intent(intent(end_at="2031-01-01T19:20:00Z"), CONFIG, NOW, []) with self.assertRaises(PolicyViolation): evaluate_intent(intent(start_at="2031-01-23T18:00:00Z", end_at="2031-01-23T19:00:00Z"), CONFIG, NOW, []) with self.assertRaises(PolicyViolation): validate_offer_slots([intent()], CONFIG, NOW, []) slots = [intent("1"), intent("2"), intent("3")] self.assertEqual(len(validate_offer_slots(slots, CONFIG, NOW, [])), 3) def test_dst_offsets_are_evaluated_in_denver_local_work_hours(self): before_spring_forward = datetime(2031, 3, 7, 16, tzinfo=UTC) spring = intent("1", start_at="2031-03-10T15:00:00Z", end_at="2031-03-10T16:00:00Z", expires_at="2031-03-10T15:30:00Z") self.assertEqual(evaluate_intent(spring, CONFIG, before_spring_forward, []).start_at, "2031-03-10T15:00:00Z") before_fall_back = datetime(2031, 10, 31, 16, 30, tzinfo=UTC) fall = intent("2", start_at="2031-11-03T16:00:00Z", end_at="2031-11-03T17:00:00Z", expires_at="2031-11-03T16:20:00Z") self.assertEqual(evaluate_intent(fall, CONFIG, before_fall_back, []).start_at, "2031-11-03T16:00:00Z") def test_gmail_metadata_is_bound_without_retaining_headers(self): intake = SanitizedGmailMetadata("m1", "t1", "owner@example.test", "route@example.test", "intake", authenticated=True, forwarded_requester="requester@example.test") self.assertEqual(validate_intake(intake, "owner@example.test", "route@example.test", "intake").message_id, "m1") reply = SanitizedGmailMetadata("m2", "t1", "requester@example.test", "route@example.test", "intake", "outbound-1", ("outbound-1",), "token", True) self.assertEqual(validate_reply_lineage(reply, "requester@example.test", "route@example.test", "intake", "outbound-1", "token").message_id, "m2") with self.assertRaises(GmailValidationError): validate_reply_lineage(reply, "requester@example.test", "route@example.test", "intake", "other", "token") def test_owner_forward_boundary_uses_canonical_route_and_provider_context(self): headers = (("From", "owner@example.test"), ("To", "route@example.test")) validate_owner_forward_boundary(headers, "owner@example.test", "route@example.test", authenticated_mailbox=True, intake_label_present=True) with self.assertRaises(GmailValidationError): validate_owner_forward_boundary(headers, "owner@example.test", "route@example.test", authenticated_mailbox=False, intake_label_present=True) def test_restart_recovers_expired_lease_and_uncertain_operation(self): with tempfile.TemporaryDirectory() as directory: path = Path(directory) / "ledger.sqlite3" ledger = Ledger(path) checked = evaluate_intent(intent(), CONFIG, NOW, []) ledger.create_transaction(checked, CONFIG.revision, CONFIG.max_exchange_messages) self.assertIsNotNone(ledger.claim("tx-1", 1, NOW)) # Simulate a new process after the lease has elapsed. restarted = Ledger(path) self.assertIsNotNone(restarted.claim("tx-1", 1, NOW + timedelta(seconds=2))) lease = restarted.claim("tx-1", 60, NOW + timedelta(seconds=4)).lease_token restarted.record_postmark_send("tx-1", lease, "op-1", "petra@example.test", "requester@example.test", NOW + timedelta(seconds=4)) restarted.transition_operation("tx-1", lease, "op-1", OperationState.UNCERTAIN) self.assertEqual(restarted.operations_needing_reconciliation()[0]["operation_key"], "op-1") def test_existing_postmark_operation_remains_idempotent_after_expiry(self): with tempfile.TemporaryDirectory() as directory: ledger = Ledger(Path(directory) / "ledger.sqlite3") checked = evaluate_intent(intent(), CONFIG, NOW, []) ledger.create_transaction(checked, CONFIG.revision, CONFIG.max_exchange_messages) lease = ledger.claim("tx-1", 60, NOW).lease_token first = ledger.record_postmark_send("tx-1", lease, "send-1", "petra@example.test", "requester@example.test", NOW) expired_lease = ledger.claim("tx-1", 60, datetime(2031, 1, 3, tzinfo=UTC)).lease_token same = ledger.record_postmark_send("tx-1", expired_lease, "send-1", "petra@example.test", "requester@example.test", datetime(2031, 1, 3, tzinfo=UTC)) self.assertEqual(first["id"], same["id"]) def test_https_client_cannot_call_without_explicit_enablement(self): client = PostmarkHttpsClient(lambda: "not-used") with self.assertRaises(ProviderDisabled): client.status("message-id") def test_reviewed_policy_file_loads_approved_defaults(self): config = load_policy_config(Path("config/scheduling-policy.json"), "petra@example.test") self.assertEqual(config.minimum_notice_minutes, 24 * 60) self.assertEqual(config.offer_count, 3) self.assertTrue(config.guests_can_invite_others)