.NET: Durable extension: initial src and unit tests (#1900)

This commit is contained in:
Chris Gillum
2025-11-05 10:54:29 -08:00
committed by GitHub
parent 1d5677be11
commit 5686a009fb
40 changed files with 2810 additions and 0 deletions
@@ -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);
}
}
@@ -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>