gemini_test.go

Overview

This file provides unit and integration tests for the Gemini model implementation within the gemini package. The Gemini model is an LLM (Large Language Model) client interface designed to interact with Google’s Gemini language models using the genai SDK. The tests focus on validating the model's content generation capabilities, streaming response handling, and proper inclusion of HTTP tracking headers during API calls.

The file also contains supporting utility code for test client configuration, response reading/parsing from streaming LLM output, and an HTTP interceptor to verify request headers.

Detailed Breakdown

Test Functions


TestModel_Generate(t *testing.T)


TestModel_GenerateStream(t *testing.T)


TestModel_TrackingHeaders(t *testing.T)


Helper Functions and Types


newGeminiTestClientConfig(t *testing.T, rrfile string) *genai.ClientConfig


type TextResponse


func readResponse(s iter.Seq2[*model.LLMResponse, error]) (TextResponse, error)


type headerInterceptor


Implementation Details and Algorithms


Interactions with Other Parts of the System


Usage Example (Simplified)

// Create Gemini model client with test config
cfg := newGeminiTestClientConfig(t, "testdata/TestModel_Generate.ok.httprr")
model, err := NewModel(t.Context(), "gemini-2.0-flash", cfg)
if err != nil {
    t.Fatal(err)
}

// Prepare LLM request
req := &model.LLMRequest{
    Contents: genai.Text("What is the capital of France?"),
    Config: &genai.GenerateContentConfig{Temperature: new(float32)},
}

// Generate response synchronously
for resp, err := range model.GenerateContent(t.Context(), req, false) {
    if err != nil {
        t.Fatal(err)
    }
    fmt.Println(resp.Content.Parts[0].Text) // Expected "Paris\n"
}

Diagram: Flowchart of Test Functions and Utility Relationships

flowchart TD
subgraph Tests
TG[TestModel_Generate]
TS[TestModel_GenerateStream]
TH[TestModel_TrackingHeaders]
end
subgraph Utilities
NG[newGeminiTestClientConfig]
RR[readResponse]
HI[headerInterceptor]
end
TG --> NG
TG -->|calls| NewModel
TS --> NG
TS -->|calls| NewModel
TS --> RR
TH --> NG
TH --> HI
HI -->|wraps| HTTPTransport
NewModel[NewModel Factory]
HTTPTransport[http.RoundTripper]
style Tests fill:none,stroke:#333,stroke-width:1px
style Utilities fill:none,stroke:#333,stroke-width:1px

This documentation describes the testing logic and support mechanisms for ensuring the Gemini model's API client behaves correctly in generating content, streaming responses, and including telemetry headers during HTTP requests. It is a critical part of verifying the integration and correctness of the Gemini LLM client within the system.