Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
Mirrored from https://github.com/Camo67/composio.git
Synced Nov 21, 03:37 UTC
| 1 | """Exercise the same approval helpers used by the runnable examples.""" |
| 2 | |
| 3 | import pytest |
| 4 | from composio.exceptions import ToolInputRequiredError |
| 5 | from examples.approvals import ask_for_approval, run_with_approvals |
| 6 | |
| 7 | QUESTION = { |
| 8 | "type": "elicitation", |
| 9 | "mode": "form", |
| 10 | "message": "Allow this read-only call?", |
| 11 | "requested_schema": { |
| 12 | "type": "object", |
| 13 | "properties": { |
| 14 | "decision": { |
| 15 | "type": "string", |
| 16 | "oneOf": [ |
| 17 | {"const": "approve"}, |
| 18 | {"const": "always_approve"}, |
| 19 | {"const": "deny"}, |
| 20 | ], |
| 21 | } |
| 22 | }, |
| 23 | "required": ["decision"], |
| 24 | }, |
| 25 | } |
| 26 | |
| 27 | |
| 28 | @pytest.mark.parametrize( |
| 29 | "choice", ["approve", "always_approve", "deny", "decline", "cancel"] |
| 30 | ) |
| 31 | def test_forwards_the_persons_choice(choice): |
| 32 | response = ask_for_approval(QUESTION, lambda _: choice) |
| 33 | assert response == ( |
| 34 | {"action": choice} |
| 35 | if choice in ("decline", "cancel") |
| 36 | else {"action": "accept", "content": {"decision": choice}} |
| 37 | ) |
| 38 | |
| 39 | |
| 40 | def test_invalid_choice_is_not_submitted(): |
| 41 | answers = iter(["yes", "approve"]) |
| 42 | assert ask_for_approval(QUESTION, lambda _: next(answers)) == { |
| 43 | "action": "accept", |
| 44 | "content": {"decision": "approve"}, |
| 45 | } |
| 46 | assert list(answers) == [] |
| 47 | |
| 48 | |
| 49 | @pytest.mark.parametrize("error", [EOFError, KeyboardInterrupt]) |
| 50 | def test_closed_terminal_cancels(error): |
| 51 | def closed(_): |
| 52 | raise error() |
| 53 | |
| 54 | assert ask_for_approval(QUESTION, closed) == {"action": "cancel"} |
| 55 | |
| 56 | |
| 57 | def test_all_batch_answers_and_new_continuation_state_are_forwarded(): |
| 58 | calls = [] |
| 59 | answers = iter( |
| 60 | [ |
| 61 | {"action": "accept", "content": {"decision": "approve"}}, |
| 62 | {"action": "decline"}, |
| 63 | {"action": "cancel"}, |
| 64 | ] |
| 65 | ) |
| 66 | |
| 67 | def execute(**options): |
| 68 | calls.append(options) |
| 69 | if len(calls) == 1: |
| 70 | raise ToolInputRequiredError( |
| 71 | "Batch", |
| 72 | input_requests={"first": QUESTION, "second": QUESTION}, |
| 73 | request_state="first-state", |
| 74 | ) |
| 75 | if len(calls) == 2: |
| 76 | assert options == { |
| 77 | "input_responses": { |
| 78 | "first": {"action": "accept", "content": {"decision": "approve"}}, |
| 79 | "second": {"action": "decline"}, |
| 80 | }, |
| 81 | "request_state": "first-state", |
| 82 | } |
| 83 | raise ToolInputRequiredError( |
| 84 | "Batch", input_requests={"next": QUESTION}, request_state="next-state" |
| 85 | ) |
| 86 | assert options == { |
| 87 | "input_responses": {"next": {"action": "cancel"}}, |
| 88 | "request_state": "next-state", |
| 89 | } |
| 90 | return "done" |
| 91 | |
| 92 | assert run_with_approvals(execute, lambda _: next(answers)) == "done" |
| 93 | assert len(calls) == 3 |
| 94 | |
| 95 | |
| 96 | def test_waits_for_the_person_before_resuming(): |
| 97 | calls = [] |
| 98 | |
| 99 | def execute(**options): |
| 100 | calls.append(options) |
| 101 | if not options: |
| 102 | raise ToolInputRequiredError("Read", input_requests={"id": QUESTION}) |
| 103 | return "cancelled" |
| 104 | |
| 105 | def ask_user(_): |
| 106 | assert calls == [{}] |
| 107 | return {"action": "cancel"} |
| 108 | |
| 109 | assert run_with_approvals(execute, ask_user) == "cancelled" |
| 110 | assert calls[1]["input_responses"] == {"id": {"action": "cancel"}} |
| 111 | |
| 112 | |
| 113 | def test_execution_failure_is_not_retried(): |
| 114 | calls = [] |
| 115 | |
| 116 | def execute(**options): |
| 117 | calls.append(options) |
| 118 | raise RuntimeError("network failure") |
| 119 | |
| 120 | with pytest.raises(RuntimeError, match="network failure"): |
| 121 | run_with_approvals(execute, lambda _: pytest.fail("must not ask")) |
| 122 | assert calls == [{}] |