Spaces:
Sleeping
Sleeping
| """Test Forge routes.""" | |
| from flask import Blueprint, render_template, request, jsonify | |
| from .forge import generate_tests | |
| bp = Blueprint("test_forge", __name__, template_folder="templates") | |
| SUPPORTED_FRAMEWORKS = { | |
| "TypeScript / Jest", | |
| "Python / pytest", | |
| "JavaScript / Mocha", | |
| "Java / JUnit", | |
| "Go / testing", | |
| "Ruby / RSpec", | |
| "PHP / PHPUnit", | |
| "C# / xUnit", | |
| } | |
| def index(): | |
| return render_template("test_forge/index.html") | |
| def api_generate(): | |
| body = request.get_json(silent=True) or {} | |
| source_code = (body.get("source_code") or "").strip() | |
| framework = (body.get("framework") or "Python / pytest").strip() | |
| instructions = (body.get("instructions") or "").strip() | |
| if not source_code: | |
| return jsonify({"error": "source_code is required — paste your code"}), 400 | |
| if len(source_code) < 20: | |
| return jsonify({"error": "Source code too short — paste a real function or class"}), 400 | |
| if framework not in SUPPORTED_FRAMEWORKS: | |
| return jsonify({"error": f"Unsupported framework: {framework}"}), 400 | |
| result = generate_tests(source_code, framework, instructions) | |
| if not result: | |
| return jsonify({"error": "AI failed to generate tests — please try again"}), 502 | |
| return jsonify(result) | |