import tempfile import unittest from datetime import UTC, datetime, timedelta from pathlib import Path from scheduling_core.ledger import Ledger, OperationState from scheduling_core.policy import PolicyConfig, evaluate_intent from scheduling_core.workflow import SchedulingWorkflow, WorkflowError NOW = datetime(2030, 12, 31, 16, tzinfo=UTC) CONFIG = PolicyConfig(revision="test", fixed_sender="petra@example.test", max_exchange_messages=8) def intent(): return evaluate_intent({"transaction_id":"workflow-tx","requester":"requester@example.test","sender":"petra@example.test","start_at":"2031-01-01T18:00:00Z","end_at":"2031-01-01T19:00:00Z","expires_at":"2031-01-02T00:00:00Z","authentication_evidence_id":"auth","availability_evidence_id":"calendar"}, CONFIG, NOW, []) class WorkflowTests(unittest.TestCase): def setUp(self): self.temp = tempfile.TemporaryDirectory() self.ledger = Ledger(Path(self.temp.name) / "ledger.sqlite3") self.ledger.create_transaction(intent(), CONFIG.revision, CONFIG.max_exchange_messages, owner="owner@example.test") self.workflow = SchedulingWorkflow(self.ledger) def tearDown(self): self.temp.cleanup() def test_offer_selection_booking_and_confirmation(self): slots = tuple({"slot_id": name, "start_at":"2031-01-01T18:00:00Z", "end_at":"2031-01-01T19:00:00Z", "expires_at":"2031-01-02T00:00:00Z", "availability_evidence_id":"calendar", "authentication_evidence_id":"auth"} for name in ("slot-a", "slot-b", "slot-c")) round_data = self.workflow.begin_round("workflow-tx", slots, NOW + timedelta(hours=2), "gmail-thread", NOW) self.workflow.bind_outbound("workflow-tx", round_data["round_number"], "postmark-outbound") self.assertEqual(self.workflow.accept_selection("workflow-tx", slot_id="slot-b", transaction_token=round_data["transaction_token"], thread_id="gmail-thread", in_reply_to="postmark-outbound", references=("postmark-outbound",), now=NOW), "slot-b") lease = self.ledger.claim("workflow-tx", 60, NOW).lease_token self.ledger.record_booking_evidence("workflow-tx", lease, {"availability_evidence_id":"fresh", "observed_at":NOW.isoformat(), "slot_id":"slot-b", "start_at":"2031-01-01T18:00:00Z", "end_at":"2031-01-01T19:00:00Z"}, NOW) self.ledger.reserve_closing_capacity("workflow-tx", lease, NOW) self.ledger.reserve_booking_operation("workflow-tx", lease, "calendar-op", "calendar") self.ledger.reserve_booking_operation("workflow-tx", lease, "zoom-op", "zoom") self.ledger.transition_operation("workflow-tx", lease, "zoom-op", OperationState.ACCEPTED, "zoom-id") import hashlib digest = lambda value: hashlib.sha256(value.strip().casefold().encode()).hexdigest() self.ledger.transition_operation("workflow-tx", lease, "zoom-op", OperationState.COMPLETED, "zoom-id", {"meeting_id":"zoom-id", "join_url_digest":digest("https://zoom.test/j/zoom-id"), "start_time":"2031-01-01T18:00:00+00:00", "duration_minutes":60, "timezone":"UTC", "host_fingerprint":digest("owner@example.test")}) self.ledger.transition_operation("workflow-tx", lease, "calendar-op", OperationState.ACCEPTED, "calendar-id") self.ledger.transition_operation("workflow-tx", lease, "calendar-op", OperationState.COMPLETED, "calendar-id", {"event_id":"calendar-id", "start_time":"2031-01-01T18:00:00Z", "end_time":"2031-01-01T19:00:00Z", "attendee_fingerprints":sorted([digest("requester@example.test"), digest("owner@example.test")]), "status":"confirmed", "visibility":"private", "join_url_candidates":[digest("https://zoom.test/j/zoom-id")], "guests_can_modify":None}) self.workflow.require_booking_confirmed("workflow-tx") self.assertEqual(self.ledger.transaction_status("workflow-tx")["state"], "completed") def test_pending_negotiation_uses_default_duration_then_offers_before_selection(self): ledger = Ledger(Path(self.temp.name) / "negotiation.sqlite3") ledger.create_pending_negotiation(transaction_id="negotiation", requester="requester@example.test", sender="petra@example.test", owner="owner@example.test", authentication_evidence_id="auth", gmail_thread_id="thread", policy_revision="test", default_duration_minutes=60, max_exchanges=8, expires_at=NOW + timedelta(hours=72)) workflow = SchedulingWorkflow(ledger) offer = workflow.begin_round("negotiation", ("slot-1", "slot-2", "slot-3"), NOW + timedelta(hours=72), "thread", NOW) self.assertEqual(offer["slot_ids"], ("slot-1", "slot-2", "slot-3")) self.assertEqual(ledger.transaction_status("negotiation")["state"], "awaiting_requester") def test_rejects_unbound_duplicate_or_expired_selection(self): round_data = self.workflow.begin_round("workflow-tx", ("slot-a",), NOW + timedelta(hours=1), "thread", NOW) self.workflow.bind_outbound("workflow-tx", 1, "outbound") with self.assertRaises(WorkflowError): self.workflow.accept_selection("workflow-tx", slot_id="other", transaction_token=round_data["transaction_token"], thread_id="thread", in_reply_to="outbound", references=("outbound",), now=NOW) self.workflow.accept_selection("workflow-tx", slot_id="slot-a", transaction_token=round_data["transaction_token"], thread_id="thread", in_reply_to="outbound", references=("outbound",), now=NOW) with self.assertRaises(WorkflowError): self.workflow.accept_selection("workflow-tx", slot_id="slot-a", transaction_token=round_data["transaction_token"], thread_id="thread", in_reply_to="outbound", references=("outbound",), now=NOW) self.ledger.create_transaction(evaluate_intent({"transaction_id":"expired-tx","requester":"requester@example.test","sender":"petra@example.test","start_at":"2031-01-01T18:00:00Z","end_at":"2031-01-01T19:00:00Z","expires_at":"2031-01-02T00:00:00Z","authentication_evidence_id":"auth","availability_evidence_id":"calendar"}, CONFIG, NOW, []), CONFIG.revision, CONFIG.max_exchange_messages) expired = self.workflow.begin_round("expired-tx", ("slot-a",), NOW + timedelta(minutes=1), "thread", NOW) self.workflow.bind_outbound("expired-tx", 1, "outbound") with self.assertRaises(WorkflowError): self.workflow.accept_selection("expired-tx", slot_id="slot-a", transaction_token=expired["transaction_token"], thread_id="thread", in_reply_to="outbound", references=("outbound",), now=NOW + timedelta(minutes=2))