|
| 1 | +// ---------------------------------------------------------------------------------- |
| 2 | +// |
| 3 | +// Copyright Microsoft Corporation |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// Unless required by applicable law or agreed to in writing, software |
| 9 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +// See the License for the specific language governing permissions and |
| 12 | +// limitations under the License. |
| 13 | +// ---------------------------------------------------------------------------------- |
| 14 | + |
| 15 | +using Microsoft.Azure.PowerShell.AzPredictor.Test.Mocks; |
| 16 | +using System.Collections.Generic; |
| 17 | +using System.Linq; |
| 18 | +using System.Management.Automation.Subsystem; |
| 19 | +using System.Threading; |
| 20 | +using Xunit; |
| 21 | + |
| 22 | +namespace Microsoft.Azure.PowerShell.AzPredictor.Test |
| 23 | +{ |
| 24 | + /// <summary> |
| 25 | + /// Tests for <see cref="AzPredictor"/> |
| 26 | + /// </summary> |
| 27 | + [Collection("Model collection")] |
| 28 | + public sealed class AzPredictorTests |
| 29 | + { |
| 30 | + private readonly ModelFixture _fixture; |
| 31 | + private readonly MockAzPredictorTelemetryClient _telemetryClient; |
| 32 | + private readonly MockAzPredictorService _service; |
| 33 | + private readonly AzPredictor _azPredictor; |
| 34 | + |
| 35 | + /// <summary> |
| 36 | + /// Constructs a new instance of <see cref="AzPredictorTests"/> |
| 37 | + /// </summary> |
| 38 | + public AzPredictorTests(ModelFixture modelFixture) |
| 39 | + { |
| 40 | + this._fixture = modelFixture; |
| 41 | + var startHistory = $"{AzPredictorConstants.CommandHistoryPlaceholder}{AzPredictorConstants.CommandConcatenator}{AzPredictorConstants.CommandHistoryPlaceholder}"; |
| 42 | + |
| 43 | + this._service = new MockAzPredictorService(this._fixture.PredictionCollection[startHistory], this._fixture.CommandCollection); |
| 44 | + this._telemetryClient = new MockAzPredictorTelemetryClient(); |
| 45 | + this._azPredictor = new AzPredictor(this._service, this._telemetryClient); |
| 46 | + } |
| 47 | + |
| 48 | + /// <summary> |
| 49 | + /// Verifies when the last command in history are not supported. |
| 50 | + /// We don't collect the telemetry and only request prediction while StartEarlyProcess is called. |
| 51 | + /// </summary> |
| 52 | + [Theory] |
| 53 | + [InlineData("start_of_snippet\nstart_of_snippet\nstart_of_snippet")] |
| 54 | + [InlineData("start_of_snippet")] |
| 55 | + [InlineData("")] |
| 56 | + [InlineData("git status")] |
| 57 | + [InlineData("git status\nGet-ChildItem")] |
| 58 | + [InlineData("^29a9l2")] |
| 59 | + [InlineData("'Get-AzResource'")] |
| 60 | + [InlineData("Get-AzResource\ngit log")] |
| 61 | + [InlineData("Get-ChildItem")] |
| 62 | + public void VerifyWithNonSupportedCommand(string historyLine) |
| 63 | + { |
| 64 | + IReadOnlyList<string> history = historyLine.Split('\n'); |
| 65 | + |
| 66 | + this._telemetryClient.RecordedSuggestion = null; |
| 67 | + this._service.IsPredictionRequested = false; |
| 68 | + |
| 69 | + this._azPredictor.StartEarlyProcessing(history); |
| 70 | + |
| 71 | + Assert.True(this._service.IsPredictionRequested); |
| 72 | + Assert.Null(this._telemetryClient.RecordedSuggestion); |
| 73 | + } |
| 74 | + |
| 75 | + /// <summary> |
| 76 | + /// Verifies when the last command in history are not supported. |
| 77 | + /// We don't collect the telemetry and only request prediction while StartEarlyProcess is called. |
| 78 | + /// </summary> |
| 79 | + [Theory] |
| 80 | + [InlineData("start_of_snippet\nConnect-AzAccount")] |
| 81 | + [InlineData("Get-AzResource")] |
| 82 | + [InlineData("git status\nGet-AzContext")] |
| 83 | + [InlineData("Get-AzContext\nGet-AzLog")] |
| 84 | + public void VerifyWithOneSupportedCommand(string historyLine) |
| 85 | + { |
| 86 | + IReadOnlyList<string> history = historyLine.Split('\n'); |
| 87 | + |
| 88 | + this._telemetryClient.RecordedSuggestion = null; |
| 89 | + this._service.IsPredictionRequested = false; |
| 90 | + |
| 91 | + this._azPredictor.StartEarlyProcessing(history); |
| 92 | + |
| 93 | + Assert.True(this._service.IsPredictionRequested); |
| 94 | + Assert.NotNull(this._telemetryClient.RecordedSuggestion); |
| 95 | + } |
| 96 | + |
| 97 | + /// <summary> |
| 98 | + /// Verify that the supported commands parameter values are masked. |
| 99 | + /// </summary> |
| 100 | + [Fact] |
| 101 | + public void VerifySupportedCommandMasked() |
| 102 | + { |
| 103 | + var input = "Get-AzVMExtension -ResourceGroupName 'ResourceGroup11' -VMName 'VirtualMachine22'"; |
| 104 | + var expected = "Get-AzVMExtension -ResourceGroupName *** -VMName ***"; |
| 105 | + |
| 106 | + this._telemetryClient.RecordedSuggestion = null; |
| 107 | + this._service.IsPredictionRequested = false; |
| 108 | + |
| 109 | + this._azPredictor.StartEarlyProcessing(new List<string> { input } ); |
| 110 | + |
| 111 | + Assert.True(this._service.IsPredictionRequested); |
| 112 | + Assert.NotNull(this._telemetryClient.RecordedSuggestion); |
| 113 | + Assert.Equal(expected, this._telemetryClient.RecordedSuggestion.HistoryLine); |
| 114 | + |
| 115 | + input = "Get-AzStorageAccountKey -Name:'ContosoStorage' -ResourceGroupName:'ContosoGroup02'"; |
| 116 | + expected = "Get-AzStorageAccountKey -Name:*** -ResourceGroupName:***"; |
| 117 | + |
| 118 | + |
| 119 | + this._telemetryClient.RecordedSuggestion = null; |
| 120 | + this._service.IsPredictionRequested = false; |
| 121 | + |
| 122 | + this._azPredictor.StartEarlyProcessing(new List<string> { input } ); |
| 123 | + |
| 124 | + Assert.True(this._service.IsPredictionRequested); |
| 125 | + Assert.NotNull(this._telemetryClient.RecordedSuggestion); |
| 126 | + Assert.Equal(expected, this._telemetryClient.RecordedSuggestion.HistoryLine); |
| 127 | + } |
| 128 | + |
| 129 | + /// <summary> |
| 130 | + /// Verifies AzPredictor returns the same value as AzPredictorService for the prediction. |
| 131 | + /// </summary> |
| 132 | + [Theory] |
| 133 | + [InlineData("git status")] |
| 134 | + [InlineData("new-azresourcegroup -name hello")] |
| 135 | + [InlineData("Get-AzContext -Name")] |
| 136 | + [InlineData("Get-AzContext -ErrorAction")] |
| 137 | + [InlineData("Get-AzADServicePrincipal -ApplicationObject")] |
| 138 | + public void VerifySuggestion(string userInput) |
| 139 | + { |
| 140 | + var predictionContext = PredictionContext.Create(userInput); |
| 141 | + var expected = this._service.GetSuggestion(predictionContext.InputAst, CancellationToken.None); |
| 142 | + var actual = this._azPredictor.GetSuggestion(predictionContext, CancellationToken.None); |
| 143 | + if (actual == null) |
| 144 | + { |
| 145 | + Assert.Null(expected); |
| 146 | + } |
| 147 | + else |
| 148 | + { |
| 149 | + Assert.Single(actual); |
| 150 | + Assert.Equal(expected, actual.First().SuggestionText); |
| 151 | + } |
| 152 | + |
| 153 | + } |
| 154 | + } |
| 155 | +} |
0 commit comments