212 lines
13 KiB
C#
212 lines
13 KiB
C#
using GAJ3T.MusicGen.RiffGrooveBuilder.Core.Articulations;
|
|
using GAJ3T.MusicGen.RiffGrooveBuilder.Core.Composition;
|
|
using static GAJ3T.MusicGen.RiffGrooveBuilder.UnitTests.Articulations.SyntheticProfiles;
|
|
|
|
namespace GAJ3T.MusicGen.RiffGrooveBuilder.UnitTests.Articulations;
|
|
|
|
public sealed class InstrumentProfileValidatorTests
|
|
{
|
|
public static IEnumerable<object[]> InvalidProfiles()
|
|
{
|
|
var p = new Data();
|
|
var key = Switched();
|
|
yield return Case(p with { Id = "" }, "invalid_profile_id");
|
|
yield return Case(p with { Id = "bad id" }, "invalid_profile_id");
|
|
foreach (var id in new string[] { null!, "", " ", "bad id", "bad/path", "é" })
|
|
yield return Case(p with { InstrumentId = id }, "invalid_instrument_id");
|
|
yield return Case(p with { ProductName = "" }, "missing_identity");
|
|
yield return Case(p with { ProductVersion = " " }, "missing_identity");
|
|
yield return Case(p with { MappingVersion = "" }, "missing_identity");
|
|
yield return Case(p with { Role = (TrackRole)100 }, "invalid_role");
|
|
yield return Case(p with { Evidence = p.Evidence with { Source = "" } }, "missing_evidence");
|
|
yield return Case(p with { Evidence = p.Evidence with { Authorization = "" } }, "missing_evidence");
|
|
yield return Case(p with { Evidence = p.Evidence with { VerificationRecord = "" } }, "missing_evidence");
|
|
yield return Case(p with { Evidence = p.Evidence with { Status = MappingVerification.Unverified } }, "unverified_profile");
|
|
yield return Case(p with { Evidence = p.Evidence with { Status = (MappingVerification)999 } }, "missing_evidence");
|
|
yield return Case(p with { Evidence = null! }, "missing_evidence");
|
|
yield return Case(p with { Id = "not-test" }, "invalid_synthetic_identity");
|
|
yield return Case(p with { ProductName = "Not test" }, "invalid_synthetic_identity");
|
|
yield return Case(p with { Channel = -1 }, "invalid_channel");
|
|
yield return Case(p with { Channel = 16 }, "invalid_channel");
|
|
yield return Case(p with { MinimumPitch = -1 }, "invalid_pitch_range");
|
|
yield return Case(p with { MaximumPitch = 128 }, "invalid_pitch_range");
|
|
yield return Case(p with { MinimumPitch = 101 }, "invalid_pitch_range");
|
|
yield return Case(p with { Rules = [] }, "missing_rules");
|
|
yield return Case(p with { Rules = [null!] }, "invalid_rule");
|
|
yield return Case(p with { Rules = [Rule(), Rule()] }, "duplicate_rule");
|
|
yield return Case(p with { Rules = [Rule((Articulation)123)] }, "invalid_rule");
|
|
yield return Case(p with { Rules = [Rule(behavior: (StateBehavior)123)] }, "invalid_rule");
|
|
yield return Case(p with { Rules = [Rule(min: 0)] }, "invalid_velocity_layer");
|
|
yield return Case(p with { Rules = [Rule(max: 128)] }, "invalid_velocity_layer");
|
|
yield return Case(p with { Rules = [Rule(min: 70, max: 69)] }, "invalid_velocity_layer");
|
|
yield return Case(p with { Rules = [Rule(min: 60, max: 100, output: 59)] }, "invalid_velocity_layer");
|
|
yield return Case(p with { Rules = [Rule(output: 128)] }, "invalid_velocity_layer");
|
|
yield return Case(p with { Rules = [new(Articulation.Sustain, null, null, StateBehavior.None, null, null!, [])] }, "invalid_velocity_layer");
|
|
foreach (var action in new[] { new KeyswitchAction(-1, 90, 0, 1), new(128, 90, 0, 1),
|
|
new(10, 0, 0, 1), new(10, 128, 0, 1), new(10, 90, -1, 1),
|
|
new(10, 90, 0, 0), new(10, 90, 0, -1) })
|
|
yield return Case(key with { Rules = [Rule(behavior: StateBehavior.Latched, key: action)] }, "invalid_keyswitch");
|
|
foreach (var pitch in new[] { 40, 64, 100 })
|
|
yield return Case(key with { Rules = [Rule(behavior: StateBehavior.Latched, key: new(pitch, 90, 0, 1))] }, "keyswitch_collision");
|
|
yield return Case(p with { Rules = [Rule(key: new(10, 90, 0, 1))] }, "invalid_state_behavior");
|
|
yield return Case(p with { Rules = [Rule(behavior: StateBehavior.Latched)] }, "invalid_state_behavior");
|
|
foreach (var cc in new[] { new ControllerValue(-1, 0), new(128, 0), new(1, -1), new(1, 128), null! })
|
|
yield return Case(p with { InitialControllers = [cc] }, "invalid_controller");
|
|
yield return Case(p with { InitialControllers = [new(1, 0), new(1, 0)] }, "invalid_controller");
|
|
yield return Case(p with { FinalControllers = [new(1, 128)] }, "invalid_controller");
|
|
yield return Case(p with { Rules = [Rule(behavior: StateBehavior.Latched, cc: [new(128, 0)])] }, "invalid_controller");
|
|
yield return Case(Switched(cc: true) with { InitialControllers = [] }, "incomplete_controller_state");
|
|
yield return Case(p with { InitialControllers = [new(1, 0)] }, "incomplete_controller_state");
|
|
yield return Case(key with { FinalControllers = [new(1, 0)] }, "incomplete_controller_state");
|
|
yield return Case(key with { Initial = Articulation.PalmMute, Rules = [key.Rules[0]] }, "invalid_initial_state");
|
|
yield return Case(key with { Initial = (Articulation)99 }, "invalid_initial_state");
|
|
yield return Case(Switched(StateBehavior.Momentary) with { Initial = Articulation.PalmMute }, "invalid_initial_state");
|
|
yield return Case(p with { Reset = Articulation.PalmMute }, "invalid_final_reset");
|
|
yield return Case(Switched(cc: true) with { InitialControllers = [new(20, 1)] }, "invalid_initial_state");
|
|
yield return Case(Switched(cc: true) with { Reset = Articulation.Sustain, FinalControllers = [new(20, 100)] }, "conflicting_final_reset");
|
|
yield return Case(p with { Rules = [Rule(), Rule(Articulation.PalmMute)] }, "ambiguous_velocity_layers");
|
|
yield return Case(p with { Rules = [Rule(max: 64), Rule(Articulation.PalmMute, min: 64)] }, "ambiguous_velocity_layers");
|
|
yield return Case(p with { Rules = [Rule(voice: DrumVoice.Kick, drumPitch: 50)] }, "invalid_pitched_rule");
|
|
yield return Case(key with { Rules = [key.Rules[0], Rule(Articulation.PalmMute)] }, "incomplete_latched_state");
|
|
yield return Case(key with { Rules = [key.Rules[0], Rule(Articulation.PalmMute, StateBehavior.Momentary, new(11, 90, 0, 1))] }, "mixed_state_behavior");
|
|
yield return Case(Switched(cc: true) with { Rules = [Switched(cc: true).Rules[0], key.Rules[1]] }, "incomplete_controller_state");
|
|
var drums = p with { Role = TrackRole.Drums, Initial = null, Rules = [Rule(voice: DrumVoice.Kick, drumPitch: 50)] };
|
|
yield return Case(drums with { Rules = [Rule()] }, "invalid_drum_rule");
|
|
yield return Case(drums with { Rules = [Rule(voice: (DrumVoice)99, drumPitch: 50)] }, "invalid_drum_rule");
|
|
yield return Case(drums with { Rules = [Rule(Articulation.PalmMute, voice: DrumVoice.Kick, drumPitch: 50)] }, "invalid_drum_rule");
|
|
yield return Case(drums with { Rules = [Rule(voice: DrumVoice.Kick, drumPitch: 101)] }, "invalid_drum_pitch");
|
|
yield return Case(drums with { Rules = [Rule(voice: DrumVoice.Kick, drumPitch: 50), Rule(voice: DrumVoice.Snare, drumPitch: 50)] }, "invalid_drum_pitch");
|
|
yield return Case(drums with { Initial = Articulation.Sustain }, "invalid_drum_state");
|
|
yield return Case(drums with { Rules = [Rule(behavior: StateBehavior.Latched, key: new(10, 90, 0, 1), voice: DrumVoice.Kick, drumPitch: 50)] }, "invalid_drum_state");
|
|
}
|
|
|
|
private static object[] Case(Data data, string code) => [data.Build(), code];
|
|
|
|
[Theory]
|
|
[MemberData(nameof(InvalidProfiles))]
|
|
public void RejectsInvalidProfiles(InstrumentProfile profile, string code) =>
|
|
Assert.Contains(InstrumentProfileValidator.Validate(profile, allowSynthetic: true), e => e.Code == code);
|
|
|
|
[Fact]
|
|
public void SyntheticRequiresExplicitOptInAndMissingProfileFails()
|
|
{
|
|
Assert.Contains(InstrumentProfileValidator.Validate(new Data().Build()), e => e.Code == "unverified_profile");
|
|
Assert.Equal("missing_profile", Assert.Single(InstrumentProfileValidator.Validate(null)).Code);
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData(MappingVerification.Verified, true)]
|
|
[InlineData(MappingVerification.Documented, true)]
|
|
[InlineData(MappingVerification.Unverified, false)]
|
|
[InlineData(MappingVerification.Synthetic, false)]
|
|
public void ProductionEvidenceStagesStayDistinct(MappingVerification status, bool admitted)
|
|
{
|
|
var data = new Data();
|
|
var profile = (data with
|
|
{
|
|
Evidence = data.Evidence with { Status = status, Source = "https://example.org/mapping" },
|
|
CompatibilityNotes = Notes
|
|
}).Build();
|
|
Assert.Equal(admitted, InstrumentProfileValidator.Validate(profile).Count == 0);
|
|
Assert.Equal(status, profile.Evidence.Status);
|
|
Assert.Equal(0, (int)MappingVerification.Unverified);
|
|
Assert.Equal(1, (int)MappingVerification.Verified);
|
|
Assert.Equal(2, (int)MappingVerification.Synthetic);
|
|
}
|
|
|
|
private static MappingCompatibilityNotes Notes => new("Test-only non-versioned source scope",
|
|
"Test default configuration", "Test explicit channel policy", "Test timing policy, not vendor minima");
|
|
|
|
[Fact]
|
|
public void DocumentedRequiresNotesWhileLegacyVerifiedDoesNotAcquireDefaults()
|
|
{
|
|
var data = new Data();
|
|
Assert.Null(data.Build().CompatibilityNotes);
|
|
Assert.Empty(InstrumentProfileValidator.Validate((data with
|
|
{ Evidence = data.Evidence with { Status = MappingVerification.Verified } }).Build()));
|
|
var profile = (data with { Evidence = data.Evidence with
|
|
{ Status = MappingVerification.Documented, Source = "https://example.org/mapping" } }).Build();
|
|
Assert.Equal("missing_compatibility_notes", Assert.Single(InstrumentProfileValidator.Validate(profile)).Code);
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData(null)]
|
|
[InlineData("")]
|
|
[InlineData(" ")]
|
|
[InlineData(" padded")]
|
|
[InlineData("line\nbreak")]
|
|
public void EveryCompatibilityDisclosureMustBeExplicitAndValid(string? value)
|
|
{
|
|
foreach (var notes in new[] { Notes with { VersionScope = value! },
|
|
Notes with { ConfigurationRequirements = value! }, Notes with { RoutingPolicy = value! },
|
|
Notes with { TimingPolicy = value! } })
|
|
{
|
|
var data = new Data();
|
|
var profile = (data with { CompatibilityNotes = notes,
|
|
Evidence = data.Evidence with { Status = MappingVerification.Documented,
|
|
Source = "https://example.org/mapping" } }).Build();
|
|
Assert.Equal("invalid_compatibility_notes", Assert.Single(InstrumentProfileValidator.Validate(profile)).Code);
|
|
}
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData("uncited")]
|
|
[InlineData("file:///manual.pdf")]
|
|
[InlineData("https://user:secret@example.org/manual")]
|
|
public void DocumentedSourcesMustBePublicHttpUrls(string source)
|
|
{
|
|
var data = new Data();
|
|
Assert.Contains(InstrumentProfileValidator.Validate((data with { CompatibilityNotes = Notes,
|
|
Evidence = data.Evidence with { Status = MappingVerification.Documented, Source = source } }).Build()),
|
|
e => e.Code == "invalid_documented_source");
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData(false)]
|
|
[InlineData(true)]
|
|
public void UnknownArticulationIsValidWithExplicitInitialControllerState(bool cc) =>
|
|
Assert.Empty(InstrumentProfileValidator.Validate((Switched(cc: cc) with { Initial = null }).Build(), true));
|
|
|
|
[Theory]
|
|
[InlineData(0, 1, 127)]
|
|
[InlineData(15, 1, 1)]
|
|
[InlineData(9, 127, 127)]
|
|
public void InclusiveMidiBoundariesAreValid(int channel, int min, int max)
|
|
{
|
|
var p = new Data { Channel = channel, MinimumPitch = 0, MaximumPitch = 127,
|
|
Rules = [Rule(min: min, max: max)] }.Build();
|
|
Assert.Empty(InstrumentProfileValidator.Validate(p, true));
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData(StateBehavior.Latched, false)]
|
|
[InlineData(StateBehavior.Latched, true)]
|
|
[InlineData(StateBehavior.Momentary, false)]
|
|
[InlineData(StateBehavior.Momentary, true)]
|
|
public void ExplicitControlSchemesAreValid(StateBehavior behavior, bool cc) =>
|
|
Assert.Empty(InstrumentProfileValidator.Validate(Switched(behavior, advance: 0, duration: 1, cc: cc).Build(), true));
|
|
|
|
[Fact]
|
|
public void VelocityOnlyHasDisjointInclusiveLayers() =>
|
|
Assert.Empty(InstrumentProfileValidator.Validate(new Data
|
|
{
|
|
Rules = [Rule(max: 63), Rule(Articulation.PalmMute, min: 64, output: 100)]
|
|
}.Build(), true));
|
|
|
|
[Fact]
|
|
public void ProfilesAndRulesSnapshotInputCollections()
|
|
{
|
|
var controllers = new[] { new ControllerValue(20, 0) };
|
|
var rules = new[] { Rule(behavior: StateBehavior.Latched, cc: controllers) };
|
|
var data = new Data { Rules = rules, InitialControllers = controllers };
|
|
var profile = data.Build();
|
|
controllers[0] = new(21, 127);
|
|
rules[0] = Rule(Articulation.PalmMute);
|
|
Assert.Equal(20, profile.InitialControllers[0].Number);
|
|
Assert.Equal(20, profile.Rules[0].Controllers[0].Number);
|
|
Assert.Equal(Articulation.Sustain, profile.Rules[0].Articulation);
|
|
Assert.Throws<NotSupportedException>(() => ((IList<ArticulationRule>)profile.Rules).Clear());
|
|
Assert.Throws<NotSupportedException>(() => ((IList<ControllerValue>)profile.InitialControllers).Clear());
|
|
}
|
|
}
|