13
13
14
14
using System ;
15
15
using System . Collections . Generic ;
16
+ using System . Linq ;
16
17
using System . Net . Http ;
18
+ using System . Threading . Tasks ;
17
19
using Dapr . AI . Conversation ;
18
20
using Dapr . AI . Conversation . Extensions ;
19
21
using Microsoft . Extensions . Configuration ;
@@ -34,7 +36,7 @@ public void AddDaprConversationClient_FromIConfiguration()
34
36
var services = new ServiceCollection ( ) ;
35
37
services . AddSingleton < IConfiguration > ( configuration ) ;
36
38
37
- services . AddDaprAiConversation ( ) ;
39
+ services . AddDaprConversationClient ( ) ;
38
40
39
41
var app = services . BuildServiceProvider ( ) ;
40
42
@@ -45,18 +47,66 @@ public void AddDaprConversationClient_FromIConfiguration()
45
47
}
46
48
47
49
[ Fact ]
48
- public void AddDaprAiConversation_WithoutConfigure_ShouldAddServices ( )
50
+ public void AddDaprConversationClient_RegistersDaprClientOnlyOnce ( )
49
51
{
50
52
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 ( ) ;
52
102
Assert . NotNull ( builder ) ;
53
103
}
54
104
55
105
[ Fact ]
56
- public void AddDaprAiConversation_RegistersIHttpClientFactory ( )
106
+ public void AddDaprConversationClient_RegistersIHttpClientFactory ( )
57
107
{
58
108
var services = new ServiceCollection ( ) ;
59
- services . AddDaprAiConversation ( ) ;
109
+ services . AddDaprConversationClient ( ) ;
60
110
var serviceProvider = services . BuildServiceProvider ( ) ;
61
111
62
112
var httpClientFactory = serviceProvider . GetService < IHttpClientFactory > ( ) ;
@@ -67,9 +117,66 @@ public void AddDaprAiConversation_RegistersIHttpClientFactory()
67
117
}
68
118
69
119
[ Fact ]
70
- public void AddDaprAiConversation_NullServices_ShouldThrowException ( )
120
+ public void AddDaprConversationClient_NullServices_ShouldThrowException ( )
71
121
{
72
122
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" ;
74
181
}
75
182
}
0 commit comments