Backend API Endpoints
Relevant source files
The following files were used as context for generating this wiki page:
The TestGenAI backend is responsible for processing GitHub repositories, creating a context from the source code, and interfacing with the Google Gemini AI to generate test cases. The project has undergone an architectural shift, moving from a traditional Go-based server backend to a "client-side backend" that runs entirely in the browser. This document covers both implementations.
The initial architecture involved a React frontend communicating with a Go backend server. The current, recommended architecture replaces the Go server with a TypeScript module (ClientBackend) that performs the same functions by calling external APIs (GitHub, Gemini) directly from the browser. This simplifies deployment and reduces server costs.
Sources: README.md:16-25, testgen/CLIENT_BACKEND.md:3-12
Architecture Evolution
The system's architecture has evolved from a client-server model to a serverless, client-only model.
This diagram illustrates the two architectural approaches.
Go Backend API (Legacy)
Sources: testgen-backend/main.go, README.md:112-163
The legacy backend is a server written in Go. It exposes a REST API to handle repository cloning and test generation. It runs on port 3001 and is configured to allow CORS requests from the frontend, which is expected to run on http://localhost:8080.
Sources: README.md:200-202, testgen-backend/main.go:318-321, 574-575
API Endpoints
POST /api/clone-repo
This endpoint clones a public GitHub repository, filters its files to extract relevant source code, generates a context file, and returns metadata about the processed files.
Request Body
| Parameter | Type | Description |
|---|---|---|
repoUrl | string | The full URL of the GitHub repository to clone. |
| Sources: README.md:115-118, testgen-backend/main.go:13-15 |
Response Body
| Parameter | Type | Description |
|---|---|---|
success | boolean | Indicates if the operation was successful. |
message | string | A status message. |
filesCount | integer | The number of relevant files found and processed. |
contextPath | string | The server path to the generated context file. |
files | []FileContent | An array of objects containing file paths and content. |
| Sources: README.md:120-127, testgen-backend/main.go:17-24 |
Workflow
The following diagram shows the sequence of operations for the /api/clone-repo endpoint.
Sources: testgen-backend/main.go:317-391
POST /api/generate-tests
This endpoint takes a code context and an API key, sends a prompt to the Google Gemini API, and returns a structured list of generated test cases.
Request Body
| Parameter | Type | Description |
|---|---|---|
apiKey | string | The user's Google Gemini API key. |
codeContext | string | The code context generated from the repository. |
additionalPrompt | string | (Optional) Extra instructions for the AI. |
| Sources: README.md:130-135, testgen-backend/main.go:47-51 |
Response Body
The response is a JSON object containing testCases and a summary.
{
"testCases": [
{
"id": "test_1",
"name": "Test Case Name",
"description": "Test description",
"input": {},
"expected": {},
"code": "function to test",
"testType": "unit",
"priority": "high"
}
],
"summary": {
"totalTests": 10,
"unitTests": 8,
"integrationTests": 2
}
}
Sources: README.md:137-158, testgen-backend/main.go:26-45
GET /api/context/{owner}/{repo}
Returns the content of a previously generated context file for a specific repository.
Workflow
- The handler extracts the
ownerandrepofrom the URL path. - It constructs the local file path to the context file (e.g.,
repos/owner-repo-context.txt). - It reads the file from the disk and returns its content in a JSON response.
Sources: testgen-backend/main.go:393-429
Client-Side Backend (Current)
Sources: testgen/src/lib/backend.ts, testgen/CLIENT_BACKEND.md
The current implementation replaces the Go server with a TypeScript class, ClientBackend, that runs in the browser. It leverages the public GitHub API for fetching files and calls the Gemini API directly. This approach eliminates the need for a dedicated backend server.
The ApiService class acts as a wrapper around ClientBackend, providing a consistent interface for the frontend components.
Sources: testgen/CLIENT_BACKEND.md:5-12, testgen/src/lib/api.ts:18-28
Key Methods
ClientBackend.cloneRepository(repoUrl)
This method fetches repository contents directly using the GitHub REST API, replacing the git clone command and file system operations of the Go backend.
Workflow
The following diagram shows the client-side repository fetching process.
ClientBackend.generateTests(request)
This method constructs a prompt and calls the Google Gemini API directly from the browser.
Workflow
Data Structures
The TypeScript implementation defines interfaces that mirror the Go structures.
| Interface | Description |
|---|---|
RepoRequest | Contains the repoUrl string. |
FileContent | Represents a single file with path, content, and size. |
RepoResponse | The result of cloneRepository, containing status and an array of FileContent. |
GeminiRequest | The request for generateTests, containing apiKey and codeContext. |
GeminiResponse | The structured test case data returned from the Gemini API. |
Sources: testgen/src/lib/backend.ts:4-40
Summary
The project provides two backend implementations for the same core tasks: repository analysis and AI-driven test generation. The original Go backend offers a traditional server-based solution, while the newer client-side TypeScript backend provides a more modern, serverless approach that is simpler to deploy and maintain. The ClientBackend is the current and recommended implementation, making direct API calls to GitHub and Google Gemini from the user's browser.
Sources: testgen/CLIENT_BACKEND.md:14-22