test
Some checks failed
CodeQL / Analyze (csharp) (push) Has been cancelled
CodeQL / Analyze (python) (push) Has been cancelled
dotnet-build-and-test / paths-filter (push) Has been cancelled
dotnet-build-and-test / dotnet-build-and-test (Debug, windows-latest, net9.0) (push) Has been cancelled
dotnet-build-and-test / dotnet-build-and-test (Release, integration, true, ubuntu-latest, net10.0) (push) Has been cancelled
dotnet-build-and-test / dotnet-build-and-test (Release, integration, true, windows-latest, net472) (push) Has been cancelled
dotnet-build-and-test / dotnet-build-and-test (Release, ubuntu-latest, net8.0) (push) Has been cancelled
dotnet-build-and-test / dotnet-build-and-test-check (push) Has been cancelled
Python - Merge - Tests / paths-filter (push) Has been cancelled
Python - Merge - Tests / Python Tests - Core (integration, ubuntu-latest, 3.10) (push) Has been cancelled
Python - Merge - Tests / Python Tests - Azure AI (integration, ubuntu-latest, 3.10) (push) Has been cancelled
Python - Merge - Tests / python-integration-tests-check (push) Has been cancelled
Python - Lab Tests / paths-filter (push) Has been cancelled
Python - Lab Tests / Python Lab Tests (ubuntu-latest, 3.10) (push) Has been cancelled
Python - Lab Tests / Python Lab Tests (ubuntu-latest, 3.11) (push) Has been cancelled
Python - Lab Tests / Python Lab Tests (ubuntu-latest, 3.12) (push) Has been cancelled
Python - Lab Tests / Python Lab Tests (ubuntu-latest, 3.13) (push) Has been cancelled
Python - Lab Tests / Python Lab Tests (ubuntu-latest, 3.14) (push) Has been cancelled
Python - Lab Tests / Python Lab Tests (windows-latest, 3.10) (push) Has been cancelled
Python - Lab Tests / Python Lab Tests (windows-latest, 3.11) (push) Has been cancelled
Python - Lab Tests / Python Lab Tests (windows-latest, 3.12) (push) Has been cancelled
Python - Lab Tests / Python Lab Tests (windows-latest, 3.13) (push) Has been cancelled
Python - Lab Tests / Python Lab Tests (windows-latest, 3.14) (push) Has been cancelled
Check .md links / markdown-link-check (push) Has been cancelled
Some checks failed
CodeQL / Analyze (csharp) (push) Has been cancelled
CodeQL / Analyze (python) (push) Has been cancelled
dotnet-build-and-test / paths-filter (push) Has been cancelled
dotnet-build-and-test / dotnet-build-and-test (Debug, windows-latest, net9.0) (push) Has been cancelled
dotnet-build-and-test / dotnet-build-and-test (Release, integration, true, ubuntu-latest, net10.0) (push) Has been cancelled
dotnet-build-and-test / dotnet-build-and-test (Release, integration, true, windows-latest, net472) (push) Has been cancelled
dotnet-build-and-test / dotnet-build-and-test (Release, ubuntu-latest, net8.0) (push) Has been cancelled
dotnet-build-and-test / dotnet-build-and-test-check (push) Has been cancelled
Python - Merge - Tests / paths-filter (push) Has been cancelled
Python - Merge - Tests / Python Tests - Core (integration, ubuntu-latest, 3.10) (push) Has been cancelled
Python - Merge - Tests / Python Tests - Azure AI (integration, ubuntu-latest, 3.10) (push) Has been cancelled
Python - Merge - Tests / python-integration-tests-check (push) Has been cancelled
Python - Lab Tests / paths-filter (push) Has been cancelled
Python - Lab Tests / Python Lab Tests (ubuntu-latest, 3.10) (push) Has been cancelled
Python - Lab Tests / Python Lab Tests (ubuntu-latest, 3.11) (push) Has been cancelled
Python - Lab Tests / Python Lab Tests (ubuntu-latest, 3.12) (push) Has been cancelled
Python - Lab Tests / Python Lab Tests (ubuntu-latest, 3.13) (push) Has been cancelled
Python - Lab Tests / Python Lab Tests (ubuntu-latest, 3.14) (push) Has been cancelled
Python - Lab Tests / Python Lab Tests (windows-latest, 3.10) (push) Has been cancelled
Python - Lab Tests / Python Lab Tests (windows-latest, 3.11) (push) Has been cancelled
Python - Lab Tests / Python Lab Tests (windows-latest, 3.12) (push) Has been cancelled
Python - Lab Tests / Python Lab Tests (windows-latest, 3.13) (push) Has been cancelled
Python - Lab Tests / Python Lab Tests (windows-latest, 3.14) (push) Has been cancelled
Check .md links / markdown-link-check (push) Has been cancelled
This commit is contained in:
@@ -0,0 +1,198 @@
|
||||
// Copyright (c) Microsoft. All rights reserved.
|
||||
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Text.Json;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.Agents.AI.AGUI.Shared;
|
||||
using Moq;
|
||||
using Moq.Protected;
|
||||
|
||||
namespace Microsoft.Agents.AI.AGUI.UnitTests;
|
||||
|
||||
/// <summary>
|
||||
/// Unit tests for the <see cref="AGUIHttpService"/> class.
|
||||
/// </summary>
|
||||
public sealed class AGUIHttpServiceTests
|
||||
{
|
||||
[Fact]
|
||||
public async Task PostRunAsync_SendsRequestAndParsesSSEStream_SuccessfullyAsync()
|
||||
{
|
||||
// Arrange
|
||||
BaseEvent[] events =
|
||||
[
|
||||
new RunStartedEvent { ThreadId = "thread1", RunId = "run1" },
|
||||
new TextMessageStartEvent { MessageId = "msg1", Role = AGUIRoles.Assistant },
|
||||
new TextMessageContentEvent { MessageId = "msg1", Delta = "Hello" },
|
||||
new TextMessageEndEvent { MessageId = "msg1" },
|
||||
new RunFinishedEvent { ThreadId = "thread1", RunId = "run1" }
|
||||
];
|
||||
|
||||
HttpClient httpClient = CreateMockHttpClient(events, HttpStatusCode.OK);
|
||||
AGUIHttpService service = new(httpClient, "http://localhost/agent");
|
||||
RunAgentInput input = new()
|
||||
{
|
||||
ThreadId = "thread1",
|
||||
RunId = "run1",
|
||||
Messages = [new AGUIUserMessage { Id = "m1", Content = "Test" }]
|
||||
};
|
||||
|
||||
// Act
|
||||
List<BaseEvent> resultEvents = [];
|
||||
await foreach (BaseEvent evt in service.PostRunAsync(input, CancellationToken.None))
|
||||
{
|
||||
resultEvents.Add(evt);
|
||||
}
|
||||
|
||||
// Assert
|
||||
Assert.Equal(5, resultEvents.Count);
|
||||
Assert.IsType<RunStartedEvent>(resultEvents[0]);
|
||||
Assert.IsType<TextMessageStartEvent>(resultEvents[1]);
|
||||
Assert.IsType<TextMessageContentEvent>(resultEvents[2]);
|
||||
Assert.IsType<TextMessageEndEvent>(resultEvents[3]);
|
||||
Assert.IsType<RunFinishedEvent>(resultEvents[4]);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task PostRunAsync_WithNonSuccessStatusCode_ThrowsHttpRequestExceptionAsync()
|
||||
{
|
||||
// Arrange
|
||||
HttpClient httpClient = CreateMockHttpClient([], HttpStatusCode.InternalServerError);
|
||||
AGUIHttpService service = new(httpClient, "http://localhost/agent");
|
||||
RunAgentInput input = new()
|
||||
{
|
||||
ThreadId = "thread1",
|
||||
RunId = "run1",
|
||||
Messages = [new AGUIUserMessage { Id = "m1", Content = "Test" }]
|
||||
};
|
||||
|
||||
// Act & Assert
|
||||
await Assert.ThrowsAsync<HttpRequestException>(async () =>
|
||||
{
|
||||
await foreach (var _ in service.PostRunAsync(input, CancellationToken.None))
|
||||
{
|
||||
// Consume the stream
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task PostRunAsync_DeserializesMultipleEventTypes_CorrectlyAsync()
|
||||
{
|
||||
// Arrange
|
||||
BaseEvent[] events =
|
||||
[
|
||||
new RunStartedEvent { ThreadId = "thread1", RunId = "run1" },
|
||||
new RunErrorEvent { Message = "Error occurred", Code = "ERR001" },
|
||||
new RunFinishedEvent { ThreadId = "thread1", RunId = "run1", Result = JsonElement.Parse("\"Success\"") }
|
||||
];
|
||||
|
||||
HttpClient httpClient = CreateMockHttpClient(events, HttpStatusCode.OK);
|
||||
AGUIHttpService service = new(httpClient, "http://localhost/agent");
|
||||
RunAgentInput input = new()
|
||||
{
|
||||
ThreadId = "thread1",
|
||||
RunId = "run1",
|
||||
Messages = [new AGUIUserMessage { Id = "m1", Content = "Test" }]
|
||||
};
|
||||
|
||||
// Act
|
||||
List<BaseEvent> resultEvents = [];
|
||||
await foreach (BaseEvent evt in service.PostRunAsync(input, CancellationToken.None))
|
||||
{
|
||||
resultEvents.Add(evt);
|
||||
}
|
||||
|
||||
// Assert
|
||||
Assert.Equal(3, resultEvents.Count);
|
||||
RunStartedEvent startedEvent = Assert.IsType<RunStartedEvent>(resultEvents[0]);
|
||||
Assert.Equal("thread1", startedEvent.ThreadId);
|
||||
RunErrorEvent errorEvent = Assert.IsType<RunErrorEvent>(resultEvents[1]);
|
||||
Assert.Equal("Error occurred", errorEvent.Message);
|
||||
RunFinishedEvent finishedEvent = Assert.IsType<RunFinishedEvent>(resultEvents[2]);
|
||||
Assert.Equal("Success", finishedEvent.Result?.GetString());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task PostRunAsync_WithEmptyEventStream_CompletesSuccessfullyAsync()
|
||||
{
|
||||
// Arrange
|
||||
HttpClient httpClient = CreateMockHttpClient([], HttpStatusCode.OK);
|
||||
AGUIHttpService service = new(httpClient, "http://localhost/agent");
|
||||
RunAgentInput input = new()
|
||||
{
|
||||
ThreadId = "thread1",
|
||||
RunId = "run1",
|
||||
Messages = [new AGUIUserMessage { Id = "m1", Content = "Test" }]
|
||||
};
|
||||
|
||||
// Act
|
||||
List<BaseEvent> resultEvents = [];
|
||||
await foreach (BaseEvent evt in service.PostRunAsync(input, CancellationToken.None))
|
||||
{
|
||||
resultEvents.Add(evt);
|
||||
}
|
||||
|
||||
// Assert
|
||||
Assert.Empty(resultEvents);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task PostRunAsync_WithCancellationToken_CancelsRequestAsync()
|
||||
{
|
||||
// Arrange
|
||||
CancellationTokenSource cts = new();
|
||||
cts.Cancel();
|
||||
|
||||
Mock<HttpMessageHandler> handlerMock = new(MockBehavior.Strict);
|
||||
handlerMock
|
||||
.Protected()
|
||||
.Setup<Task<HttpResponseMessage>>(
|
||||
"SendAsync",
|
||||
ItExpr.IsAny<HttpRequestMessage>(),
|
||||
ItExpr.IsAny<CancellationToken>())
|
||||
.ThrowsAsync(new TaskCanceledException());
|
||||
|
||||
HttpClient httpClient = new(handlerMock.Object);
|
||||
AGUIHttpService service = new(httpClient, "http://localhost/agent");
|
||||
RunAgentInput input = new()
|
||||
{
|
||||
ThreadId = "thread1",
|
||||
RunId = "run1",
|
||||
Messages = [new AGUIUserMessage { Id = "m1", Content = "Test" }]
|
||||
};
|
||||
|
||||
// Act & Assert
|
||||
await Assert.ThrowsAsync<TaskCanceledException>(async () =>
|
||||
{
|
||||
await foreach (var _ in service.PostRunAsync(input, cts.Token))
|
||||
{
|
||||
// Intentionally empty - consuming stream to trigger cancellation
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private static HttpClient CreateMockHttpClient(BaseEvent[] events, HttpStatusCode statusCode)
|
||||
{
|
||||
string sseContent = string.Concat(events.Select(e =>
|
||||
$"data: {JsonSerializer.Serialize(e, AGUIJsonSerializerContext.Default.BaseEvent)}\n\n"));
|
||||
|
||||
Mock<HttpMessageHandler> handlerMock = new(MockBehavior.Strict);
|
||||
handlerMock
|
||||
.Protected()
|
||||
.Setup<Task<HttpResponseMessage>>(
|
||||
"SendAsync",
|
||||
ItExpr.IsAny<HttpRequestMessage>(),
|
||||
ItExpr.IsAny<CancellationToken>())
|
||||
.ReturnsAsync(new HttpResponseMessage
|
||||
{
|
||||
StatusCode = statusCode,
|
||||
Content = new StringContent(sseContent)
|
||||
});
|
||||
|
||||
return new HttpClient(handlerMock.Object);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user