inmemory_test.go

Overview

This file contains unit tests for the in-memory implementation of the memory.Service interface, specifically testing the Search functionality provided by the InMemoryService() from the memory package. The tests verify that the in-memory memory service correctly indexes, stores, and retrieves session events based on search queries, while enforcing proper scoping by application name and user ID.

The file defines a helper struct testSession which implements the session.Session interface, allowing the creation of mock sessions with lists of events for testing purposes. The test cases cover typical scenarios including matching events, absence of matches, and ensuring no data leakage across different apps or users.

The tests utilize external libraries such as go-cmp for deep comparison of expected and actual results, genai for generating content with roles, and Go standard libraries for time parsing and sorting.


Detailed Descriptions

Test_inMemoryService_SearchMemory

req := &memory.SearchRequest{
    AppName: "app1",
    UserID:  "user1",
    Query:   "quick hello",
}
resp, err := s.Search(ctx, req)
if err != nil {
    // handle error
}
// process resp.Memories

makeSession

sess := makeSession(t, "app1", "user1", "sess1", events)

testSession Struct and Methods


sortMemories Transformer


must Helper Function

t := must(time.Parse(time.RFC3339, "2023-10-01T10:00:00Z"))

Important Implementation Details


Interaction with Other System Components


Mermaid Diagram: File Structure and Function Relationships

flowchart TD
A[Test_inMemoryService_SearchMemory]
B[makeSession]
C[testSession]
D[must]
E[sortMemories]
A --> B
A --> E
A --> C
B --> C
A --> D