// 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;
///
/// Unit tests for the class.
///
public sealed class StreamingUpdatePipelineResponseTests
{
///
/// Verify that Status property returns 200.
///
[Fact]
public void Status_ReturnsOkStatus()
{
// Arrange
IAsyncEnumerable updates = CreateTestUpdatesAsync();
PipelineResponse response = new StreamingUpdatePipelineResponse(updates);
// Act
int status = response.Status;
// Assert
Assert.Equal(200, status);
}
///
/// Verify that ReasonPhrase property returns "OK".
///
[Fact]
public void ReasonPhrase_ReturnsOk()
{
// Arrange
IAsyncEnumerable updates = CreateTestUpdatesAsync();
PipelineResponse response = new StreamingUpdatePipelineResponse(updates);
// Act
string reasonPhrase = response.ReasonPhrase;
// Assert
Assert.Equal("OK", reasonPhrase);
}
///
/// Verify that ContentStream getter returns null.
///
[Fact]
public void ContentStream_Get_ReturnsNull()
{
// Arrange
IAsyncEnumerable updates = CreateTestUpdatesAsync();
PipelineResponse response = new StreamingUpdatePipelineResponse(updates);
// Act
System.IO.Stream? contentStream = response.ContentStream;
// Assert
Assert.Null(contentStream);
}
///
/// Verify that ContentStream setter is a no-op.
///
[Fact]
public void ContentStream_Set_IsNoOp()
{
// Arrange
IAsyncEnumerable updates = CreateTestUpdatesAsync();
PipelineResponse response = new StreamingUpdatePipelineResponse(updates);
var testStream = new System.IO.MemoryStream();
// Act
response.ContentStream = testStream;
// Assert
Assert.Null(response.ContentStream);
testStream.Dispose();
}
///
/// Verify that Content property returns empty BinaryData.
///
[Fact]
public void Content_ReturnsEmptyBinaryData()
{
// Arrange
IAsyncEnumerable updates = CreateTestUpdatesAsync();
PipelineResponse response = new StreamingUpdatePipelineResponse(updates);
// Act
BinaryData content = response.Content;
// Assert
Assert.NotNull(content);
Assert.Equal(string.Empty, content.ToString());
}
///
/// Verify that BufferContent throws NotSupportedException.
///
[Fact]
public void BufferContent_ThrowsNotSupportedException()
{
// Arrange
IAsyncEnumerable updates = CreateTestUpdatesAsync();
PipelineResponse response = new StreamingUpdatePipelineResponse(updates);
// Act & Assert
var exception = Assert.Throws(() => response.BufferContent());
Assert.Contains("Buffering content is not supported", exception.Message);
}
///
/// Verify that BufferContentAsync throws NotSupportedException.
///
[Fact]
public async Task BufferContentAsync_ThrowsNotSupportedExceptionAsync()
{
// Arrange
IAsyncEnumerable updates = CreateTestUpdatesAsync();
PipelineResponse response = new StreamingUpdatePipelineResponse(updates);
// Act & Assert
var exception = await Assert.ThrowsAsync(
async () => await response.BufferContentAsync());
Assert.Contains("Buffering content asynchronously is not supported", exception.Message);
}
///
/// Verify that Dispose does not throw.
///
[Fact]
public void Dispose_DoesNotThrow()
{
// Arrange
IAsyncEnumerable updates = CreateTestUpdatesAsync();
PipelineResponse response = new StreamingUpdatePipelineResponse(updates);
// Act & Assert
response.Dispose();
}
private static async IAsyncEnumerable CreateTestUpdatesAsync()
{
yield return new AgentResponseUpdate(Microsoft.Extensions.AI.ChatRole.Assistant, "test");
await Task.CompletedTask;
}
}