|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Reflection; |
| 6 | +using System.Text.Json; |
| 7 | +using Azure.Mcp.Tests.Client; |
| 8 | +using Azure.Mcp.Tests.Client.Attributes; |
| 9 | +using Azure.Mcp.Tests.Client.Helpers; |
| 10 | +using Azure.Mcp.Tests.Generated.Models; |
| 11 | +using Azure.Mcp.Tests.Helpers; |
| 12 | +using Microsoft.Extensions.FileSystemGlobbing; |
| 13 | +using NSubstitute; |
| 14 | +using Xunit; |
| 15 | +using Xunit.v3; |
| 16 | + |
| 17 | +namespace Azure.Mcp.Core.LiveTests.RecordingFramework; |
| 18 | + |
| 19 | +public sealed class RecordedCommandTestsBaseTest : IAsyncLifetime |
| 20 | +{ |
| 21 | + private string RecordingFileLocation = string.Empty; |
| 22 | + private string TestDisplayName = string.Empty; |
| 23 | + private TestProxyFixture Fixture = new TestProxyFixture(); |
| 24 | + private ITestOutputHelper CollectedOutput = Substitute.For<ITestOutputHelper>(); |
| 25 | + private RecordedCommandTestHarness? DefaultHarness; |
| 26 | + |
| 27 | + [Fact] |
| 28 | + public async Task ProxyRecordProducesRecording() |
| 29 | + { |
| 30 | + await DefaultHarness!.InitializeAsync(); |
| 31 | + |
| 32 | + Assert.NotNull(Fixture.Proxy); |
| 33 | + Assert.False(string.IsNullOrWhiteSpace(Fixture.Proxy!.BaseUri)); |
| 34 | + |
| 35 | + DefaultHarness!.RegisterVariable("sampleKey", "sampleValue"); |
| 36 | + await DefaultHarness!.DisposeAsync(); |
| 37 | + |
| 38 | + Assert.True(File.Exists(RecordingFileLocation)); |
| 39 | + |
| 40 | + using var document = JsonDocument.Parse(await File.ReadAllTextAsync(RecordingFileLocation, CancellationToken.None)); |
| 41 | + Assert.True(document.RootElement.TryGetProperty("Variables", out var variablesElement)); |
| 42 | + Assert.Equal("sampleValue", variablesElement.GetProperty("sampleKey").GetString()); |
| 43 | + } |
| 44 | + |
| 45 | + [CustomMatcher(IgnoreQueryOrdering = true, CompareBodies = true)] |
| 46 | + [Fact] |
| 47 | + public async Task PerTestMatcherAttributeAppliesWhenPresent() |
| 48 | + { |
| 49 | + var activeMatcher = GetActiveMatcher(); |
| 50 | + Assert.NotNull(activeMatcher); |
| 51 | + Assert.True(activeMatcher!.CompareBodies); |
| 52 | + Assert.True(activeMatcher.IgnoreQueryOrdering); |
| 53 | + |
| 54 | + DefaultHarness = new RecordedCommandTestHarness(CollectedOutput, Fixture) |
| 55 | + { |
| 56 | + DesiredMode = TestMode.Record, |
| 57 | + EnableDefaultSanitizerAdditions = false, |
| 58 | + }; |
| 59 | + var recordingId = string.Empty; |
| 60 | + |
| 61 | + await DefaultHarness.InitializeAsync(); |
| 62 | + DefaultHarness.RegisterVariable("attrKey", "attrValue"); |
| 63 | + await DefaultHarness.DisposeAsync(); |
| 64 | + |
| 65 | + var playbackHarness = new RecordedCommandTestHarness(CollectedOutput, Fixture) |
| 66 | + { |
| 67 | + DesiredMode = TestMode.Playback, |
| 68 | + EnableDefaultSanitizerAdditions = false, |
| 69 | + }; |
| 70 | + |
| 71 | + await playbackHarness.InitializeAsync(); |
| 72 | + recordingId = playbackHarness.GetRecordingId(); |
| 73 | + await playbackHarness.DisposeAsync(); |
| 74 | + |
| 75 | + CollectedOutput.Received().WriteLine(Arg.Is<string>(s => s.Contains($"Applying custom matcher to recordingId \"{recordingId}\""))); |
| 76 | + } |
| 77 | + |
| 78 | + [Fact] |
| 79 | + public void CustomMatcherAttributeClearsAfterExecution() |
| 80 | + { |
| 81 | + var attribute = new CustomMatcherAttribute(compareBody: true, ignoreQueryordering: true); |
| 82 | + var xunitTest = Substitute.For<IXunitTest>(); |
| 83 | + var methodInfo = typeof(RecordedCommandTestsBaseTest).GetMethod(nameof(CustomMatcherAttributeClearsAfterExecution)) |
| 84 | + ?? throw new InvalidOperationException("Unable to locate test method for CustomMatcherAttribute verification."); |
| 85 | + |
| 86 | + attribute.Before(methodInfo, xunitTest); |
| 87 | + try |
| 88 | + { |
| 89 | + var active = GetActiveMatcher(); |
| 90 | + Assert.Same(attribute, active); |
| 91 | + Assert.True(active!.CompareBodies); |
| 92 | + Assert.True(active.IgnoreQueryOrdering); |
| 93 | + } |
| 94 | + finally |
| 95 | + { |
| 96 | + attribute.After(methodInfo, xunitTest); |
| 97 | + } |
| 98 | + |
| 99 | + Assert.Null(GetActiveMatcher()); |
| 100 | + } |
| 101 | + |
| 102 | + private static CustomMatcherAttribute? GetActiveMatcher() |
| 103 | + { |
| 104 | + var method = typeof(CustomMatcherAttribute).GetMethod("GetActive", BindingFlags.NonPublic | BindingFlags.Static); |
| 105 | + return (CustomMatcherAttribute?)method?.Invoke(null, null); |
| 106 | + } |
| 107 | + |
| 108 | + [Fact] |
| 109 | + public async Task GlobalMatcherAndSanitizerAppliesWhenPresent() |
| 110 | + { |
| 111 | + DefaultHarness = new RecordedCommandTestHarness(CollectedOutput, Fixture) |
| 112 | + { |
| 113 | + DesiredMode = TestMode.Record, |
| 114 | + TestMatcher = new CustomDefaultMatcher |
| 115 | + { |
| 116 | + CompareBodies = true, |
| 117 | + IgnoreQueryOrdering = true, |
| 118 | + } |
| 119 | + }; |
| 120 | + |
| 121 | + DefaultHarness.GeneralRegexSanitizers.Add(new GeneralRegexSanitizer(new GeneralRegexSanitizerBody |
| 122 | + { |
| 123 | + Regex = "sample", |
| 124 | + Value = "sanitized", |
| 125 | + })); |
| 126 | + DefaultHarness.DisabledDefaultSanitizers.Add("UriSubscriptionIdSanitizer"); |
| 127 | + |
| 128 | + await DefaultHarness.InitializeAsync(); |
| 129 | + await DefaultHarness.DisposeAsync(); |
| 130 | + |
| 131 | + CollectedOutput.Received().WriteLine(Arg.Is<string>(s => s.Contains("Applying custom matcher to global settings"))); |
| 132 | + } |
| 133 | + |
| 134 | + [Fact] |
| 135 | + public async Task VariableSurvivesRecordPlaybackRoundtrip() |
| 136 | + { |
| 137 | + await DefaultHarness!.InitializeAsync(); |
| 138 | + DefaultHarness.RegisterVariable("roundtrip", "value"); |
| 139 | + await DefaultHarness.DisposeAsync(); |
| 140 | + |
| 141 | + var playbackHarness = new RecordedCommandTestHarness(CollectedOutput, Fixture) |
| 142 | + { |
| 143 | + DesiredMode = TestMode.Playback, |
| 144 | + }; |
| 145 | + await playbackHarness.InitializeAsync(); |
| 146 | + Assert.True(playbackHarness.Variables.TryGetValue("roundtrip", out var variableValue)); |
| 147 | + Assert.Equal("value", variableValue); |
| 148 | + await playbackHarness.DisposeAsync(); |
| 149 | + } |
| 150 | + |
| 151 | + public ValueTask InitializeAsync() |
| 152 | + { |
| 153 | + TestDisplayName = TestContext.Current?.Test?.TestCase?.TestCaseDisplayName ?? throw new InvalidDataException("Test case display name is not available."); |
| 154 | + |
| 155 | + var harness = new RecordedCommandTestHarness(CollectedOutput, Fixture) |
| 156 | + { |
| 157 | + DesiredMode = TestMode.Record |
| 158 | + }; |
| 159 | + |
| 160 | + RecordingFileLocation = harness.GetRecordingAbsolutePath(TestDisplayName); |
| 161 | + |
| 162 | + if (File.Exists(RecordingFileLocation)) |
| 163 | + { |
| 164 | + File.Delete(RecordingFileLocation); |
| 165 | + } |
| 166 | + |
| 167 | + DefaultHarness = harness; |
| 168 | + return ValueTask.CompletedTask; |
| 169 | + } |
| 170 | + |
| 171 | + public async ValueTask DisposeAsync() |
| 172 | + { |
| 173 | + // always clean up this recording file on our way out of the test if it exists |
| 174 | + if (File.Exists(RecordingFileLocation)) |
| 175 | + { |
| 176 | + File.Delete(RecordingFileLocation); |
| 177 | + } |
| 178 | + |
| 179 | + // automatically collect the proxy fixture so that writers of tests don't need to remember to do so and the proxy process doesn't run forever |
| 180 | + await Fixture.DisposeAsync(); |
| 181 | + } |
| 182 | +} |
0 commit comments