"""The write path, tested where it is decided rather than where it is described. Every rule here used to live in the system prompt, which is to say it used to be a request. These tests are the difference: they fail if the mechanism stops enforcing what the prompt merely asks for. """ from __future__ import annotations from typing import Any import pytest from pydantic_ai.tools import ToolDefinition from catalogue_agent.a2a_server import _reads_as_approval from catalogue_agent.approval import is_write, validate_draft def tool(name: str, *, read_only: bool | None = None) -> ToolDefinition: metadata: dict[str, Any] = {} if read_only is not None: metadata["annotations"] = {"readOnlyHint": read_only} return ToolDefinition(name=name, parameters_json_schema={}, metadata=metadata) class TestIsWrite: def test_the_servers_annotation_decides(self) -> None: # Not the name. The gateway publishes readOnlyHint for every tool, and # that is what the agent obeys — so renaming a tool cannot silently # change whether it needs approval. assert is_write(tool("catalog_get-product", read_only=True)) is False assert is_write(tool("catalog_create-product", read_only=False)) is True def test_an_annotation_beats_a_reassuring_name(self) -> None: assert is_write(tool("catalog_list-products", read_only=False)) is True def test_the_verb_answers_when_nothing_is_annotated(self) -> None: assert is_write(tool("catalog_create-product")) is True assert is_write(tool("catalog_list-products")) is False def test_an_unrecognisable_unannotated_tool_is_treated_as_a_write(self) -> None: # Asking for approval needlessly is a nuisance. Skipping it is not. assert is_write(tool("catalog_reconcile")) is True class TestValidateDraft: def valid(self) -> dict[str, Any]: return { "productcode": "S99_TEST", "productname": "Test Roadster", "productline": "Classic Cars", "productscale": "1:18", "productvendor": "Lab Fixtures", "productdescription": "a test", "quantityinstock": 10, "buyprice": "50.00", "msrp": "99.00", } def test_a_complete_draft_passes(self) -> None: assert validate_draft("catalog_create-product", {"body": self.valid()}) is None def test_a_read_tool_has_nothing_to_validate(self) -> None: assert validate_draft("catalog_list-products", {"query_page": 2}) is None @pytest.mark.parametrize( ("field", "value"), [("buyprice", "0"), ("msrp", "0"), ("quantityinstock", -1), ("productname", "")], ) def test_values_a_json_schema_would_accept_are_refused(self, field: str, value: Any) -> None: # The MCP server already checks types and required fields. These are the # ones only a validator can catch. body = self.valid() | {field: value} assert validate_draft("catalog_create-product", {"body": body}) is not None def test_a_price_that_is_not_a_number_is_refused(self) -> None: body = self.valid() | {"msrp": "ask the vendor"} problem = validate_draft("catalog_create-product", {"body": body}) assert problem is not None and "not a number" in problem def test_selling_below_cost_is_refused(self) -> None: body = self.valid() | {"buyprice": "99.00", "msrp": "50.00"} problem = validate_draft("catalog_create-product", {"body": body}) assert problem is not None and "not above buyprice" in problem def test_a_missing_field_is_refused_rather_than_defaulted(self) -> None: body = self.valid() del body["buyprice"] assert validate_draft("catalog_create-product", {"body": body}) is not None def test_an_unexpected_field_is_refused(self) -> None: body = self.valid() | {"discount": "20%"} assert validate_draft("catalog_create-product", {"body": body}) is not None class TestReadsAsApproval: @pytest.mark.parametrize("reply", ["approve", "Approve.", " oui ", "OK", "confirme"]) def test_an_unambiguous_yes_approves(self, reply: str) -> None: assert _reads_as_approval(reply) is True @pytest.mark.parametrize( "reply", [ "oui mais change le prix", # a condition, not an approval "why are you asking?", "no", "", "approuve le suivant plutôt", ], ) def test_anything_else_cancels(self, reply: str) -> None: # Silence dressed as agreement is the failure this guards against: a # reply that merely contains "oui" must not be read as consent to the # record as proposed. assert _reads_as_approval(reply) is False