Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| from dotenv import load_dotenv | |
| from tools.generate_unit_test import generate_unit_test | |
| from tools.setup_jest import setup_jest | |
| load_dotenv() | |
| sample_code = """ | |
| /** | |
| * Applies a discount to a price. | |
| * @param price The original price. | |
| * @param discountRate The discount rate (e.g., 0.15 for 15% off). | |
| * @returns The discounted price. | |
| */ | |
| export function calculateDiscount(price: number, discountRate: number): number { | |
| // No discount if rate is invalid (e.g., greater than 1, implying more than 100% discount) | |
| if (discountRate > 1) { | |
| return price; | |
| } | |
| return price * (1 - discountRate); | |
| } | |
| """ | |
| sample_source_code_path = "src/calculate_discount.ts" | |
| sample_test_root_dir_path = "test/" | |
| sourceCodeContent = gr.Textbox(label="TypeScript code", lines=10, value=sample_code) | |
| sourceCodeFullPath = gr.Textbox(label="Path of TypeScript code", lines=1, value=sample_source_code_path) | |
| testFileFullPath = gr.Textbox(label="Path of root directory for Jest test files", lines=1, | |
| value=sample_test_root_dir_path) | |
| additionalSystemPrompt = gr.Textbox(label="Additional System Prompt", lines=10, value="") | |
| anthropicApiKey = gr.Textbox(label="Your Anthropic API Key", lines=1, type="password", value="") | |
| overview = """ | |
| ## Overview | |
| Developers spend up to 40% of their time writing and maintaining tests—time they could be spending on building new features. That's why I built CraftJest. | |
| CraftJest is an intelligent protocol that automatically analyzes your TypeScript code and crafts high-quality, human-readable unit tests using Jest. It doesn't just create boilerplate; it understands the context, logic, and potential edge cases of your code to generate meaningful tests. | |
| With a simple prompt, you can slash your testing time, improve code coverage, and let you focus on what truly matters: innovation! | |
| ## The Problem | |
| In modern software development, unit testing is the bedrock of code quality and stability. However, it's also a major bottleneck. Writing comprehensive, high-quality unit tests is tedious, time-consuming, and often pushed aside under tight deadlines. This leads to inconsistent test quality, reduced code coverage, and a higher risk of bugs in production. Developers are forced to choose between building new features and ensuring the reliability of existing ones. This painful trade-off slows down innovation and demoralizes development teams. | |
| ## The Solution | |
| CraftJest is a revolutionary "Model Context Protocol" designed to eliminate the friction of unit testing. It’s an intelligent tool that acts as a co-pilot for developers, automatically generating robust and readable Jest unit tests for any TypeScript codebase. | |
| I call it CraftJest because it doesn't just generate tests—it crafts them. Using a sophisticated system prompt, it goes beyond simple function signatures to understand the underlying logic, variable types, dependencies, and intended behavior of your code. | |
| ## Demo | |
| - **MCP Server URL:** https://agents-mcp-hackathon-craftjest.hf.space/gradio_api/mcp/sse | |
| ## Supported Prompts | |
| - "Setup Jest in this project." | |
| - "Generate unit test for this file." | |
| - "Generate unit test for this file. Use CleanArchitectureUnitTestGuideline.md as additional context." | |
| """ | |
| generate_unit_test_demo = gr.Interface( | |
| fn=generate_unit_test, | |
| inputs=[sourceCodeContent, sourceCodeFullPath, testFileFullPath, additionalSystemPrompt, anthropicApiKey], | |
| outputs=gr.Textbox(label="Jest Unit Test", lines=10), | |
| description=overview | |
| ) | |
| setup_jest_demo = gr.Interface( | |
| fn=setup_jest, | |
| inputs=[], | |
| outputs=gr.Textbox(label="Jest Setup Guide", lines=10), | |
| description="Generate a comprehensive guide for Jest setup, including TypeScript integration and code coverage configuration.", | |
| ) | |
| # Create tabbed interface | |
| demo = gr.TabbedInterface( | |
| [generate_unit_test_demo, setup_jest_demo], | |
| ["Generate Unit Test", "Setup Jest"], | |
| title="CraftJest: Spend less time testing, more time creating!", | |
| theme=gr.themes.Soft(), | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch(mcp_server=True) | |