|
| 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 | +import base64 |
| 16 | + |
| 17 | +from google.adk.models.llm_request import LlmRequest |
| 18 | +from google.adk.tools.load_artifacts_tool import _maybe_base64_to_bytes |
| 19 | +from google.adk.tools.load_artifacts_tool import load_artifacts_tool |
| 20 | +from google.genai import types |
| 21 | +from pytest import mark |
| 22 | + |
| 23 | + |
| 24 | +class _StubToolContext: |
| 25 | + """Minimal ToolContext stub for LoadArtifactsTool tests.""" |
| 26 | + |
| 27 | + def __init__(self, artifacts_by_name: dict[str, types.Part]): |
| 28 | + self._artifacts_by_name = artifacts_by_name |
| 29 | + |
| 30 | + async def list_artifacts(self) -> list[str]: |
| 31 | + return list(self._artifacts_by_name.keys()) |
| 32 | + |
| 33 | + async def load_artifact(self, name: str) -> types.Part | None: |
| 34 | + return self._artifacts_by_name.get(name) |
| 35 | + |
| 36 | + |
| 37 | +@mark.asyncio |
| 38 | +async def test_load_artifacts_converts_unsupported_mime_to_text(): |
| 39 | + """Unsupported inline MIME types are converted to text parts.""" |
| 40 | + artifact_name = 'test.csv' |
| 41 | + csv_bytes = b'col1,col2\n1,2\n' |
| 42 | + artifact = types.Part( |
| 43 | + inline_data=types.Blob(data=csv_bytes, mime_type='application/csv') |
| 44 | + ) |
| 45 | + |
| 46 | + tool_context = _StubToolContext({artifact_name: artifact}) |
| 47 | + llm_request = LlmRequest( |
| 48 | + contents=[ |
| 49 | + types.Content( |
| 50 | + role='user', |
| 51 | + parts=[ |
| 52 | + types.Part( |
| 53 | + function_response=types.FunctionResponse( |
| 54 | + name='load_artifacts', |
| 55 | + response={'artifact_names': [artifact_name]}, |
| 56 | + ) |
| 57 | + ) |
| 58 | + ], |
| 59 | + ) |
| 60 | + ] |
| 61 | + ) |
| 62 | + |
| 63 | + await load_artifacts_tool.process_llm_request( |
| 64 | + tool_context=tool_context, llm_request=llm_request |
| 65 | + ) |
| 66 | + |
| 67 | + assert llm_request.contents[-1].parts[0].text == ( |
| 68 | + f'Artifact {artifact_name} is:' |
| 69 | + ) |
| 70 | + artifact_part = llm_request.contents[-1].parts[1] |
| 71 | + assert artifact_part.inline_data is None |
| 72 | + assert artifact_part.text == csv_bytes.decode('utf-8') |
| 73 | + |
| 74 | + |
| 75 | +@mark.asyncio |
| 76 | +async def test_load_artifacts_converts_base64_unsupported_mime_to_text(): |
| 77 | + """Unsupported base64 string data is converted to text parts.""" |
| 78 | + artifact_name = 'test.csv' |
| 79 | + csv_bytes = b'col1,col2\n1,2\n' |
| 80 | + csv_base64 = base64.b64encode(csv_bytes).decode('ascii') |
| 81 | + artifact = types.Part( |
| 82 | + inline_data=types.Blob(data=csv_base64, mime_type='application/csv') |
| 83 | + ) |
| 84 | + |
| 85 | + tool_context = _StubToolContext({artifact_name: artifact}) |
| 86 | + llm_request = LlmRequest( |
| 87 | + contents=[ |
| 88 | + types.Content( |
| 89 | + role='user', |
| 90 | + parts=[ |
| 91 | + types.Part( |
| 92 | + function_response=types.FunctionResponse( |
| 93 | + name='load_artifacts', |
| 94 | + response={'artifact_names': [artifact_name]}, |
| 95 | + ) |
| 96 | + ) |
| 97 | + ], |
| 98 | + ) |
| 99 | + ] |
| 100 | + ) |
| 101 | + |
| 102 | + await load_artifacts_tool.process_llm_request( |
| 103 | + tool_context=tool_context, llm_request=llm_request |
| 104 | + ) |
| 105 | + |
| 106 | + artifact_part = llm_request.contents[-1].parts[1] |
| 107 | + assert artifact_part.inline_data is None |
| 108 | + assert artifact_part.text == csv_bytes.decode('utf-8') |
| 109 | + |
| 110 | + |
| 111 | +@mark.asyncio |
| 112 | +async def test_load_artifacts_keeps_supported_mime_types(): |
| 113 | + """Supported inline MIME types are passed through unchanged.""" |
| 114 | + artifact_name = 'test.pdf' |
| 115 | + artifact = types.Part( |
| 116 | + inline_data=types.Blob(data=b'%PDF-1.4', mime_type='application/pdf') |
| 117 | + ) |
| 118 | + |
| 119 | + tool_context = _StubToolContext({artifact_name: artifact}) |
| 120 | + llm_request = LlmRequest( |
| 121 | + contents=[ |
| 122 | + types.Content( |
| 123 | + role='user', |
| 124 | + parts=[ |
| 125 | + types.Part( |
| 126 | + function_response=types.FunctionResponse( |
| 127 | + name='load_artifacts', |
| 128 | + response={'artifact_names': [artifact_name]}, |
| 129 | + ) |
| 130 | + ) |
| 131 | + ], |
| 132 | + ) |
| 133 | + ] |
| 134 | + ) |
| 135 | + |
| 136 | + await load_artifacts_tool.process_llm_request( |
| 137 | + tool_context=tool_context, llm_request=llm_request |
| 138 | + ) |
| 139 | + |
| 140 | + artifact_part = llm_request.contents[-1].parts[1] |
| 141 | + assert artifact_part.inline_data is not None |
| 142 | + assert artifact_part.inline_data.mime_type == 'application/pdf' |
| 143 | + |
| 144 | + |
| 145 | +def test_maybe_base64_to_bytes_decodes_standard_base64(): |
| 146 | + """Standard base64 encoded strings are decoded correctly.""" |
| 147 | + original = b'hello world' |
| 148 | + encoded = base64.b64encode(original).decode('ascii') |
| 149 | + assert _maybe_base64_to_bytes(encoded) == original |
| 150 | + |
| 151 | + |
| 152 | +def test_maybe_base64_to_bytes_decodes_urlsafe_base64(): |
| 153 | + """URL-safe base64 encoded strings are decoded correctly.""" |
| 154 | + original = b'\xfb\xff\xfe' # bytes that produce +/ in std but -_ in urlsafe |
| 155 | + encoded = base64.urlsafe_b64encode(original).decode('ascii') |
| 156 | + assert _maybe_base64_to_bytes(encoded) == original |
| 157 | + |
| 158 | + |
| 159 | +def test_maybe_base64_to_bytes_returns_none_for_invalid(): |
| 160 | + """Invalid base64 strings return None.""" |
| 161 | + # Single character is invalid (base64 requires length % 4 == 0 after padding) |
| 162 | + assert _maybe_base64_to_bytes('x') is None |
0 commit comments