chore: import upstream snapshot with attribution
dotnet-build-and-test / dotnet-test-functions (push) Has been cancelled
dotnet-build-and-test / paths-filter (push) Has been cancelled
dotnet-build-and-test / dotnet-build (Debug, windows-latest, net9.0) (push) Has been cancelled
dotnet-build-and-test / dotnet-build (Release, ubuntu-latest, net10.0) (push) Has been cancelled
dotnet-build-and-test / dotnet-build (Release, ubuntu-latest, net8.0) (push) Has been cancelled
dotnet-build-and-test / dotnet-build (Release, windows-latest, net472) (push) Has been cancelled
dotnet-build-and-test / dotnet-test (Release, integration, true, ubuntu-latest, net10.0) (push) Has been cancelled
dotnet-build-and-test / dotnet-test (Release, integration, true, windows-latest, net472) (push) Has been cancelled
dotnet-build-and-test / dotnet-foundry-hosted-it (push) Has been cancelled
dotnet-build-and-test / dotnet-build-and-test-check (push) Has been cancelled
dotnet-build-and-test / Integration Test Report (push) Has been cancelled
CodeQL / Analyze (csharp) (push) Has been cancelled
CodeQL / Analyze (python) (push) Has been cancelled
dotnet-build-and-test / dotnet-test-functions (push) Has been cancelled
dotnet-build-and-test / paths-filter (push) Has been cancelled
dotnet-build-and-test / dotnet-build (Debug, windows-latest, net9.0) (push) Has been cancelled
dotnet-build-and-test / dotnet-build (Release, ubuntu-latest, net10.0) (push) Has been cancelled
dotnet-build-and-test / dotnet-build (Release, ubuntu-latest, net8.0) (push) Has been cancelled
dotnet-build-and-test / dotnet-build (Release, windows-latest, net472) (push) Has been cancelled
dotnet-build-and-test / dotnet-test (Release, integration, true, ubuntu-latest, net10.0) (push) Has been cancelled
dotnet-build-and-test / dotnet-test (Release, integration, true, windows-latest, net472) (push) Has been cancelled
dotnet-build-and-test / dotnet-foundry-hosted-it (push) Has been cancelled
dotnet-build-and-test / dotnet-build-and-test-check (push) Has been cancelled
dotnet-build-and-test / Integration Test Report (push) Has been cancelled
CodeQL / Analyze (csharp) (push) Has been cancelled
CodeQL / Analyze (python) (push) Has been cancelled
This commit is contained in:
+109
@@ -0,0 +1,109 @@
|
||||
// Copyright (c) Microsoft. All rights reserved.
|
||||
|
||||
using System.ClientModel;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.Extensions.AI;
|
||||
using OpenAI.Chat;
|
||||
|
||||
namespace Microsoft.Agents.AI.OpenAI.UnitTests.ChatClient;
|
||||
|
||||
/// <summary>
|
||||
/// Unit tests for the <see cref="AsyncStreamingChatCompletionUpdateCollectionResult"/> class.
|
||||
/// </summary>
|
||||
public sealed class AsyncStreamingChatCompletionUpdateCollectionResultTests
|
||||
{
|
||||
/// <summary>
|
||||
/// Verify that GetContinuationToken returns null.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void GetContinuationToken_ReturnsNull()
|
||||
{
|
||||
// Arrange
|
||||
IAsyncEnumerable<AgentResponseUpdate> updates = CreateTestUpdatesAsync();
|
||||
AsyncCollectionResult<StreamingChatCompletionUpdate> collectionResult = new AsyncStreamingChatCompletionUpdateCollectionResult(updates);
|
||||
|
||||
// Act
|
||||
ContinuationToken? token = collectionResult.GetContinuationToken(null!);
|
||||
|
||||
// Assert
|
||||
Assert.Null(token);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verify that GetRawPagesAsync returns a single page.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public async Task GetRawPagesAsync_ReturnsSinglePageAsync()
|
||||
{
|
||||
// Arrange
|
||||
IAsyncEnumerable<AgentResponseUpdate> updates = CreateTestUpdatesAsync();
|
||||
AsyncCollectionResult<StreamingChatCompletionUpdate> collectionResult = new AsyncStreamingChatCompletionUpdateCollectionResult(updates);
|
||||
|
||||
// Act
|
||||
List<ClientResult> pages = [];
|
||||
await foreach (ClientResult page in collectionResult.GetRawPagesAsync())
|
||||
{
|
||||
pages.Add(page);
|
||||
}
|
||||
|
||||
// Assert
|
||||
Assert.Single(pages);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verify that iterating through the collection yields streaming updates.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public async Task IterateCollection_YieldsUpdatesAsync()
|
||||
{
|
||||
// Arrange
|
||||
IAsyncEnumerable<AgentResponseUpdate> updates = CreateTestUpdatesAsync();
|
||||
AsyncCollectionResult<StreamingChatCompletionUpdate> collectionResult = new AsyncStreamingChatCompletionUpdateCollectionResult(updates);
|
||||
|
||||
// Act
|
||||
List<StreamingChatCompletionUpdate> results = [];
|
||||
await foreach (StreamingChatCompletionUpdate update in collectionResult)
|
||||
{
|
||||
results.Add(update);
|
||||
}
|
||||
|
||||
// Assert
|
||||
Assert.Single(results);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verify that iterating through the collection with multiple updates yields all updates.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public async Task IterateCollection_WithMultipleUpdates_YieldsAllUpdatesAsync()
|
||||
{
|
||||
// Arrange
|
||||
IAsyncEnumerable<AgentResponseUpdate> updates = CreateMultipleTestUpdatesAsync();
|
||||
AsyncCollectionResult<StreamingChatCompletionUpdate> collectionResult = new AsyncStreamingChatCompletionUpdateCollectionResult(updates);
|
||||
|
||||
// Act
|
||||
List<StreamingChatCompletionUpdate> results = [];
|
||||
await foreach (StreamingChatCompletionUpdate update in collectionResult)
|
||||
{
|
||||
results.Add(update);
|
||||
}
|
||||
|
||||
// Assert
|
||||
Assert.Equal(3, results.Count);
|
||||
}
|
||||
|
||||
private static async IAsyncEnumerable<AgentResponseUpdate> CreateTestUpdatesAsync()
|
||||
{
|
||||
yield return new AgentResponseUpdate(ChatRole.Assistant, "test");
|
||||
await Task.CompletedTask;
|
||||
}
|
||||
|
||||
private static async IAsyncEnumerable<AgentResponseUpdate> CreateMultipleTestUpdatesAsync()
|
||||
{
|
||||
yield return new AgentResponseUpdate(ChatRole.Assistant, "first");
|
||||
yield return new AgentResponseUpdate(ChatRole.Assistant, "second");
|
||||
yield return new AgentResponseUpdate(ChatRole.Assistant, "third");
|
||||
await Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
+190
@@ -0,0 +1,190 @@
|
||||
// Copyright (c) Microsoft. All rights reserved.
|
||||
|
||||
using System;
|
||||
using System.ClientModel;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.Extensions.AI;
|
||||
using OpenAI.Responses;
|
||||
|
||||
namespace Microsoft.Agents.AI.OpenAI.UnitTests.ChatClient;
|
||||
|
||||
/// <summary>
|
||||
/// Unit tests for the <see cref="AsyncStreamingResponseUpdateCollectionResult"/> class.
|
||||
/// </summary>
|
||||
public sealed class AsyncStreamingResponseUpdateCollectionResultTests
|
||||
{
|
||||
/// <summary>
|
||||
/// Verify that GetContinuationToken returns null.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void GetContinuationToken_ReturnsNull()
|
||||
{
|
||||
// Arrange
|
||||
IAsyncEnumerable<AgentResponseUpdate> updates = CreateTestUpdatesAsync();
|
||||
AsyncCollectionResult<StreamingResponseUpdate> collectionResult = new AsyncStreamingResponseUpdateCollectionResult(updates);
|
||||
|
||||
// Act
|
||||
ContinuationToken? token = collectionResult.GetContinuationToken(null!);
|
||||
|
||||
// Assert
|
||||
Assert.Null(token);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verify that GetRawPagesAsync returns a single page.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public async Task GetRawPagesAsync_ReturnsSinglePageAsync()
|
||||
{
|
||||
// Arrange
|
||||
IAsyncEnumerable<AgentResponseUpdate> updates = CreateTestUpdatesAsync();
|
||||
AsyncCollectionResult<StreamingResponseUpdate> collectionResult = new AsyncStreamingResponseUpdateCollectionResult(updates);
|
||||
|
||||
// Act
|
||||
List<ClientResult> pages = [];
|
||||
await foreach (ClientResult page in collectionResult.GetRawPagesAsync())
|
||||
{
|
||||
pages.Add(page);
|
||||
}
|
||||
|
||||
// Assert
|
||||
Assert.Single(pages);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verify that iterating through the collection yields streaming updates when RawRepresentation is a StreamingResponseUpdate.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public async Task IterateCollection_WithStreamingResponseUpdateRawRepresentation_YieldsUpdatesAsync()
|
||||
{
|
||||
// Arrange
|
||||
StreamingResponseUpdate rawUpdate = CreateStreamingResponseUpdate();
|
||||
IAsyncEnumerable<AgentResponseUpdate> updates = CreateTestUpdatesWithRawRepresentationAsync(rawUpdate);
|
||||
AsyncCollectionResult<StreamingResponseUpdate> collectionResult = new AsyncStreamingResponseUpdateCollectionResult(updates);
|
||||
|
||||
// Act
|
||||
List<StreamingResponseUpdate> results = [];
|
||||
await foreach (StreamingResponseUpdate update in collectionResult)
|
||||
{
|
||||
results.Add(update);
|
||||
}
|
||||
|
||||
// Assert
|
||||
Assert.Single(results);
|
||||
Assert.Same(rawUpdate, results[0]);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verify that iterating through the collection yields updates when RawRepresentation is a ChatResponseUpdate containing a StreamingResponseUpdate.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public async Task IterateCollection_WithChatResponseUpdateContainingStreamingResponseUpdate_YieldsUpdatesAsync()
|
||||
{
|
||||
// Arrange
|
||||
StreamingResponseUpdate rawUpdate = CreateStreamingResponseUpdate();
|
||||
ChatResponseUpdate chatResponseUpdate = new() { RawRepresentation = rawUpdate };
|
||||
IAsyncEnumerable<AgentResponseUpdate> updates = CreateTestUpdatesWithChatResponseUpdateAsync(chatResponseUpdate);
|
||||
AsyncCollectionResult<StreamingResponseUpdate> collectionResult = new AsyncStreamingResponseUpdateCollectionResult(updates);
|
||||
|
||||
// Act
|
||||
List<StreamingResponseUpdate> results = [];
|
||||
await foreach (StreamingResponseUpdate update in collectionResult)
|
||||
{
|
||||
results.Add(update);
|
||||
}
|
||||
|
||||
// Assert
|
||||
Assert.Single(results);
|
||||
Assert.Same(rawUpdate, results[0]);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verify that iterating through the collection skips updates when RawRepresentation is not a StreamingResponseUpdate.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public async Task IterateCollection_WithNonStreamingResponseUpdateRawRepresentation_SkipsUpdateAsync()
|
||||
{
|
||||
// Arrange
|
||||
IAsyncEnumerable<AgentResponseUpdate> updates = CreateTestUpdatesAsync();
|
||||
AsyncCollectionResult<StreamingResponseUpdate> collectionResult = new AsyncStreamingResponseUpdateCollectionResult(updates);
|
||||
|
||||
// Act
|
||||
List<StreamingResponseUpdate> results = [];
|
||||
await foreach (StreamingResponseUpdate update in collectionResult)
|
||||
{
|
||||
results.Add(update);
|
||||
}
|
||||
|
||||
// Assert
|
||||
Assert.Empty(results);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verify that iterating through the collection skips updates when RawRepresentation is a ChatResponseUpdate without StreamingResponseUpdate.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public async Task IterateCollection_WithChatResponseUpdateWithoutStreamingResponseUpdate_SkipsUpdateAsync()
|
||||
{
|
||||
// Arrange
|
||||
ChatResponseUpdate chatResponseUpdate = new() { RawRepresentation = "not a streaming update" };
|
||||
IAsyncEnumerable<AgentResponseUpdate> updates = CreateTestUpdatesWithChatResponseUpdateAsync(chatResponseUpdate);
|
||||
AsyncCollectionResult<StreamingResponseUpdate> collectionResult = new AsyncStreamingResponseUpdateCollectionResult(updates);
|
||||
|
||||
// Act
|
||||
List<StreamingResponseUpdate> results = [];
|
||||
await foreach (StreamingResponseUpdate update in collectionResult)
|
||||
{
|
||||
results.Add(update);
|
||||
}
|
||||
|
||||
// Assert
|
||||
Assert.Empty(results);
|
||||
}
|
||||
|
||||
private static async IAsyncEnumerable<AgentResponseUpdate> CreateTestUpdatesAsync()
|
||||
{
|
||||
yield return new AgentResponseUpdate(ChatRole.Assistant, "test");
|
||||
await Task.CompletedTask;
|
||||
}
|
||||
|
||||
private static async IAsyncEnumerable<AgentResponseUpdate> CreateTestUpdatesWithRawRepresentationAsync(object rawRepresentation)
|
||||
{
|
||||
AgentResponseUpdate update = new(ChatRole.Assistant, "test")
|
||||
{
|
||||
RawRepresentation = rawRepresentation
|
||||
};
|
||||
yield return update;
|
||||
await Task.CompletedTask;
|
||||
}
|
||||
|
||||
private static async IAsyncEnumerable<AgentResponseUpdate> CreateTestUpdatesWithChatResponseUpdateAsync(ChatResponseUpdate chatResponseUpdate)
|
||||
{
|
||||
AgentResponseUpdate update = new(ChatRole.Assistant, "test")
|
||||
{
|
||||
RawRepresentation = chatResponseUpdate
|
||||
};
|
||||
yield return update;
|
||||
await Task.CompletedTask;
|
||||
}
|
||||
|
||||
private static StreamingResponseUpdate CreateStreamingResponseUpdate()
|
||||
{
|
||||
const string Json = """
|
||||
{
|
||||
"type": "response.output_item.added",
|
||||
"sequence_number": 1,
|
||||
"output_index": 0,
|
||||
"item": {
|
||||
"id": "item_abc123",
|
||||
"type": "message",
|
||||
"status": "in_progress",
|
||||
"role": "assistant",
|
||||
"content": []
|
||||
}
|
||||
}
|
||||
""";
|
||||
|
||||
return System.ClientModel.Primitives.ModelReaderWriter.Read<StreamingResponseUpdate>(BinaryData.FromString(Json))!;
|
||||
}
|
||||
}
|
||||
+154
@@ -0,0 +1,154 @@
|
||||
// Copyright (c) Microsoft. All rights reserved.
|
||||
|
||||
using System;
|
||||
using System.ClientModel.Primitives;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Microsoft.Agents.AI.OpenAI.UnitTests.ChatClient;
|
||||
|
||||
/// <summary>
|
||||
/// Unit tests for the <see cref="StreamingUpdatePipelineResponse"/> class.
|
||||
/// </summary>
|
||||
public sealed class StreamingUpdatePipelineResponseTests
|
||||
{
|
||||
/// <summary>
|
||||
/// Verify that Status property returns 200.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void Status_ReturnsOkStatus()
|
||||
{
|
||||
// Arrange
|
||||
IAsyncEnumerable<AgentResponseUpdate> updates = CreateTestUpdatesAsync();
|
||||
PipelineResponse response = new StreamingUpdatePipelineResponse(updates);
|
||||
|
||||
// Act
|
||||
int status = response.Status;
|
||||
|
||||
// Assert
|
||||
Assert.Equal(200, status);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verify that ReasonPhrase property returns "OK".
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void ReasonPhrase_ReturnsOk()
|
||||
{
|
||||
// Arrange
|
||||
IAsyncEnumerable<AgentResponseUpdate> updates = CreateTestUpdatesAsync();
|
||||
PipelineResponse response = new StreamingUpdatePipelineResponse(updates);
|
||||
|
||||
// Act
|
||||
string reasonPhrase = response.ReasonPhrase;
|
||||
|
||||
// Assert
|
||||
Assert.Equal("OK", reasonPhrase);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verify that ContentStream getter returns null.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void ContentStream_Get_ReturnsNull()
|
||||
{
|
||||
// Arrange
|
||||
IAsyncEnumerable<AgentResponseUpdate> updates = CreateTestUpdatesAsync();
|
||||
PipelineResponse response = new StreamingUpdatePipelineResponse(updates);
|
||||
|
||||
// Act
|
||||
System.IO.Stream? contentStream = response.ContentStream;
|
||||
|
||||
// Assert
|
||||
Assert.Null(contentStream);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verify that ContentStream setter is a no-op.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void ContentStream_Set_IsNoOp()
|
||||
{
|
||||
// Arrange
|
||||
IAsyncEnumerable<AgentResponseUpdate> updates = CreateTestUpdatesAsync();
|
||||
PipelineResponse response = new StreamingUpdatePipelineResponse(updates);
|
||||
var testStream = new System.IO.MemoryStream();
|
||||
|
||||
// Act
|
||||
response.ContentStream = testStream;
|
||||
|
||||
// Assert
|
||||
Assert.Null(response.ContentStream);
|
||||
|
||||
testStream.Dispose();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verify that Content property returns empty BinaryData.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void Content_ReturnsEmptyBinaryData()
|
||||
{
|
||||
// Arrange
|
||||
IAsyncEnumerable<AgentResponseUpdate> updates = CreateTestUpdatesAsync();
|
||||
PipelineResponse response = new StreamingUpdatePipelineResponse(updates);
|
||||
|
||||
// Act
|
||||
BinaryData content = response.Content;
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(content);
|
||||
Assert.Equal(string.Empty, content.ToString());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verify that BufferContent throws NotSupportedException.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void BufferContent_ThrowsNotSupportedException()
|
||||
{
|
||||
// Arrange
|
||||
IAsyncEnumerable<AgentResponseUpdate> updates = CreateTestUpdatesAsync();
|
||||
PipelineResponse response = new StreamingUpdatePipelineResponse(updates);
|
||||
|
||||
// Act & Assert
|
||||
var exception = Assert.Throws<NotSupportedException>(() => response.BufferContent());
|
||||
Assert.Contains("Buffering content is not supported", exception.Message);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verify that BufferContentAsync throws NotSupportedException.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public async Task BufferContentAsync_ThrowsNotSupportedExceptionAsync()
|
||||
{
|
||||
// Arrange
|
||||
IAsyncEnumerable<AgentResponseUpdate> updates = CreateTestUpdatesAsync();
|
||||
PipelineResponse response = new StreamingUpdatePipelineResponse(updates);
|
||||
|
||||
// Act & Assert
|
||||
var exception = await Assert.ThrowsAsync<NotSupportedException>(
|
||||
async () => await response.BufferContentAsync());
|
||||
Assert.Contains("Buffering content asynchronously is not supported", exception.Message);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verify that Dispose does not throw.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void Dispose_DoesNotThrow()
|
||||
{
|
||||
// Arrange
|
||||
IAsyncEnumerable<AgentResponseUpdate> updates = CreateTestUpdatesAsync();
|
||||
PipelineResponse response = new StreamingUpdatePipelineResponse(updates);
|
||||
|
||||
// Act & Assert
|
||||
response.Dispose();
|
||||
}
|
||||
|
||||
private static async IAsyncEnumerable<AgentResponseUpdate> CreateTestUpdatesAsync()
|
||||
{
|
||||
yield return new AgentResponseUpdate(Microsoft.Extensions.AI.ChatRole.Assistant, "test");
|
||||
await Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user