Class AbstractSessionRepositoryContractTests

java.lang.Object
org.springframework.ai.test.session.AbstractSessionRepositoryContractTests

public abstract class AbstractSessionRepositoryContractTests extends Object
Contract tests every SessionRepository implementation must pass. Subclass it, return your repository from createRepository(), and run the tests with the JUnit Platform:

 class RedisSessionRepositoryContractTests extends AbstractSessionRepositoryContractTests {

     @Override
     protected SessionRepository createRepository() {
         return RedisSessionRepository.builder().connectionFactory(...).build();
     }

 }
 

Every test creates its own sessions with random ids, so createRepository() may return a shared store. The tests cover session lifecycle, idempotent append, the read contract of SessionRepository.findEvents(java.lang.String, org.springframework.ai.session.EventFilter) (pushdown or EventFilter.apply(List) must give the same result, including a lastN window that keeps turns whole), cross-session reads, the atomic SessionRepository.appendEvents(java.lang.String, java.util.List<org.springframework.ai.session.SessionEvent>, long) and the active view a boundary defines.

Since:
2.1.0
Author:
Christian Tzolov
  • Field Details

  • Constructor Details

    • AbstractSessionRepositoryContractTests

      public AbstractSessionRepositoryContractTests()
  • Method Details

    • createRepository

      protected abstract SessionRepository createRepository()
      Returns the repository under test. Called before every test.
      Returns:
      the repository
    • newSession

      protected Session newSession(String principalName)
      Saves a new session with a random id for the given principal.
    • findSession

      protected Session findSession(String id)
      The session with the given id, which must exist.
    • principalSession

      protected static Session principalSession(String id, String principalName)
      A session with the given id whose metadata names the principal.
    • append

      protected void append(String sessionId, Message... messages)
      Appends each message as its own event.
    • texts

      protected List<String> texts(String sessionId, EventFilter filter)
      The texts of the message events matching the filter, in the order returned.
    • event

      protected static SessionEvent event(String sessionId, Message message)
    • status

      protected static SessionEvent status(String sessionId, String status)
      A status event, the simplest event that is not a message.
    • compact

      protected void compact(String sessionId, List<SessionEvent> pinned, List<SessionEvent> summary, List<SessionEvent> preserved)
      Records a compaction the way the service does: appends a boundary naming the pinned, summary and preserved events, followed by the summary events, under the current version.
    • synthetic

      protected static SessionEvent synthetic(String sessionId, Message message)
      A synthetic event, as a compaction strategy would produce it.
    • summaryTurn

      protected static List<SessionEvent> summaryTurn(String sessionId, String summary)
      A synthetic shadow-prompt + summary pair, as the summarizing strategy produces.
    • labels

      protected static List<String> labels(List<SessionEvent> events)
      Event labels: the text, "Σ?" for a synthetic shadow prompt, "Σ:text" for a summary, "A[name]" for an assistant tool call, "T[name]" for a tool result, "⊥" for a compaction boundary and the status (or the type name) for any other event.
    • ids

      protected static Set<String> ids(List<SessionEvent> events, String... labels)
      The ids of the events with the given labels.
    • user

      protected static Message user(String text)
    • assistant

      protected static Message assistant(String text)
    • system

      protected static Message system(String text)
    • toolCall

      protected static Message toolCall(String callId, String toolName)
      An assistant message that calls one tool.
    • toolResponse

      protected static Message toolResponse(String callId, String toolName, String data)
      The result of one tool call.