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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
import asyncio
import json
from datetime import datetime
from typing import Any, Dict, List, Optional
from uuid import UUID

import httpx
from loguru import logger

# Configure logger
logger.add(
    "tests/api_test_{time}.log",
    rotation="1 day",
    retention="7 days",
    level="DEBUG",
    format="{time:YYYY-MM-DD HH:mm:ss} | {level} | {message}",
)


class TestConfig:
    """Test configuration and utilities"""

    BASE_URL: str = "http://localhost:8000/v1"
    TEST_USERNAME: str = "test_user"
    api_key: Optional[str] = None
    user_id: Optional[UUID] = None
    test_agent_id: Optional[UUID] = None


class TestResult:
    """Model for test results"""

    def __init__(
        self,
        test_name: str,
        status: str,
        duration: float,
        error: Optional[str] = None,
        details: Optional[Dict[str, Any]] = None,
    ):
        self.test_name = test_name
        self.status = status
        self.duration = duration
        self.error = error
        self.details = details or {}

    def dict(self):
        return {
            "test_name": self.test_name,
            "status": self.status,
            "duration": self.duration,
            "error": self.error,
            "details": self.details,
        }


async def log_response(
    response: httpx.Response, test_name: str
) -> None:
    """Log API response details"""
    logger.debug(f"\n{test_name} Response:")
    logger.debug(f"Status Code: {response.status_code}")
    logger.debug(f"Headers: {dict(response.headers)}")
    try:
        logger.debug(f"Body: {response.json()}")
    except json.JSONDecodeError:
        logger.debug(f"Body: {response.text}")


async def create_test_user() -> TestResult:
    """Create a test user and get API key"""
    start_time = datetime.now()
    try:
        async with httpx.AsyncClient() as client:
            response = await client.post(
                f"{TestConfig.BASE_URL}/users",
                json={"username": TestConfig.TEST_USERNAME},
            )
            await log_response(response, "Create User")

            if response.status_code == 200:
                data = response.json()
                TestConfig.api_key = data["api_key"]
                TestConfig.user_id = UUID(data["user_id"])
                return TestResult(
                    test_name="create_test_user",
                    status="passed",
                    duration=(
                        datetime.now() - start_time
                    ).total_seconds(),
                    details={"user_id": str(TestConfig.user_id)},
                )
            else:
                return TestResult(
                    test_name="create_test_user",
                    status="failed",
                    duration=(
                        datetime.now() - start_time
                    ).total_seconds(),
                    error=f"Failed to create user: {response.text}",
                )
    except Exception as e:
        logger.error(f"Error in create_test_user: {str(e)}")
        return TestResult(
            test_name="create_test_user",
            status="error",
            duration=(datetime.now() - start_time).total_seconds(),
            error=str(e),
        )


async def create_test_agent() -> TestResult:
    """Create a test agent"""
    start_time = datetime.now()
    try:
        # Create agent config according to the AgentConfig model
        agent_config = {
            "agent_name": "test_agent",
            "model_name": "gpt-4",
            "description": "Test agent for API testing",
            "system_prompt": "You are a test agent.",
            "temperature": 0.1,
            "max_loops": 1,
            "dynamic_temperature_enabled": True,
            "user_name": TestConfig.TEST_USERNAME,
            "retry_attempts": 1,
            "context_length": 4000,
            "output_type": "string",
            "streaming_on": False,
            "tags": ["test", "api"],
            "stopping_token": "<DONE>",
            "auto_generate_prompt": False,
        }

        async with httpx.AsyncClient() as client:
            response = await client.post(
                f"{TestConfig.BASE_URL}/agent",
                json=agent_config,
                headers={"api-key": TestConfig.api_key},
            )
            await log_response(response, "Create Agent")

            if response.status_code == 200:
                data = response.json()
                TestConfig.test_agent_id = UUID(data["agent_id"])
                return TestResult(
                    test_name="create_test_agent",
                    status="passed",
                    duration=(
                        datetime.now() - start_time
                    ).total_seconds(),
                    details={
                        "agent_id": str(TestConfig.test_agent_id)
                    },
                )
            else:
                return TestResult(
                    test_name="create_test_agent",
                    status="failed",
                    duration=(
                        datetime.now() - start_time
                    ).total_seconds(),
                    error=f"Failed to create agent: {response.text}",
                )
    except Exception as e:
        logger.error(f"Error in create_test_agent: {str(e)}")
        return TestResult(
            test_name="create_test_agent",
            status="error",
            duration=(datetime.now() - start_time).total_seconds(),
            error=str(e),
        )


