using Managing.Application.Abstractions; using Managing.Application.Abstractions.Repositories; using Managing.Application.Abstractions.Services; using Managing.Application.Bots.Grains; using Managing.Application.Bots.Models; using Managing.Domain.Bots; using Managing.Domain.Statistics; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Moq; using Xunit; namespace Managing.Application.Tests; public class AgentGrainTests { private readonly Mock> _mockState; private readonly Mock> _mockLogger; private readonly Mock _mockBotService; private readonly Mock _mockAgentService; private readonly Mock _mockExchangeService; private readonly Mock _mockUserService; private readonly Mock _mockAccountService; private readonly Mock _mockTradingService; private readonly Mock _mockScopeFactory; private readonly Mock _mockAgentBalanceRepository; public AgentGrainTests() { _mockState = new Mock>(); _mockLogger = new Mock>(); _mockBotService = new Mock(); _mockAgentService = new Mock(); _mockExchangeService = new Mock(); _mockUserService = new Mock(); _mockAccountService = new Mock(); _mockTradingService = new Mock(); _mockScopeFactory = new Mock(); _mockAgentBalanceRepository = new Mock(); // Setup default state _mockState.Setup(x => x.State).Returns(new AgentGrainState { AgentName = "TestAgent", BotIds = new HashSet { Guid.NewGuid() } }); } [Fact] public async Task RegisterBotAsync_ShouldUpdateSummary() { // Arrange var agentGrain = CreateAgentGrain(); var newBotId = Guid.NewGuid(); // Setup mocks _mockBotService.Setup(x => x.GetBotsByIdsAsync(It.IsAny>())) .ReturnsAsync(new List()); _mockAgentService.Setup(x => x.SaveOrUpdateAgentSummary(It.IsAny())) .Returns(Task.CompletedTask); // Act await agentGrain.RegisterBotAsync(newBotId); // Assert _mockAgentService.Verify(x => x.SaveOrUpdateAgentSummary(It.IsAny()), Times.Once); } [Fact] public async Task UnregisterBotAsync_ShouldUpdateSummary() { // Arrange var agentGrain = CreateAgentGrain(); var botId = _mockState.Object.State.BotIds.First(); // Setup mocks _mockBotService.Setup(x => x.GetBotsByIdsAsync(It.IsAny>())) .ReturnsAsync(new List()); _mockAgentService.Setup(x => x.SaveOrUpdateAgentSummary(It.IsAny())) .Returns(Task.CompletedTask); // Act await agentGrain.UnregisterBotAsync(botId); // Assert _mockAgentService.Verify(x => x.SaveOrUpdateAgentSummary(It.IsAny()), Times.Once); } private AgentGrain CreateAgentGrain() { return new AgentGrain( _mockState.Object, _mockLogger.Object, _mockBotService.Object, _mockAgentService.Object, _mockExchangeService.Object, _mockUserService.Object, _mockAccountService.Object, _mockTradingService.Object, _mockAgentBalanceRepository.Object, _mockScopeFactory.Object); } }