instruction_processor_test.go

Overview

This file contains comprehensive unit tests for the session state and artifact injection functionality within instruction templates, specifically testing the InjectSessionState function. The purpose of these tests is to verify that placeholders in instruction template strings are correctly replaced with values from session state or artifact contents, and that error handling behaves as expected in various scenarios.

These tests ensure the robustness of the dynamic instruction injection mechanism, which is a critical part of the Instruction Injection subtopic. This functionality supports the generation of context-aware prompts by embedding live session and artifact data into LLM instructions.


Detailed Explanation of Test Structure and Logic

Test Function: TestInjectSessionState

This is the sole exported test function in the file, defined using Go's testing package. It employs table-driven testing to cover multiple scenarios of template injection:

func TestInjectSessionState(t *testing.T) { ... }

Test Case Structure

Each test case is defined as a struct with the following fields:

Field Name

Type

Description

name

string

Identifier for the sub-test, describing the scenario.

template

string

Input instruction template string containing placeholders.

state

map[string]any

Initial session state key-value pairs to inject.

artifacts

map[string]*genai.Part

Artifacts keyed by filename, representing saved artifact data.

expectNilService

bool

If true, simulates uninitialized artifact service (nil), to test error handling.

want

string

Expected resulting string after injection.

wantErr

bool

Whether an error is expected from injection.

wantErrMsg

string

Substring expected in the error message, for assertion.

Test Case Examples


Test Workflow

For each test case, the following steps occur:

  1. Subtest Initialization with t.Run
    Each case runs as a subtest, isolating execution and improving output clarity.

  2. Session Service Setup

    • An in-memory session service is created (session.InMemoryService()).

    • A session is created with the test case state via sessionService.Create.

    • A mutable session wrapper is constructed (sessioninternal.NewMutableSession).

  3. Artifact Service Setup

    • If expectNilService is false, an in-memory artifact service is initialized and wrapped.

    • Artifacts from the test case are saved into the artifact service.

  4. Invocation Context Creation

    • An InvocationContext is constructed with the session and artifact services (icontext.NewInvocationContext).

  5. Injection Execution

    • The function under test, InjectSessionState(ctx, template), is called with the context and template.

  6. Assertion

    • If an error is expected (wantErr), asserts an error occurred and optionally checks error message content.

    • Otherwise, asserts no error and that the output matches the expected string exactly.


Important Implementation Details and Algorithms Tested


Interaction with Other Parts of the System

This test file validates core behavior of the InjectSessionState function, which is part of the Instruction Template Processing subsystem (80563) and Instruction Injection (80568). Specifically:


Usage Example from Test Cases

template := "Hello {user_name}, you are in {app_state} state."
state := map[string]any{"user_name": "Foo", "app_state": "active"}

sessionService := session.InMemoryService()
createResp, err := sessionService.Create(ctx, &session.CreateRequest{
    AppName:   "testApp",
    UserID:    "testUser",
    SessionID: "testSession",
    State:     state,
})
sess := sessioninternal.NewMutableSession(sessionService, createResp.Session)

artifacts := &artifactinternal.Artifacts{
    Service:   artifact.InMemoryService(),
    AppName:   "testApp",
    UserID:    "testUser",
    SessionID: "testSession",
}

ctx := icontext.NewInvocationContext(context.Background(), icontext.InvocationContextParams{
    Artifacts: artifacts,
    Session:   sess,
})

result, err := InjectSessionState(ctx, template)
// result should be "Hello Foo, you are in active state."

This example shows the setup of session and artifact services, creation of an invocation context, and execution of the injection function under test.


Mermaid Diagram: Test Structure and Workflow

flowchart TD
Start[TestInjectSessionState]
Start --> DefineTestCases[Test Cases Defined]
DefineTestCases --> ForEachTestCase[For Each Test Case]
ForEachTestCase --> SetupSessionService[Setup Session Service]
SetupSessionService --> SetupArtifactService[Setup Artifact Service]
SetupArtifactService --> CreateInvocationContext[Create Invocation Context]
CreateInvocationContext --> CallInjectSessionState[Call InjectSessionState]
CallInjectSessionState --> AssertResults[Assert Output or Error]
AssertResults --> NextTestCase{More Tests?}
NextTestCase -->|Yes| ForEachTestCase
NextTestCase -->|No| End[Test Complete]

Summary of Key Points


This documentation references the following related topics for additional context: