|
| 1 | +import 'dart:convert'; |
| 2 | +import '../services/services.dart'; |
| 3 | +import '../../models/models.dart'; |
| 4 | + |
| 5 | +class TestGeneratorFeature { |
| 6 | + final DashBotService _service; |
| 7 | + |
| 8 | + TestGeneratorFeature(this._service); |
| 9 | + |
| 10 | + Future<String> generateApiTests({ |
| 11 | + required RequestModel? requestModel, |
| 12 | + required dynamic responseModel, |
| 13 | + }) async { |
| 14 | + if (requestModel == null || responseModel == null) { |
| 15 | + return "No recent API requests found."; |
| 16 | + } |
| 17 | + |
| 18 | + final method = requestModel.httpRequestModel?.method |
| 19 | + .toString() |
| 20 | + .split('.') |
| 21 | + .last |
| 22 | + .toUpperCase() ?? |
| 23 | + "GET"; |
| 24 | + final endpoint = requestModel.httpRequestModel?.url ?? "Unknown Endpoint"; |
| 25 | + final rawResponse = responseModel.body; |
| 26 | + final responseBody = |
| 27 | + rawResponse is String ? rawResponse : jsonEncode(rawResponse); |
| 28 | + final statusCode = responseModel.statusCode ?? 0; |
| 29 | + |
| 30 | + Uri uri = Uri.parse(endpoint); |
| 31 | + final baseUrl = "${uri.scheme}://${uri.host}"; |
| 32 | + final path = uri.path; |
| 33 | + |
| 34 | + final parameterAnalysis = _analyzeParameters(uri.queryParameters); |
| 35 | + |
| 36 | + final prompt = """ |
| 37 | +EXECUTABLE API TEST CASES GENERATOR |
| 38 | +
|
| 39 | +**API Analysis:** |
| 40 | +- Base URL: $baseUrl |
| 41 | +- Endpoint: $path |
| 42 | +- Method: $method |
| 43 | +- Current Parameters: ${uri.queryParameters} |
| 44 | +- Current Response: $responseBody (Status: $statusCode) |
| 45 | +- Parameter Types: $parameterAnalysis |
| 46 | +
|
| 47 | +**Test Generation Task:** |
| 48 | +Generate practical, ready-to-use test cases for this API in cURL format. Each test should be executable immediately. |
| 49 | +
|
| 50 | +Include these test categories: |
| 51 | +1. **Valid Cases**: Different valid parameter values (use real-world examples like other country codes if this is a country API) |
| 52 | +2. **Invalid Parameter Tests**: Missing parameters, empty values, incorrect formats |
| 53 | +3. **Edge Cases**: Special characters, long values, unexpected inputs |
| 54 | +4. **Validation Tests**: Test input validation and error handling |
| 55 | +
|
| 56 | +For each test case: |
| 57 | +1. Provide a brief description of what the test verifies |
| 58 | +2. Include a complete, executable cURL command |
| 59 | +3. Show the expected outcome (status code and sample response) |
| 60 | +4. Organize tests in a way that's easy to copy and run |
| 61 | +
|
| 62 | +Focus on creating realistic test values based on the API context (e.g., for a country flag API, use real country codes, invalid codes, etc.) |
| 63 | +"""; |
| 64 | + |
| 65 | + final testCases = await _service.generateResponse(prompt); |
| 66 | + return "TEST_CASES_HIDDEN\n$testCases"; |
| 67 | + } |
| 68 | + |
| 69 | + String _analyzeParameters(Map<String, String> parameters) { |
| 70 | + if (parameters.isEmpty) { |
| 71 | + return "No parameters detected"; |
| 72 | + } |
| 73 | + |
| 74 | + Map<String, String> analysis = {}; |
| 75 | + |
| 76 | + parameters.forEach((key, value) { |
| 77 | + if (RegExp(r'^[A-Z]{3}$').hasMatch(value)) { |
| 78 | + analysis[key] = |
| 79 | + "Appears to be a 3-letter country code (ISO 3166-1 alpha-3)"; |
| 80 | + } else if (RegExp(r'^[A-Z]{2}$').hasMatch(value)) { |
| 81 | + analysis[key] = |
| 82 | + "Appears to be a 2-letter country code (ISO 3166-1 alpha-2)"; |
| 83 | + } else if (RegExp(r'^\d+$').hasMatch(value)) { |
| 84 | + analysis[key] = "Numeric value"; |
| 85 | + } else if (RegExp(r'^[a-zA-Z]+$').hasMatch(value)) { |
| 86 | + analysis[key] = "Alphabetic string"; |
| 87 | + } else { |
| 88 | + analysis[key] = "Unknown format: $value"; |
| 89 | + } |
| 90 | + }); |
| 91 | + |
| 92 | + return jsonEncode(analysis); |
| 93 | + } |
| 94 | +} |
0 commit comments