11// Copyright (c) Microsoft. All rights reserved.
22
3+ using System ;
4+ using System . Collections . Generic ;
35using System . Text . Json ;
6+ using System . Threading ;
7+ using System . Threading . Tasks ;
8+ using Microsoft . Extensions . AI ;
49
510namespace Microsoft . Agents . AI . Foundry . Hosting . UnitTests ;
611
712public sealed class InMemoryAgentSessionStoreTests
813{
9- [ Fact ]
10- public async Task SaveSessionAsync_CompositeIdentifiersPreserveBoundariesAsync ( )
14+ [ Theory ]
15+ [ InlineData ( "cedar:u-elm" , "fir" , "trail" , "cedar" , "elm:u-fir" , "trail" ) ]
16+ [ InlineData ( "cedar" , "elm:c-fir" , "trail" , "cedar" , "elm" , "fir:c-trail" ) ]
17+ [ InlineData ( "cedar:c-elm" , null , "trail" , "cedar" , null , "elm:c-trail" ) ]
18+ [ InlineData ( "cedar:u-elm" , null , "trail" , "cedar" , "elm" , "trail" ) ]
19+ [ InlineData ( "cedar" , "elm" , "trail" , "maple" , "elm" , "trail" ) ]
20+ [ InlineData ( null , "elm" , "trail" , "cedar" , "elm" , "trail" ) ]
21+ [ InlineData ( " " , "elm" , "trail" , null , "elm" , "trail" ) ]
22+ [ InlineData ( " cedar" , "elm" , "trail" , "cedar" , "elm" , "trail" ) ]
23+ [ InlineData ( "cedar " , "elm" , "trail" , "cedar" , "elm" , "trail" ) ]
24+ [ InlineData ( "Cedar" , "elm" , "trail" , "cedar" , "elm" , "trail" ) ]
25+ [ InlineData ( "cedar" , "elm" , "trail" , "cedar" , "fir" , "trail" ) ]
26+ [ InlineData ( "cedar" , "elm" , "trail" , "cedar" , null , "trail" ) ]
27+ [ InlineData ( "cedar" , " elm" , "trail" , "cedar" , "elm" , "trail" ) ]
28+ [ InlineData ( "cedar" , "elm " , "trail" , "cedar" , "elm" , "trail" ) ]
29+ [ InlineData ( "cedar" , "Elm" , "trail" , "cedar" , "elm" , "trail" ) ]
30+ [ InlineData ( "cedar" , "elm" , "trail" , "cedar" , "elm" , "glade" ) ]
31+ [ InlineData ( "cedar" , "elm" , " trail" , "cedar" , "elm" , "trail" ) ]
32+ [ InlineData ( "cedar" , "elm" , "trail " , "cedar" , "elm" , "trail" ) ]
33+ [ InlineData ( "cedar" , "elm" , "Trail" , "cedar" , "elm" , "trail" ) ]
34+ [ InlineData ( "cedar" , "elm" , "" , "cedar" , "elm" , " " ) ]
35+ public async Task SaveSessionAsync_CompositeIdentifiersPreserveBoundariesAsync (
36+ string ? firstAgentName , string ? firstUserId , string firstConversationId ,
37+ string ? secondAgentName , string ? secondUserId , string secondConversationId )
1138 {
12- // Arrange: these identifier tuples collapse to the same colon-delimited string key.
39+ // Arrange
1340 var store = new InMemoryAgentSessionStore ( ) ;
14- var agent = new TestAgent ( "concierge" ) ;
41+ var firstAgent = new TestAgent ( firstAgentName ) ;
42+ var secondAgent = new TestAgent ( secondAgentName ) ;
1543
16- // Act
17- await store . SaveSessionAsync ( agent , "delta" , new TestSession ( "first" ) , "bravo:c-charlie" ) ;
18- await store . SaveSessionAsync ( agent , "charlie:c-delta" , new TestSession ( "second" ) , "bravo" ) ;
19- var first = await store . GetSessionAsync ( agent , "delta" , "bravo:c-charlie" ) ;
20- var second = await store . GetSessionAsync ( agent , "charlie:c-delta" , "bravo" ) ;
44+ // Act & Assert
45+ await store . SaveSessionAsync ( firstAgent , firstConversationId , new TestSession ( "first" ) , firstUserId ) ;
46+ Assert . Null ( await store . GetSessionAsync ( secondAgent , secondConversationId , secondUserId ) ) ;
47+
48+ await store . SaveSessionAsync ( secondAgent , secondConversationId , new TestSession ( "second" ) , secondUserId ) ;
49+ var first = await store . GetSessionAsync ( firstAgent , firstConversationId , firstUserId ) ;
50+ var second = await store . GetSessionAsync ( secondAgent , secondConversationId , secondUserId ) ;
2151
22- // Assert
2352 Assert . Equal ( "first" , Assert . IsType < TestSession > ( first ) . Value ) ;
2453 Assert . Equal ( "second" , Assert . IsType < TestSession > ( second ) . Value ) ;
2554 }
2655
56+ [ Theory ]
57+ [ InlineData ( null , null , null , null ) ]
58+ [ InlineData ( null , null , "" , "" ) ]
59+ [ InlineData ( "" , "" , null , " \t \r \n " ) ]
60+ [ InlineData ( null , "elm" , "" , "elm" ) ]
61+ [ InlineData ( "cedar" , null , "cedar" , "" ) ]
62+ [ InlineData ( "cedar" , null , "cedar" , " " ) ]
63+ [ InlineData ( "cedar" , null , "cedar" , "\t \r \n " ) ]
64+ [ InlineData ( "cedar" , "elm" , "cedar" , "elm" ) ]
65+ [ InlineData ( " cedar " , " elm " , " cedar " , " elm " ) ]
66+ public async Task SaveSessionAsync_EquivalentIdentifiersReuseAndReplaceSessionAsync (
67+ string ? firstAgentName , string ? firstUserId , string ? secondAgentName , string ? secondUserId )
68+ {
69+ // Arrange
70+ var store = new InMemoryAgentSessionStore ( ) ;
71+ var firstAgent = new TestAgent ( firstAgentName ) ;
72+ var secondAgent = new TestAgent ( secondAgentName ) ;
73+ Assert . NotEqual ( firstAgent . Id , secondAgent . Id ) ;
74+
75+ // Act & Assert
76+ await store . SaveSessionAsync ( firstAgent , "trail" , new TestSession ( "initial" ) , firstUserId ) ;
77+ var restored = await store . GetSessionAsync ( secondAgent , "trail" , secondUserId ) ;
78+ Assert . Equal ( "initial" , Assert . IsType < TestSession > ( restored ) . Value ) ;
79+
80+ await store . SaveSessionAsync ( secondAgent , "trail" , new TestSession ( "updated" ) , secondUserId ) ;
81+ var updated = await store . GetSessionAsync ( firstAgent , "trail" , firstUserId ) ;
82+ Assert . Equal ( "updated" , Assert . IsType < TestSession > ( updated ) . Value ) ;
83+ }
84+
2785 private sealed class TestSession ( string value ) : AgentSession
2886 {
2987 public string Value { get ; } = value ;
3088 }
3189
32- private sealed class TestAgent ( string name ) : AIAgent
90+ private sealed class TestAgent ( string ? name ) : AIAgent
3391 {
3492 public override string ? Name => name ;
3593
@@ -53,14 +111,14 @@ protected override ValueTask<AgentSession> DeserializeSessionCoreAsync(
53111 ValueTask . FromResult < AgentSession > ( new TestSession ( serializedState . GetString ( ) ! ) ) ;
54112
55113 protected override Task < AgentResponse > RunCoreAsync (
56- IEnumerable < Microsoft . Extensions . AI . ChatMessage > messages ,
114+ IEnumerable < ChatMessage > messages ,
57115 AgentSession ? session = null ,
58116 AgentRunOptions ? options = null ,
59117 CancellationToken cancellationToken = default ) =>
60118 throw new NotSupportedException ( ) ;
61119
62120 protected override IAsyncEnumerable < AgentResponseUpdate > RunCoreStreamingAsync (
63- IEnumerable < Microsoft . Extensions . AI . ChatMessage > messages ,
121+ IEnumerable < ChatMessage > messages ,
64122 AgentSession ? session = null ,
65123 AgentRunOptions ? options = null ,
66124 CancellationToken cancellationToken = default ) =>
0 commit comments