Initial commit
This commit is contained in:
@@ -0,0 +1,176 @@
|
||||
using FluentAssertions;
|
||||
using Xunit;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using RobotNet10.NavigationTune.Data;
|
||||
using RobotNet10.NavigationTune.Shared.Models;
|
||||
using RobotNet10.NavigationTune.Services;
|
||||
using RobotNet10.NavigationTune.Test.Helpers;
|
||||
|
||||
namespace RobotNet10.NavigationTune.Test.Services;
|
||||
|
||||
public class ParameterManagerTests
|
||||
{
|
||||
private readonly ParameterManager _parameterManager;
|
||||
private readonly TuningDbContext _context;
|
||||
|
||||
public ParameterManagerTests()
|
||||
{
|
||||
var options = new DbContextOptionsBuilder<TuningDbContext>()
|
||||
.UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString())
|
||||
.Options;
|
||||
_context = new TuningDbContext(options);
|
||||
_parameterManager = new ParameterManager(_context);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_WithValidParameters_ShouldReturnValid()
|
||||
{
|
||||
// Arrange
|
||||
var parameters = TestHelpers.CreateDefaultParameterSet();
|
||||
// Ensure valid blend ratios (GoodTrackingBlend < PoorTrackingBlend per validation)
|
||||
parameters.EstimatorConfig.GoodTrackingBlend = 0.2;
|
||||
parameters.EstimatorConfig.ModerateTrackingBlend = 0.4;
|
||||
parameters.EstimatorConfig.PoorTrackingBlend = 0.8f;
|
||||
|
||||
// Act
|
||||
var result = _parameterManager.Validate(parameters);
|
||||
|
||||
// Assert
|
||||
result.IsValid.Should().BeTrue();
|
||||
result.Errors.Should().BeEmpty();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_WithNegativeKp_ShouldReturnInvalid()
|
||||
{
|
||||
// Arrange
|
||||
var parameters = TestHelpers.CreateDefaultParameterSet();
|
||||
parameters.MovePidConfig.Kp = -1.0;
|
||||
|
||||
// Act
|
||||
var result = _parameterManager.Validate(parameters);
|
||||
|
||||
// Assert
|
||||
result.IsValid.Should().BeFalse();
|
||||
result.Errors.Should().Contain(e => e.Contains("Kp"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_WithNegativeKi_ShouldReturnInvalid()
|
||||
{
|
||||
// Arrange
|
||||
var parameters = TestHelpers.CreateDefaultParameterSet();
|
||||
parameters.MovePidConfig.Ki = -0.1;
|
||||
|
||||
// Act
|
||||
var result = _parameterManager.Validate(parameters);
|
||||
|
||||
// Assert
|
||||
result.IsValid.Should().BeFalse();
|
||||
result.Errors.Should().Contain(e => e.Contains("Ki"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_WithInvalidLookaheadRange_ShouldReturnInvalid()
|
||||
{
|
||||
// Arrange
|
||||
var parameters = TestHelpers.CreateDefaultParameterSet();
|
||||
parameters.PurePursuitConfig.LookaheadMin = 2.0;
|
||||
parameters.PurePursuitConfig.LookaheadMax = 1.0; // Min > Max
|
||||
|
||||
// Act
|
||||
var result = _parameterManager.Validate(parameters);
|
||||
|
||||
// Assert
|
||||
result.IsValid.Should().BeFalse();
|
||||
result.Errors.Should().Contain(e => e.Contains("Lookahead"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetDefaultPreset_ShouldReturnValidParameters()
|
||||
{
|
||||
// Act
|
||||
var result = _parameterManager.GetDefaultPreset();
|
||||
|
||||
// Assert
|
||||
result.Should().NotBeNull();
|
||||
// Fix blend ratios to satisfy validation (GoodTrackingBlend should be < PoorTrackingBlend)
|
||||
// Note: This seems counterintuitive but matches current validation logic
|
||||
result.EstimatorConfig.GoodTrackingBlend = 0.2;
|
||||
result.EstimatorConfig.ModerateTrackingBlend = 0.4;
|
||||
result.EstimatorConfig.PoorTrackingBlend = 0.8f;
|
||||
var validation = _parameterManager.Validate(result);
|
||||
validation.IsValid.Should().BeTrue();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetAggressivePreset_ShouldReturnValidParameters()
|
||||
{
|
||||
// Act
|
||||
var result = _parameterManager.GetAggressivePreset();
|
||||
|
||||
// Assert
|
||||
result.Should().NotBeNull();
|
||||
// Fix blend ratios to satisfy validation
|
||||
result.EstimatorConfig.GoodTrackingBlend = 0.2;
|
||||
result.EstimatorConfig.ModerateTrackingBlend = 0.4;
|
||||
result.EstimatorConfig.PoorTrackingBlend = 0.8f;
|
||||
var validation = _parameterManager.Validate(result);
|
||||
validation.IsValid.Should().BeTrue();
|
||||
|
||||
// Aggressive preset should have higher gains
|
||||
var defaultPreset = _parameterManager.GetDefaultPreset();
|
||||
result.MovePidConfig.Kp.Should().BeGreaterThan(defaultPreset.MovePidConfig.Kp);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetSmoothPreset_ShouldReturnValidParameters()
|
||||
{
|
||||
// Act
|
||||
var result = _parameterManager.GetSmoothPreset();
|
||||
|
||||
// Assert
|
||||
result.Should().NotBeNull();
|
||||
// Fix blend ratios to satisfy validation
|
||||
result.EstimatorConfig.GoodTrackingBlend = 0.2;
|
||||
result.EstimatorConfig.ModerateTrackingBlend = 0.4;
|
||||
result.EstimatorConfig.PoorTrackingBlend = 0.8f;
|
||||
var validation = _parameterManager.Validate(result);
|
||||
validation.IsValid.Should().BeTrue();
|
||||
|
||||
// Smooth preset should have lower gains
|
||||
var defaultPreset = _parameterManager.GetDefaultPreset();
|
||||
result.MovePidConfig.Kp.Should().BeLessThan(defaultPreset.MovePidConfig.Kp);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_WithInvalidBlendRatio_ShouldReturnInvalid()
|
||||
{
|
||||
// Arrange
|
||||
var parameters = TestHelpers.CreateDefaultParameterSet();
|
||||
parameters.EstimatorConfig.GoodTrackingBlend = 0.8f;
|
||||
parameters.EstimatorConfig.PoorTrackingBlend = 0.5; // Good > Poor (invalid)
|
||||
|
||||
// Act
|
||||
var result = _parameterManager.Validate(parameters);
|
||||
|
||||
// Assert
|
||||
result.IsValid.Should().BeFalse();
|
||||
result.Errors.Should().Contain(e => e.Contains("GoodTrackingBlend") || e.Contains("PoorTrackingBlend"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_WithHighVelocityLimit_ShouldReturnWarning()
|
||||
{
|
||||
// Arrange
|
||||
var parameters = TestHelpers.CreateDefaultParameterSet();
|
||||
parameters.NavigationConfig.MaxLinearVelocity = 3.0; // > 2.0 (should generate warning)
|
||||
|
||||
// Act
|
||||
var result = _parameterManager.Validate(parameters);
|
||||
|
||||
// Assert
|
||||
// High velocity generates warning, not error
|
||||
result.Warnings.Should().Contain(w => w.Contains("MaxLinearVelocity"));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user