Backend Technology Stack
Relevant source files
The following files were used as context for generating this wiki page:
The TestGen AI platform utilizes a backend system responsible for cloning code repositories, processing source files to create a context, and interfacing with the Google Gemini AI to generate test cases. The project features two distinct backend implementations: a primary server-side backend written in Go and an alternative client-side backend written in TypeScript that runs entirely in the browser.
The Go backend provides a robust, server-based solution for handling repository operations and AI integration. The client-side backend offers a serverless alternative, simplifying deployment by leveraging the GitHub and Gemini APIs directly from the user's browser. This document details the architecture, technology stack, and API of both backend implementations.
Sources: README.md:15-25, testgen/CLIENT_BACKEND.md:5-11, testgen-backend/main.go
Architecture Overview
The backend acts as an intermediary between the React frontend and external services like Git and the Google Gemini API. Its primary role is to abstract the complexity of fetching and processing repository code before sending it to the AI for test generation.
The diagram below illustrates the high-level system architecture, showing the interaction between the frontend, backend, and external services.
This diagram shows the two backend approaches: the solid lines represent the Go backend flow, while the dashed lines represent the client-side backend flow.
Sources: README.md:15-25, testgen/CLIENT_BACKEND.md:5-11
Go Backend (testgen-backend/)
The primary backend is a server written in Go. It exposes a set of RESTful API endpoints for the frontend to consume.
Sources: testgen-backend/main.go, README.md:94-99
Technology Stack
- Language: Go (1.21+)
- Core Libraries: Go Standard Library (
net/http,os,os/exec,encoding/json) - External Dependencies: None listed in
go.modbeyond the standard library. - External Tools:
gitcommand-line tool for repository cloning.
Sources: README.md:95-99, testgen-backend/main.go:170-175
API Endpoints
The Go backend exposes three main endpoints.
| Method | Path | Handler Function | Description |
|---|---|---|---|
POST | /api/clone-repo | cloneRepoHandler | Clones a GitHub repository, processes its files, and returns a context file. |
POST | /api/generate-tests | generateTestsHandler | Sends code context to the Gemini API to generate test cases. |
GET | /api/context/{owner}/{repo} | getContextHandler | Retrieves a previously generated context file for a repository. |
Sources: testgen-backend/main.go:561-563
POST /api/clone-repo
This endpoint handles the cloning and processing of a GitHub repository.
Request Body:
{
"repoUrl": "https://github.com/username/repository%22
}
Response Body:
{
"success": true,
"message": "Repository cloned successfully",
"filesCount": 25,
"contextPath": "repos/username-repo-context.txt",
"files": [
{
"path": "src/main.go",
"content": "package main...",
"size": 1234
}
]
}
Data Flow:
The following diagram shows the process flow for the cloneRepoHandler.
This flowchart illustrates the steps taken by the Go backend to process a repository, from receiving the request to sending the final response.
Sources: testgen-backend/main.go:343-418
POST /api/generate-tests
This endpoint interfaces with the Google Gemini API to generate tests.
Request Body:
{
"apiKey": "your-gemini-api-key",
"codeContext": "repository code context...",
"additionalPrompt": "Generate unit tests"
}
Response Body (GeminiResponse):
{
"testCases": [
{
"id": "test_1",
"name": "Test Case Name",
"description": "Test description",
"input": {},
"expected": {},
"code": "function to test",
"testType": "unit",
"priority": "high"
}
],
"summary": { ... }
}
Sequence Diagram:
This diagram shows the interaction between the client, the Go backend, and the Gemini API.
Core Logic and Data Structures
The backend logic is organized into handlers and utility functions within main.go.
Key Functions:
parseGitHubURL(url string): Extracts owner and repo from various GitHub URL formats.cloneRepository(owner, repo, clonePath string): Executesgit clone --depth 1to download the repository.readRepositoryFiles(repoPath string): Walks the cloned directory, filters files based onexcludePatternsand size, and reads their content.generatePromptContext(files []FileContent): Concatenates file contents into a single string formatted for the Gemini API.shouldExcludeFile(filePath string): Checks if a file path matches any of the predefined exclusion patterns.
Sources: testgen-backend/main.go:129-341
Data Structures:
The Go backend uses several structs to model data for API requests and responses.
| Struct Name | Purpose |
|---|---|
RepoRequest | Represents the incoming request to clone a repo. |
FileContent | Holds the path, content, and size of a single file. |
RepoResponse | The successful response after cloning a repo. |
GeminiTestCase | A single test case generated by the AI. |
GeminiResponse | The complete response from the test generation API. |
GeminiRequest | The request sent to the /api/generate-tests endpoint. |
Sources: testgen-backend/main.go:15-62
Configuration
CORS (Cross-Origin Resource Sharing) is configured to allow requests from the frontend, which is expected to run on http://localhost:8080 during development.
// From cloneRepoHandler
w.Header().Set("Access-Control-Allow-Origin", "http://localhost:8080")
w.Header().Set("Access-Control-Allow-Methods", "POST, OPTIONS")
w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
Sources: testgen-backend/main.go:345-348
Client-Side Backend (testgen/src/lib/backend.ts)
As an alternative to the Go server, the project includes a "client-side backend" implemented in TypeScript. This implementation runs entirely in the browser and replicates the Go backend's functionality by calling the GitHub and Gemini APIs directly.
Sources: testgen/CLIENT_BACKEND.md:5-11, testgen/src/lib/backend.ts
Technology Stack
- Language: TypeScript
- APIs:
- GitHub REST API (for fetching repository contents)
- Google Gemini API (for test generation)
- Browser APIs:
fetch,atob(for Base64 decoding)
Sources: testgen/CLIENT_BACKEND.md:13-19
Core Logic
The client-side logic is encapsulated in the ClientBackend class. Instead of cloning the repository with Git, it uses the GitHub Contents API to recursively fetch file data.
Data Flow for Repository Fetching:
This flowchart shows how the client-side backend uses the GitHub API to fetch and process files without cloning the repository locally.
Sources: testgen/src/lib/backend.ts:182-251, testgen/src/lib/backend.ts:254-315
Comparison: Go vs. Client-Side Backend
| Feature | Go Backend (testgen-backend/) | Client-Side Backend (src/lib/backend.ts) |
|---|---|---|
| Environment | Server-side (Node.js-like environment) | Browser |
| Deployment | Requires a running server process | Deployed as part of the static frontend (e.g., on Vercel) |
| Repo Fetching | git clone via os/exec | GitHub REST API (/repos/{owner}/{repo}/contents) |
| Dependencies | git installed on the server | None (uses browser fetch API) |
| Performance | Potentially faster for large repos due to git clone. | Limited by GitHub API rate limits (60 req/hr unauthenticated). |
| Security | API keys are managed on the server. | API keys are handled in the browser, which is a security consideration. |
| File Filtering | Scans a full local copy of the repo. | Fetches file tree and filters before downloading content. |
| File Size Limit | 1MB per file. | 50KB per file. |
| Max Files | No hard limit. | Limited to 15 files. |
Sources: testgen/CLIENT_BACKEND.md:23-35, testgen-backend/main.go:215-218, testgen/src/lib/backend.ts:180, [testgen/src/lib/backend.ts:200]
Summary
The TestGen AI project employs a flexible backend strategy with two distinct implementations. The Go backend is a traditional, powerful server-side application that handles heavy lifting like cloning repositories. The TypeScript client-side backend provides a lightweight, serverless alternative that is easier to deploy and maintain, albeit with limitations related to API rate limits and security. This dual approach allows the application to be deployed in different environments and scenarios, showcasing both server-based and client-based architectural patterns for interacting with external APIs.