|
| 1 | +# Copyright 2026 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +"""Tests for ToolConfirmation. |
| 16 | +
|
| 17 | +Verifies that ToolConfirmation correctly stores and validates confirmation |
| 18 | +states. |
| 19 | +""" |
| 20 | + |
| 21 | +from __future__ import annotations |
| 22 | + |
| 23 | +from google.adk.tools.tool_confirmation import ToolConfirmation |
| 24 | +from pydantic import ValidationError |
| 25 | +import pytest |
| 26 | + |
| 27 | +# ToolConfirmation is gated behind an experimental feature flag, which emits a |
| 28 | +# UserWarning on use; that is expected and not under test here. |
| 29 | +pytestmark = pytest.mark.filterwarnings("ignore::UserWarning") |
| 30 | + |
| 31 | + |
| 32 | +class TestToolConfirmation: |
| 33 | + """Tests for the ToolConfirmation model.""" |
| 34 | + |
| 35 | + def test_default_values_are_empty(self): |
| 36 | + """A default ToolConfirmation has empty hint, unconfirmed, and no payload.""" |
| 37 | + confirmation = ToolConfirmation() |
| 38 | + |
| 39 | + assert confirmation.hint == "" |
| 40 | + assert confirmation.confirmed is False |
| 41 | + assert confirmation.payload is None |
| 42 | + |
| 43 | + def test_initialization_retains_provided_values(self): |
| 44 | + """ToolConfirmation stores values provided during initialization.""" |
| 45 | + confirmation = ToolConfirmation( |
| 46 | + hint="please confirm", confirmed=True, payload={"amount": 10} |
| 47 | + ) |
| 48 | + |
| 49 | + assert confirmation.hint == "please confirm" |
| 50 | + assert confirmation.confirmed is True |
| 51 | + assert confirmation.payload == {"amount": 10} |
| 52 | + |
| 53 | + @pytest.mark.parametrize( |
| 54 | + "payload_value", |
| 55 | + [ |
| 56 | + [1, 2, 3], |
| 57 | + "raw", |
| 58 | + ], |
| 59 | + ) |
| 60 | + def test_payload_accepts_json_serializable_values(self, payload_value): |
| 61 | + """ToolConfirmation payload accepts various JSON-serializable values.""" |
| 62 | + confirmation = ToolConfirmation(payload=payload_value) |
| 63 | + |
| 64 | + assert confirmation.payload == payload_value |
| 65 | + |
| 66 | + @pytest.mark.parametrize( |
| 67 | + "payload_value", |
| 68 | + [ |
| 69 | + lambda x: x, |
| 70 | + object(), |
| 71 | + ], |
| 72 | + ) |
| 73 | + def test_payload_accepts_non_json_serializable_values(self, payload_value): |
| 74 | + """ToolConfirmation payload accepts non-JSON-serializable values.""" |
| 75 | + confirmation = ToolConfirmation(payload=payload_value) |
| 76 | + |
| 77 | + assert confirmation.payload == payload_value |
| 78 | + |
| 79 | + def test_initialization_fails_with_extra_fields(self): |
| 80 | + """ToolConfirmation forbids extra fields during initialization.""" |
| 81 | + with pytest.raises(ValidationError): |
| 82 | + ToolConfirmation(unexpected="value") |
| 83 | + |
| 84 | + def test_serialization_round_trip_preserves_equality(self): |
| 85 | + """ToolConfirmation can be serialized and deserialized back to its original state.""" |
| 86 | + original = ToolConfirmation( |
| 87 | + hint="confirm transfer", confirmed=True, payload={"to": "bob"} |
| 88 | + ) |
| 89 | + |
| 90 | + dumped = original.model_dump() |
| 91 | + validated = ToolConfirmation.model_validate(dumped) |
| 92 | + |
| 93 | + assert validated == original |
0 commit comments