using System; using Xunit; using ThreatSource.Simulation; using ThreatSource.Tests.Simulation; namespace ThreatSource.Tests.Simulation { public class SimulationManagerTests { private readonly SimulationManager _simulationManager; private readonly TestSimulationAdapter _testAdapter; public SimulationManagerTests() { _simulationManager = new SimulationManager(); _testAdapter = new TestSimulationAdapter(_simulationManager); _simulationManager.SetSimulationAdapter(_testAdapter); } [Fact] public void TestEventPublishing() { // Arrange _testAdapter.ClearEvents(); var missileEvent = new MissileFireEvent { SenderId = "missile1", TargetId = "target1", Timestamp = 1.0 }; // Act _simulationManager.PublishEvent(missileEvent); // Assert var publishedEvents = _testAdapter.GetPublishedEvents(); Assert.Single(publishedEvents); var publishedEvent = Assert.IsType(publishedEvents[0]); Assert.Equal("missile1", publishedEvent.SenderId); Assert.Equal("target1", publishedEvent.TargetId); } [Fact] public void TestEntityManagement() { // Arrange var testEntity = new { Id = "test1", Name = "TestEntity" }; _testAdapter.AddTestEntity("external1", new { Id = "external1", Name = "ExternalEntity" }); // Act _simulationManager.RegisterEntity("test1", testEntity); // Assert // 测试内部实体获取 var internalEntity = _simulationManager.GetEntityById("test1"); Assert.NotNull(internalEntity); Assert.Equal("TestEntity", (internalEntity as dynamic).Name); // 测试外部实体获取 var externalEntity = _simulationManager.GetEntityById("external1"); Assert.NotNull(externalEntity); Assert.Equal("ExternalEntity", (externalEntity as dynamic).Name); } [Fact] public void TestEventSubscriptionAndUnsubscription() { // Arrange int eventCount = 0; Action handler = _ => eventCount++; // Act _simulationManager.SubscribeToEvent(handler); _simulationManager.PublishEvent(new MissileFireEvent()); _simulationManager.UnsubscribeFromEvent(handler); _simulationManager.PublishEvent(new MissileFireEvent()); // Assert Assert.Equal(1, eventCount); } [Fact] public void TestExternalEventReception() { // Arrange var externalEvent = new LaserIlluminationEvent { LaserDesignatorId = "laser1", TargetId = "target1" }; bool eventReceived = false; _simulationManager.SubscribeToEvent(evt => { eventReceived = true; Assert.Equal("laser1", evt.LaserDesignatorId); }); // Act _testAdapter.ReceiveFromExternalSimulation(externalEvent); // Assert Assert.True(eventReceived); } [Fact] public void TestNullEventPublishing() { // Arrange MissileFireEvent? nullEvent = null; // Act & Assert Assert.Throws(() => _simulationManager.PublishEvent(nullEvent)); } [Fact] public void TestNullEventExternalPublishing() { // Arrange MissileFireEvent? nullEvent = null; // Act & Assert Assert.Throws(() => _testAdapter.PublishToExternalSimulation(nullEvent)); } [Fact] public void TestNullEventExternalReceiving() { // Arrange MissileFireEvent? nullEvent = null; // Act & Assert Assert.Throws(() => _testAdapter.ReceiveFromExternalSimulation(nullEvent)); } [Fact] public void TestUnregisterEntity() { // Arrange var testEntity = new { Id = "test1", Name = "TestEntity" }; _simulationManager.RegisterEntity("test1", testEntity); // Act bool unregistered = _simulationManager.UnregisterEntity("test1"); bool notFound = _simulationManager.UnregisterEntity("nonexistent"); // Assert Assert.True(unregistered); Assert.False(notFound); Assert.Null(_simulationManager.GetEntityById("test1")); } [Fact] public void TestGetEntitiesByType() { // Arrange var testEntity1 = new TestEntity { Id = "test1" }; var testEntity2 = new TestEntity { Id = "test2" }; var differentEntity = new { Id = "diff1" }; _simulationManager.RegisterEntity("test1", testEntity1); _simulationManager.RegisterEntity("test2", testEntity2); _simulationManager.RegisterEntity("diff1", differentEntity); // Act var entities = _simulationManager.GetEntitiesByType(); // Assert Assert.Equal(2, entities.Count); Assert.Contains(testEntity1, entities); Assert.Contains(testEntity2, entities); } [Fact] public void TestGetAllEntities() { // Arrange var entity1 = new { Id = "test1" }; var entity2 = new { Id = "test2" }; _simulationManager.RegisterEntity("test1", entity1); _simulationManager.RegisterEntity("test2", entity2); // Act var allEntities = _simulationManager.GetAllEntities(); // Assert Assert.Equal(2, allEntities.Count); Assert.Contains(entity1, allEntities); Assert.Contains(entity2, allEntities); } [Fact] public void TestGetSimulationAdapter() { // Act var adapter = _simulationManager.GetSimulationAdapter(); // Assert Assert.NotNull(adapter); Assert.Same(_testAdapter, adapter); } [Fact] public void TestRegisterEntityWithInvalidInput() { // Arrange & Act & Assert Assert.False(_simulationManager.RegisterEntity("", new object())); Assert.False(_simulationManager.RegisterEntity("test", null!)); } [Fact] public void TestConcurrentEntityOperations() { // Arrange var entity = new { Id = "test1" }; // Act & Assert Assert.True(_simulationManager.RegisterEntity("test1", entity)); Assert.False(_simulationManager.RegisterEntity("test1", entity)); // 重复注册 Assert.True(_simulationManager.UnregisterEntity("test1")); Assert.True(_simulationManager.RegisterEntity("test1", entity)); // 重新注册 } [Fact] public void TestEventHandlerException_ContinuesExecution() { // Arrange var missileEvent = new MissileFireEvent { SenderId = "missile1", TargetId = "target1" }; bool firstHandlerCalled = false; bool secondHandlerCalled = false; _simulationManager.SubscribeToEvent(_ => { firstHandlerCalled = true; throw new Exception("测试异常"); }); _simulationManager.SubscribeToEvent(_ => { secondHandlerCalled = true; }); // Act - 不应抛出异常 _simulationManager.PublishEvent(missileEvent); // Assert - 两个处理器都应该被调用 Assert.True(firstHandlerCalled); Assert.True(secondHandlerCalled); } // 用于测试的辅助类 private class TestEntity { public string Id { get; set; } = ""; } } }