Files
wehub-resource-sync db620d33df
CodeQL / Analyze (csharp) (push) Waiting to run
CodeQL / Analyze (python) (push) Waiting to run
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
chore: import upstream snapshot with attribution
2026-07-13 13:39:25 +08:00

103 lines
3.6 KiB
C#

// Copyright (c) Microsoft. All rights reserved.
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Agents.AI.Hosting.OpenAI.Conversations;
using Microsoft.Agents.AI.Hosting.OpenAI.Conversations.Models;
using Microsoft.Agents.AI.Hosting.OpenAI.Responses;
using Microsoft.Agents.AI.Hosting.OpenAI.Responses.Models;
using Microsoft.Extensions.AI;
namespace Microsoft.Agents.AI.Hosting.OpenAI.UnitTests;
/// <summary>
/// Unit tests for <see cref="InMemoryResponsesService"/> request validation.
/// </summary>
public sealed class InMemoryResponsesServiceTests
{
[Fact]
public async Task ValidateRequestAsync_NonexistentConversation_ReturnsNotFoundErrorAsync()
{
// Arrange
using var storage = new InMemoryConversationStorage();
using var service = new InMemoryResponsesService(
new StubResponseExecutor(), new InMemoryStorageOptions(), storage);
var request = new CreateResponse
{
Input = ResponseInput.FromText("hello"),
Conversation = ConversationReference.FromId("conv_does_not_exist")
};
// Act
ResponseError? error = await service.ValidateRequestAsync(request);
// Assert
Assert.NotNull(error);
Assert.Equal("conversation_not_found", error.Code);
}
[Fact]
public async Task ValidateRequestAsync_ExistingConversation_ReturnsNullAsync()
{
// Arrange
using var storage = new InMemoryConversationStorage();
var conversation = new Conversation
{
Id = "conv_" + Guid.NewGuid().ToString("N"),
CreatedAt = DateTimeOffset.UtcNow.ToUnixTimeSeconds()
};
await storage.CreateConversationAsync(conversation);
using var service = new InMemoryResponsesService(
new StubResponseExecutor(), new InMemoryStorageOptions(), storage);
var request = new CreateResponse
{
Input = ResponseInput.FromText("hello"),
Conversation = ConversationReference.FromId(conversation.Id)
};
// Act
ResponseError? error = await service.ValidateRequestAsync(request);
// Assert
Assert.Null(error);
}
[Fact]
public async Task ValidateRequestAsync_ConversationSuppliedButNoStorage_ReturnsNullAsync()
{
// Arrange - without a conversation store there is no existence to verify.
using var service = new InMemoryResponsesService(
new StubResponseExecutor(), new InMemoryStorageOptions());
var request = new CreateResponse
{
Input = ResponseInput.FromText("hello"),
Conversation = ConversationReference.FromId("conv_does_not_exist")
};
// Act
ResponseError? error = await service.ValidateRequestAsync(request);
// Assert
Assert.Null(error);
}
private sealed class StubResponseExecutor : IResponseExecutor
{
public ValueTask<ResponseError?> ValidateRequestAsync(CreateResponse request, CancellationToken cancellationToken = default)
=> ValueTask.FromResult<ResponseError?>(null);
public async IAsyncEnumerable<StreamingResponseEvent> ExecuteAsync(
AgentInvocationContext context,
CreateResponse request,
IReadOnlyList<ChatMessage>? conversationHistory = null,
[EnumeratorCancellation] CancellationToken cancellationToken = default)
{
await Task.CompletedTask.ConfigureAwait(false);
yield break;
}
}
}