@@ -23,8 +23,9 @@ def test_query_without_instance_uses_knowledge_agent():
2323 mock_agent .process_query = AsyncMock (return_value = "ok" )
2424
2525 with (
26- patch ("redis_sre_agent.cli.query.get_knowledge_agent" , return_value = mock_agent )
27- as mock_get_knowledge ,
26+ patch (
27+ "redis_sre_agent.cli.query.get_knowledge_agent" , return_value = mock_agent
28+ ) as mock_get_knowledge ,
2829 patch ("redis_sre_agent.cli.query.get_sre_agent" ) as mock_get_sre ,
2930 patch (
3031 "redis_sre_agent.cli.query.get_instance_by_id" ,
@@ -90,7 +91,12 @@ def __init__(self, id: str, name: str): # noqa: A003 - keep click-style arg nam
9091 assert kwargs .get ("context" ) == {"instance_id" : instance .id }
9192
9293
93- def test_query_with_unknown_instance_falls_back_to_knowledge_agent ():
94+ def test_query_with_unknown_instance_exits_with_error_and_skips_agents ():
95+ """If -r is provided but the instance does not exist, CLI should error and exit.
96+
97+ This directly tests the new existence-check logic in redis_sre_agent.cli.query.
98+ """
99+
94100 runner = CliRunner ()
95101
96102 mock_agent = MagicMock ()
@@ -117,16 +123,14 @@ def test_query_with_unknown_instance_falls_back_to_knowledge_agent():
117123 ],
118124 )
119125
120- assert result .exit_code == 0 , result .output
126+ # CLI should exit with non-zero status (explicitly exit(1) in implementation)
127+ assert result .exit_code == 1
128+ assert f"Instance not found: { missing_id } " in result .output
121129
122- # We attempted to resolve the instance ID
130+ # We attempted to resolve the instance ID once
123131 mock_get_instance .assert_awaited_once_with (missing_id )
124132
125- # Without a resolved instance, we should route to the knowledge agent
126- mock_get_knowledge .assert_called_once ()
133+ # Since the instance doesn't exist, no agent should be initialized or invoked
134+ mock_get_knowledge .assert_not_called ()
127135 mock_get_sre .assert_not_called ()
128-
129- # Knowledge agent is called once; it should not receive a non-None instance context
130- mock_agent .process_query .assert_awaited_once ()
131- _ , kwargs = mock_agent .process_query .call_args
132- assert kwargs .get ("context" ) is None
136+ mock_agent .process_query .assert_not_awaited ()
0 commit comments