File size: 6,662 Bytes
0646b18
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Tools Environment Registry Tests

This directory contains comprehensive tests for the Tools Environment Registry system, including legacy OpenAPI integration, MCP server support, and end-to-end API registry server testing.

## Test Structure

```
tests/
β”œβ”€β”€ __init__.py                     # Package initialization
β”œβ”€β”€ README.md                       # This file
β”œβ”€β”€ run_all_tests.py               # Main test runner
β”œβ”€β”€ test_legacy_openapi.py         # Legacy OpenAPI service tests
β”œβ”€β”€ test_mcp_server.py             # MCP server integration tests
β”œβ”€β”€ test_mixed_configuration.py    # Mixed config support tests
└── test_e2e_api_registry.py       # End-to-end API registry server tests
```

## Test Suites

### 1. Legacy OpenAPI Integration (`test_legacy_openapi.py`)
Tests the legacy OpenAPI service integration:
- βœ… Loading and listing applications
- βœ… Listing APIs with proper OpenAPI transformation
- βœ… Calling functions with and without parameters
- βœ… Response parsing and validation

### 2. MCP Server Integration (`test_mcp_server.py`)
Tests the FastMCP server integration:
- βœ… FastMCP client initialization
- βœ… Tool loading with parameter flattening
- βœ… Proper tool prefixing (server_toolname format)
- βœ… Function calling via SSE-based MCP servers

### 3. Mixed Configuration Support (`test_mixed_configuration.py`)
Tests both legacy and MCP servers in the same configuration:
- βœ… Loading both service types simultaneously
- βœ… Proper service isolation and prefixing
- βœ… Calling functions from both service types
- βœ… Configuration format validation

### 4. E2E API Registry Server (`test_e2e_api_registry.py`)
End-to-end tests against the actual API registry server:
- βœ… HTTP endpoint testing (`/applications`, `/apis`, `/functions/call`)
- βœ… Server lifecycle management
- βœ… Function calling via REST API
- βœ… Error handling and validation

## Running Tests

### Run All Tests
```bash
cd ./src/cuga/backend/tools_env/registry/tests
uv run python run_all_tests.py
```

### Run Individual Test Suites
```bash
# Legacy OpenAPI tests
uv run python test_legacy_openapi.py

# MCP server tests  
uv run python test_mcp_server.py

# Mixed configuration tests
uv run python test_mixed_configuration.py

# E2E API registry tests
uv run python test_e2e_api_registry.py
```

### Run with Pytest
```bash
# Run all pytest tests
pytest cuga/backend/tools_env/registry/tests/

# Run specific test file
pytest cuga/backend/tools_env/registry/tests/test_legacy_openapi.py

# Run with verbose output
pytest -v cuga/backend/tools_env/registry/tests/
```

## Prerequisites

### For Legacy OpenAPI Tests
- Internet connection (accesses external Digital Sales API)
- No additional setup required

### For MCP Server Tests
- FastMCP example server running on `http://127.0.0.1:8000/sse`
- Start the server:
  ```bash
  cd ./docs/examples/cuga_with_runtime_tools
  uv run python fast_mcp_example.py &
  ```

### For E2E Tests
- API registry server will be started automatically
- Port 8001 should be available
- All dependencies installed via `uv`

## Test Configuration

Tests use temporary configuration files to avoid interfering with the main system:

### Legacy Config Example
```yaml
services:
  - digital_sales:
      url: https://digitalsales.19pc1vtv090u.us-east.codeengine.appdomain.cloud/openapi.json
      description: Digital Sales API for testing
```

### MCP Config Example
```yaml
mcpServers:
  digital_sales_mcp:
    url: "http://127.0.0.1:8000/sse"
    description: FastMCP example server
    type: mcp_server
```

### Mixed Config Example
```yaml
# Legacy services
services:
  - digital_sales_legacy:
      url: https://digitalsales.19pc1vtv090u.us-east.codeengine.appdomain.cloud/openapi.json
      description: Legacy Digital Sales API

# MCP servers
mcpServers:
  digital_sales_mcp:
    url: "http://127.0.0.1:8000/sse"
    description: FastMCP example server
    type: mcp_server
```

## Expected Results

### Successful Test Run
```
πŸš€ Registry Test Suite
Running comprehensive tests for the Tools Environment Registry

================================================================================
πŸ§ͺ RUNNING: Legacy OpenAPI Integration
================================================================================
βœ… Legacy OpenAPI Integration PASSED

================================================================================
πŸ§ͺ RUNNING: MCP Server Integration  
================================================================================
βœ… MCP Server Integration PASSED

================================================================================
πŸ§ͺ RUNNING: Mixed Configuration Support
================================================================================
βœ… Mixed Configuration Support PASSED

================================================================================
πŸ§ͺ RUNNING: E2E API Registry Server
================================================================================
βœ… E2E API Registry Server PASSED

================================================================================
πŸ“Š TEST SUMMARY
================================================================================
Total Tests: 4
Passed: 4 βœ…
Failed: 0 ❌
Total Time: 45.23s
Success Rate: 100.0%

πŸŽ‰ ALL TESTS PASSED!
```

## Key Features Tested

### Parameter Flattening
- βœ… `$defs` resolution and removal
- βœ… Complex nested objects simplified to string arrays
- βœ… Proper parameter type preservation
- βœ… Descriptive messages for simplified parameters

### Tool Prefixing
- βœ… Legacy: `digital_sales_get_my_accounts`
- βœ… MCP: `digital_sales_mcp_get_my_accounts`
- βœ… No conflicts between services

### FastMCP Integration
- βœ… Proper client initialization with config
- βœ… Tool listing and registration
- βœ… Function calling with argument handling
- βœ… Error handling and fallback mechanisms

### API Registry Server
- βœ… RESTful endpoints for applications and APIs
- βœ… Function calling via HTTP POST
- βœ… Proper error responses
- βœ… Server lifecycle management

## Troubleshooting

### Common Issues

1. **MCP Server Not Running**
   ```
   Error: Failed to connect to MCP server
   Solution: Start the FastMCP server first
   ```

2. **Port Already in Use**
   ```
   Error: Server failed to start (port 8001 in use)
   Solution: Kill existing processes on port 8001
   ```

3. **Network Issues**
   ```
   Error: Failed to fetch OpenAPI spec
   Solution: Check internet connection
   ```

### Debug Mode
Add `--verbose` or set environment variable:
```bash
export DEBUG=1
uv run python run_all_tests.py
```