|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT license. See LICENSE file in the project root for full license information. |
| 3 | + |
| 4 | +using Microsoft.Extensions.AI; |
| 5 | +using Microsoft.Testing.Platform.AI; |
| 6 | +using Microsoft.Testing.Platform.Services; |
| 7 | + |
| 8 | +namespace Microsoft.Testing.Platform.UnitTests.AI; |
| 9 | + |
| 10 | +#pragma warning disable TPEXP // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed. |
| 11 | + |
| 12 | +[TestClass] |
| 13 | +public sealed class ChatClientProviderExtensionsTests |
| 14 | +{ |
| 15 | + [TestMethod] |
| 16 | + public async Task GetChatClientAsync_WhenProviderIsUnavailable_ReturnsNull() |
| 17 | + { |
| 18 | + ServiceProvider serviceProvider = new(); |
| 19 | + UnavailableChatClientProvider provider = new(); |
| 20 | + serviceProvider.AddService(provider); |
| 21 | + |
| 22 | + IChatClient? chatClient = await serviceProvider.GetChatClientAsync(CancellationToken.None).ConfigureAwait(false); |
| 23 | + |
| 24 | + Assert.IsNull(chatClient); |
| 25 | + Assert.AreEqual(1, provider.IsAvailableCallCount); |
| 26 | + Assert.AreEqual(0, provider.CreateChatClientAsyncCallCount); |
| 27 | + } |
| 28 | + |
| 29 | + private sealed class UnavailableChatClientProvider : IChatClientProvider |
| 30 | + { |
| 31 | + public int IsAvailableCallCount { get; private set; } |
| 32 | + |
| 33 | + public int CreateChatClientAsyncCallCount { get; private set; } |
| 34 | + |
| 35 | + public bool IsAvailable |
| 36 | + { |
| 37 | + get |
| 38 | + { |
| 39 | + IsAvailableCallCount++; |
| 40 | + return false; |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + public bool HasToolsCapability => false; |
| 45 | + |
| 46 | + public string ModelName => "Unavailable"; |
| 47 | + |
| 48 | + public Task<IChatClient> CreateChatClientAsync(CancellationToken cancellationToken) |
| 49 | + { |
| 50 | + CreateChatClientAsyncCallCount++; |
| 51 | + throw new InvalidOperationException("CreateChatClientAsync should not be called for unavailable providers."); |
| 52 | + } |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +#pragma warning restore TPEXP |
0 commit comments