-
Notifications
You must be signed in to change notification settings - Fork 1.7k
.NET: Add dedicated Foundry.Hosting UnitTest project #5592
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
rogerbarreto
merged 7 commits into
microsoft:main
from
rogerbarreto:features/foundry-hosting-unittests-split
Apr 30, 2026
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
da67e7e
Foundry.Hosting.UnitTests: extract project from Foundry.UnitTests
rogerbarreto b831551
Foundry.Hosting.UnitTests: align namespaces to assembly name
rogerbarreto e46a38e
Foundry.Hosting.UnitTests: split WorkflowIntegrationTests by SUT
rogerbarreto 571ca90
Foundry.UnitTests: trim Hosting-related conditionals and dead testdata
rogerbarreto cde10f0
Foundry.Hosting.UnitTests: drop redundant 'using Microsoft.Agents.AI.…
rogerbarreto 25b5306
Foundry.Hosting: drop InternalsVisibleTo to Foundry.UnitTests
rogerbarreto f66997d
Foundry.Hosting: rename DelegatingResponsesClient to UserAgentRespons…
rogerbarreto File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
212 changes: 212 additions & 0 deletions
212
...crosoft.Agents.AI.Foundry.Hosting.UnitTests/AgentFrameworkResponseHandlerWorkflowTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,212 @@ | ||
| // Copyright (c) Microsoft. All rights reserved. | ||
|
|
||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using Azure.AI.AgentServer.Responses; | ||
| using Azure.AI.AgentServer.Responses.Models; | ||
| using Microsoft.Agents.AI.Workflows; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using Microsoft.Extensions.Logging; | ||
| using Microsoft.Extensions.Logging.Abstractions; | ||
| using Moq; | ||
|
|
||
| namespace Microsoft.Agents.AI.Foundry.Hosting.UnitTests; | ||
|
|
||
| /// <summary> | ||
| /// Unit tests for <see cref="AgentFrameworkResponseHandler"/> that verify behavior | ||
| /// when the registered agent is a workflow-backed <see cref="AIAgent"/>. These exercise | ||
| /// real workflow builders and the in-process execution environment to drive the handler | ||
| /// through realistic streaming event patterns. | ||
| /// </summary> | ||
| public class AgentFrameworkResponseHandlerWorkflowTests | ||
| { | ||
| [Fact] | ||
| public async Task SequentialWorkflow_SingleAgent_ProducesTextOutputAsync() | ||
| { | ||
| // Arrange: single-agent sequential workflow | ||
| var echoAgent = new StreamingTextAgent("echo", "Hello from the workflow!"); | ||
| var workflow = AgentWorkflowBuilder.BuildSequential("test-sequential", echoAgent); | ||
| var workflowAgent = workflow.AsAIAgent( | ||
| id: "workflow-agent", | ||
| name: "Test Workflow", | ||
| executionEnvironment: InProcessExecution.OffThread, | ||
| includeExceptionDetails: true); | ||
|
|
||
| var (handler, request, context) = CreateHandlerWithAgent(workflowAgent, "Hello"); | ||
|
|
||
| // Act | ||
| var events = await CollectEventsAsync(handler, request, context); | ||
|
|
||
| // Assert: should have lifecycle events + at least one text output + terminal | ||
| Assert.IsType<ResponseCreatedEvent>(events[0]); | ||
| Assert.IsType<ResponseInProgressEvent>(events[1]); | ||
| Assert.True(events.Count >= 4, $"Expected at least 4 events, got {events.Count}"); | ||
|
|
||
| var lastEvent = events[^1]; | ||
| Assert.True( | ||
| lastEvent is ResponseCompletedEvent || lastEvent is ResponseFailedEvent, | ||
| $"Expected terminal event, got {lastEvent.GetType().Name}"); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task SequentialWorkflow_TwoAgents_ProducesOutputFromBothAsync() | ||
| { | ||
| // Arrange: two agents in sequence | ||
| var agent1 = new StreamingTextAgent("agent1", "First agent says hello"); | ||
| var agent2 = new StreamingTextAgent("agent2", "Second agent says goodbye"); | ||
| var workflow = AgentWorkflowBuilder.BuildSequential("test-sequential-2", agent1, agent2); | ||
| var workflowAgent = workflow.AsAIAgent( | ||
| id: "seq-workflow", | ||
| name: "Sequential Workflow", | ||
| executionEnvironment: InProcessExecution.OffThread, | ||
| includeExceptionDetails: true); | ||
|
|
||
| var (handler, request, context) = CreateHandlerWithAgent(workflowAgent, "Process this"); | ||
|
|
||
| // Act | ||
| var events = await CollectEventsAsync(handler, request, context); | ||
|
|
||
| // Assert: should have workflow action events for executor lifecycle | ||
| var lastEvent = events[^1]; | ||
| Assert.True( | ||
| lastEvent is ResponseCompletedEvent || lastEvent is ResponseFailedEvent, | ||
| $"Expected terminal event, got {lastEvent.GetType().Name}"); | ||
|
|
||
| // Should have output item events (either text messages or workflow actions) | ||
| Assert.True(events.OfType<ResponseOutputItemAddedEvent>().Any(), | ||
| "Expected at least one output item from the workflow"); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task Workflow_AgentThrowsException_ProducesErrorOutputAsync() | ||
| { | ||
| // Arrange: workflow with an agent that throws | ||
| var throwingAgent = new ThrowingStreamingAgent("thrower", new InvalidOperationException("Agent crashed")); | ||
| var workflow = AgentWorkflowBuilder.BuildSequential("test-error", throwingAgent); | ||
| var workflowAgent = workflow.AsAIAgent( | ||
| id: "error-workflow", | ||
| name: "Error Workflow", | ||
| executionEnvironment: InProcessExecution.OffThread, | ||
| includeExceptionDetails: true); | ||
|
|
||
| var (handler, request, context) = CreateHandlerWithAgent(workflowAgent, "Trigger error"); | ||
|
|
||
| // Act | ||
| var events = await CollectEventsAsync(handler, request, context); | ||
|
|
||
| // Assert: should have lifecycle events + error/failure indicator | ||
| Assert.IsType<ResponseCreatedEvent>(events[0]); | ||
| Assert.IsType<ResponseInProgressEvent>(events[1]); | ||
|
|
||
| var lastEvent = events[^1]; | ||
| // Workflow errors surface as either Failed or Completed (depending on error handling) | ||
| Assert.True( | ||
| lastEvent is ResponseCompletedEvent || lastEvent is ResponseFailedEvent, | ||
| $"Expected terminal event, got {lastEvent.GetType().Name}"); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task Workflow_ExecutorEvents_ProduceWorkflowActionItemsAsync() | ||
| { | ||
| // Arrange | ||
| var agent = new StreamingTextAgent("test-agent", "Result"); | ||
| var workflow = AgentWorkflowBuilder.BuildSequential("test-actions", agent); | ||
| var workflowAgent = workflow.AsAIAgent( | ||
| id: "actions-workflow", | ||
| name: "Actions Workflow", | ||
| executionEnvironment: InProcessExecution.OffThread); | ||
|
|
||
| var (handler, request, context) = CreateHandlerWithAgent(workflowAgent, "Hello"); | ||
|
|
||
| // Act | ||
| var events = await CollectEventsAsync(handler, request, context); | ||
|
|
||
| // Assert: workflow should produce OutputItemAdded events for executor lifecycle | ||
| var addedEvents = events.OfType<ResponseOutputItemAddedEvent>().ToList(); | ||
| Assert.True(addedEvents.Count >= 1, | ||
| $"Expected at least 1 output item added event, got {addedEvents.Count}"); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task WorkflowAgent_RegisteredWithKey_ResolvesCorrectlyAsync() | ||
| { | ||
| // Arrange: workflow agent registered with a keyed service name | ||
| var agent = new StreamingTextAgent("inner", "Keyed workflow response"); | ||
| var workflow = AgentWorkflowBuilder.BuildSequential("keyed-wf", agent); | ||
| var workflowAgent = workflow.AsAIAgent( | ||
| id: "keyed-workflow", | ||
| name: "Keyed Workflow", | ||
| executionEnvironment: InProcessExecution.OffThread); | ||
|
|
||
| var services = new ServiceCollection(); | ||
| services.AddSingleton<AgentSessionStore>(new InMemoryAgentSessionStore()); | ||
| services.AddKeyedSingleton("my-workflow", workflowAgent); | ||
| var sp = services.BuildServiceProvider(); | ||
|
|
||
| var handler = new AgentFrameworkResponseHandler(sp, NullLogger<AgentFrameworkResponseHandler>.Instance); | ||
| var request = new CreateResponse { Model = "test", AgentReference = new AgentReference("my-workflow") }; | ||
| request.Input = CreateUserInput("Test keyed workflow"); | ||
| var mockContext = CreateMockContext(); | ||
|
|
||
| // Act | ||
| var events = await CollectEventsAsync(handler, request, mockContext.Object); | ||
|
|
||
| // Assert | ||
| Assert.IsType<ResponseCreatedEvent>(events[0]); | ||
| Assert.True(events.Count >= 3, $"Expected at least 3 events, got {events.Count}"); | ||
| } | ||
|
|
||
| private static (AgentFrameworkResponseHandler handler, CreateResponse request, ResponseContext context) | ||
| CreateHandlerWithAgent(AIAgent agent, string userMessage) | ||
| { | ||
| var services = new ServiceCollection(); | ||
| services.AddSingleton<AgentSessionStore>(new InMemoryAgentSessionStore()); | ||
| services.AddSingleton(agent); | ||
| services.AddSingleton<ILogger<AgentFrameworkResponseHandler>>(NullLogger<AgentFrameworkResponseHandler>.Instance); | ||
| var sp = services.BuildServiceProvider(); | ||
|
|
||
| var handler = new AgentFrameworkResponseHandler(sp, NullLogger<AgentFrameworkResponseHandler>.Instance); | ||
| var request = new CreateResponse { Model = "test" }; | ||
| request.Input = CreateUserInput(userMessage); | ||
| var mockContext = CreateMockContext(); | ||
|
|
||
| return (handler, request, mockContext.Object); | ||
| } | ||
|
|
||
| private static BinaryData CreateUserInput(string text) | ||
| { | ||
| return BinaryData.FromObjectAsJson(new[] | ||
| { | ||
| new { type = "message", id = "msg_in_1", status = "completed", role = "user", | ||
| content = new[] { new { type = "input_text", text } } | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| private static Mock<ResponseContext> CreateMockContext() | ||
| { | ||
| var mock = new Mock<ResponseContext>("resp_" + new string('0', 46)) { CallBase = true }; | ||
| mock.Setup(x => x.GetHistoryAsync(It.IsAny<CancellationToken>())) | ||
| .ReturnsAsync(Array.Empty<OutputItem>()); | ||
| mock.Setup(x => x.GetInputItemsAsync(It.IsAny<bool>(), It.IsAny<CancellationToken>())) | ||
| .ReturnsAsync(Array.Empty<Item>()); | ||
| return mock; | ||
| } | ||
|
|
||
| private static async Task<List<ResponseStreamEvent>> CollectEventsAsync( | ||
| AgentFrameworkResponseHandler handler, | ||
| CreateResponse request, | ||
| ResponseContext context) | ||
| { | ||
| var events = new List<ResponseStreamEvent>(); | ||
| await foreach (var evt in handler.CreateAsync(request, context, CancellationToken.None)) | ||
| { | ||
| events.Add(evt); | ||
| } | ||
|
|
||
| return events; | ||
| } | ||
| } |
28 changes: 28 additions & 0 deletions
28
...et/tests/Microsoft.Agents.AI.Foundry.Hosting.UnitTests/FakeAuthenticationTokenProvider.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| // Copyright (c) Microsoft. All rights reserved. | ||
|
|
||
| using System; | ||
| using System.ClientModel; | ||
| using System.ClientModel.Primitives; | ||
| using System.Collections.Generic; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
|
|
||
| namespace Microsoft.Agents.AI.Foundry.Hosting.UnitTests; | ||
|
|
||
| internal sealed class FakeAuthenticationTokenProvider : AuthenticationTokenProvider | ||
| { | ||
| public override GetTokenOptions? CreateTokenOptions(IReadOnlyDictionary<string, object> properties) | ||
| { | ||
| return new GetTokenOptions(new Dictionary<string, object>()); | ||
| } | ||
|
|
||
| public override AuthenticationToken GetToken(GetTokenOptions options, CancellationToken cancellationToken) | ||
| { | ||
| return new AuthenticationToken("token-value", "token-type", DateTimeOffset.UtcNow.AddHours(1)); | ||
| } | ||
|
|
||
| public override ValueTask<AuthenticationToken> GetTokenAsync(GetTokenOptions options, CancellationToken cancellationToken) | ||
| { | ||
| return new ValueTask<AuthenticationToken>(this.GetToken(options, cancellationToken)); | ||
| } | ||
| } | ||
3 changes: 1 addition & 2 deletions
3
...s/Hosting/FoundryAIToolExtensionsTests.cs → ...UnitTests/FoundryAIToolExtensionsTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.