// Copyright (c) Microsoft Corporation. All rights reserved. // MessagingTestFixture.cs using Microsoft.AutoGen.Contracts; namespace Microsoft.AutoGen.Core.Tests; public sealed class MessagingTestFixture { private Dictionary AgentsTypeMap { get; } = new(); public InProcessRuntime Runtime { get; private set; } = new(); public ValueTask RegisterFactoryMapInstances(AgentType type, Func> factory) where TAgent : IHostableAgent { Func> wrappedFactory = async (id, runtime) => { TAgent agent = await factory(id, runtime); this.GetAgentInstances()[id] = agent; return agent; }; return this.Runtime.RegisterAgentFactoryAsync(type, wrappedFactory); } public ValueTask RegisterDefaultSubscriptions(AgentType type) where TAgentType : IHostableAgent { return this.Runtime.RegisterImplicitAgentSubscriptionsAsync(type); } public Dictionary GetAgentInstances() where TAgent : IHostableAgent { if (!AgentsTypeMap.TryGetValue(typeof(TAgent), out object? maybeAgentMap) || maybeAgentMap is not Dictionary result) { this.AgentsTypeMap[typeof(TAgent)] = result = new Dictionary(); } return result; } public async ValueTask RunSendTestAsync(AgentId sendTarget, object message, string? messageId = null) { messageId ??= Guid.NewGuid().ToString(); await this.Runtime.StartAsync(); object? result = await this.Runtime.SendMessageAsync(message, sendTarget, messageId: messageId); await this.Runtime.RunUntilIdleAsync(); return result; } public async ValueTask RunPublishTestAsync(TopicId sendTarget, object message, string? messageId = null) { messageId ??= Guid.NewGuid().ToString(); await this.Runtime.StartAsync(); await this.Runtime.PublishMessageAsync(message, sendTarget, messageId: messageId); await this.Runtime.RunUntilIdleAsync(); } }