forked from langflow-ai/langflow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_cuga_agent.py
More file actions
628 lines (493 loc) · 25.6 KB
/
Copy pathtest_cuga_agent.py
File metadata and controls
628 lines (493 loc) · 25.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
import os
from typing import Any
from uuid import uuid4
import pytest
from langflow.custom import Component
from lfx.components.agents.cuga_agent import CugaComponent
from lfx.components.tools.calculator import CalculatorToolComponent
from tests.base import ComponentTestBaseWithClient, ComponentTestBaseWithoutClient
from tests.unit.mock_language_model import MockLanguageModel
# Load environment variables from .env file
class TestCugaComponent(ComponentTestBaseWithoutClient):
"""Test suite for CugaComponent without client dependencies.
This class contains unit tests for the CugaComponent that don't require
external API calls or client connections.
"""
@pytest.fixture
def component_class(self):
"""Return the CugaComponent class for testing.
Returns:
type: The CugaComponent class
"""
return CugaComponent
@pytest.fixture
def file_names_mapping(self):
"""Return empty file names mapping for testing.
Returns:
list: Empty list since no file mappings are needed
"""
return []
async def component_setup(self, component_class: type[Any], default_kwargs: dict[str, Any]) -> Component:
"""Set up component instance for testing with mocked methods.
Args:
component_class: The component class to instantiate
default_kwargs: Default keyword arguments for the component
Returns:
Component: Configured component instance with mocked methods
"""
component_instance = await super().component_setup(component_class, default_kwargs)
# Mock _should_process_output method
component_instance._should_process_output = lambda output: False # noqa: ARG005
return component_instance
@pytest.fixture
def default_kwargs(self):
"""Return default keyword arguments for CugaComponent testing.
Returns:
dict: Default configuration for the CugaComponent
"""
return {
"_type": "Cuga",
"add_current_date_tool": True,
"agent_llm": MockLanguageModel(),
"policies": "You are a helpful assistant.",
"input_value": "",
"n_messages": 100,
"format_instructions": "You are an AI that extracts structured JSON objects from unstructured text.",
"output_schema": [],
"browser_enabled": False,
"web_apps": "",
"API": False,
"lite_mode": True,
"lite_mode_tool_threshold": 25,
}
async def test_build_config_update(self, component_class, default_kwargs):
"""Test that build configuration updates correctly for different providers.
This test verifies that the component's build configuration is properly
updated when switching between different model providers (OpenAI, Custom).
"""
component = await self.component_setup(component_class, default_kwargs)
frontend_node = component.to_frontend_node()
build_config = frontend_node["data"]["node"]["template"]
# Test updating build config for OpenAI
component.set(agent_llm="OpenAI")
updated_config = await component.update_build_config(build_config, "OpenAI", "agent_llm")
assert "agent_llm" in updated_config
assert updated_config["agent_llm"]["value"] == "OpenAI"
assert isinstance(updated_config["agent_llm"]["options"], list)
assert len(updated_config["agent_llm"]["options"]) > 0
assert all(provider in updated_config["agent_llm"]["options"] for provider in ["OpenAI", "Custom"])
assert "Custom" in updated_config["agent_llm"]["options"]
# Verify model_name field is populated for OpenAI
assert "model_name" in updated_config
model_name_dict = updated_config["model_name"]
assert isinstance(model_name_dict["options"], list)
assert len(model_name_dict["options"]) > 0 # OpenAI should have available models
assert "gpt-4o" in model_name_dict["options"]
# Test Anthropic
# TBD: Add test for Anthropic currently cuga does not support Anthropic
# Test updating build config for Custom
updated_config = await component.update_build_config(build_config, "Custom", "agent_llm")
assert "agent_llm" in updated_config
assert updated_config["agent_llm"]["value"] == "Custom"
assert isinstance(updated_config["agent_llm"]["options"], list)
assert len(updated_config["agent_llm"]["options"]) > 0
assert all(provider in updated_config["agent_llm"]["options"] for provider in ["OpenAI", "Custom"])
assert "Custom" in updated_config["agent_llm"]["options"]
assert updated_config["agent_llm"]["input_types"] == ["LanguageModel"]
# Verify model_name field is cleared for Custom
assert "model_name" not in updated_config
async def test_cuga_has_dual_outputs(self, component_class, default_kwargs):
"""Test that Cuga component has both Response and Structured Response outputs.
This test verifies that the CugaComponent has the correct output configuration
with both regular message response and structured JSON response capabilities.
"""
component = await self.component_setup(component_class, default_kwargs)
assert len(component.outputs) == 2
assert component.outputs[0].name == "response"
assert component.outputs[0].display_name == "Response"
assert component.outputs[0].method == "message_response"
assert component.outputs[1].name == "structured_response"
assert component.outputs[1].display_name == "Structured Response"
assert component.outputs[1].method == "json_response"
assert component.outputs[1].tool_mode is False
async def test_json_mode_filtered_from_openai_inputs(self, component_class, default_kwargs):
"""Test that json_mode is filtered out from OpenAI inputs.
This test ensures that the json_mode parameter is properly excluded from
the component's input fields since Cuga handles structured output differently.
"""
component = await self.component_setup(component_class, default_kwargs)
# Check that json_mode is not in the component's inputs
input_names = [inp.name for inp in component.inputs if hasattr(inp, "name")]
assert "json_mode" not in input_names
# Verify other OpenAI inputs are still present
assert "model_name" in input_names
assert "api_key" in input_names
assert "temperature" in input_names
async def test_json_response_parsing_valid_json(self, component_class, default_kwargs):
"""Test that json_response correctly parses JSON from agent response.
This test verifies that the json_response method can properly parse
valid JSON content from the agent's response.
"""
component = await self.component_setup(component_class, default_kwargs)
# Mock the get_agent_requirements method to avoid actual LLM calls
from unittest.mock import AsyncMock
component.get_agent_requirements = AsyncMock(return_value=(MockLanguageModel(), [], []))
component.call_agent = AsyncMock(return_value='{"name": "test", "value": 123}')
result = await component.json_response()
from lfx.schema.data import Data
assert isinstance(result, Data)
assert result.data == {"name": "test", "value": 123}
async def test_json_response_parsing_embedded_json(self, component_class, default_kwargs):
"""Test that json_response handles text containing JSON.
This test verifies that the json_response method can extract JSON
from text that contains other content alongside the JSON.
"""
component = await self.component_setup(component_class, default_kwargs)
# Mock the get_agent_requirements method to avoid actual LLM calls
from unittest.mock import AsyncMock
component.get_agent_requirements = AsyncMock(return_value=(MockLanguageModel(), [], []))
component.call_agent = AsyncMock(return_value='Here is the result: {"status": "success"} - done!')
result = await component.json_response()
from lfx.schema.data import Data
assert isinstance(result, Data)
assert result.data == {"status": "success"}
async def test_json_response_error_handling(self, component_class, default_kwargs):
"""Test that json_response handles completely non-JSON responses.
This test verifies that the json_response method gracefully handles
responses that don't contain any valid JSON content.
"""
component = await self.component_setup(component_class, default_kwargs)
# Mock the get_agent_requirements method to avoid actual LLM calls
from unittest.mock import AsyncMock
component.get_agent_requirements = AsyncMock(return_value=(MockLanguageModel(), [], []))
component.call_agent = AsyncMock(return_value="This is just plain text with no JSON")
result = await component.json_response()
from lfx.schema.data import Data
assert isinstance(result, Data)
assert "error" in result.data
assert result.data["content"] == "This is just plain text with no JSON"
async def test_model_building_without_json_mode(self, component_class, default_kwargs):
"""Test that model building works without json_mode attribute.
This test ensures that the component can build models without requiring
the json_mode attribute that has been filtered out.
"""
component = await self.component_setup(component_class, default_kwargs)
component.agent_llm = "OpenAI"
# Mock component for testing
from unittest.mock import Mock
mock_component = Mock()
mock_component.set.return_value = mock_component
# Should not raise AttributeError for missing json_mode
result = component.set_component_params(mock_component)
assert result is not None
# Verify set was called (meaning no AttributeError occurred)
mock_component.set.assert_called_once()
async def test_json_response_with_schema_validation(self, component_class, default_kwargs):
"""Test that json_response validates against provided schema.
This test verifies that the json_response method can validate JSON
content against a provided Pydantic schema.
"""
# Set up component with output schema
default_kwargs["output_schema"] = [
{"name": "name", "type": "str", "description": "Name field", "multiple": False},
{"name": "age", "type": "int", "description": "Age field", "multiple": False},
]
component = await self.component_setup(component_class, default_kwargs)
# Mock the get_agent_requirements method
from unittest.mock import AsyncMock
component.get_agent_requirements = AsyncMock(return_value=(MockLanguageModel(), [], []))
component.call_agent = AsyncMock(return_value='{"name": "John", "age": 25}')
result = await component.json_response()
from langflow.schema.data import Data
assert isinstance(result, Data)
assert result.data == {"name": "John", "age": 25}
async def test_cuga_component_initialization(self, component_class, default_kwargs):
"""Test that Cuga component initializes correctly with filtered inputs.
This test verifies that the CugaComponent can be properly initialized
with all required attributes and filtered input fields.
"""
component = await self.component_setup(component_class, default_kwargs)
# Should not raise any errors during initialization
assert component.display_name == "Cuga"
assert component.name == "Cuga"
assert len(component.inputs) > 0
assert len(component.outputs) == 2
async def test_frontend_node_structure(self, component_class, default_kwargs):
"""Test that frontend node has correct structure with filtered inputs.
This test verifies that the frontend node representation has the correct
structure and excludes unwanted fields like json_mode.
"""
component = await self.component_setup(component_class, default_kwargs)
frontend_node = component.to_frontend_node()
build_config = frontend_node["data"]["node"]["template"]
# Verify json_mode is not in build config
assert "json_mode" not in build_config
# Verify other expected fields are present
assert "agent_llm" in build_config
assert "policies" in build_config
assert "add_current_date_tool" in build_config
assert "browser_enabled" in build_config
assert "web_apps" in build_config
assert "API" in build_config
async def test_preprocess_schema(self, component_class, default_kwargs):
"""Test that _preprocess_schema correctly handles schema validation.
This test verifies that the schema preprocessing method correctly
converts string boolean values to actual booleans and validates field types.
"""
component = await self.component_setup(component_class, default_kwargs)
# Test schema preprocessing
raw_schema = [
{"name": "field1", "type": "str", "description": "Test field", "multiple": "true"},
{"name": "field2", "type": "int", "description": "Another field", "multiple": False},
]
processed = component._preprocess_schema(raw_schema)
assert len(processed) == 2
assert processed[0]["multiple"] is True # String "true" should be converted to bool
assert processed[1]["multiple"] is False
async def test_build_structured_output_base_with_validation(self, component_class, default_kwargs):
"""Test build_structured_output_base with schema validation.
This test verifies that the structured output building method can
validate JSON content against a provided schema.
"""
default_kwargs["output_schema"] = [
{"name": "name", "type": "str", "description": "Name field", "multiple": False},
{"name": "count", "type": "int", "description": "Count field", "multiple": False},
]
component = await self.component_setup(component_class, default_kwargs)
# Test valid JSON that matches schema
valid_content = '{"name": "test", "count": 42}'
result = await component.build_structured_output_base(valid_content)
assert result == [{"name": "test", "count": 42}]
async def test_build_structured_output_base_without_schema(self, component_class, default_kwargs):
"""Test build_structured_output_base without schema validation.
This test verifies that the structured output building method works
correctly when no schema validation is provided.
"""
component = await self.component_setup(component_class, default_kwargs)
# Test with no output_schema
content = '{"any": "data", "number": 123}'
result = await component.build_structured_output_base(content)
assert result == {"any": "data", "number": 123}
async def test_build_structured_output_base_embedded_json(self, component_class, default_kwargs):
"""Test extraction of JSON from embedded text.
This test verifies that the structured output building method can
extract JSON content from text that contains other content.
"""
component = await self.component_setup(component_class, default_kwargs)
content = 'Here is some text with {"embedded": "json"} inside it.'
result = await component.build_structured_output_base(content)
assert result == {"embedded": "json"}
async def test_build_structured_output_base_no_json(self, component_class, default_kwargs):
"""Test handling of content with no JSON.
This test verifies that the structured output building method handles
content that doesn't contain any JSON gracefully.
"""
component = await self.component_setup(component_class, default_kwargs)
content = "This is just plain text with no JSON at all."
result = await component.build_structured_output_base(content)
assert "error" in result
assert result["content"] == content
async def test_new_input_fields_present(self, component_class, default_kwargs):
"""Test that new input fields are present in the component.
This test verifies that all the new input fields specific to the Cuga
component are properly defined and have correct default values.
"""
component = await self.component_setup(component_class, default_kwargs)
input_names = [inp.name for inp in component.inputs if hasattr(inp, "name")]
# Test for new fields specific to Cuga
assert "policies" in input_names
assert "format_instructions" in input_names
assert "output_schema" in input_names
assert "n_messages" in input_names
assert "browser_enabled" in input_names
assert "web_apps" in input_names
assert "API" in input_names
assert "lite_mode" in input_names
assert "lite_mode_tool_threshold" in input_names
# Verify default values
assert hasattr(component, "policies")
assert hasattr(component, "format_instructions")
assert hasattr(component, "output_schema")
assert hasattr(component, "n_messages")
assert hasattr(component, "browser_enabled")
assert hasattr(component, "web_apps")
assert hasattr(component, "API")
assert hasattr(component, "lite_mode")
assert hasattr(component, "lite_mode_tool_threshold")
assert component.n_messages == 100
assert component.browser_enabled is False
assert component.API is False
assert component.lite_mode is True
assert component.lite_mode_tool_threshold == 25
async def test_cuga_has_correct_outputs(self, component_class, default_kwargs):
"""Test that Cuga component has the correct output configuration.
This test verifies that the CugaComponent has the expected output
configuration with both response and structured response outputs.
"""
component = await self.component_setup(component_class, default_kwargs)
assert len(component.outputs) == 2
# Test response output
response_output = component.outputs[0]
assert response_output.name == "response"
assert response_output.display_name == "Response"
assert response_output.method == "message_response"
# Test structured response output
structured_output = component.outputs[1]
assert structured_output.name == "structured_response"
assert structured_output.display_name == "Structured Response"
assert structured_output.method == "json_response"
assert structured_output.tool_mode is False
async def test_memory_inputs_advanced_setting(self, component_class, default_kwargs):
"""Test that memory inputs are properly set to advanced.
This test verifies that memory-related input fields are properly
configured as advanced settings.
Note:
This test is currently a placeholder (TBD).
"""
# TBD: Add test for memory inputs
async def test_browser_configuration(self, component_class, default_kwargs):
"""Test browser configuration options.
This test verifies that the browser-related configuration options
(browser_enabled, web_apps) work correctly.
"""
component = await self.component_setup(component_class, default_kwargs)
# Test default browser settings
assert component.browser_enabled is False
assert component.web_apps == ""
# Test setting browser enabled
component.browser_enabled = True
component.web_apps = "https://example.com"
assert component.browser_enabled is True
assert component.web_apps == "https://example.com"
async def test_api_subagent_configuration(self, component_class, default_kwargs):
"""Test API sub-agent configuration.
This test verifies that the API sub-agent configuration option
works correctly.
"""
component = await self.component_setup(component_class, default_kwargs)
# Test default API setting
assert component.API is False
# Test setting API enabled
component.API = True
assert component.API is True
class TestCugaComponentWithClient(ComponentTestBaseWithClient):
"""Test suite for CugaComponent with client dependencies.
This class contains integration tests for the CugaComponent that require
external API calls and client connections.
"""
@pytest.fixture
def component_class(self):
"""Return the CugaComponent class for testing.
Returns:
type: The CugaComponent class
"""
return CugaComponent
@pytest.fixture
def file_names_mapping(self):
"""Return empty file names mapping for testing.
Returns:
list: Empty list since no file mappings are needed
"""
return []
@pytest.mark.api_key_required
@pytest.mark.no_blockbuster
async def test_cuga_component_with_calculator(self):
"""Test CugaComponent with calculator tool using real API.
This integration test verifies that the CugaComponent can work with
actual tools (calculator) and make real API calls to OpenAI.
Requires:
OPENAI_API_KEY environment variable
"""
# Now you can access the environment variables
api_key = os.getenv("OPENAI_API_KEY")
tools = [CalculatorToolComponent().build_tool()] # Use the Calculator component as a tool
input_value = "What is 2 + 2?"
temperature = 0.1
# Initialize the CugaComponent with mocked inputs
cuga = CugaComponent(
tools=tools,
input_value=input_value,
api_key=api_key,
model_name="gpt-4o",
agent_llm="OpenAI",
temperature=temperature,
_session_id=str(uuid4()),
)
response = await cuga.message_response()
assert "4" in response.data.get("text")
@pytest.mark.api_key_required
@pytest.mark.no_blockbuster
@pytest.mark.timeout(300) # 5 minutes timeout for testing key OpenAI models
async def test_cuga_component_with_all_openai_models(self):
"""Test CugaComponent with multiple OpenAI models.
This integration test verifies that the CugaComponent works correctly
with various OpenAI model configurations.
Requires:
OPENAI_API_KEY environment variable
"""
# Mock inputs
api_key = os.getenv("OPENAI_API_KEY")
input_value = "What is 2 + 2?"
# Test only key OpenAI models to avoid timeout and complexity
key_models = ["gpt-4o", "gpt-4o-mini"]
failed_models = []
for model_name in key_models:
try:
# Initialize the CugaComponent with mocked inputs
tools = [CalculatorToolComponent().build_tool()] # Use the Calculator component as a tool
cuga = CugaComponent(
tools=tools,
input_value=input_value,
api_key=api_key,
model_name=model_name,
agent_llm="OpenAI",
_session_id=str(uuid4()),
)
response = await cuga.message_response()
if "4" not in response.data.get("text"):
failed_models.append(model_name)
except Exception as e:
failed_models.append(f"{model_name} (error: {e!s})")
assert not failed_models, f"The following models failed the test: {failed_models}"
@pytest.mark.api_key_required
@pytest.mark.no_blockbuster
async def test_cuga_structured_response_with_schema(self):
"""Test CugaComponent structured response with schema validation.
This test verifies that the CugaComponent can generate structured
responses with schema validation using real API calls.
Note:
This test is currently a placeholder (TODO).
Requires:
OPENAI_API_KEY environment variable
"""
# TODO: Add test for structured response with schema
@pytest.mark.api_key_required
@pytest.mark.no_blockbuster
async def test_cuga_with_policies(self):
"""Test Cuga with custom policies.
This integration test verifies that the CugaComponent can apply
custom policies to modify its behavior during execution.
Requires:
OPENAI_API_KEY environment variable
"""
api_key = os.getenv("OPENAI_API_KEY")
input_value = "What is 2 + 2?"
policies = "## Answer\n\nYou must always respond with enthusiasm and use exclamation marks!"
tools = [CalculatorToolComponent().build_tool()]
cuga = CugaComponent(
input_value=input_value,
api_key=api_key,
model_name="gpt-4o",
agent_llm="OpenAI",
policies=policies,
tools=tools,
_session_id=str(uuid4()),
)
response = await cuga.message_response()
response_text = response.data.get("text", "")
# Should contain the calculation result
assert "4" in response_text
assert "!" in response_text
# Should show some enthusiasm (though this might be flaky depending on the model)
# We'll just check that we got a response
assert len(response_text) > 0