17
17
using System . Linq ;
18
18
using System . Management . Automation . Subsystem ;
19
19
using System . Threading ;
20
+ using System . Threading . Tasks ;
20
21
using Xunit ;
21
22
22
23
namespace Microsoft . Azure . PowerShell . Tools . AzPredictor . Test
@@ -27,6 +28,7 @@ namespace Microsoft.Azure.PowerShell.Tools.AzPredictor.Test
27
28
[ Collection ( "Model collection" ) ]
28
29
public sealed class AzPredictorTests
29
30
{
31
+ private const string AzPredictorClient = "Test" ;
30
32
private readonly ModelFixture _fixture ;
31
33
private readonly MockAzPredictorTelemetryClient _telemetryClient ;
32
34
private readonly MockAzPredictorService _service ;
@@ -54,65 +56,66 @@ public AzPredictorTests(ModelFixture modelFixture)
54
56
/// Verify we replace unsupported command with <see cref="AzPredictorConstants.CommandPlaceholder"/>.
55
57
/// </summary>
56
58
[ Fact ]
57
- public void VerifyRequestPredictionForOneUnsupportedCommandInHistory ( )
59
+ public async Task VerifyRequestPredictionForOneUnsupportedCommandInHistory ( )
58
60
{
59
61
IReadOnlyList < string > history = new List < string > ( )
60
62
{
61
63
"git status"
62
64
} ;
63
65
64
- _telemetryClient . RecordedSuggestion = null ;
65
66
_service . Commands = null ;
66
67
_service . History = null ;
68
+ _service . ResetRequestPredictionTask ( ) ;
67
69
68
- _azPredictor . StartEarlyProcessing ( history ) ;
70
+ _azPredictor . StartEarlyProcessing ( AzPredictorTests . AzPredictorClient , history ) ;
71
+ await _service . RequestPredictionTaskCompletionSource . Task ;
69
72
70
73
Assert . Equal ( new List < string > ( ) { AzPredictorConstants . CommandPlaceholder , AzPredictorConstants . CommandPlaceholder } , _service . Commands ) ;
71
- Assert . Equal ( AzPredictorConstants . CommandPlaceholder , _telemetryClient . RecordedSuggestion . HistoryLine ) ;
72
74
Assert . Null ( _service . History ) ;
73
75
}
74
76
75
77
/// <summary>
76
78
/// Verify that we masked the supported command in requesting prediction and telemetry.
77
79
/// </summary>
78
80
[ Fact ]
79
- public void VerifyRequestPredictionForOneSupportedCommandInHistory ( )
81
+ public async Task VerifyRequestPredictionForOneSupportedCommandInHistory ( )
80
82
{
81
83
IReadOnlyList < string > history = new List < string > ( )
82
84
{
83
85
"New-AzVM -Name hello -Location WestUS"
84
86
} ;
85
87
86
- _telemetryClient . RecordedSuggestion = null ;
87
88
_service . Commands = null ;
88
89
_service . History = null ;
90
+ _service . ResetRequestPredictionTask ( ) ;
89
91
90
- _azPredictor . StartEarlyProcessing ( history ) ;
92
+ _azPredictor . StartEarlyProcessing ( AzPredictorTests . AzPredictorClient , history ) ;
93
+ await _service . RequestPredictionTaskCompletionSource . Task ;
91
94
92
95
string maskedCommand = "New-AzVM -Location *** -Name ***" ;
93
96
94
97
Assert . Equal ( new List < string > ( ) { AzPredictorConstants . CommandPlaceholder , maskedCommand } , _service . Commands ) ;
95
- Assert . Equal ( maskedCommand , _telemetryClient . RecordedSuggestion . HistoryLine ) ;
96
98
Assert . Equal ( history [ 0 ] , _service . History . ToString ( ) ) ;
97
99
}
98
100
99
101
/// <summary>
100
102
/// Verify that we can handle the two supported command in sequences.
101
103
/// </summary>
102
104
[ Fact ]
103
- public void VerifyRequestPredictionForTwoSupportedCommandInHistory ( )
105
+ public async Task VerifyRequestPredictionForTwoSupportedCommandInHistory ( )
104
106
{
105
107
IReadOnlyList < string > history = new List < string > ( )
106
108
{
107
109
"New-AzResourceGroup -Name 'resourceGroup01'" ,
108
110
"New-AzVM -Name:hello -Location:WestUS"
109
111
} ;
110
112
111
- _telemetryClient . RecordedSuggestion = null ;
112
113
_service . Commands = null ;
113
114
_service . History = null ;
115
+ _service . ResetRequestPredictionTask ( ) ;
114
116
115
- _azPredictor . StartEarlyProcessing ( history ) ;
117
+ _azPredictor . StartEarlyProcessing ( AzPredictorTests . AzPredictorClient , history ) ;
118
+ await _service . RequestPredictionTaskCompletionSource . Task ;
116
119
117
120
var maskedCommands = new List < string > ( )
118
121
{
@@ -121,27 +124,27 @@ public void VerifyRequestPredictionForTwoSupportedCommandInHistory()
121
124
} ;
122
125
123
126
Assert . Equal ( maskedCommands , _service . Commands ) ;
124
- Assert . Equal ( maskedCommands [ 1 ] , _telemetryClient . RecordedSuggestion . HistoryLine ) ;
125
127
Assert . Equal ( history [ 1 ] , _service . History . ToString ( ) ) ;
126
128
}
127
129
128
130
/// <summary>
129
131
/// Verify that we can handle the two unsupported command in sequences.
130
132
/// </summary>
131
133
[ Fact ]
132
- public void VerifyRequestPredictionForTwoUnsupportedCommandInHistory ( )
134
+ public async Task VerifyRequestPredictionForTwoUnsupportedCommandInHistory ( )
133
135
{
134
136
IReadOnlyList < string > history = new List < string > ( )
135
137
{
136
138
"git status" ,
137
139
@"$a='ResourceGroup01'" ,
138
140
} ;
139
141
140
- _telemetryClient . RecordedSuggestion = null ;
141
142
_service . Commands = null ;
142
143
_service . History = null ;
144
+ _service . ResetRequestPredictionTask ( ) ;
143
145
144
- _azPredictor . StartEarlyProcessing ( history ) ;
146
+ _azPredictor . StartEarlyProcessing ( AzPredictorTests . AzPredictorClient , history ) ;
147
+ await _service . RequestPredictionTaskCompletionSource . Task ;
145
148
146
149
var maskedCommands = new List < string > ( )
147
150
{
@@ -150,33 +153,37 @@ public void VerifyRequestPredictionForTwoUnsupportedCommandInHistory()
150
153
} ;
151
154
152
155
Assert . Equal ( maskedCommands , _service . Commands ) ;
153
- Assert . Equal ( maskedCommands [ 1 ] , _telemetryClient . RecordedSuggestion . HistoryLine ) ;
154
156
Assert . Null ( _service . History ) ;
155
157
}
156
158
157
159
/// <summary>
158
160
/// Verify that we skip the unsupported commands.
159
161
/// </summary>
160
162
[ Fact ]
161
- public void VerifyNotTakeUnsupportedCommands ( )
163
+ public async Task VerifyNotTakeUnsupportedCommands ( )
162
164
{
163
165
var history = new List < string > ( )
164
166
{
165
167
"New-AzResourceGroup -Name:resourceGroup01" ,
166
168
"New-AzVM -Name hello -Location WestUS"
167
169
} ;
168
170
169
- _telemetryClient . RecordedSuggestion = null ;
170
171
_service . Commands = null ;
171
172
_service . History = null ;
173
+ _service . ResetRequestPredictionTask ( ) ;
172
174
173
- _azPredictor . StartEarlyProcessing ( history ) ;
175
+ _azPredictor . StartEarlyProcessing ( AzPredictorTests . AzPredictorClient , history ) ;
176
+ await _service . RequestPredictionTaskCompletionSource . Task ;
174
177
178
+ _service . ResetRequestPredictionTask ( ) ;
175
179
history . Add ( "git status" ) ;
176
- _azPredictor . StartEarlyProcessing ( history ) ;
180
+ _azPredictor . StartEarlyProcessing ( AzPredictorTests . AzPredictorClient , history ) ;
181
+ // Don't need to await on _service.RequestPredictionTask, because "git" isn't a supported command and RequestPredictionsAsync isn't called.
177
182
183
+ _service . ResetRequestPredictionTask ( ) ;
178
184
history . Add ( @"$a='NewResourceName'" ) ;
179
- _azPredictor . StartEarlyProcessing ( history ) ;
185
+ _azPredictor . StartEarlyProcessing ( AzPredictorTests . AzPredictorClient , history ) ;
186
+ // Don't need to await on _service.RequestPredictionTask, because assignment isn't supported and RequestPredictionsAsync isn't called.
180
187
181
188
// We don't take the last two unsupported command to request predictions.
182
189
// But we send the masked one in telemetry.
@@ -188,13 +195,14 @@ public void VerifyNotTakeUnsupportedCommands()
188
195
} ;
189
196
190
197
Assert . Equal ( maskedCommands , _service . Commands ) ;
191
- Assert . Equal ( AzPredictorConstants . CommandPlaceholder , _telemetryClient . RecordedSuggestion . HistoryLine ) ;
192
198
Assert . Equal ( history [ 1 ] , _service . History . ToString ( ) ) ;
193
199
194
200
// When there is a new supported command, we'll use that for prediction.
195
201
202
+ _service . ResetRequestPredictionTask ( ) ;
196
203
history . Add ( "Get-AzResourceGroup -Name ResourceGroup01" ) ;
197
- _azPredictor . StartEarlyProcessing ( history ) ;
204
+ _azPredictor . StartEarlyProcessing ( AzPredictorTests . AzPredictorClient , history ) ;
205
+ await _service . RequestPredictionTaskCompletionSource . Task ;
198
206
199
207
maskedCommands = new List < string > ( )
200
208
{
@@ -203,30 +211,32 @@ public void VerifyNotTakeUnsupportedCommands()
203
211
} ;
204
212
205
213
Assert . Equal ( maskedCommands , _service . Commands ) ;
206
- Assert . Equal ( maskedCommands [ 1 ] , _telemetryClient . RecordedSuggestion . HistoryLine ) ;
207
214
Assert . Equal ( history . Last ( ) , _service . History . ToString ( ) ) ;
208
215
}
209
216
210
217
/// <summary>
211
218
/// Verify that we handle the three supported command in the same order.
212
219
/// </summary>
213
220
[ Fact ]
214
- public void VerifyThreeSupportedCommands ( )
221
+ public async Task VerifyThreeSupportedCommands ( )
215
222
{
216
223
var history = new List < string > ( )
217
224
{
218
225
"New-AzResourceGroup -Name resourceGroup01" ,
219
226
"New-AzVM -Name:hello -Location:WestUS"
220
227
} ;
221
228
222
- _telemetryClient . RecordedSuggestion = null ;
223
229
_service . Commands = null ;
224
230
_service . History = null ;
231
+ _service . ResetRequestPredictionTask ( ) ;
225
232
226
- _azPredictor . StartEarlyProcessing ( history ) ;
233
+ _azPredictor . StartEarlyProcessing ( AzPredictorTests . AzPredictorClient , history ) ;
234
+ await _service . RequestPredictionTaskCompletionSource . Task ;
227
235
236
+ _service . ResetRequestPredictionTask ( ) ;
228
237
history . Add ( "Get-AzResourceGroup -Name resourceGroup01" ) ;
229
- _azPredictor . StartEarlyProcessing ( history ) ;
238
+ _azPredictor . StartEarlyProcessing ( AzPredictorTests . AzPredictorClient , history ) ;
239
+ await _service . RequestPredictionTaskCompletionSource . Task ;
230
240
231
241
var maskedCommands = new List < string > ( )
232
242
{
@@ -235,27 +245,27 @@ public void VerifyThreeSupportedCommands()
235
245
} ;
236
246
237
247
Assert . Equal ( maskedCommands , _service . Commands ) ;
238
- Assert . Equal ( maskedCommands [ 1 ] , _telemetryClient . RecordedSuggestion . HistoryLine ) ;
239
248
Assert . Equal ( history . Last ( ) , _service . History . ToString ( ) ) ;
240
249
}
241
250
242
251
/// <summary>
243
252
/// Verify that we handle the sequence of one unsupported command and one supported command.
244
253
/// </summary>
245
254
[ Fact ]
246
- public void VerifyUnsupportedAndSupportedCommands ( )
255
+ public async Task VerifyUnsupportedAndSupportedCommands ( )
247
256
{
248
257
var history = new List < string > ( )
249
258
{
250
259
"git status" ,
251
260
"New-AzVM -Name:hello -Location:WestUS"
252
261
} ;
253
262
254
- _telemetryClient . RecordedSuggestion = null ;
255
263
_service . Commands = null ;
256
264
_service . History = null ;
265
+ _service . ResetRequestPredictionTask ( ) ;
257
266
258
- _azPredictor . StartEarlyProcessing ( history ) ;
267
+ _azPredictor . StartEarlyProcessing ( AzPredictorTests . AzPredictorClient , history ) ;
268
+ await _service . RequestPredictionTaskCompletionSource . Task ;
259
269
260
270
var maskedCommands = new List < string > ( )
261
271
{
@@ -264,27 +274,27 @@ public void VerifyUnsupportedAndSupportedCommands()
264
274
} ;
265
275
266
276
Assert . Equal ( maskedCommands , _service . Commands ) ;
267
- Assert . Equal ( maskedCommands [ 1 ] , _telemetryClient . RecordedSuggestion . HistoryLine ) ;
268
277
Assert . Equal ( history . Last ( ) , _service . History . ToString ( ) ) ;
269
278
}
270
279
271
280
/// <summary>
272
281
/// Verify that we handle the sequence of one supported command and one unsupported command.
273
282
/// </summary>
274
283
[ Fact ]
275
- public void VerifySupportedAndUnsupportedCommands ( )
284
+ public async Task VerifySupportedAndUnsupportedCommands ( )
276
285
{
277
286
var history = new List < string > ( )
278
287
{
279
288
"New-AzVM -Name hello -Location WestUS" ,
280
289
"git status" ,
281
290
} ;
282
291
283
- _telemetryClient . RecordedSuggestion = null ;
284
292
_service . Commands = null ;
285
293
_service . History = null ;
294
+ _service . ResetRequestPredictionTask ( ) ;
286
295
287
- _azPredictor . StartEarlyProcessing ( history ) ;
296
+ _azPredictor . StartEarlyProcessing ( AzPredictorTests . AzPredictorClient , history ) ;
297
+ await _service . RequestPredictionTaskCompletionSource . Task ;
288
298
289
299
var maskedCommands = new List < string > ( )
290
300
{
@@ -293,7 +303,6 @@ public void VerifySupportedAndUnsupportedCommands()
293
303
} ;
294
304
295
305
Assert . Equal ( maskedCommands , _service . Commands ) ;
296
- Assert . Equal ( AzPredictorConstants . CommandPlaceholder , _telemetryClient . RecordedSuggestion . HistoryLine ) ;
297
306
Assert . Equal ( history . First ( ) , _service . History . ToString ( ) ) ;
298
307
}
299
308
@@ -307,10 +316,10 @@ public void VerifySuggestion(string userInput)
307
316
{
308
317
var predictionContext = PredictionContext . Create ( userInput ) ;
309
318
var expected = this . _service . GetSuggestion ( predictionContext , 1 , 1 , CancellationToken . None ) ;
310
- var actual = this . _azPredictor . GetSuggestion ( predictionContext , CancellationToken . None ) ;
319
+ var actual = this . _azPredictor . GetSuggestion ( AzPredictorTests . AzPredictorClient , predictionContext , CancellationToken . None ) ;
311
320
312
- Assert . Equal ( expected . Count , actual . Count ) ;
313
- Assert . Equal ( expected . PredictiveSuggestions . First ( ) . SuggestionText , actual . First ( ) . SuggestionText ) ;
321
+ Assert . Equal ( expected . Count , actual . SuggestionEntries . Count ) ;
322
+ Assert . Equal ( expected . PredictiveSuggestions . First ( ) . SuggestionText , actual . SuggestionEntries . First ( ) . SuggestionText ) ;
314
323
}
315
324
316
325
/// <summary>
@@ -331,9 +340,9 @@ public void VerifySuggestionOnIncompleteCommand()
331
340
var expected = "New-AzResourceGroup -Name 'ResourceGroup01' -Location 'Central US' -WhatIf -Tag value1" ;
332
341
333
342
var predictionContext = PredictionContext . Create ( userInput ) ;
334
- var actual = localAzPredictor . GetSuggestion ( predictionContext , CancellationToken . None ) ;
343
+ var actual = localAzPredictor . GetSuggestion ( AzPredictorTests . AzPredictorClient , predictionContext , CancellationToken . None ) ;
335
344
336
- Assert . Equal ( expected , actual . First ( ) . SuggestionText ) ;
345
+ Assert . Equal ( expected , actual . SuggestionEntries . First ( ) . SuggestionText ) ;
337
346
}
338
347
339
348
/// <summary>
@@ -348,9 +357,9 @@ public void VerifySuggestionOnIncompleteCommand()
348
357
public void VerifyMalFormattedCommandLine ( string userInput )
349
358
{
350
359
var predictionContext = PredictionContext . Create ( userInput ) ;
351
- var actual = _azPredictor . GetSuggestion ( predictionContext , CancellationToken . None ) ;
360
+ var actual = _azPredictor . GetSuggestion ( AzPredictorTests . AzPredictorClient , predictionContext , CancellationToken . None ) ;
352
361
353
- Assert . Empty ( actual ) ;
362
+ Assert . Null ( actual . SuggestionEntries ) ;
354
363
}
355
364
}
356
365
}
0 commit comments