.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
Unverified
parent 1d5677be11
commit 5686a009fb
40 changed files with 2810 additions and 0 deletions
@@ -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);
}
}