xigau
This commit is contained in:
@@ -0,0 +1,128 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Moq;
|
||||
using OrpaonVision.Core.Results;
|
||||
using OrpaonVision.SiteApp.Runtime.Contracts;
|
||||
using OrpaonVision.SiteApp.Runtime.Services;
|
||||
|
||||
namespace OrpaonVision.SiteApp.Tests.Runtime.Services;
|
||||
|
||||
/// <summary>
|
||||
/// 运行时状态机服务单元测试。
|
||||
/// </summary>
|
||||
[TestClass]
|
||||
public class RuntimeStateMachineServiceTests
|
||||
{
|
||||
private IRuntimeStateMachineService _stateMachine = null!;
|
||||
private Mock<ILogger<IRuntimeStateMachineService>> _loggerMock = null!;
|
||||
|
||||
[TestInitialize]
|
||||
public void TestInitialize()
|
||||
{
|
||||
_loggerMock = new Mock<ILogger<IRuntimeStateMachineService>>();
|
||||
|
||||
// 使用简单状态机实现进行测试
|
||||
_stateMachine = new SimpleRuntimeStateMachineService(_loggerMock.Object);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void GetSnapshot_InitialState_ReturnsCorrectSnapshot()
|
||||
{
|
||||
// Act
|
||||
var snapshot = _stateMachine.GetSnapshot();
|
||||
|
||||
// Assert
|
||||
Assert.IsNotNull(snapshot);
|
||||
Assert.AreEqual(0, snapshot.CurrentLayer);
|
||||
Assert.AreEqual(0, snapshot.TotalLayers);
|
||||
Assert.IsTrue(snapshot.StateText.Contains("Idle") || snapshot.StateText.Contains("初始"));
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void MoveToNextLayer_FromInitialState_ReturnsSuccess()
|
||||
{
|
||||
// Act
|
||||
var result = _stateMachine.MoveToNextLayer();
|
||||
|
||||
// Assert
|
||||
Assert.IsTrue(result.Succeeded);
|
||||
var snapshot = _stateMachine.GetSnapshot();
|
||||
Assert.AreEqual(1, snapshot.CurrentLayer);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void MoveToNextLayer_SequentialCalls_ReturnsSuccess()
|
||||
{
|
||||
// Act & Assert - 第一层
|
||||
var result1 = _stateMachine.MoveToNextLayer();
|
||||
Assert.IsTrue(result1.Succeeded);
|
||||
var snapshot1 = _stateMachine.GetSnapshot();
|
||||
Assert.AreEqual(1, snapshot1.CurrentLayer);
|
||||
|
||||
// Act & Assert - 第二层
|
||||
var result2 = _stateMachine.MoveToNextLayer();
|
||||
Assert.IsTrue(result2.Succeeded);
|
||||
var snapshot2 = _stateMachine.GetSnapshot();
|
||||
Assert.AreEqual(2, snapshot2.CurrentLayer);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Reset_AfterProgress_ReturnsToInitialState()
|
||||
{
|
||||
// Arrange - 推进几层
|
||||
_stateMachine.MoveToNextLayer();
|
||||
_stateMachine.MoveToNextLayer();
|
||||
var snapshotBefore = _stateMachine.GetSnapshot();
|
||||
Assert.AreEqual(2, snapshotBefore.CurrentLayer);
|
||||
|
||||
// Act
|
||||
_stateMachine.Reset();
|
||||
|
||||
// Assert
|
||||
var snapshotAfter = _stateMachine.GetSnapshot();
|
||||
Assert.AreEqual(0, snapshotAfter.CurrentLayer);
|
||||
Assert.AreEqual(0, snapshotAfter.TotalLayers);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void StateTransitionFlow_CompleteWorkflow_ReturnsExpectedStates()
|
||||
{
|
||||
// Arrange & Act - 初始状态
|
||||
var initialSnapshot = _stateMachine.GetSnapshot();
|
||||
Assert.AreEqual(0, initialSnapshot.CurrentLayer);
|
||||
|
||||
// Act & Assert - 第一层
|
||||
var result1 = _stateMachine.MoveToNextLayer();
|
||||
Assert.IsTrue(result1.Succeeded);
|
||||
var snapshot1 = _stateMachine.GetSnapshot();
|
||||
Assert.AreEqual(1, snapshot1.CurrentLayer);
|
||||
|
||||
// Act & Assert - 第二层
|
||||
var result2 = _stateMachine.MoveToNextLayer();
|
||||
Assert.IsTrue(result2.Succeeded);
|
||||
var snapshot2 = _stateMachine.GetSnapshot();
|
||||
Assert.AreEqual(2, snapshot2.CurrentLayer);
|
||||
|
||||
// Act & Assert - 重置
|
||||
_stateMachine.Reset();
|
||||
var finalSnapshot = _stateMachine.GetSnapshot();
|
||||
Assert.AreEqual(0, finalSnapshot.CurrentLayer);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void GetSnapshot_AfterMultipleTransitions_ReturnsCorrectCurrentState()
|
||||
{
|
||||
// Arrange
|
||||
for (int i = 0; i < 5; i++)
|
||||
{
|
||||
_stateMachine.MoveToNextLayer();
|
||||
}
|
||||
|
||||
// Act
|
||||
var snapshot = _stateMachine.GetSnapshot();
|
||||
|
||||
// Assert
|
||||
Assert.AreEqual(5, snapshot.CurrentLayer);
|
||||
Assert.IsNotNull(snapshot.StateText);
|
||||
Assert.IsFalse(string.IsNullOrEmpty(snapshot.StateText));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user