using System.Text.Json.Nodes; using GAJ3T.MusicGen.RiffGrooveBuilder.Core.Composition; using GAJ3T.MusicGen.RiffGrooveBuilder.Core.Instruments; using GAJ3T.MusicGen.RiffGrooveBuilder.Generation.Instruments; namespace GAJ3T.MusicGen.RiffGrooveBuilder.UnitTests.Instruments; [Collection("Catalog working directory")] public sealed class InstrumentCatalogTests { public static TheoryData Targets => new() { { "ample-abj", "ABJ", "Ample Bass J", TrackRole.Bass, "Ample Sound", "https://www.amplesound.net/en/Main_Panel_Manual-ABJ.pdf" }, { "ample-aglp", "AGLP", "Ample Guitar LP", TrackRole.Lead, "Ample Sound", "https://www.amplesound.net/en/Main_Panel_Manual-AGLP.pdf" }, { "ample-agm", "AGM", "Ample Guitar M", TrackRole.Lead, "Ample Sound", "https://www.amplesound.net/en/Main_Panel_Manual-AGM.pdf" }, { "ample-agt", "AGT", "Ample Guitar T", TrackRole.Lead, "Ample Sound", "https://www.amplesound.net/en/Main_Panel_Manual-AGT.pdf" }, { "ample-ame", "AME", "Ample Metal Eclipse", TrackRole.Lead, "Ample Sound", "https://www.amplesound.net/en/Main_Panel_Manual-AME.pdf" }, { "ample-amr", "AMR", "Ample Metal Ray5", TrackRole.Bass, "Ample Sound", "https://www.amplesound.net/en/Main_Panel_Manual-AMR.pdf" }, { "toontrack-sd3", "SD3", "Superior Drummer 3", TrackRole.Drums, "Toontrack", "https://www.toontrack.com/manual/superior-drummer-3/" } }; [Theory] [MemberData(nameof(Targets))] public void ExactRequestedIdentityHasNoInventedVersionOrMapping(string id, string code, string name, TrackRole role, string vendor, string url) { var instrument = InstrumentCatalog.LoadEmbedded().Find(id); Assert.Equal(new InstrumentDefinition(id, vendor, code, name, role, url), instrument); Assert.Equal(6, typeof(InstrumentDefinition).GetProperties().Length); } [Fact] public void EmbeddedMetadataIsOrderedImmutableAndIndependentOfWorkingDirectory() { var previous = Environment.CurrentDirectory; try { Environment.CurrentDirectory = Environment.SystemDirectory; var catalog = InstrumentCatalog.LoadEmbedded(); Assert.Equal(7, catalog.Instruments.Count); Assert.Equal(catalog.Instruments.OrderBy(i => i.Id, StringComparer.Ordinal), catalog.Instruments); Assert.Equal(4, catalog.ForRole(TrackRole.Lead).Count); Assert.Equal(2, catalog.ForRole(TrackRole.Bass).Count); Assert.Single(catalog.ForRole(TrackRole.Drums)); Assert.Equal(catalog.Instruments.Where(i => i.Role == TrackRole.Lead), catalog.ForRole(TrackRole.Lead)); Assert.Throws(() => ((IList)catalog.Instruments).Clear()); Assert.Throws(() => ((IList)catalog.ForRole(TrackRole.Lead)).Clear()); Assert.Null(catalog.Find("unknown")); Assert.Null(catalog.Find("AMPLE-ABJ")); Assert.Throws(() => catalog.ForRole((TrackRole)100)); } finally { Environment.CurrentDirectory = previous; } } [Theory] [InlineData("")] [InlineData("{")] [InlineData("{}")] [InlineData("null")] [InlineData("[null]")] [InlineData("[{}]")] [InlineData("[{\"unknown\":1}]")] [InlineData("[] garbage")] public void MalformedResourcesNeverBecomeSuccessfulEmptyCatalogs(string json) => Assert.Throws(() => CatalogFixtures.Instruments(json)); [Theory] [InlineData("id")] [InlineData("vendor")] [InlineData("productCode")] [InlineData("displayName")] [InlineData("role")] [InlineData("documentationUrl")] public void MissingAndNullRequiredFieldsFail(string field) { var node = JsonNode.Parse(CatalogFixtures.Read("instruments.json"))!; node[0]!.AsObject().Remove(field); Assert.Throws(() => CatalogFixtures.Instruments(node.ToJsonString())); node[0]![field] = null; Assert.Throws(() => CatalogFixtures.Instruments(node.ToJsonString())); } [Theory] [InlineData("id", "")] [InlineData("id", "bad id")] [InlineData("id", "é")] [InlineData("id", "bad/path")] [InlineData("vendor", " ")] [InlineData("vendor", " Vendor")] [InlineData("vendor", "Vendor\nName")] [InlineData("productCode", "")] [InlineData("productCode", "bad code")] [InlineData("displayName", "")] [InlineData("displayName", "Product ")] [InlineData("role", "Unknown")] [InlineData("role", "999")] [InlineData("role", "Lead, Bass")] [InlineData("role", "Lead, Lead")] [InlineData("documentationUrl", "")] [InlineData("documentationUrl", "relative/manual.pdf")] [InlineData("documentationUrl", "file:///C:/manual.pdf")] [InlineData("documentationUrl", "ftp://example.invalid/manual")] [InlineData("documentationUrl", "https://")] [InlineData("documentationUrl", "https://user:password@example.invalid/manual")] [InlineData("documentationUrl", "https://example.invalid/a b")] [InlineData("documentationUrl", " https://example.invalid/manual")] public void InvalidRequiredIdentityRoleAndUrlValuesFail(string field, string value) { var node = JsonNode.Parse(CatalogFixtures.Read("instruments.json"))!; node[0]![field] = value; Assert.Throws(() => CatalogFixtures.Instruments(node.ToJsonString())); } [Fact] public void NumericRoleUnknownDuplicateFieldsAndDuplicateIdsAreRejected() { var json = CatalogFixtures.Read("instruments.json"); var node = JsonNode.Parse(json)!; node[0]!["role"] = 0; Assert.Throws(() => CatalogFixtures.Instruments(node.ToJsonString())); node[0]!["role"] = "Lead"; node[0]!["unexpected"] = true; Assert.Throws(() => CatalogFixtures.Instruments(node.ToJsonString())); Assert.Throws(() => CatalogFixtures.Instruments( json.Replace("\"role\": \"Lead\"", "\"role\": \"Lead\", \"role\": \"Bass\"", StringComparison.Ordinal))); node = JsonNode.Parse(json)!; node.AsArray().Add(node[0]!.DeepClone()); Assert.Contains("duplicate_instrument_id", Assert.Throws(() => CatalogFixtures.Instruments(node.ToJsonString())).Message); } [Fact] public void TestOnlyAdditionalTargetsLoadAsDataInDeterministicOrder() { var node = JsonNode.Parse(CatalogFixtures.Read("instruments.json"))!.AsArray(); var reversed = new JsonArray(node.Reverse().Select(i => i!.DeepClone()).ToArray()); var catalog = CatalogFixtures.Instruments(); Assert.Equal(catalog.Instruments, CatalogFixtures.Instruments(reversed.ToJsonString()).Instruments); Assert.Equal("Fixture Vendor", catalog.Find("synthetic-extra")!.Vendor); Assert.Equal(3, catalog.ForRole(TrackRole.Lead).Count); Assert.DoesNotContain(InstrumentCatalog.LoadEmbedded().Instruments, i => i.Id.StartsWith("synthetic-", StringComparison.Ordinal)); } [Theory] [InlineData("productCode", "DIFFERENT")] [InlineData("displayName", "Synthetic Different")] public void OneProductCannotAcquireMultipleInstrumentIdentities(string field, string differentValue) { var node = JsonNode.Parse(CatalogFixtures.Read("instruments.json"))!.AsArray(); var duplicate = node[0]!.DeepClone(); duplicate["id"] = "synthetic-duplicate"; duplicate[field] = differentValue; node.Add(duplicate); Assert.Contains("duplicate_product_identity", Assert.Throws(() => CatalogFixtures.Instruments(node.ToJsonString())).Message); } }