using System.Text.Json; using FluentAssertions; using NSubstitute; using SlpModularCms.Modules.Offerings.Models; using SlpModularCms.Modules.Offerings.Services; namespace SlpModularCms.Modules.Offerings.Tests.Services; public class OfferingsIndexMarkersTests { private static OfferingDto Dto(string title, string price = "€ 300", string note = "eenmalig") => new( Guid.NewGuid().ToString(), "slug", title, "Beschrijving", price, note, ["a"], "Start", false, 0, null, null); [Theory] [InlineData("€ 300", "300")] [InlineData("€300", "300")] [InlineData("€ 1.500,50", "1500.50")] [InlineData("€ 1.500", "1500")] [InlineData("€ 49,-", "49")] [InlineData("vanaf € 79,95 p/m", "79.95")] [InlineData("Op aanvraag", null)] [InlineData("", null)] [InlineData("$ 300", null)] public void ParsePrice_HandlesDutchNotation_AndNeverGuesses(string label, string? expected) => OfferingsIndexMarkers.ParsePrice(label).Should().Be(expected); [Fact] public void JsonLd_IsAnItemListOfOffers_WithPositions() { var json = OfferingsIndexMarkers.JsonLd([Dto("Start"), Dto("Pro")], "https://slpsoftware.nl"); json.Should().StartWith(""); var doc = JsonDocument.Parse(json["".Length]).RootElement; doc.GetProperty("@context").GetString().Should().Be("https://schema.org"); doc.GetProperty("@type").GetString().Should().Be("ItemList"); var items = doc.GetProperty("itemListElement").EnumerateArray().ToList(); items.Should().HaveCount(2); items[1].GetProperty("position").GetInt32().Should().Be(2); var offer = items[0].GetProperty("item"); offer.GetProperty("@type").GetString().Should().Be("Offer"); offer.GetProperty("name").GetString().Should().Be("Start"); offer.GetProperty("url").GetString().Should().Be("https://slpsoftware.nl/#pakketten"); offer.GetProperty("price").GetString().Should().Be("300"); offer.GetProperty("priceCurrency").GetString().Should().Be("EUR"); } [Fact] public void JsonLd_OmitsPriceAndCurrency_WhenTheLabelIsNotParseable() { var json = OfferingsIndexMarkers.JsonLd([Dto("Maatwerk", price: "Op aanvraag")], "https://x.nl"); json.Should().NotContain("\"price\"").And.NotContain("priceCurrency"); } [Fact] public void JsonLd_CannotCloseTheScriptElement() { var json = OfferingsIndexMarkers.JsonLd([Dto("x & y")], "https://x.nl"); json.Substring("", string.Empty, StringComparison.Ordinal) .Should().NotContain("<"); } [Fact] public void Html_IsASemanticList_WithEverythingEncoded() { var html = OfferingsIndexMarkers.Html([Dto("A & B", price: "€ 300", note: "eenmalig")]); html.Should().Be( "
"); } [Fact] public void WithoutDescription_NothingEmptyIsRendered() { var empty = Dto("Zwangerschap", price: "€ 175", note: "") with { Description = "" }; var html = OfferingsIndexMarkers.Html([empty]); html.Should().Be("
"); html.Should().NotContain("

"); var json = OfferingsIndexMarkers.JsonLd([empty], "https://x.nl"); json.Should().NotContain("\"description\""); json.Should().Contain("\"price\":\"175\""); } [Fact] public void WithoutDescriptionAndPrice_TheListItemHasOnlyATitle() { var bare = Dto("Maatwerk", price: "", note: "") with { Description = "" }; OfferingsIndexMarkers.Html([bare]).Should().Contain("
  • Maatwerk

  • "); } [Fact] public async Task WithoutOfferings_BothMarkersAreEmpty() { var service = Substitute.For(); service.GetOfferingsAsync().Returns([]); var markers = await new OfferingsIndexMarkers(service).RenderAsync("https://x.nl", CancellationToken.None); markers[OfferingsIndexMarkers.JsonLdMarker].Should().BeEmpty(); markers[OfferingsIndexMarkers.HtmlMarker].Should().BeEmpty(); } }