132 lines
8.3 KiB
Python
132 lines
8.3 KiB
Python
import tempfile
|
|
import unittest
|
|
from datetime import UTC, datetime, timedelta
|
|
from pathlib import Path
|
|
|
|
from scheduling_core.ledger import Ledger
|
|
from scheduling_core.policy import PolicyConfig, PolicyViolation, evaluate_intent
|
|
from scheduling_core.postmark import BoundMessage, PostmarkAdapter, ProviderDisabled
|
|
from scheduling_core.mcp_stdio import Adapter
|
|
from scheduling_core.workflow import SchedulingWorkflow
|
|
|
|
NOW = datetime(2030, 12, 31, 16, tzinfo=UTC)
|
|
CONFIG = PolicyConfig(revision="offline-test", fixed_sender="[email protected]", max_exchange_messages=2)
|
|
|
|
|
|
def raw_intent(**changes):
|
|
value = {"transaction_id": "tx-1", "requester": "[email protected]", "sender": "[email protected]", "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-opaque", "availability_evidence_id": "freebusy-opaque"}
|
|
value.update(changes)
|
|
return value
|
|
|
|
|
|
class LedgerTests(unittest.TestCase):
|
|
def setUp(self):
|
|
self.temp = tempfile.TemporaryDirectory()
|
|
self.ledger = Ledger(Path(self.temp.name) / "ledger.sqlite3")
|
|
intent = evaluate_intent(raw_intent(), CONFIG, NOW, [])
|
|
self.ledger.create_transaction(intent, CONFIG.revision, CONFIG.max_exchange_messages)
|
|
|
|
def tearDown(self):
|
|
self.temp.cleanup()
|
|
|
|
def lease(self):
|
|
return self.ledger.claim("tx-1", 300).lease_token
|
|
|
|
def test_invalid_expired_and_buffered_inputs_rejected(self):
|
|
with self.assertRaises(PolicyViolation):
|
|
evaluate_intent(raw_intent(expires_at="2030-12-31T15:00:00Z"), CONFIG, NOW, [])
|
|
with self.assertRaises(PolicyViolation):
|
|
evaluate_intent(raw_intent(), CONFIG, NOW, [{"start_at": "2031-01-01T17:50:00Z", "end_at": "2031-01-01T18:10:00Z"}])
|
|
with self.assertRaises(PolicyViolation):
|
|
evaluate_intent(raw_intent(sender="[email protected]"), CONFIG, NOW, [])
|
|
|
|
def test_bound_sender_recipient_cap_and_duplicate_are_enforced(self):
|
|
lease = self.lease()
|
|
first = self.ledger.record_postmark_send("tx-1", lease, "send-1", "[email protected]", "[email protected]")
|
|
same = self.ledger.record_postmark_send("tx-1", lease, "send-1", "[email protected]", "[email protected]")
|
|
self.assertEqual(first["id"], same["id"])
|
|
self.ledger.record_postmark_send("tx-1", lease, "send-2", "[email protected]", "[email protected]")
|
|
with self.assertRaises(ValueError):
|
|
self.ledger.record_postmark_send("tx-1", lease, "send-3", "[email protected]", "[email protected]")
|
|
with self.assertRaises(ValueError):
|
|
self.ledger.record_postmark_send("tx-1", lease, "send-x", "[email protected]", "[email protected]")
|
|
|
|
def test_crash_or_timeout_is_reconciliation_not_resend(self):
|
|
class TimeoutClient:
|
|
def send(self, **_):
|
|
raise TimeoutError()
|
|
adapter = PostmarkAdapter(self.ledger, TimeoutClient())
|
|
lease = self.lease()
|
|
self.assertIsNone(adapter.send_bound_message("tx-1", lease, "send-1", "[email protected]", "[email protected]", BoundMessage("Subject", "Body")))
|
|
self.assertEqual(self.ledger.operations_needing_reconciliation()[0]["operation_key"], "send-1")
|
|
self.assertIsNone(adapter.send_bound_message("tx-1", lease, "send-1", "[email protected]", "[email protected]", BoundMessage("Subject", "Body")))
|
|
|
|
def test_stranded_dispatch_is_never_retried(self):
|
|
lease = self.lease()
|
|
self.ledger.record_postmark_send("tx-1", lease, "send-crash", "[email protected]", "[email protected]")
|
|
self.ledger.transition_operation("tx-1", lease, "send-crash", "dispatching")
|
|
class Client:
|
|
def send(self, **_): raise AssertionError("must reconcile, not resend")
|
|
self.assertIsNone(PostmarkAdapter(self.ledger, Client()).send_bound_message("tx-1", lease, "send-crash", "[email protected]", "[email protected]", BoundMessage("Subject", "Body")))
|
|
self.assertIn("send-crash", [row["operation_key"] for row in self.ledger.operations_needing_reconciliation()])
|
|
|
|
def test_disabled_adapter_never_sends(self):
|
|
with self.assertRaises(ProviderDisabled):
|
|
PostmarkAdapter(self.ledger).send_bound_message("tx-1", self.lease(), "send-disabled", "[email protected]", "[email protected]", BoundMessage("Subject", "Body"))
|
|
self.assertEqual(self.ledger.operations_needing_reconciliation(), [])
|
|
|
|
def test_claim_is_exclusive(self):
|
|
self.assertIsNotNone(self.ledger.claim("tx-1", 30))
|
|
self.assertIsNone(self.ledger.claim("tx-1", 30))
|
|
|
|
def test_mcp_adapter_exposes_only_narrow_tools(self):
|
|
response = Adapter(self.ledger, CONFIG).handle_request({"jsonrpc": "2.0", "id": 1, "method": "tools/list"})
|
|
self.assertIn("confirm_booking_availability", {tool["name"] for tool in response["result"]["tools"]})
|
|
self.assertIn("complete_booking", {tool["name"] for tool in response["result"]["tools"]})
|
|
|
|
def test_mcp_rejects_extra_or_untyped_arguments(self):
|
|
adapter = Adapter(self.ledger, CONFIG)
|
|
response = adapter.handle_request({"jsonrpc": "2.0", "id": 1, "method": "tools/call", "params": {"name": "claim_transaction", "arguments": {"transaction_id": "tx-1", "lease_seconds": "60"}}})
|
|
self.assertIn("error", response)
|
|
response = adapter.handle_request({"jsonrpc": "2.0", "id": 2, "method": "tools/call", "params": {"name": "claim_transaction", "arguments": {"transaction_id": "tx-1", "unexpected": True}}})
|
|
self.assertIn("error", response)
|
|
|
|
def test_mcp_reserves_calendar_and_zoom_without_calling_providers(self):
|
|
adapter = Adapter(self.ledger, CONFIG)
|
|
workflow = SchedulingWorkflow(self.ledger)
|
|
offer = workflow.begin_round("tx-1", ("slot-a",), datetime.now(UTC) + timedelta(hours=1), "thread")
|
|
workflow.bind_outbound("tx-1", offer["round_number"], "outbound")
|
|
workflow.accept_selection("tx-1", slot_id="slot-a", transaction_token=offer["transaction_token"], thread_id="thread", in_reply_to="outbound", references=("outbound",))
|
|
lease=adapter.call("claim_transaction", {"transaction_id":"tx-1"})["lease_token"]
|
|
with self.assertRaises(ValueError):
|
|
adapter.call("reserve_calendar_event", {"transaction_id": "tx-1", "lease_token":lease,"operation_key": "calendar-1"})
|
|
|
|
def test_mcp_postmark_execution_requires_injected_runtime_client(self):
|
|
lease = self.lease()
|
|
arguments = {"transaction_id": "tx-1", "lease_token":lease, "operation_key": "postmark-1", "sender": "[email protected]", "requester": "[email protected]", "purpose": "offer"}
|
|
disabled = Adapter(self.ledger, CONFIG).call("prepare_postmark_send", arguments)
|
|
self.assertEqual(disabled["status"], "disabled")
|
|
self.assertIsNone(self.ledger.operation_for_key("postmark-1"))
|
|
class Client:
|
|
def send(self, **kwargs):
|
|
self.kwargs = kwargs
|
|
return "postmark-provider-id"
|
|
workflow = SchedulingWorkflow(self.ledger)
|
|
workflow.begin_round("tx-1", ({"slot_id":"slot-a", "start_at":"2031-01-01T18:00:00Z", "end_at":"2031-01-01T19:00:00Z"},), datetime.now(UTC) + timedelta(hours=1), "thread")
|
|
client = Client()
|
|
result = Adapter(self.ledger, CONFIG, client).call("prepare_postmark_send", arguments)
|
|
self.assertEqual(result["status"], "accepted")
|
|
self.assertEqual(result["provider_id"], "postmark-provider-id")
|
|
self.assertRegex(result["outbound_rfc_message_id"], r"^<sched-[0-9a-f-]+@example\.test>$")
|
|
self.assertEqual(client.kwargs["recipient"], "[email protected]")
|
|
|
|
def test_mcp_rejects_calendar_or_postmark_before_offer_acceptance(self):
|
|
adapter = Adapter(self.ledger, CONFIG)
|
|
with self.assertRaises(ValueError):
|
|
adapter.call("reserve_calendar_event", {"transaction_id": "tx-1", "lease_token":"bad", "operation_key": "calendar-early"})
|
|
class Client:
|
|
def send(self, **_): return "unexpected"
|
|
with self.assertRaises(ValueError):
|
|
adapter = Adapter(self.ledger, CONFIG, Client())
|
|
adapter.call("prepare_postmark_send", {"transaction_id":"tx-1","lease_token":self.lease(),"operation_key":"mail-early","sender":"[email protected]","requester":"[email protected]","purpose":"offer","subject":"s","text_body":"b"})
|