memory_test.go
Overview
The memory_test.go file contains unit tests for the in-memory memory service implementation within the system. Its primary purpose is to verify the correct behavior of memory operations such as adding session data and searching memory entries based on query strings. The tests ensure that memory entries are correctly indexed, retrieved, and isolated per user and session, validating the core functionalities of the memory subsystem.
The file focuses on the interaction between session events and memory storage, simulating user sessions with associated events and testing search capabilities against these stored memories. It exercises the imemory.Memory struct, which wraps the memory service and session information, verifying the integration between session data and memory retrieval.
Key Components
Types and Imports
Package: memory_test
Imports: Testing package (
testing), time utilities (time), comparison utilities (github.com/google/go-cmp/cmp), and internal packages for memory, session management, and data models.Core Test Subject:
imemory.Memory— a wrapper around the in-memory memory service (memory.InMemoryService()), associated with a specific user, app, and session.
Test Functions
1. TestMemory_AddAndSearch
Purpose: Tests the addition of session events into memory and subsequent search queries.
Setup:
Creates a memory service instance for a specific app, user, and session.
Defines two user-generated content events with timestamps.
Creates a session with the session service, appends the events to this session.
Adds the session to memory using AddSession.
Search Tests: Multiple test cases check search queries against the stored memory entries:
Case-insensitive search.
Matching single or multiple entries.
Queries returning no results or empty queries.
Assertions: Compares the search results against expected memory entries using deep comparison with
cmp.Diff.
Parameters:
t *testing.T: The testing context for running subtests and assertions.
Usage Example:
memoryService := imemory.Memory{
Service: memory.InMemoryService(),
UserID: "userID",
AppName: "appName",
SessionID: "sessionID",
}
// Add sessions and then perform a search.
2. TestMemory_Search_NoData
Purpose: Ensures searching an empty memory yields zero results without errors.
Setup: Instantiates an empty memory service for a test user and session.
Assertions: Verifies the returned memories slice is empty when no data is stored.
3. TestMemory_Search_Isolation
Purpose: Tests that memory entries are isolated per user and session.
Setup:
Creates two separate memory instances for different users but the same app.
Adds distinct session events for each user.
Assertions: Checks that searches return only the memories for the correct user, ensuring no cross-user data leakage.
Important Implementation Details
Memory Service: Uses an in-memory implementation (
memory.InMemoryService()) suitable for testing and transient data.Session and Events: Sessions are created and managed via
session.InMemoryService(). Events hold timestamped user content, which is stored and later indexed in memory.Search Behavior:
Case-insensitive matching is validated.
Searches support multi-word queries, matching any word within the stored content.
Empty queries or unmatched terms return empty results.
Context Propagation: Each test uses
t.Context()to pass context for API calls, ensuring test lifecycle awareness.Comparison: Uses
cmp.Diffwithcmpopts.EquateEmpty()to handle empty slices and detect discrepancies.
Relationships with Other System Components
Session Management (
sessionpackage): The tests depend on thesession.InMemoryService()to create and manage user sessions and events, demonstrating interaction with the session subsystem (Session Management).Memory Service (
memorypackage): The core memory service under test is the in-memory implementation, which stores and indexes session events for query-based retrieval.Model Layer (
modelpackage): Usesmodel.LLMResponseto encapsulate content within session events.Content Abstraction (
genaipackage): Content is created usinggenai.NewContentFromTextwith role annotations, representing user-generated text in a structured format.Mutable Session Adapter (
sessioninternalpackage): Wraps session objects to provide a mutable interface for the memory service.
The file exercises the integration points between session event storage and memory indexing/search, validating that the memory layer correctly reflects session data and isolates data per user and session.
Visual Diagram: Test Structure and Workflow
flowchart TD
A[TestMemory_AddAndSearch] --> B[Create Memory Service]
B --> C[Create Session Service]
C --> D[Create Session]
D --> E[Append Events to Session]
E --> F[Add Session to Memory]
F --> G[Run Search Queries]
G --> H[Compare Results]
A2[TestMemory_Search_NoData] --> B2[Create Empty Memory Service]
B2 --> G2[Search Any Query]
G2 --> H2[Verify Empty Results]
A3[TestMemory_Search_Isolation] --> B3[Create Memory1 for User1]
B3 --> C3[Create Session1 and Add Event]
C3 --> F3[Add Session1 to Memory1]
A3 --> B4[Create Memory2 for User2]
B4 --> C4[Create Session2 and Add Event]
C4 --> F4[Add Session2 to Memory2]
F3 --> G3[Search in Memory1]
F4 --> G4[Search in Memory2]
G3 --> H3[Verify User1 Results Only]
G4 --> H4[Verify User2 Results Only]
Detailed Function and Method Descriptions
TestMemory_AddAndSearch(t *testing.T)
Description: Tests adding session events to memory and performing search queries.
Parameters:
t *testing.T: Test harness context.
Behavior:
Sets up a memory instance linked to a user and session.
Creates a session and appends two events with textual content.
Adds the session to memory, which indexes the events.
Runs multiple search queries to verify correct retrieval based on keywords.
Return: None (test function).
Errors: Causes test failure on any error in session creation, event appending, memory addition, or search.
TestMemory_Search_NoData(t *testing.T)
Description: Verifies that searching an empty memory yields zero results.
Parameters:
t *testing.T
Behavior:
Creates a memory service instance without adding any session data.
Executes a search query and confirms the memory is empty.
Return: None.
TestMemory_Search_Isolation(t *testing.T)
Description: Ensures that memory entries are isolated per user/session context.
Parameters:
t *testing.T
Behavior:
Creates two separate memory instances for different users.
Adds distinct session events for each user.
Performs search queries on each memory instance.
Verifies that search results only include data for the corresponding user.
Return: None.
This file plays a critical role in validating the correctness and isolation of the in-memory memory service, ensuring it properly indexes session events and supports accurate, user-scoped search queries. It exercises system components responsible for session event management and memory indexing, confirming their interoperability and expected behavior under various conditions. The testing patterns demonstrated here can be referenced when extending memory functionality or integrating with other services such as Session Management.