164 lines
6.0 KiB
C#
164 lines
6.0 KiB
C#
using Heimdall.DetectionProfiles.Domain;
|
|
using Shouldly;
|
|
using Xunit;
|
|
|
|
namespace Heimdall.Migration.Domain.Tests;
|
|
|
|
public sealed class DetectionProfileTests
|
|
{
|
|
private static readonly Guid ProfileId = Guid.Parse("00000000-0000-0000-0000-000000000001");
|
|
private static readonly Guid TenantId = Guid.Parse("00000000-0000-0000-0000-000000000002");
|
|
|
|
[Fact]
|
|
public void Create_ValidInput_CopiesRulesAndPreservesIdentity()
|
|
{
|
|
List<DetectionClassRule> source = [new(0, 0.5)];
|
|
DetectionProfile profile = DetectionProfile.Create(ProfileId, TenantId, " Entrée ", source);
|
|
source.Clear();
|
|
profile.Id.ShouldBe(ProfileId);
|
|
profile.TenantId.ShouldBe(TenantId);
|
|
profile.Name.ShouldBe("Entrée");
|
|
profile.Version.ShouldBe(1);
|
|
profile.Rules.Count.ShouldBe(1);
|
|
Should.Throw<NotSupportedException>(() => ((IList<DetectionClassRule>)profile.Rules).Clear());
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData(true)]
|
|
[InlineData(false)]
|
|
public void Create_MissingIdentity_RejectsProfile(bool missingProfile)
|
|
{
|
|
Should.Throw<DetectionProfilesDomainException>(() => DetectionProfile.Create(
|
|
missingProfile ? Guid.Empty : ProfileId, missingProfile ? TenantId : Guid.Empty, "Profil", [new(0, 0.5)]));
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData("")]
|
|
[InlineData(" ")]
|
|
public void Create_InvalidName_RejectsProfile(string name)
|
|
{
|
|
Should.Throw<DetectionProfilesDomainException>(() => DetectionProfile.Create(ProfileId, TenantId, name, [new(0, 0.5)]));
|
|
}
|
|
|
|
[Fact]
|
|
public void Create_LongName_RejectsProfile()
|
|
{
|
|
Should.Throw<DetectionProfilesDomainException>(() => DetectionProfile.Create(ProfileId, TenantId, new string('x', 201), [new(0, 0.5)]));
|
|
}
|
|
|
|
[Fact]
|
|
public void Create_DuplicateClass_RejectsAmbiguousThresholds()
|
|
{
|
|
Should.Throw<DetectionProfilesDomainException>(() => DetectionProfile.Create(ProfileId, TenantId, "Profil", [new(0, 0.5), new(0, 0.9)]));
|
|
}
|
|
|
|
[Fact]
|
|
public void Create_EmptyRules_RejectsProfile()
|
|
{
|
|
Should.Throw<DetectionProfilesDomainException>(() => DetectionProfile.Create(ProfileId, TenantId, "Profil", []));
|
|
}
|
|
|
|
[Fact]
|
|
public void Create_NullRule_RejectsProfile()
|
|
{
|
|
Should.Throw<DetectionProfilesDomainException>(() => DetectionProfile.Create(ProfileId, TenantId, "Profil", [null!]));
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData(256, true)]
|
|
[InlineData(257, false)]
|
|
public void Create_ClassLimit_EnforcesBound(int count, bool valid)
|
|
{
|
|
DetectionClassRule[] rules = Enumerable.Range(0, count).Select(id => new DetectionClassRule(id, 0.5)).ToArray();
|
|
if (valid)
|
|
{
|
|
DetectionProfile.Create(ProfileId, TenantId, "Profil", rules).Rules.Count.ShouldBe(count);
|
|
}
|
|
else
|
|
{
|
|
Should.Throw<DetectionProfilesDomainException>(() => DetectionProfile.Create(ProfileId, TenantId, "Profil", rules));
|
|
}
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData(-1, 0.5)]
|
|
[InlineData(0, -0.01)]
|
|
[InlineData(0, 1.01)]
|
|
[InlineData(0, double.NaN)]
|
|
[InlineData(0, double.PositiveInfinity)]
|
|
[InlineData(0, double.NegativeInfinity)]
|
|
public void Rule_InvalidClassOrThreshold_RejectsConfiguration(int classId, double threshold)
|
|
{
|
|
Should.Throw<DetectionProfilesDomainException>(() => new DetectionClassRule(classId, threshold));
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData(0, 0.49, false)]
|
|
[InlineData(0, 0.5, true)]
|
|
[InlineData(0, 1, true)]
|
|
[InlineData(1, 1, false)]
|
|
public void Accepts_ClassAndThreshold_FiltersResults(int classId, double confidence, bool expected)
|
|
{
|
|
DetectionProfile profile = DetectionProfile.Create(ProfileId, TenantId, "Profil", [new(0, 0.5)]);
|
|
profile.Accepts(classId, confidence).ShouldBe(expected);
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData(0)]
|
|
[InlineData(1)]
|
|
public void Accepts_BoundaryThreshold_AcceptsExactValue(double threshold)
|
|
{
|
|
DetectionProfile profile = DetectionProfile.Create(ProfileId, TenantId, "Profil", [new(0, threshold)]);
|
|
profile.Accepts(0, threshold).ShouldBeTrue();
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData(-1, 0.5)]
|
|
[InlineData(0, -0.01)]
|
|
[InlineData(0, 1.01)]
|
|
[InlineData(0, double.NaN)]
|
|
[InlineData(0, double.PositiveInfinity)]
|
|
[InlineData(0, double.NegativeInfinity)]
|
|
public void Accepts_InvalidResult_RejectsInput(int classId, double confidence)
|
|
{
|
|
DetectionProfile profile = DetectionProfile.Create(ProfileId, TenantId, "Profil", [new(0, 0.5)]);
|
|
Should.Throw<DetectionProfilesDomainException>(() => profile.Accepts(classId, confidence));
|
|
}
|
|
|
|
[Fact]
|
|
public void Update_ValidRevision_ReplacesPolicyAndIncrementsVersion()
|
|
{
|
|
DetectionProfile profile = DetectionProfile.Create(ProfileId, TenantId, "Avant", [new(0, 0.5)]);
|
|
List<DetectionClassRule> source = [new(1, 0.9)];
|
|
profile.Update(1, " Après ", source);
|
|
source.Clear();
|
|
profile.Name.ShouldBe("Après");
|
|
profile.Version.ShouldBe(2);
|
|
profile.Accepts(0, 1).ShouldBeFalse();
|
|
profile.Accepts(1, 0.9).ShouldBeTrue();
|
|
}
|
|
|
|
[Fact]
|
|
public void Update_StaleRevision_PreservesCurrentPolicy()
|
|
{
|
|
DetectionProfile profile = DetectionProfile.Create(ProfileId, TenantId, "Avant", [new(0, 0.5)]);
|
|
profile.Update(1, "Actuel", [new(1, 0.9)]);
|
|
Should.Throw<DetectionProfilesDomainException>(() => profile.Update(1, "Obsolète", [new(0, 0.1)]));
|
|
profile.Version.ShouldBe(2);
|
|
profile.Name.ShouldBe("Actuel");
|
|
profile.Accepts(0, 1).ShouldBeFalse();
|
|
}
|
|
|
|
[Fact]
|
|
public void Update_InvalidRules_PreservesAllState()
|
|
{
|
|
DetectionProfile profile = DetectionProfile.Create(ProfileId, TenantId, "Avant", [new(0, 0.5)]);
|
|
IReadOnlyList<DetectionClassRule> previous = profile.Rules;
|
|
Should.Throw<DetectionProfilesDomainException>(() => profile.Update(1, "Après", [new(0, 0.1), new(0, 0.9)]));
|
|
profile.Version.ShouldBe(1);
|
|
profile.Name.ShouldBe("Avant");
|
|
profile.Rules.ShouldBeSameAs(previous);
|
|
profile.Accepts(0, 0.4).ShouldBeFalse();
|
|
}
|
|
}
|