Files
agent-framework/dotnet/tests/Microsoft.Agents.Workflows.UnitTests/TestRunContext.cs
T
Jacob Alber e1147273da .NET: fix: FanIn Edge does not work (#541)
* fix: FanIn Edge does not work

We were not creating the state for FanIn edge in EdgeMap correctly, leading to crashes. After fixing that, it turns out the logic in FanInEdgeRunner was only forwarding the last message, not all of them.

* fix: Remove duplicate code and fix typo
2025-08-28 22:25:57 +00:00

77 lines
2.4 KiB
C#

// Copyright (c) Microsoft. All rights reserved.
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Agents.Workflows.Execution;
namespace Microsoft.Agents.Workflows.UnitTests;
public class TestRunContext : IRunnerContext
{
private sealed class BoundContext(string executorId, TestRunContext runnerContext) : IWorkflowContext
{
public ValueTask AddEventAsync(WorkflowEvent workflowEvent)
=> runnerContext.AddEventAsync(workflowEvent);
public ValueTask QueueClearScopeAsync(string? scopeName = null)
=> default;
public ValueTask QueueStateUpdateAsync<T>(string key, T? value, string? scopeName = null)
=> default;
public ValueTask<T?> ReadStateAsync<T>(string key, string? scopeName = null)
=> new(default(T?));
public ValueTask<HashSet<string>> ReadStateKeysAsync(string? scopeName = null)
=> new([]);
public ValueTask SendMessageAsync(object message, string? targetId = null)
=> runnerContext.SendMessageAsync(executorId, message, targetId);
}
public List<WorkflowEvent> Events { get; } = new();
public ValueTask AddEventAsync(WorkflowEvent workflowEvent)
{
this.Events.Add(workflowEvent);
return default;
}
public IWorkflowContext Bind(string executorId)
{
return new BoundContext(executorId, this);
}
public List<ExternalRequest> ExternalRequests { get; } = new();
public ValueTask PostAsync(ExternalRequest request)
{
this.ExternalRequests.Add(request);
return default;
}
internal Dictionary<string, List<MessageEnvelope>> QueuedMessages { get; } = new();
public ValueTask SendMessageAsync(string sourceId, object message, string? targetId = null)
{
if (!this.QueuedMessages.TryGetValue(sourceId, out List<MessageEnvelope>? deliveryQueue))
{
this.QueuedMessages[sourceId] = deliveryQueue = new();
}
deliveryQueue.Add(new(message, targetId: targetId));
return default;
}
StepContext IRunnerContext.Advance()
{
throw new NotImplementedException();
}
public Dictionary<string, Executor> Executors { get; } = new();
ValueTask<Executor> IRunnerContext.EnsureExecutorAsync(string executorId, IStepTracer? tracer)
{
return new(this.Executors[executorId]);
}
}