mirror of
https://github.com/microsoft/agent-framework.git
synced 2026-06-16 21:04:09 +08:00
fb97e93a01
* Foundry.Hosting.UnitTests: extract project from Foundry.UnitTests Move all Hosting/* tests, three toolbox TestData JSONs, and the FakeAuthenticationTokenProvider/HttpHandlerAssert/TestDataUtil helpers (trimmed to toolbox getters) into a new Microsoft.Agents.AI.Foundry.Hosting.UnitTests project. Add it to the slnx and grant the new assembly InternalsVisibleTo from Microsoft.Agents.AI.Foundry and Microsoft.Agents.AI.Foundry.Hosting. * Foundry.Hosting.UnitTests: align namespaces to assembly name Rename namespaces from Microsoft.Agents.AI.Foundry.UnitTests(.Hosting) to Microsoft.Agents.AI.Foundry.Hosting.UnitTests across all moved tests, the duplicated helpers, and the trimmed TestDataUtil. Also fixes the prior namespace inconsistency in FoundryToolboxTests. * Foundry.Hosting.UnitTests: split WorkflowIntegrationTests by SUT Replace the WorkflowIntegrationTests file (an IT-named file inside a UT project) with two SUT-focused files plus a shared test-doubles file: - AgentFrameworkResponseHandlerWorkflowTests.cs - the 5 handler-driven tests that exercise AgentFrameworkResponseHandler with a real workflow agent. - OutputConverterWorkflowTests.cs - the 5 OutputConverter tests driven by hand-crafted update sequences mirroring real workflow patterns. - WorkflowTestAgents.cs - StreamingTextAgent and ThrowingStreamingAgent extracted as internal types used by both files. * Foundry.UnitTests: trim Hosting-related conditionals and dead testdata Now that Hosting tests live in their own project: - drop the Compile Remove guard for the Hosting subfolder, - drop the .NETCoreApp-only PackageReferences (Azure.AI.AgentServer.Responses, Microsoft.AspNetCore.TestHost, OpenTelemetry, OpenTelemetry.Exporter.InMemory), - drop the conditional ProjectReference to Microsoft.Agents.AI.Foundry.Hosting, - delete the three Toolbox JSON files and the matching Toolbox getters in TestDataUtil. * Foundry.Hosting.UnitTests: drop redundant 'using Microsoft.Agents.AI.Foundry.Hosting' The new project namespace is Microsoft.Agents.AI.Foundry.Hosting.UnitTests, which already brings the parent Microsoft.Agents.AI.Foundry.Hosting namespace into scope. The explicit using statement is therefore redundant (IDE0005). Caught by 'dotnet format --verify-no-changes' running on Linux against the .NET 10 SDK. * Foundry.Hosting: drop InternalsVisibleTo to Foundry.UnitTests The non-hosting Foundry.UnitTests project no longer holds any Hosting tests after the split, so it doesn't need access to internal types in Microsoft.Agents.AI.Foundry.Hosting. Only Microsoft.Agents.AI.Foundry.Hosting.UnitTests needs it. * Foundry.Hosting: rename DelegatingResponsesClient to UserAgentResponsesClient Address westey-m's review feedback on PR #5453: `Delegating*` is conventionally reserved for inheritable base classes (mirroring `DelegatingHandler`) where consumers override one or two members. This polyfill is sealed and only injects the User-Agent supplement, so the new name reflects its actual purpose. Renamed via `git mv` to preserve history: * `src/Microsoft.Agents.AI.Foundry.Hosting/DelegatingResponsesClient.cs` to `UserAgentResponsesClient.cs` * `tests/Microsoft.Agents.AI.Foundry.Hosting.UnitTests/DelegatingResponsesClientTests.cs` to `UserAgentResponsesClientTests.cs` Class, constructor, and all references updated across: * `src/.../UserAgentResponsesClient.cs` (class + constructor + internal log message) * `src/.../ServiceCollectionExtensions.cs` (cref + type check + instantiation) * `src/.../HostedAgentUserAgentPolicy.cs` (cref) * `tests/Foundry.UnitTests/RequestOptionsExtensionsTests.cs` (comment) * `tests/Foundry.Hosting.UnitTests/UserAgentResponsesClientTests.cs` (class + cref + instantiations)
166 lines
7.5 KiB
C#
166 lines
7.5 KiB
C#
// Copyright (c) Microsoft. All rights reserved.
|
|
|
|
using System.ClientModel.Primitives;
|
|
using System.IO;
|
|
using Azure.AI.Projects.Agents;
|
|
|
|
namespace Microsoft.Agents.AI.Foundry.UnitTests;
|
|
|
|
/// <summary>
|
|
/// Utility class for loading and processing test data files.
|
|
/// </summary>
|
|
internal static class TestDataUtil
|
|
{
|
|
private static readonly string s_agentResponseJson = File.ReadAllText("TestData/AgentResponse.json");
|
|
private static readonly string s_agentVersionResponseJson = File.ReadAllText("TestData/AgentVersionResponse.json");
|
|
private static readonly string s_openAIDefaultResponseJson = File.ReadAllText("TestData/OpenAIDefaultResponse.json");
|
|
|
|
private const string AgentDefinitionPlaceholder = "\"agent-definition-placeholder\"";
|
|
|
|
private const string DefaultAgentDefinition = """
|
|
{
|
|
"kind": "prompt",
|
|
"model": "gpt-5-mini",
|
|
"instructions": "You are a storytelling agent. You craft engaging one-line stories based on user prompts and context.",
|
|
"tools": []
|
|
}
|
|
""";
|
|
|
|
/// <summary>
|
|
/// Gets the agent response JSON with optional placeholder replacements applied.
|
|
/// </summary>
|
|
public static string GetAgentResponseJson(string? agentName = null, ProjectsAgentDefinition? agentDefinition = null, string? instructions = null, string? description = null)
|
|
{
|
|
var json = s_agentResponseJson;
|
|
json = ApplyAgentName(json, agentName);
|
|
json = ApplyAgentDefinition(json, agentDefinition);
|
|
json = ApplyInstructions(json, instructions);
|
|
json = ApplyDescription(json, description);
|
|
return json;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets the agent version response JSON with optional placeholder replacements applied.
|
|
/// </summary>
|
|
public static string GetAgentVersionResponseJson(string? agentName = null, ProjectsAgentDefinition? agentDefinition = null, string? instructions = null, string? description = null)
|
|
{
|
|
var json = s_agentVersionResponseJson;
|
|
json = ApplyAgentName(json, agentName);
|
|
json = ApplyAgentDefinition(json, agentDefinition);
|
|
json = ApplyInstructions(json, instructions);
|
|
json = ApplyDescription(json, description);
|
|
return json;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets the agent version response JSON with empty version and ID fields for testing hosted agents like MCP agents.
|
|
/// </summary>
|
|
public static string GetAgentVersionResponseJsonWithEmptyVersion(string? agentName = null, ProjectsAgentDefinition? agentDefinition = null, string? instructions = null, string? description = null)
|
|
{
|
|
var json = s_agentVersionResponseJson;
|
|
json = ApplyAgentName(json, agentName);
|
|
json = ApplyAgentDefinition(json, agentDefinition);
|
|
json = ApplyInstructions(json, instructions);
|
|
json = ApplyDescription(json, description);
|
|
// Remove the version and id fields to simulate hosted agents without version
|
|
json = json.Replace("\"version\": \"1\",", "\"version\": \"\",")
|
|
.Replace("\"id\": \"agent_abc123:1\",", "\"id\": \"\",");
|
|
return json;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets the agent response JSON with empty version and ID fields in the latest version for testing hosted agents like MCP agents.
|
|
/// </summary>
|
|
public static string GetAgentResponseJsonWithEmptyVersion(string? agentName = null, ProjectsAgentDefinition? agentDefinition = null, string? instructions = null, string? description = null)
|
|
{
|
|
var json = s_agentResponseJson;
|
|
json = ApplyAgentName(json, agentName);
|
|
json = ApplyAgentDefinition(json, agentDefinition);
|
|
json = ApplyInstructions(json, instructions);
|
|
json = ApplyDescription(json, description);
|
|
// Remove the version and id fields to simulate hosted agents without version
|
|
json = json.Replace("\"version\": \"1\",", "\"version\": \"\",")
|
|
.Replace("\"id\": \"agent_abc123:1\",", "\"id\": \"\",");
|
|
return json;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets the agent version response JSON with whitespace-only version and ID fields for testing hosted agents like MCP agents.
|
|
/// </summary>
|
|
public static string GetAgentVersionResponseJsonWithWhitespaceVersion(string? agentName = null, ProjectsAgentDefinition? agentDefinition = null, string? instructions = null, string? description = null)
|
|
{
|
|
var json = s_agentVersionResponseJson;
|
|
json = ApplyAgentName(json, agentName);
|
|
json = ApplyAgentDefinition(json, agentDefinition);
|
|
json = ApplyInstructions(json, instructions);
|
|
json = ApplyDescription(json, description);
|
|
// Use whitespace-only version and id fields to simulate hosted agents without version
|
|
return json
|
|
.Replace("\"version\": \"1\",", "\"version\": \" \",")
|
|
.Replace("\"id\": \"agent_abc123:1\",", "\"id\": \" \",");
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets the agent response JSON with whitespace-only version and ID fields in the latest version for testing hosted agents like MCP agents.
|
|
/// </summary>
|
|
public static string GetAgentResponseJsonWithWhitespaceVersion(string? agentName = null, ProjectsAgentDefinition? agentDefinition = null, string? instructions = null, string? description = null)
|
|
{
|
|
var json = s_agentResponseJson;
|
|
json = ApplyAgentName(json, agentName);
|
|
json = ApplyAgentDefinition(json, agentDefinition);
|
|
json = ApplyInstructions(json, instructions);
|
|
json = ApplyDescription(json, description);
|
|
// Use whitespace-only version and id fields to simulate hosted agents without version
|
|
return json
|
|
.Replace("\"version\": \"1\",", "\"version\": \" \",")
|
|
.Replace("\"id\": \"agent_abc123:1\",", "\"id\": \" \",");
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets the OpenAI default response JSON with optional placeholder replacements applied.
|
|
/// </summary>
|
|
public static string GetOpenAIDefaultResponseJson(string? agentName = null, ProjectsAgentDefinition? agentDefinition = null, string? instructions = null, string? description = null)
|
|
{
|
|
var json = s_openAIDefaultResponseJson;
|
|
json = ApplyAgentName(json, agentName);
|
|
json = ApplyAgentDefinition(json, agentDefinition);
|
|
json = ApplyInstructions(json, instructions);
|
|
json = ApplyDescription(json, description);
|
|
return json;
|
|
}
|
|
|
|
private static string ApplyAgentName(string json, string? agentName)
|
|
{
|
|
if (!string.IsNullOrEmpty(agentName))
|
|
{
|
|
return json.Replace("\"agent_abc123\"", $"\"{agentName}\"");
|
|
}
|
|
return json;
|
|
}
|
|
|
|
private static string ApplyAgentDefinition(string json, ProjectsAgentDefinition? definition)
|
|
{
|
|
return (definition is not null)
|
|
? json.Replace(AgentDefinitionPlaceholder, ModelReaderWriter.Write(definition).ToString())
|
|
: json.Replace(AgentDefinitionPlaceholder, DefaultAgentDefinition);
|
|
}
|
|
|
|
private static string ApplyInstructions(string json, string? instructions)
|
|
{
|
|
if (!string.IsNullOrEmpty(instructions))
|
|
{
|
|
return json.Replace("You are a storytelling agent. You craft engaging one-line stories based on user prompts and context.", instructions);
|
|
}
|
|
return json;
|
|
}
|
|
|
|
private static string ApplyDescription(string json, string? description)
|
|
{
|
|
if (!string.IsNullOrEmpty(description))
|
|
{
|
|
return json.Replace("\"description\": \"\"", $"\"description\": \"{description}\"");
|
|
}
|
|
return json;
|
|
}
|
|
}
|