Skip to content

Commit a39e5ee

Browse files
authored
Merge pull request #90 from redis/copilot/sub-pr-86-another-one
Fix CLI token show command to exit with code 1 on errors in JSON mode
2 parents cbc4d78 + c0f9a50 commit a39e5ee

File tree

2 files changed

+89
-15
lines changed

2 files changed

+89
-15
lines changed

agent_memory_server/cli.py

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -501,24 +501,44 @@ async def show_token():
501501
matching_hashes = [h for h in token_hashes if h.startswith(token_hash)]
502502

503503
if not matching_hashes:
504-
if output_format == "text":
504+
if output_format == "json":
505+
click.echo(
506+
json.dumps({"error": f"No token found matching '{token_hash}'"})
507+
)
508+
sys.exit(1)
509+
else:
505510
click.echo(f"No token found matching '{token_hash}'")
506-
return None
511+
sys.exit(1)
507512
if len(matching_hashes) > 1:
508-
if output_format == "text":
513+
if output_format == "json":
514+
click.echo(
515+
json.dumps(
516+
{
517+
"error": f"Multiple tokens match '{token_hash}'",
518+
"matches": [
519+
f"{h[:8]}...{h[-8:]}" for h in matching_hashes
520+
],
521+
}
522+
)
523+
)
524+
sys.exit(1)
525+
else:
509526
click.echo(f"Multiple tokens match '{token_hash}':")
510527
for h in matching_hashes:
511528
click.echo(f" {h[:8]}...{h[-8:]}")
512-
return None
529+
sys.exit(1)
513530
token_hash = matching_hashes[0]
514531

515532
key = Keys.auth_token_key(token_hash)
516533
token_data = await redis.get(key)
517534

518535
if not token_data:
519-
if output_format == "text":
536+
if output_format == "json":
537+
click.echo(json.dumps({"error": f"Token not found: {token_hash}"}))
538+
sys.exit(1)
539+
else:
520540
click.echo(f"Token not found: {token_hash}")
521-
return None
541+
sys.exit(1)
522542

523543
try:
524544
token_info = TokenInfo.model_validate_json(token_data)
@@ -541,13 +561,6 @@ async def show_token():
541561
sys.exit(1)
542562

543563
result = asyncio.run(show_token())
544-
545-
if result is None:
546-
if output_format == "json":
547-
click.echo(json.dumps({"error": "Token not found or error occurred"}))
548-
sys.exit(1)
549-
return
550-
551564
token_hash, token_info, status = result
552565

553566
if output_format == "json":

tests/test_token_cli.py

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,13 +248,74 @@ def test_token_show_command_partial_hash(
248248
def test_token_show_command_not_found(self, mock_get_redis, mock_redis, cli_runner):
249249
"""Test token show command with non-existent token."""
250250
mock_get_redis.return_value = mock_redis
251-
mock_redis.get.return_value = None
251+
mock_redis.smembers.return_value = set()
252252

253253
result = cli_runner.invoke(token, ["show", "nonexistent"])
254254

255-
assert result.exit_code == 0
255+
assert result.exit_code == 1
256256
assert "No token found matching" in result.output
257257

258+
@patch("agent_memory_server.cli.get_redis_conn")
259+
def test_token_show_command_not_found_json(
260+
self, mock_get_redis, mock_redis, cli_runner
261+
):
262+
"""Test token show command with non-existent token (JSON output)."""
263+
mock_get_redis.return_value = mock_redis
264+
mock_redis.smembers.return_value = set()
265+
266+
import json
267+
268+
result = cli_runner.invoke(token, ["show", "nonexistent", "--format", "json"])
269+
270+
assert result.exit_code == 1
271+
data = json.loads(result.output)
272+
assert "error" in data
273+
assert "No token found matching" in data["error"]
274+
275+
@patch("agent_memory_server.cli.get_redis_conn")
276+
def test_token_show_command_multiple_matches_json(
277+
self, mock_get_redis, mock_redis, cli_runner
278+
):
279+
"""Test token show command with multiple matches (JSON output)."""
280+
mock_get_redis.return_value = mock_redis
281+
282+
# Create multiple token hashes with same prefix
283+
token_hashes = {
284+
"test_hash_111111111111111111111111111111",
285+
"test_hash_222222222222222222222222222222",
286+
}
287+
288+
mock_redis.smembers.return_value = token_hashes
289+
290+
import json
291+
292+
result = cli_runner.invoke(token, ["show", "test_hash", "--format", "json"])
293+
294+
assert result.exit_code == 1
295+
data = json.loads(result.output)
296+
assert "error" in data
297+
assert "Multiple tokens match" in data["error"]
298+
assert "matches" in data
299+
assert len(data["matches"]) == 2
300+
301+
@patch("agent_memory_server.cli.get_redis_conn")
302+
def test_token_show_command_token_not_in_redis_json(
303+
self, mock_get_redis, mock_redis, cli_runner
304+
):
305+
"""Test token show command when token not in Redis (JSON output)."""
306+
mock_get_redis.return_value = mock_redis
307+
mock_redis.get.return_value = None
308+
309+
import json
310+
311+
token_hash = "test_hash_123456789012345678901234567890"
312+
result = cli_runner.invoke(token, ["show", token_hash, "--format", "json"])
313+
314+
assert result.exit_code == 1
315+
data = json.loads(result.output)
316+
assert "error" in data
317+
assert "Token not found" in data["error"]
318+
258319
@patch("agent_memory_server.cli.get_redis_conn")
259320
def test_token_remove_command_with_confirmation(
260321
self, mock_get_redis, mock_redis, cli_runner

0 commit comments

Comments
 (0)