Spaces:
Sleeping
Sleeping
| """Test Forge — generates complete test suites from source code via AI.""" | |
| import json, re | |
| from app.core.ai import call_ai | |
| _SYSTEM = """You are a senior software engineer and testing expert with deep knowledge of | |
| testing best practices across all major languages and frameworks. | |
| You write thorough, runnable test suites that cover happy paths, edge cases, | |
| error scenarios, and boundary conditions.""" | |
| _PROMPT_TMPL = """Generate a complete test suite for the following source code. | |
| FRAMEWORK: {framework} | |
| SOURCE CODE: | |
| --- | |
| {source_code} | |
| --- | |
| ADDITIONAL INSTRUCTIONS: {instructions} | |
| Output the complete, ready-to-run test file — all imports, fixtures, setup, teardown. | |
| Use the exact {framework} syntax. Do NOT use placeholder comments like "// add more tests here". | |
| At the very end of the file, on its own line, add this comment with real numbers filled in: | |
| # FORGE_META:{{"test_count":<N>,"coverage_estimate":<N>,"edge_cases_count":<N>,"notes":"<1 sentence>"}} | |
| Output the test file only — no markdown fences, no preamble, no trailing explanation.""" | |
| # Language-specific comment prefixes for the metadata line | |
| _COMMENT_PREFIX = { | |
| "Python / pytest": "#", | |
| "TypeScript / Jest": "//", | |
| "JavaScript / Mocha": "//", | |
| "Java / JUnit": "//", | |
| "Go / testing": "//", | |
| "Ruby / RSpec": "#", | |
| "PHP / PHPUnit": "//", | |
| "C# / xUnit": "//", | |
| } | |
| _META_RE = re.compile(r'(?://|#)\s*FORGE_META:(\{.*\})', re.IGNORECASE) | |
| def generate_tests(source_code: str, framework: str, instructions: str = "") -> dict: | |
| """Generate a test suite for the given source code.""" | |
| comment = _COMMENT_PREFIX.get(framework, "//") | |
| prompt = _PROMPT_TMPL.format( | |
| framework=framework, | |
| source_code=source_code[:8000], | |
| instructions=instructions.strip() or "None — generate comprehensive coverage", | |
| comment=comment, | |
| ) | |
| raw = call_ai( | |
| [{"role": "user", "content": prompt}], | |
| system=_SYSTEM, | |
| max_tokens=4096, | |
| ) | |
| if not raw: | |
| return {} | |
| # Strip any accidental markdown fences | |
| code = re.sub(r'^```[a-z]*\n?', '', raw.strip(), flags=re.MULTILINE) | |
| code = re.sub(r'\n?```$', '', code, flags=re.MULTILINE).strip() | |
| # Extract metadata comment line | |
| meta = {"test_count": 0, "coverage_estimate": 0, "edge_cases_count": 0, "notes": ""} | |
| m = _META_RE.search(code) | |
| if m: | |
| try: | |
| meta = json.loads(m.group(1)) | |
| except json.JSONDecodeError: | |
| pass | |
| # Remove the meta comment line from the code | |
| code = code[:m.start()].rstrip() + code[m.end():] | |
| code = code.strip() | |
| meta["test_code"] = code | |
| return meta | |