test_prompts_cli.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. """
  2. Tests for the prompts commands in the CLI.
  3. - list
  4. - retrieve
  5. x delete
  6. """
  7. import json
  8. import uuid
  9. import pytest
  10. from click.testing import CliRunner
  11. from cli.commands.prompts import list, retrieve
  12. from r2r import R2RAsyncClient
  13. from tests.cli.async_invoke import async_invoke
  14. def extract_json_block(output: str) -> dict:
  15. """Extract and parse the first valid JSON object found in the output."""
  16. start = output.find("{")
  17. if start == -1:
  18. raise ValueError("No JSON object start found in output")
  19. brace_count = 0
  20. for i, char in enumerate(output[start:], start=start):
  21. if char == "{":
  22. brace_count += 1
  23. elif char == "}":
  24. brace_count -= 1
  25. if brace_count == 0:
  26. json_str = output[start : i + 1].strip()
  27. return json.loads(json_str)
  28. raise ValueError("No complete JSON object found in output")
  29. @pytest.mark.asyncio
  30. async def test_prompts_list():
  31. """Test listing prompts."""
  32. client = R2RAsyncClient(base_url="http://localhost:7272")
  33. runner = CliRunner(mix_stderr=False)
  34. # Test basic list
  35. list_result = await async_invoke(runner, list, obj=client)
  36. assert list_result.exit_code == 0
  37. @pytest.mark.asyncio
  38. async def test_prompts_retrieve():
  39. """Test retrieving prompts with various parameters."""
  40. client = R2RAsyncClient(base_url="http://localhost:7272")
  41. runner = CliRunner(mix_stderr=False)
  42. # Test retrieve with just name
  43. name = "hyde"
  44. retrieve_result = await async_invoke(runner, retrieve, name, obj=client)
  45. assert retrieve_result.exit_code == 0
  46. @pytest.mark.asyncio
  47. async def test_nonexistent_prompt():
  48. """Test operations on a nonexistent prompt."""
  49. client = R2RAsyncClient(base_url="http://localhost:7272")
  50. runner = CliRunner(mix_stderr=False)
  51. nonexistent_name = f"nonexistent-{uuid.uuid4()}"
  52. # Test retrieve
  53. retrieve_result = await async_invoke(
  54. runner, retrieve, nonexistent_name, obj=client
  55. )
  56. assert "not found" in retrieve_result.stderr_bytes.decode().lower()
  57. @pytest.mark.asyncio
  58. async def test_prompt_retrieve_with_all_options():
  59. """Test retrieving a prompt with all options combined."""
  60. client = R2RAsyncClient(base_url="http://localhost:7272")
  61. runner = CliRunner(mix_stderr=False)
  62. name = "example-prompt"
  63. inputs = "input1,input2"
  64. override = "custom prompt text"
  65. retrieve_result = await async_invoke(
  66. runner,
  67. retrieve,
  68. name,
  69. "--inputs",
  70. inputs,
  71. "--prompt-override",
  72. override,
  73. obj=client,
  74. )
  75. assert retrieve_result.exit_code == 0