using Xunit; using Moq; using FluentAssertions; using Microsoft.Extensions.Logging; using Haoliang.Core.Services; using Haoliang.Models.Device; using Haoliang.Models.Production; using Haoliang.Data.Repositories; namespace Haoliang.Tests; public class DataStorageServiceTests { private readonly Mock> _mockLogger; private readonly Mock _mockDeviceRepository; private readonly DataStorageService _dataStorageService; public DataStorageServiceTests() { _mockLogger = new Mock>(); _mockDeviceRepository = new Mock(); _dataStorageService = new DataStorageService( _mockLogger.Object, _mockDeviceRepository.Object); } [Fact] public async Task StoreDeviceDataAsync_CallsUpdateDeviceStatus() { var data = new ParsedDeviceData { DeviceId = 1, DeviceName = "Test Device", Timestamp = DateTime.Now }; _mockDeviceRepository.Setup(r => r.UpdateDeviceStatusAsync(1, true, true)).Returns(Task.CompletedTask); await _dataStorageService.StoreDeviceDataAsync(data); _mockDeviceRepository.Verify(r => r.UpdateDeviceStatusAsync(1, true, true), Times.Once); } [Fact] public async Task StoreDeviceDataAsync_CompletesSuccessfully() { var data = new ParsedDeviceData { DeviceId = 1, DeviceName = "Test" }; _mockDeviceRepository.Setup(r => r.UpdateDeviceStatusAsync(It.IsAny(), It.IsAny(), It.IsAny())).Returns(Task.CompletedTask); var action = async () => await _dataStorageService.StoreDeviceDataAsync(data); await action.Should().NotThrowAsync(); } [Fact] public async Task StoreDeviceDataBatchAsync_ProcessesAllDevices() { var dataList = new List { new ParsedDeviceData { DeviceId = 1, DeviceName = "Device 1" }, new ParsedDeviceData { DeviceId = 2, DeviceName = "Device 2" }, new ParsedDeviceData { DeviceId = 3, DeviceName = "Device 3" } }; _mockDeviceRepository.Setup(r => r.UpdateDeviceStatusAsync(It.IsAny(), It.IsAny(), It.IsAny())).Returns(Task.CompletedTask); await _dataStorageService.StoreDeviceDataBatchAsync(dataList); _mockDeviceRepository.Verify(r => r.UpdateDeviceStatusAsync(1, true, true), Times.Once); _mockDeviceRepository.Verify(r => r.UpdateDeviceStatusAsync(2, true, true), Times.Once); _mockDeviceRepository.Verify(r => r.UpdateDeviceStatusAsync(3, true, true), Times.Once); } [Fact] public async Task StoreDeviceDataBatchAsync_HandlesEmptyList() { var dataList = new List(); var action = async () => await _dataStorageService.StoreDeviceDataBatchAsync(dataList); await action.Should().NotThrowAsync(); } [Fact] public async Task StoreProductionRecordAsync_CompletesSuccessfully() { var record = new ProductionRecord { DeviceId = 1, ProgramName = "PROGRAM001", Quantity = 100, ProductionDate = DateTime.Now }; var action = async () => await _dataStorageService.StoreProductionRecordAsync(record); await action.Should().NotThrowAsync(); } }