|
| 1 | +""" |
| 2 | +Regression tests for group config tool post-operation verification. |
| 3 | +
|
| 4 | +Verifies that group tools confirm entity state after service calls, |
| 5 | +preventing the false-success anti-pattern where the tool returns |
| 6 | +success before the entity is actually queryable. |
| 7 | +""" |
| 8 | + |
| 9 | +import logging |
| 10 | + |
| 11 | +import pytest |
| 12 | + |
| 13 | +from ...utilities.assertions import assert_mcp_success, safe_call_tool |
| 14 | + |
| 15 | +logger = logging.getLogger(__name__) |
| 16 | + |
| 17 | + |
| 18 | +@pytest.mark.group |
| 19 | +class TestGroupVerification: |
| 20 | + """Verify that group operations confirm entity state.""" |
| 21 | + |
| 22 | + async def test_created_group_is_immediately_queryable( |
| 23 | + self, mcp_client, cleanup_tracker |
| 24 | + ): |
| 25 | + """After ha_config_set_group succeeds, the entity must be queryable. |
| 26 | +
|
| 27 | + Regression test: before verification was added, the tool returned |
| 28 | + success with a predicted entity_id that might not exist yet. |
| 29 | + """ |
| 30 | + object_id = "test_e2e_verify_create" |
| 31 | + |
| 32 | + result = await mcp_client.call_tool( |
| 33 | + "ha_config_set_group", |
| 34 | + { |
| 35 | + "object_id": object_id, |
| 36 | + "name": "Verification Test Group", |
| 37 | + "entities": ["light.bed_light"], |
| 38 | + }, |
| 39 | + ) |
| 40 | + |
| 41 | + data = assert_mcp_success(result, "Create group") |
| 42 | + entity_id = data.get("entity_id") |
| 43 | + assert entity_id == f"group.{object_id}" |
| 44 | + cleanup_tracker.track("group", object_id) |
| 45 | + |
| 46 | + # The entity must be queryable immediately after the tool returns |
| 47 | + state_result = await mcp_client.call_tool( |
| 48 | + "ha_get_state", {"entity_id": entity_id} |
| 49 | + ) |
| 50 | + state_data = assert_mcp_success(state_result, "Get group state after create") |
| 51 | + # Response may nest entity data under "data" key |
| 52 | + inner = state_data.get("data", state_data) |
| 53 | + assert inner.get("entity_id") == entity_id, ( |
| 54 | + f"Created group not queryable immediately after tool returned success: {state_data}" |
| 55 | + ) |
| 56 | + logger.info(f"Group {entity_id} confirmed queryable after create") |
| 57 | + |
| 58 | + # Cleanup |
| 59 | + await mcp_client.call_tool( |
| 60 | + "ha_config_remove_group", {"object_id": object_id} |
| 61 | + ) |
| 62 | + |
| 63 | + async def test_removed_group_is_immediately_gone( |
| 64 | + self, mcp_client, cleanup_tracker |
| 65 | + ): |
| 66 | + """After ha_config_remove_group succeeds, the entity must be gone. |
| 67 | +
|
| 68 | + Regression test: before verification was added, the tool returned |
| 69 | + success but the entity could still be queryable briefly. |
| 70 | + """ |
| 71 | + object_id = "test_e2e_verify_remove" |
| 72 | + |
| 73 | + # Create a group first |
| 74 | + result = await mcp_client.call_tool( |
| 75 | + "ha_config_set_group", |
| 76 | + { |
| 77 | + "object_id": object_id, |
| 78 | + "name": "Removal Verification Group", |
| 79 | + "entities": ["light.bed_light"], |
| 80 | + }, |
| 81 | + ) |
| 82 | + assert_mcp_success(result, "Create group for removal test") |
| 83 | + cleanup_tracker.track("group", object_id) |
| 84 | + |
| 85 | + # Remove it |
| 86 | + remove_result = await mcp_client.call_tool( |
| 87 | + "ha_config_remove_group", {"object_id": object_id} |
| 88 | + ) |
| 89 | + assert_mcp_success(remove_result, "Remove group") |
| 90 | + |
| 91 | + # The entity must NOT be queryable after the tool returns |
| 92 | + entity_id = f"group.{object_id}" |
| 93 | + state_data = await safe_call_tool( |
| 94 | + mcp_client, "ha_get_state", {"entity_id": entity_id} |
| 95 | + ) |
| 96 | + # Should either fail or return not_found |
| 97 | + is_gone = ( |
| 98 | + not state_data.get("success") |
| 99 | + or state_data.get("state") == "unavailable" |
| 100 | + or "not found" in str(state_data).lower() |
| 101 | + ) |
| 102 | + assert is_gone, ( |
| 103 | + f"Removed group still queryable after tool returned success: {state_data}" |
| 104 | + ) |
| 105 | + logger.info(f"Group {entity_id} confirmed gone after remove") |
0 commit comments