Gemini API Integration
Relevant source files
The following files were used as context for generating this wiki page:
The Gemini API integration is a core feature of the TestGen platform, responsible for automatically generating test cases from a provided code context. The system is designed with two distinct implementation strategies for this integration: a Go-based backend server and a client-side TypeScript module that runs directly in the browser. Both implementations connect to Google's Gemini API to analyze code and produce structured test suites.
The primary goal is to take a string of code context, combine it with a structured prompt, and send it to the Gemini gemini-1.5-flash-latest model. The API is expected to return a JSON object containing an array of test cases and a summary, which is then parsed and displayed in the frontend.
Sources: README.md, testgen/IMPLEMENTATION.md, testgen/CLIENT_BACKEND.md
Architecture Overview
The system supports two architectural patterns for Gemini integration. The primary method, used in the live deployed application, is the client-side approach, which simplifies deployment. The Go backend serves as an alternative or original implementation.
This diagram illustrates the general data flow for test generation.
Data Structures
The request and response payloads for the Gemini API are consistently defined across both the Go backend and the TypeScript client.
GeminiRequest
This structure encapsulates the data sent to the TestGen system to initiate test generation.
| Field | Type | Description | Source |
|---|---|---|---|
apiKey / APIKey | string | The user's Google Gemini API key. | testgen-backend/main.go:65, testgen/src/lib/backend.ts:38 |
codeContext / CodeContext | string | The stringified code from the repository for analysis. | testgen-backend/main.go:66, testgen/src/lib/backend.ts:39 |
additionalPrompt / AdditionalPrompt | string (optional) | Any extra requirements or context for the AI. | testgen-backend/main.go:67, testgen/src/lib/backend.ts:40 |
GeminiResponse
This structure represents the JSON object expected from the Gemini API.
| Field | Type | Description | Source |
|---|---|---|---|
testCases | []GeminiTestCase | An array of generated test cases. | testgen-backend/main.go:53, testgen/src/lib/backend.ts:30 |
summary | struct | A summary of the generated tests, categorized by type. | testgen-backend/main.go:54-60, testgen/src/lib/backend.ts:31-37 |
GeminiTestCase
This structure defines a single, detailed test case.
| Field | Type | Description | Source |
|---|---|---|---|
id / ID | string | A unique identifier for the test case. | testgen-backend/main.go:44, testgen/src/lib/backend.ts:22 |
name / Name | string | A descriptive name for the test. | testgen-backend/main.go:45, testgen/src/lib/backend.ts:23 |
description / Description | string | A detailed explanation of the test's purpose. | testgen-backend/main.go:46, testgen/src/lib/backend.ts:24 |
input / Input | any / interface{} | The input data for the function being tested. | testgen-backend/main.go:47, testgen/src/lib/backend.ts:25 |
expected / Expected | any / interface{} | The expected output or result of the test. | testgen-backend/main.go:48, testgen/src/lib/backend.ts:26 |
code / Code | string | The function or code snippet being tested. | testgen-backend/main.go:49, testgen/src/lib/backend.ts:27 |
testType / TestType | string | Category of the test (e.g., 'unit', 'integration'). | testgen-backend/main.go:50, testgen/src/lib/backend.ts:28 |
priority / Priority | string | Priority of the test (e.g., 'high', 'medium', 'low'). | testgen-backend/main.go:51, testgen/src/lib/backend.ts:29 |
Client-Side Integration (TypeScript)
The primary implementation uses a TypeScript class, ClientBackend, to directly call the Gemini API from the user's browser. This approach is documented as being simpler for deployment and maintenance.
Sources: testgen/CLIENT_BACKEND.md, testgen/src/lib/backend.ts
Sequence Flow
The following diagram shows the sequence of events when generating tests via the client-side module.
Key Method: generateTests
The static method ClientBackend.generateTests orchestrates the entire client-side process.
- Prompt Construction: It builds a detailed prompt containing the code context, an optional additional prompt, and strict instructions for the AI to return valid JSON.
- API Call: It makes a
fetchrequest tohttps://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash-latest:generateContent. - Configuration: The request includes generation parameters.
temperature: 0.7topK: 40topP: 0.95maxOutputTokens: 8192
- Response Handling: It parses the JSON response, extracts the text, and uses a regex (
/\{[\s\S]*\}/) to isolate the JSON block, ensuring robustness against extraneous text from the API. - Data Validation & Enrichment: It validates the structure of the parsed response and adds unique IDs, default
testType, andpriorityif they are missing.
Sources: testgen/src/lib/backend.ts:468-554
Backend Integration (Go)
An alternative implementation exists as a Go backend server. It exposes an API endpoint that the frontend can call, which then communicates with the Gemini API.
Sources: testgen-backend/main.go
API Endpoint: /api/generate-tests
- Method:
POST - Handler:
generateTestsHandler - Request Body:
GeminiRequest - Response Body:
GeminiResponse
This endpoint performs similar logic to the client-side implementation: it constructs a prompt, calls the Gemini API, and parses the response.
Sources: testgen-backend/main.go:477-622, README.md
Sequence Flow
Sources: testgen-backend/main.go:477-622
Prompt Engineering
The quality of the generated tests heavily depends on the prompt sent to the Gemini API. Both Go and TypeScript implementations use a nearly identical, carefully crafted prompt template.
The prompt includes:
- Role-playing: "You are an expert software testing engineer."
- Core Task: "Analyze the provided code and generate comprehensive test cases."
- Context Injection: The full code context is embedded.
- JSON Format Specification: A clear example of the desired JSON output structure is provided.
- Guidelines: A list of instructions to guide the AI, such as covering edge cases, including positive/negative tests, and creating descriptive names.
- Constraint: A final instruction to "Return only valid JSON, no additional text or markdown formatting."
// Snippet from Go implementation
prompt := fmt.Sprintf(`
You are an expert software testing engineer. Analyze the provided code and generate comprehensive test cases.
Code Context:
%s
%s
Please generate test cases in the following JSON format:
{
"testCases": [
{
"id": "unique_id",
"name": "descriptive_test_name",
...
}
],
...
}
Guidelines:
...
Return only valid JSON, no additional text or markdown formatting.`, req.CodeContext, req.AdditionalPrompt)
Sources: testgen-backend/main.go:500-542, testgen/src/lib/backend.ts:471-512
Summary
The Gemini API integration is the intelligent core of TestGen, enabling the automated creation of diverse and comprehensive test cases. The project features a flexible architecture with both a server-side Go implementation and a more streamlined client-side TypeScript version. Both paths leverage sophisticated prompt engineering to guide the gemini-1.5-flash-latest model, ensuring the output is structured, relevant, and immediately usable by the application's test execution engine. The client-side approach, which calls the Gemini API directly from the browser, is highlighted as the preferred method for its simplicity in deployment and maintenance.
Sources: README.md, testgen/CLIENT_BACKEND.md, testgen-backend/main.go, testgen/src/lib/backend.ts