1313
1414using System ;
1515using System . Collections . Generic ;
16+ using System . Linq ;
1617using System . Net . Http ;
18+ using System . Threading . Tasks ;
1719using Dapr . AI . Conversation ;
1820using Dapr . AI . Conversation . Extensions ;
1921using Microsoft . Extensions . Configuration ;
@@ -34,7 +36,7 @@ public void AddDaprConversationClient_FromIConfiguration()
3436 var services = new ServiceCollection ( ) ;
3537 services . AddSingleton < IConfiguration > ( configuration ) ;
3638
37- services . AddDaprAiConversation ( ) ;
39+ services . AddDaprConversationClient ( ) ;
3840
3941 var app = services . BuildServiceProvider ( ) ;
4042
@@ -45,18 +47,66 @@ public void AddDaprConversationClient_FromIConfiguration()
4547 }
4648
4749 [ Fact ]
48- public void AddDaprAiConversation_WithoutConfigure_ShouldAddServices ( )
50+ public void AddDaprConversationClient_RegistersDaprClientOnlyOnce ( )
4951 {
5052 var services = new ServiceCollection ( ) ;
51- var builder = services . AddDaprAiConversation ( ) ;
53+
54+ var clientBuilder = new Action < IServiceProvider , DaprConversationClientBuilder > ( ( sp , builder ) =>
55+ {
56+ builder . UseDaprApiToken ( "abc" ) ;
57+ } ) ;
58+
59+ services . AddDaprConversationClient ( ) ; //Sets a default API token value of an empty string
60+ services . AddDaprConversationClient ( clientBuilder ) ; //Sets the API token value
61+
62+ var serviceProvider = services . BuildServiceProvider ( ) ;
63+ var daprConversationClient = serviceProvider . GetService < DaprConversationClient > ( ) ;
64+
65+ Assert . NotNull ( daprConversationClient ! . HttpClient ) ;
66+ Assert . False ( daprConversationClient . HttpClient . DefaultRequestHeaders . TryGetValues ( "dapr-api-token" , out var _ ) ) ;
67+ }
68+
69+ [ Fact ]
70+ public void AddDaprConversationClient_RegistersUsingDependencyFromIServiceProvider ( )
71+ {
72+ var services = new ServiceCollection ( ) ;
73+ services . AddSingleton < TestSecretRetriever > ( ) ;
74+ services . AddDaprConversationClient ( ( provider , builder ) =>
75+ {
76+ var configProvider = provider . GetRequiredService < TestSecretRetriever > ( ) ;
77+ var apiToken = configProvider . GetApiTokenValue ( ) ;
78+ builder . UseDaprApiToken ( apiToken ) ;
79+ } ) ;
80+
81+ var serviceProvider = services . BuildServiceProvider ( ) ;
82+ var client = serviceProvider . GetRequiredService < DaprConversationClient > ( ) ;
83+
84+ //Validate it's set on the GrpcClient - note that it doesn't get set on the HttpClient
85+ Assert . NotNull ( client ) ;
86+ Assert . NotNull ( client . DaprApiToken ) ;
87+ Assert . Equal ( "abcdef" , client . DaprApiToken ) ;
88+ Assert . NotNull ( client . HttpClient ) ;
89+
90+ if ( ! client . HttpClient . DefaultRequestHeaders . TryGetValues ( "dapr-api-token" , out var daprApiToken ) )
91+ {
92+ Assert . Fail ( ) ;
93+ }
94+ Assert . Equal ( "abcdef" , daprApiToken . FirstOrDefault ( ) ) ;
95+ }
96+
97+ [ Fact ]
98+ public void AddDaprConversationClient_WithoutConfigure_ShouldAddServices ( )
99+ {
100+ var services = new ServiceCollection ( ) ;
101+ var builder = services . AddDaprConversationClient ( ) ;
52102 Assert . NotNull ( builder ) ;
53103 }
54104
55105 [ Fact ]
56- public void AddDaprAiConversation_RegistersIHttpClientFactory ( )
106+ public void AddDaprConversationClient_RegistersIHttpClientFactory ( )
57107 {
58108 var services = new ServiceCollection ( ) ;
59- services . AddDaprAiConversation ( ) ;
109+ services . AddDaprConversationClient ( ) ;
60110 var serviceProvider = services . BuildServiceProvider ( ) ;
61111
62112 var httpClientFactory = serviceProvider . GetService < IHttpClientFactory > ( ) ;
@@ -67,9 +117,66 @@ public void AddDaprAiConversation_RegistersIHttpClientFactory()
67117 }
68118
69119 [ Fact ]
70- public void AddDaprAiConversation_NullServices_ShouldThrowException ( )
120+ public void AddDaprConversationClient_NullServices_ShouldThrowException ( )
71121 {
72122 IServiceCollection services = null ;
73- Assert . Throws < ArgumentNullException > ( ( ) => services . AddDaprAiConversation ( ) ) ;
123+ Assert . Throws < ArgumentNullException > ( ( ) => services . AddDaprConversationClient ( ) ) ;
124+ }
125+
126+ [ Fact ]
127+ public void AddDaprConversationClient_ShouldRegisterSingleton_WhenLifetimeIsSingleton ( )
128+ {
129+ var services = new ServiceCollection ( ) ;
130+
131+ services . AddDaprConversationClient ( ( _ , _ ) => { } , ServiceLifetime . Singleton ) ;
132+ var serviceProvider = services . BuildServiceProvider ( ) ;
133+
134+ var daprConversationClient1 = serviceProvider . GetService < DaprConversationClient > ( ) ;
135+ var daprConversationClient2 = serviceProvider . GetService < DaprConversationClient > ( ) ;
136+
137+ Assert . NotNull ( daprConversationClient1 ) ;
138+ Assert . NotNull ( daprConversationClient2 ) ;
139+
140+ Assert . Same ( daprConversationClient1 , daprConversationClient2 ) ;
141+ }
142+
143+ [ Fact ]
144+ public async Task AddDaprConversationClient_ShouldRegisterScoped_WhenLifetimeIsScoped ( )
145+ {
146+ var services = new ServiceCollection ( ) ;
147+
148+ services . AddDaprConversationClient ( ( _ , _ ) => { } , ServiceLifetime . Scoped ) ;
149+ var serviceProvider = services . BuildServiceProvider ( ) ;
150+
151+ await using var scope1 = serviceProvider . CreateAsyncScope ( ) ;
152+ var daprConversationClient1 = scope1 . ServiceProvider . GetService < DaprConversationClient > ( ) ;
153+
154+ await using var scope2 = serviceProvider . CreateAsyncScope ( ) ;
155+ var daprConversationClient2 = scope2 . ServiceProvider . GetService < DaprConversationClient > ( ) ;
156+
157+ Assert . NotNull ( daprConversationClient1 ) ;
158+ Assert . NotNull ( daprConversationClient2 ) ;
159+ Assert . NotSame ( daprConversationClient1 , daprConversationClient2 ) ;
160+ }
161+
162+ [ Fact ]
163+ public void AddDaprConversationClient_ShouldRegisterTransient_WhenLifetimeIsTransient ( )
164+ {
165+ var services = new ServiceCollection ( ) ;
166+
167+ services . AddDaprConversationClient ( ( _ , _ ) => { } , ServiceLifetime . Transient ) ;
168+ var serviceProvider = services . BuildServiceProvider ( ) ;
169+
170+ var daprConversationClient1 = serviceProvider . GetService < DaprConversationClient > ( ) ;
171+ var daprConversationClient2 = serviceProvider . GetService < DaprConversationClient > ( ) ;
172+
173+ Assert . NotNull ( daprConversationClient1 ) ;
174+ Assert . NotNull ( daprConversationClient2 ) ;
175+ Assert . NotSame ( daprConversationClient1 , daprConversationClient2 ) ;
176+ }
177+
178+ private class TestSecretRetriever
179+ {
180+ public string GetApiTokenValue ( ) => "abcdef" ;
74181 }
75182}
0 commit comments