stream_aggregator_test.go

Overview

This file contains unit tests for validating the streaming aggregation behavior of a language model interface. It simulates multiple calls to a streaming generation method and verifies that intermediate and accumulated responses are correctly aggregated and flagged as partial or complete. The tests cover scenarios including multiple streaming calls, intermediate resets caused by empty responses or multimedia content, and handling of purely audio/video streams without aggregation.

The core focus is on verifying how partial streaming responses are combined and when the aggregation state resets, ensuring that the aggregation logic behaves as expected under various simulated streaming conditions.


Types and Structs

streamAggregatorTest

This struct defines a test case for the stream aggregator tests.

Field

Type

Description

name

string

Descriptive name of the test case.

initialResponses

[]*genai.Content

Initial set of responses provided by the mock model to simulate streaming responses.

numberOfStreamCalls

int

Number of separate streaming calls to simulate in the test.

streamResponsesCount

int

Number of responses expected per streaming call.

want

[]*genai.Content

Expected aggregated stream responses after processing.

wantPartial

[]bool

Expected partial flag for each response indicating whether it is an intermediate partial response or a complete one.


Functions

TestStreamAggregator

func TestStreamAggregator(t *testing.T)

Description

This is the main test function that runs multiple subtests to validate the streaming aggregation behavior. Each subtest uses a streamAggregatorTest struct instance to provide inputs and expected outputs.

Parameters

Behavior

Usage Example

func TestStreamAggregator(t *testing.T) {
    // Runs predefined test cases such as multiple streams with resets and audio streams.
}

Implementation Details and Algorithms


Interaction with Other Components

This file is part of internal testing for streaming aggregation functionality likely implemented in the core model interaction or streaming layer, which ties to the broader LLM Integration and Agents topic.


Mermaid Diagram: Function Flow and Test Structure

flowchart TD
A[TestStreamAggregator] --> B{For each Test Case}
B --> C[Initialize MockModel with initialResponses]
C --> D[Loop over numberOfStreamCalls]
D --> E[Call GenerateStream -> Receive stream responses]
E --> F[For each response]
F --> G{Check for errors}
G -->|Error| H[Test fails]
G -->|No Error| I[Compare response content with want]
I --> J[Compare partial flag with wantPartial]
J --> K{All responses checked?}
K -->|No| E
K -->|Yes| L{All stream calls done?}
L -->|No| D
L -->|Yes| M[Verify total responses count]
M --> N[Test passes or fails]

Summary of Key Points