mirror of
https://github.com/microsoft/agent-framework.git
synced 2026-06-16 21:04:09 +08:00
.NET: Durable extension: initial src and unit tests (#1900)
This commit is contained in:
committed by
GitHub
Unverified
parent
1d5677be11
commit
5686a009fb
@@ -0,0 +1,56 @@
|
||||
// Copyright (c) Microsoft. All rights reserved.
|
||||
|
||||
using Microsoft.DurableTask.Entities;
|
||||
|
||||
namespace Microsoft.Agents.AI.DurableTask.UnitTests;
|
||||
|
||||
public sealed class AgentSessionIdTests
|
||||
{
|
||||
[Fact]
|
||||
public void ParseValidSessionId()
|
||||
{
|
||||
const string Name = "test-agent";
|
||||
const string Key = "12345";
|
||||
string sessionIdString = $"@dafx-{Name}@{Key}";
|
||||
AgentSessionId sessionId = AgentSessionId.Parse(sessionIdString);
|
||||
|
||||
Assert.Equal(Name, sessionId.Name);
|
||||
Assert.Equal(Key, sessionId.Key);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ParseInvalidSessionId()
|
||||
{
|
||||
const string InvalidSessionIdString = "@test-agent@12345"; // Missing "dafx-" prefix
|
||||
Assert.Throws<ArgumentException>(() => AgentSessionId.Parse(InvalidSessionIdString));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void FromEntityId()
|
||||
{
|
||||
const string Name = "test-agent";
|
||||
const string Key = "12345";
|
||||
|
||||
EntityInstanceId entityId = new($"dafx-{Name}", Key);
|
||||
AgentSessionId sessionId = (AgentSessionId)entityId;
|
||||
|
||||
Assert.Equal(Name, sessionId.Name);
|
||||
Assert.Equal(Key, sessionId.Key);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void FromInvalidEntityId()
|
||||
{
|
||||
const string Name = "test-agent";
|
||||
const string Key = "12345";
|
||||
|
||||
EntityInstanceId entityId = new(Name, Key); // Missing "dafx-" prefix
|
||||
|
||||
Assert.Throws<ArgumentException>(() =>
|
||||
{
|
||||
// This assignment should throw an exception because
|
||||
// the entity ID is not a valid agent session ID.
|
||||
AgentSessionId sessionId = entityId;
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
// Copyright (c) Microsoft. All rights reserved.
|
||||
|
||||
using System.Text.Json;
|
||||
|
||||
namespace Microsoft.Agents.AI.DurableTask.UnitTests;
|
||||
|
||||
public sealed class DurableAgentThreadTests
|
||||
{
|
||||
[Fact]
|
||||
public void BuiltInSerialization()
|
||||
{
|
||||
AgentSessionId sessionId = AgentSessionId.WithRandomKey("test-agent");
|
||||
AgentThread thread = new DurableAgentThread(sessionId);
|
||||
|
||||
JsonElement serializedThread = thread.Serialize();
|
||||
|
||||
// Expected format: "{\"sessionId\":\"@dafx-test-agent@<random-key>\"}"
|
||||
string expectedSerializedThread = $"{{\"sessionId\":\"@dafx-{sessionId.Name}@{sessionId.Key}\"}}";
|
||||
Assert.Equal(expectedSerializedThread, serializedThread.ToString());
|
||||
|
||||
DurableAgentThread deserializedThread = DurableAgentThread.Deserialize(serializedThread);
|
||||
Assert.Equal(sessionId, deserializedThread.SessionId);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void STJSerialization()
|
||||
{
|
||||
AgentSessionId sessionId = AgentSessionId.WithRandomKey("test-agent");
|
||||
AgentThread thread = new DurableAgentThread(sessionId);
|
||||
|
||||
// Need to specify the type explicitly because STJ, unlike other serializers,
|
||||
// does serialization based on the static type of the object, not the runtime type.
|
||||
string serializedThread = JsonSerializer.Serialize(thread, typeof(DurableAgentThread));
|
||||
|
||||
// Expected format: "{\"sessionId\":\"@dafx-test-agent@<random-key>\"}"
|
||||
string expectedSerializedThread = $"{{\"sessionId\":\"@dafx-{sessionId.Name}@{sessionId.Key}\"}}";
|
||||
Assert.Equal(expectedSerializedThread, serializedThread);
|
||||
|
||||
DurableAgentThread? deserializedThread = JsonSerializer.Deserialize<DurableAgentThread>(serializedThread);
|
||||
Assert.NotNull(deserializedThread);
|
||||
Assert.Equal(sessionId, deserializedThread.SessionId);
|
||||
}
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFrameworks>$(ProjectsCoreTargetFrameworks)</TargetFrameworks>
|
||||
<TargetFrameworks Condition="'$(Configuration)' == 'Debug'">$(ProjectsDebugCoreTargetFrameworks)</TargetFrameworks>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\src\Microsoft.Agents.AI.DurableTask\Microsoft.Agents.AI.DurableTask.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
+95
@@ -0,0 +1,95 @@
|
||||
// Copyright (c) Microsoft. All rights reserved.
|
||||
|
||||
using Microsoft.Azure.Functions.Worker.Core.FunctionMetadata;
|
||||
using Microsoft.Extensions.Logging.Abstractions;
|
||||
|
||||
namespace Microsoft.Agents.AI.Hosting.AzureFunctions.UnitTests;
|
||||
|
||||
public sealed class DurableAgentFunctionMetadataTransformerTests
|
||||
{
|
||||
[Theory]
|
||||
[InlineData(0)] // Empty original metadata list
|
||||
[InlineData(3)] // Non-empty original metadata list
|
||||
public void Transform_AddsAgentAndHttpTriggers_ForEachAgent(int initialMetadataEntryCount)
|
||||
{
|
||||
Dictionary<string, Func<IServiceProvider, AIAgent>> agents = new()
|
||||
{
|
||||
{ "testAgent", _ => null! }
|
||||
};
|
||||
DurableAgentFunctionMetadataTransformer transformer = new(agents, GetTestLogger());
|
||||
List<IFunctionMetadata> metadataList = BuildFunctionMetadataList(initialMetadataEntryCount);
|
||||
|
||||
transformer.Transform(metadataList);
|
||||
|
||||
Assert.Equal(initialMetadataEntryCount + 2, metadataList.Count); // each agent adds 2 functions (http + entity).
|
||||
|
||||
DefaultFunctionMetadata agentTrigger = Assert.IsType<DefaultFunctionMetadata>(metadataList[initialMetadataEntryCount]);
|
||||
Assert.Equal("dafx-testAgent", agentTrigger.Name);
|
||||
Assert.Equal("dotnet-isolated", agentTrigger.Language);
|
||||
Assert.Contains("type\":\"entityTrigger", agentTrigger.RawBindings![0]);
|
||||
|
||||
DefaultFunctionMetadata httpTrigger = Assert.IsType<DefaultFunctionMetadata>(metadataList[initialMetadataEntryCount + 1]);
|
||||
Assert.Equal("testAgent_http", httpTrigger.Name);
|
||||
Assert.Equal("dotnet-isolated", httpTrigger.Language);
|
||||
Assert.Contains("type\":\"httpTrigger", httpTrigger.RawBindings![0]);
|
||||
Assert.Contains("route\":\"agents/testAgent/run", httpTrigger.RawBindings[0]);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Transform_AddsTriggers_ForMultipleAgents()
|
||||
{
|
||||
Dictionary<string, Func<IServiceProvider, AIAgent>> agents = new()
|
||||
{
|
||||
{ "agentA", _ => null! },
|
||||
{ "agentB", _ => null! },
|
||||
{ "agentC", _ => null! }
|
||||
};
|
||||
DurableAgentFunctionMetadataTransformer transformer = new(agents, GetTestLogger());
|
||||
const int InitialMetadataEntryCount = 2;
|
||||
List<IFunctionMetadata> metadataList = BuildFunctionMetadataList(InitialMetadataEntryCount);
|
||||
|
||||
transformer.Transform(metadataList);
|
||||
|
||||
Assert.Equal(InitialMetadataEntryCount + (agents.Count * 2), metadataList.Count);
|
||||
|
||||
foreach (string agentName in agents.Keys)
|
||||
{
|
||||
// The agent's entity trigger name is prefixed with "dafx-"
|
||||
DefaultFunctionMetadata entityMeta =
|
||||
Assert.IsType<DefaultFunctionMetadata>(
|
||||
Assert.Single(metadataList, m => m.Name == "dafx-" + agentName));
|
||||
Assert.NotNull(entityMeta.RawBindings);
|
||||
Assert.Contains("entityTrigger", entityMeta.RawBindings[0]);
|
||||
|
||||
DefaultFunctionMetadata httpMeta =
|
||||
Assert.IsType<DefaultFunctionMetadata>(
|
||||
Assert.Single(metadataList, m => m.Name == agentName + "_http"));
|
||||
Assert.NotNull(httpMeta.RawBindings);
|
||||
Assert.Contains("httpTrigger", httpMeta.RawBindings[0]);
|
||||
Assert.Contains($"agents/{agentName}/run", httpMeta.RawBindings[0]);
|
||||
}
|
||||
}
|
||||
|
||||
private static List<IFunctionMetadata> BuildFunctionMetadataList(int numberOfFunctions)
|
||||
{
|
||||
List<IFunctionMetadata> list = [];
|
||||
for (int i = 0; i < numberOfFunctions; i++)
|
||||
{
|
||||
list.Add(new DefaultFunctionMetadata
|
||||
{
|
||||
Language = "dotnet-isolated",
|
||||
Name = $"SingleAgentOrchestration{i + 1}",
|
||||
EntryPoint = "MyApp.Functions.SingleAgentOrchestration",
|
||||
RawBindings =
|
||||
[
|
||||
"{\r\n \"name\": \"context\",\r\n \"direction\": \"In\",\r\n \"type\": \"orchestrationTrigger\",\r\n \"properties\": {}\r\n }"
|
||||
],
|
||||
ScriptFile = "MyApp.dll"
|
||||
});
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
private static NullLogger<DurableAgentFunctionMetadataTransformer> GetTestLogger() => new();
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFrameworks>$(ProjectsCoreTargetFrameworks)</TargetFrameworks>
|
||||
<TargetFrameworks Condition="'$(Configuration)' == 'Debug'">$(ProjectsDebugCoreTargetFrameworks)</TargetFrameworks>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\src\Microsoft.Agents.AI.Hosting.AzureFunctions\Microsoft.Agents.AI.Hosting.AzureFunctions.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
Reference in New Issue
Block a user