ThreatSourceLibaray/ThreatSource.Tests/src/Guidance/LaserSemiActiveGuidanceSystemCodeTests.cs

233 lines
8.8 KiB
C#

using Xunit;
using ThreatSource.Guidance;
using ThreatSource.Simulation;
using ThreatSource.Utils;
using ThreatSource.Simulation.Testing;
namespace ThreatSource.Tests.Guidance
{
public class LaserSemiActiveGuidanceSystemCodeTests
{
private readonly SimulationManager _simulationManager;
private readonly TestSimulationAdapter _testAdapter;
private readonly LaserSemiActiveGuidanceSystem _guidanceSystem;
public LaserSemiActiveGuidanceSystemCodeTests()
{
_simulationManager = new SimulationManager();
_testAdapter = new TestSimulationAdapter(_simulationManager);
_simulationManager.SetSimulationAdapter(_testAdapter);
_guidanceSystem = new LaserSemiActiveGuidanceSystem(
"guidance1",
50, // maxAcceleration
3, // guidanceCoefficient
_simulationManager
);
_guidanceSystem.ParentId = "missile1";
_simulationManager.RegisterEntity("guidance1", _guidanceSystem);
}
[Fact]
public void SetExpectedLaserCode_SetsCodeCorrectly()
{
// Act
_guidanceSystem.SetExpectedLaserCode(LaserCodeType.PRF, 1234);
// Assert - We can't directly check the private field, but we can test the behavior
// This will be tested in the ProcessLaserIlluminationEvent test
}
[Fact]
public void AddExpectedCodeParameter_AddsParameterCorrectly()
{
// Arrange
_guidanceSystem.SetExpectedLaserCode(LaserCodeType.PPM, 5678);
// Act
_guidanceSystem.AddExpectedCodeParameter("PulseWidth", 0.001);
// Assert - We can't directly check the private field, but we can test the behavior
// This will be tested in the ProcessLaserIlluminationEvent test
}
[Fact]
public void SetCodeMatchRequired_SetsRequirementCorrectly()
{
// Act
_guidanceSystem.SetCodeMatchRequired(true);
// Assert - We can't directly check the private field, but we can test the behavior
// This will be tested in the ProcessLaserIlluminationEvent test
}
[Fact]
public void ProcessLaserIlluminationEvent_WithMatchingCode_PublishesMatchEvent()
{
// Arrange
_guidanceSystem.SetExpectedLaserCode(LaserCodeType.PRF, 1234);
_guidanceSystem.SetCodeMatchRequired(true);
var illuminationEvent = new LaserIlluminationStartEvent
{
LaserDesignatorId = "laser1",
TargetId = "target1",
IsCodeEnabled = true,
LaserCode = new LaserCode
{
CodeType = LaserCodeType.PRF,
CodeValue = 1234
}
};
// Act
_guidanceSystem.ProcessLaserIlluminationEvent(illuminationEvent);
// Assert
var matchEvents = _testAdapter.GetPublishedEvents<LaserCodeMatchEvent>();
Assert.NotEmpty(matchEvents);
var lastEvent = matchEvents[matchEvents.Count - 1];
Assert.Equal("missile1", lastEvent.MissileId);
Assert.Equal("laser1", lastEvent.DesignatorId);
Assert.NotNull(lastEvent.MatchedCode);
Assert.Equal(LaserCodeType.PRF, lastEvent.MatchedCode.CodeType);
Assert.Equal(1234, lastEvent.MatchedCode.CodeValue);
}
[Fact]
public void ProcessLaserIlluminationEvent_WithMismatchingCode_PublishesMismatchEvent()
{
// Arrange
_guidanceSystem.SetExpectedLaserCode(LaserCodeType.PRF, 1234);
_guidanceSystem.SetCodeMatchRequired(true);
var illuminationEvent = new LaserIlluminationStartEvent
{
LaserDesignatorId = "laser1",
TargetId = "target1",
IsCodeEnabled = true,
LaserCode = new LaserCode
{
CodeType = LaserCodeType.PRF,
CodeValue = 5678 // Different code value
}
};
// Act
_guidanceSystem.ProcessLaserIlluminationEvent(illuminationEvent);
// Assert
var mismatchEvents = _testAdapter.GetPublishedEvents<LaserCodeMismatchEvent>();
Assert.NotEmpty(mismatchEvents);
var lastEvent = mismatchEvents[mismatchEvents.Count - 1];
Assert.Equal("missile1", lastEvent.MissileId);
Assert.Equal("laser1", lastEvent.DesignatorId);
Assert.NotNull(lastEvent.ExpectedCode);
Assert.Equal(LaserCodeType.PRF, lastEvent.ExpectedCode.CodeType);
Assert.Equal(1234, lastEvent.ExpectedCode.CodeValue);
Assert.NotNull(lastEvent.ReceivedCode);
Assert.Equal(LaserCodeType.PRF, lastEvent.ReceivedCode.CodeType);
Assert.Equal(5678, lastEvent.ReceivedCode.CodeValue);
}
[Fact]
public void ProcessLaserIlluminationEvent_WithDifferentCodeType_PublishesMismatchEvent()
{
// Arrange
_guidanceSystem.SetExpectedLaserCode(LaserCodeType.PRF, 1234);
_guidanceSystem.SetCodeMatchRequired(true);
var illuminationEvent = new LaserIlluminationStartEvent
{
LaserDesignatorId = "laser1",
TargetId = "target1",
IsCodeEnabled = true,
LaserCode = new LaserCode
{
CodeType = LaserCodeType.PPM, // Different code type
CodeValue = 1234
}
};
// Act
_guidanceSystem.ProcessLaserIlluminationEvent(illuminationEvent);
// Assert
var mismatchEvents = _testAdapter.GetPublishedEvents<LaserCodeMismatchEvent>();
Assert.NotEmpty(mismatchEvents);
var lastEvent = mismatchEvents[mismatchEvents.Count - 1];
Assert.Equal("missile1", lastEvent.MissileId);
Assert.Equal("laser1", lastEvent.DesignatorId);
Assert.NotNull(lastEvent.ExpectedCode);
Assert.Equal(LaserCodeType.PRF, lastEvent.ExpectedCode.CodeType);
Assert.Equal(1234, lastEvent.ExpectedCode.CodeValue);
Assert.NotNull(lastEvent.ReceivedCode);
Assert.Equal(LaserCodeType.PPM, lastEvent.ReceivedCode.CodeType);
Assert.Equal(1234, lastEvent.ReceivedCode.CodeValue);
}
[Fact]
public void ProcessLaserIlluminationEvent_WithCodeDisabled_IgnoresCode()
{
// Arrange
_guidanceSystem.SetExpectedLaserCode(LaserCodeType.PRF, 1234);
_guidanceSystem.SetCodeMatchRequired(false); // Not required
var illuminationEvent = new LaserIlluminationStartEvent
{
LaserDesignatorId = "laser1",
TargetId = "target1",
IsCodeEnabled = false, // Code disabled
LaserCode = new LaserCode
{
CodeType = LaserCodeType.PRF,
CodeValue = 5678 // Different code value
}
};
// Act
_guidanceSystem.ProcessLaserIlluminationEvent(illuminationEvent);
// Assert - No mismatch events should be published
var mismatchEvents = _testAdapter.GetPublishedEvents<LaserCodeMismatchEvent>();
Assert.Empty(mismatchEvents);
}
[Fact]
public void ProcessLaserIlluminationUpdateEvent_WithMatchingCode_PublishesMatchEvent()
{
// Arrange
_guidanceSystem.SetExpectedLaserCode(LaserCodeType.PRF, 1234);
_guidanceSystem.SetCodeMatchRequired(true);
var illuminationEvent = new LaserIlluminationUpdateEvent
{
LaserDesignatorId = "laser1",
TargetId = "target1",
IsCodeEnabled = true,
LaserCode = new LaserCode
{
CodeType = LaserCodeType.PRF,
CodeValue = 1234
}
};
// Act
_guidanceSystem.ProcessLaserIlluminationUpdateEvent(illuminationEvent);
// Assert
var matchEvents = _testAdapter.GetPublishedEvents<LaserCodeMatchEvent>();
Assert.NotEmpty(matchEvents);
var lastEvent = matchEvents[matchEvents.Count - 1];
Assert.Equal("missile1", lastEvent.MissileId);
Assert.Equal("laser1", lastEvent.DesignatorId);
Assert.NotNull(lastEvent.MatchedCode);
Assert.Equal(LaserCodeType.PRF, lastEvent.MatchedCode.CodeType);
Assert.Equal(1234, lastEvent.MatchedCode.CodeValue);
}
}
}