|
| 1 | +// Copyright (c) Microsoft. All rights reserved. |
| 2 | + |
| 3 | +using System; |
| 4 | +using System.Net; |
| 5 | +using System.Net.Http; |
| 6 | +using System.Threading; |
| 7 | +using System.Threading.Tasks; |
| 8 | +using Microsoft.SemanticKernel.Plugins.AI.CrewAI; |
| 9 | +using Moq; |
| 10 | +using Moq.Protected; |
| 11 | +using Xunit; |
| 12 | + |
| 13 | +namespace SemanticKernel.Plugins.AI.UnitTests.CrewAI; |
| 14 | + |
| 15 | +/// <summary> |
| 16 | +/// Tests for the <see cref="CrewAIEnterpriseClient"/> class. |
| 17 | +/// </summary> |
| 18 | +public sealed partial class CrewAIEnterpriseClientTests |
| 19 | +{ |
| 20 | + private readonly Mock<HttpMessageHandler> _httpMessageHandlerMock; |
| 21 | + private readonly CrewAIEnterpriseClient _client; |
| 22 | + |
| 23 | + /// <summary> |
| 24 | + /// Initializes a new instance of the <see cref="CrewAIEnterpriseClientTests"/> class. |
| 25 | + /// </summary> |
| 26 | + public CrewAIEnterpriseClientTests() |
| 27 | + { |
| 28 | + this._httpMessageHandlerMock = new Mock<HttpMessageHandler>(); |
| 29 | + using var httpClientFactory = new MockHttpClientFactory(this._httpMessageHandlerMock); |
| 30 | + this._client = new CrewAIEnterpriseClient( |
| 31 | + endpoint: new Uri("http://example.com"), |
| 32 | + authTokenProvider: () => Task.FromResult("token"), |
| 33 | + httpClientFactory); |
| 34 | + } |
| 35 | + |
| 36 | + /// <summary> |
| 37 | + /// Tests that <see cref="CrewAIEnterpriseClient.GetInputsAsync"/> returns the required inputs from the CrewAI API. |
| 38 | + /// </summary> |
| 39 | + /// <returns></returns> |
| 40 | + [Fact] |
| 41 | + public async Task GetInputsAsyncReturnsCrewAIRequiredInputsAsync() |
| 42 | + { |
| 43 | + // Arrange |
| 44 | + var responseContent = "{\"inputs\": [\"input1\", \"input2\"]}"; |
| 45 | + using var responseMessage = new HttpResponseMessage |
| 46 | + { |
| 47 | + StatusCode = HttpStatusCode.OK, |
| 48 | + Content = new StringContent(responseContent) |
| 49 | + }; |
| 50 | + |
| 51 | + this._httpMessageHandlerMock.Protected() |
| 52 | + .Setup<Task<HttpResponseMessage>>( |
| 53 | + "SendAsync", |
| 54 | + ItExpr.IsAny<HttpRequestMessage>(), |
| 55 | + ItExpr.IsAny<CancellationToken>()) |
| 56 | + .ReturnsAsync(responseMessage); |
| 57 | + |
| 58 | + // Act |
| 59 | + var result = await this._client.GetInputsAsync(); |
| 60 | + |
| 61 | + // Assert |
| 62 | + Assert.NotNull(result); |
| 63 | + Assert.Equal(2, result.Inputs.Count); |
| 64 | + Assert.Contains("input1", result.Inputs); |
| 65 | + Assert.Contains("input2", result.Inputs); |
| 66 | + } |
| 67 | + |
| 68 | + /// <summary> |
| 69 | + /// Tests that <see cref="CrewAIEnterpriseClient.KickoffAsync"/> returns the kickoff id from the CrewAI API. |
| 70 | + /// </summary> |
| 71 | + /// <returns></returns> |
| 72 | + [Fact] |
| 73 | + public async Task KickoffAsyncReturnsCrewAIKickoffResponseAsync() |
| 74 | + { |
| 75 | + // Arrange |
| 76 | + var responseContent = "{\"kickoff_id\": \"12345\"}"; |
| 77 | + using var responseMessage = new HttpResponseMessage |
| 78 | + { |
| 79 | + StatusCode = HttpStatusCode.OK, |
| 80 | + Content = new StringContent(responseContent) |
| 81 | + }; |
| 82 | + |
| 83 | + this._httpMessageHandlerMock.Protected() |
| 84 | + .Setup<Task<HttpResponseMessage>>( |
| 85 | + "SendAsync", |
| 86 | + ItExpr.IsAny<HttpRequestMessage>(), |
| 87 | + ItExpr.IsAny<CancellationToken>()) |
| 88 | + .ReturnsAsync(responseMessage); |
| 89 | + |
| 90 | + // Act |
| 91 | + var result = await this._client.KickoffAsync(new { key = "value" }); |
| 92 | + |
| 93 | + // Assert |
| 94 | + Assert.NotNull(result); |
| 95 | + Assert.Equal("12345", result.KickoffId); |
| 96 | + } |
| 97 | + |
| 98 | + /// <summary> |
| 99 | + /// Tests that <see cref="CrewAIEnterpriseClient.GetStatusAsync"/> returns the status of the CrewAI Crew. |
| 100 | + /// </summary> |
| 101 | + /// <param name="state"></param> |
| 102 | + /// <returns></returns> |
| 103 | + /// <exception cref="ArgumentOutOfRangeException"></exception> |
| 104 | + [Theory] |
| 105 | + [InlineData(CrewAIKickoffState.Pending)] |
| 106 | + [InlineData(CrewAIKickoffState.Started)] |
| 107 | + [InlineData(CrewAIKickoffState.Running)] |
| 108 | + [InlineData(CrewAIKickoffState.Success)] |
| 109 | + [InlineData(CrewAIKickoffState.Failed)] |
| 110 | + [InlineData(CrewAIKickoffState.Failure)] |
| 111 | + [InlineData(CrewAIKickoffState.NotFound)] |
| 112 | + public async Task GetStatusAsyncReturnsCrewAIStatusResponseAsync(CrewAIKickoffState state) |
| 113 | + { |
| 114 | + var crewAIStatusState = state switch |
| 115 | + { |
| 116 | + CrewAIKickoffState.Pending => "PENDING", |
| 117 | + CrewAIKickoffState.Started => "STARTED", |
| 118 | + CrewAIKickoffState.Running => "RUNNING", |
| 119 | + CrewAIKickoffState.Success => "SUCCESS", |
| 120 | + CrewAIKickoffState.Failed => "FAILED", |
| 121 | + CrewAIKickoffState.Failure => "FAILURE", |
| 122 | + CrewAIKickoffState.NotFound => "NOT FOUND", |
| 123 | + _ => throw new ArgumentOutOfRangeException(nameof(state), state, null) |
| 124 | + }; |
| 125 | + |
| 126 | + // Arrange |
| 127 | + var responseContent = $"{{\"state\": \"{crewAIStatusState}\", \"result\": \"The Result\", \"last_step\": {{\"step1\": \"value1\"}}}}"; |
| 128 | + using var responseMessage = new HttpResponseMessage |
| 129 | + { |
| 130 | + StatusCode = HttpStatusCode.OK, |
| 131 | + Content = new StringContent(responseContent) |
| 132 | + }; |
| 133 | + |
| 134 | + this._httpMessageHandlerMock.Protected() |
| 135 | + .Setup<Task<HttpResponseMessage>>( |
| 136 | + "SendAsync", |
| 137 | + ItExpr.IsAny<HttpRequestMessage>(), |
| 138 | + ItExpr.IsAny<CancellationToken>()) |
| 139 | + .ReturnsAsync(responseMessage); |
| 140 | + |
| 141 | + // Act |
| 142 | + var result = await this._client.GetStatusAsync("12345"); |
| 143 | + |
| 144 | + // Assert |
| 145 | + Assert.NotNull(result); |
| 146 | + Assert.Equal(state, result.State); |
| 147 | + Assert.Equal("The Result", result.Result); |
| 148 | + Assert.NotNull(result.LastStep); |
| 149 | + Assert.Equal("value1", result.LastStep["step1"].ToString()); |
| 150 | + } |
| 151 | +} |
0 commit comments