async def test_agent_completion() -> TestResult:
    """Test agent completion endpoint"""
    start_time = datetime.now()
    try:
        completion_request = {
            "prompt": "Hello, this is a test prompt.",
            "agent_id": str(TestConfig.test_agent_id),
            "max_tokens": 100,
            "temperature_override": 0.5,
            "stream": False,
        }

        async with httpx.AsyncClient() as client:
            response = await client.post(
                f"{TestConfig.BASE_URL}/agent/completions",
                json=completion_request,
                headers={"api-key": TestConfig.api_key},
            )
            await log_response(response, "Agent Completion")

            if response.status_code == 200:
                return TestResult(
                    test_name="test_agent_completion",
                    status="passed",
                    duration=(
                        datetime.now() - start_time
                    ).total_seconds(),
                    details={"response": response.json()},
                )
            else:
                return TestResult(
                    test_name="test_agent_completion",
                    status="failed",
                    duration=(
                        datetime.now() - start_time
                    ).total_seconds(),
                    error=f"Failed completion test: {response.text}",
                )
    except Exception as e:
        logger.error(f"Error in test_agent_completion: {str(e)}")
        return TestResult(
            test_name="test_agent_completion",
            status="error",
            duration=(datetime.now() - start_time).total_seconds(),
            error=str(e),
        )


async def test_agent_metrics() -> TestResult:
    """Test agent metrics endpoint"""
    start_time = datetime.now()
    try:
        if not TestConfig.test_agent_id:
            return TestResult(
                test_name="test_agent_metrics",
                status="failed",
                duration=(
                    datetime.now() - start_time
                ).total_seconds(),
                error="No test agent ID available",
            )

        async with httpx.AsyncClient() as client:
            response = await client.get(
                f"{TestConfig.BASE_URL}/agent/{str(TestConfig.test_agent_id)}/metrics",
                headers={"api-key": TestConfig.api_key},
            )
            await log_response(response, "Agent Metrics")

            if response.status_code == 200:
                return TestResult(
                    test_name="test_agent_metrics",
                    status="passed",
                    duration=(
                        datetime.now() - start_time
                    ).total_seconds(),
                    details={"metrics": response.json()},
                )
            else:
                return TestResult(
                    test_name="test_agent_metrics",
                    status="failed",
                    duration=(
                        datetime.now() - start_time
                    ).total_seconds(),
                    error=f"Failed metrics test: {response.text}",
                )
    except Exception as e:
        logger.error(f"Error in test_agent_metrics: {str(e)}")
        return TestResult(
            test_name="test_agent_metrics",
            status="error",
            duration=(datetime.now() - start_time).total_seconds(),
            error=str(e),
        )


async def test_update_agent() -> TestResult:
    """Test agent update endpoint"""
    start_time = datetime.now()
    try:
        if not TestConfig.test_agent_id:
            return TestResult(
                test_name="test_update_agent",
                status="failed",
                duration=(
                    datetime.now() - start_time
                ).total_seconds(),
                error="No test agent ID available",
            )

        update_data = {
            "description": "Updated test agent description",
            "tags": ["test", "updated"],
            "max_loops": 2,
        }

        async with httpx.AsyncClient() as client:
            response = await client.patch(
                f"{TestConfig.BASE_URL}/agent/{str(TestConfig.test_agent_id)}",
                json=update_data,
                headers={"api-key": TestConfig.api_key},
            )
            await log_response(response, "Update Agent")

            if response.status_code == 200:
                return TestResult(
                    test_name="test_update_agent",
                    status="passed",
                    duration=(
                        datetime.now() - start_time
                    ).total_seconds(),
                    details={"update_response": response.json()},
                )
            else:
                return TestResult(
                    test_name="test_update_agent",
                    status="failed",
                    duration=(
                        datetime.now() - start_time
                    ).total_seconds(),
                    error=f"Failed update test: {response.text}",
                )
    except Exception as e:
        logger.error(f"Error in test_update_agent: {str(e)}")
        return TestResult(
            test_name="test_update_agent",
            status="error",
            duration=(datetime.now() - start_time).total_seconds(),
            error=str(e),
        )


