Getting Started
Relevant source files
The following files were used as context for generating this wiki page:
TestGen is an AI-powered platform designed to automatically generate and execute test cases for code repositories. It leverages Google's Gemini AI to analyze code and create comprehensive test suites. The system comprises a React frontend for user interaction and a Go backend that handles repository cloning and analysis. An alternative client-side implementation also exists, which runs entirely in the browser using the GitHub and Gemini APIs directly.
This guide provides instructions for setting up the development environment, an overview of the architecture, and a walkthrough of the primary user workflow.
Sources: README.md:3-6, testgen/CLIENT_BACKEND.md:5-11
Architecture Overview
The TestGen platform follows a client-server architecture. The React frontend communicates with the Go backend via an HTTP API. The backend, in turn, interacts with the Google Gemini API to generate tests. A client-side "backend" also exists within the frontend, allowing it to interact with the GitHub and Gemini APIs directly from the browser, simplifying deployment.
Sources: README.md:15-25, testgen/CLIENT_BACKEND.md:5-11
The diagram below illustrates the high-level system architecture.
Project Structure
The repository is organized into two main directories: testgen for the frontend and testgen-backend for the backend.
testgenai/
├── testgen/ # Frontend (React + Vite + TypeScript)
│ ├── src/
│ │ ├── components/ # UI Components (TestRunner.tsx)
│ │ ├── pages/
│ │ │ └── Dashboard.tsx # Main application page
│ │ ├── lib/
│ │ │ ├── backend.ts # Client-side backend logic
│ │ │ └── github.ts # GitHub API utilities
│ ├── package.json
│ └── ...
├── testgen-backend/ # Backend (Go)
│ ├── main.go # Main server file
│ ├── go.mod # Go dependencies
│ └── repos/ # Cloned repositories (temporary)
└── README.md # Project README
Sources: README.md:27-46
Local Development Setup
You can run the application locally for development purposes.
Prerequisites
- Node.js 18+
- Go 1.21+
- Git
- A Google Gemini API Key
Sources: README.md:214-218
Frontend Setup (testgen/)
The frontend is a Vite-based React application.
# Navigate to the frontend directory
cd testgen/
# Install dependencies
npm install
# Start the development server
npm run dev
The frontend will be available at http://localhost:8080.
Sources: README.md:220-224, testgen/README.md:22-29
Backend Setup (testgen-backend/)
The backend is a Go server.
# Navigate to the backend directory
cd testgen-backend/
# Tidy dependencies
go mod tidy
# Run the server
go run main.go
The backend server will run on http://localhost:3001.
Sources: README.md:226-230
Usage Workflow
The application follows a three-step process: repository analysis, test generation, and test execution.
The diagram below shows the typical user workflow.
- Enter API Key: In the
Settingstab, enter your Google Gemini API key. - Clone Repository: In the
Generatetab, enter a public GitHub repository URL and click "Fetch Content". The application will fetch the repository's source files. - Generate Tests: Once the code context is loaded, click "Generate Test Cases".
- Execute Tests: Navigate to the
Executetab to run the generated tests. - View Results: See the pass/fail status and other details in the
Resultstab.
Sources: README.md:232-236, testgen/src/pages/Dashboard.tsx
Core Components
Frontend (testgen)
The frontend is built with React, TypeScript, and Vite.
Sources: testgen/package.json
Key UI Components
| Component | File Path | Description |
|---|---|---|
| Dashboard | src/pages/Dashboard.tsx | The main application interface, containing tabs for generating, executing, and viewing tests, as well as settings. |
| TestRunner | src/components/TestRunner.tsx | An engine for executing the generated JavaScript test cases. It includes a mock function library and provides real-time execution feedback. |
Sources: README.md:52-70, testgen/src/pages/Dashboard.tsx, testgen/src/components/TestRunner.tsx
Client-Side Backend (src/lib/backend.ts)
For simplified deployment, a client-side "backend" is implemented in src/lib/backend.ts. This module uses the GitHub API to fetch repository files directly from the browser, bypassing the need for the Go backend.
The cloneRepository function orchestrates this process:
- Parses the GitHub URL.
- Uses the GitHub Contents API to list files and directories.
- Recursively fetches the content of relevant source files, excluding patterns defined in
EXCLUDE_PATTERNS. - Generates a prompt context from the fetched files.
The following sequence diagram illustrates the file fetching process.
Backend (testgen-backend)
The Go backend provides a more robust solution for repository processing.
API Endpoints
The server exposes several endpoints to manage repository cloning and test generation.
| Endpoint | Method | Description |
|---|---|---|
/api/clone-repo | POST | Clones a GitHub repository, reads its files, generates a context file, and returns file metadata. |
/api/generate-tests | POST | Accepts code context and an API key, calls the Gemini API, and returns generated test cases. |
/api/context/{owner}/{repo} | GET | Retrieves a previously generated context file for a repository. |
Sources: testgen-backend/main.go:323-587, README.md:92-142
Repository Cloning (/api/clone-repo)
When this endpoint is called, the Go backend performs the following steps:
- Parses the GitHub URL to get the owner and repo name.
- Uses the
git clonecommand to clone the repository into a temporary directory underrepos/. - Walks the cloned directory, reading files that are not excluded by
excludePatterns. - Constructs a single context string containing the content of all relevant files.
- Saves this context to a
.txtfile. - Deletes the cloned repository directory.
- Returns a JSON response with the file count and paths.
Sources: testgen-backend/main.go:323-401
The cloning and context generation flow is shown below.
Sources: testgen-backend/main.go:153-241