async def test_error_handling() -> TestResult:
    """Test API error handling"""
    start_time = datetime.now()
    try:
        async with httpx.AsyncClient() as client:
            # Test with invalid API key
            invalid_agent_id = "00000000-0000-0000-0000-000000000000"
            response = await client.get(
                f"{TestConfig.BASE_URL}/agent/{invalid_agent_id}/metrics",
                headers={"api-key": "invalid_key"},
            )
            await log_response(response, "Invalid API Key Test")

            if response.status_code in [401, 403]:
                return TestResult(
                    test_name="test_error_handling",
                    status="passed",
                    duration=(
                        datetime.now() - start_time
                    ).total_seconds(),
                    details={"error_response": response.json()},
                )
            else:
                return TestResult(
                    test_name="test_error_handling",
                    status="failed",
                    duration=(
                        datetime.now() - start_time
                    ).total_seconds(),
                    error="Error handling test failed",
                )
    except Exception as e:
        logger.error(f"Error in test_error_handling: {str(e)}")
        return TestResult(
            test_name="test_error_handling",
            status="error",
            duration=(datetime.now() - start_time).total_seconds(),
            error=str(e),
        )


async def cleanup_test_resources() -> TestResult:
    """Clean up test resources"""
    start_time = datetime.now()
    try:
        if TestConfig.test_agent_id:
            async with httpx.AsyncClient() as client:
                response = await client.delete(
                    f"{TestConfig.BASE_URL}/agent/{str(TestConfig.test_agent_id)}",
                    headers={"api-key": TestConfig.api_key},
                )
                await log_response(response, "Delete Agent")

        return TestResult(
            test_name="cleanup_test_resources",
            status="passed",
            duration=(datetime.now() - start_time).total_seconds(),
            details={"cleanup": "completed"},
        )
    except Exception as e:
        logger.error(f"Error in cleanup_test_resources: {str(e)}")
        return TestResult(
            test_name="cleanup_test_resources",
            status="error",
            duration=(datetime.now() - start_time).total_seconds(),
            error=str(e),
        )


async def run_all_tests() -> List[TestResult]:
    """Run all tests in sequence"""
    logger.info("Starting API test suite")
    results = []

    # Initialize
    results.append(await create_test_user())
    if results[-1].status != "passed":
        logger.error(
            "Failed to create test user, aborting remaining tests"
        )
        return results

    # Add delay to ensure user is properly created
    await asyncio.sleep(1)

    # Core tests
    test_functions = [
        create_test_agent,
        test_agent_completion,
        test_agent_metrics,
        test_update_agent,
        test_error_handling,
    ]

    for test_func in test_functions:
        result = await test_func()
        results.append(result)
        logger.info(f"Test {result.test_name}: {result.status}")
        if result.error:
            logger.error(
                f"Error in {result.test_name}: {result.error}"
            )

        # Add small delay between tests
        await asyncio.sleep(0.5)

    # Cleanup
    results.append(await cleanup_test_resources())

    # Log summary
    passed = sum(1 for r in results if r.status == "passed")
    failed = sum(1 for r in results if r.status == "failed")
    errors = sum(1 for r in results if r.status == "error")

    logger.info("\nTest Summary:")
    logger.info(f"Total Tests: {len(results)}")
    logger.info(f"Passed: {passed}")
    logger.info(f"Failed: {failed}")
    logger.info(f"Errors: {errors}")

    return results


def main():
    """Main entry point for running tests"""
    logger.info("Starting API testing suite")
    try:
        results = asyncio.run(run_all_tests())

        # Write results to JSON file
        with open("test_results.json", "w") as f:
            json.dump(
                [result.dict() for result in results],
                f,
                indent=2,
                default=str,
            )

        logger.info("Test results written to test_results.json")

    except Exception:
        logger.error("Fatal error in test suite: ")


main()