Files
slp-modular-cms/src/SlpModularCms.Modules.Offerings.Tests/Services/OfferingsIndexMarkersTests.cs
T
SluijsensandClaude Sonnet 5.5 d8141db050 Offerings: beschrijving en prijsnotitie optioneel (API, admin, index-markers, OpenAPI)
Ontbrekend/null/leeg wordt als lege string opgeslagen en teruggegeven;
JSON-LD laat description weg en de HTML-markers renderen geen lege <p>.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-15 14:14:43 +01:00

108 lines
4.5 KiB
C#

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("<script type=\"application/ld+json\">").And.EndWith("</script>");
var doc = JsonDocument.Parse(json["<script type=\"application/ld+json\">".Length..^"</script>".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("</script><b>x</b> & y")], "https://x.nl");
json.Substring("<script type=\"application/ld+json\">".Length).Replace("</script>", string.Empty, StringComparison.Ordinal)
.Should().NotContain("<");
}
[Fact]
public void Html_IsASemanticList_WithEverythingEncoded()
{
var html = OfferingsIndexMarkers.Html([Dto("A <b>& B</b>", price: "€ 300", note: "eenmalig")]);
html.Should().Be(
"<section id=\"packages-static\" aria-label=\"Pakketten\"><ul><li><h3>A &lt;b&gt;&amp; B&lt;/b&gt;</h3>" +
"<p>Beschrijving</p><p>€ 300 eenmalig</p></li></ul></section>");
}
[Fact]
public void WithoutDescription_NothingEmptyIsRendered()
{
var empty = Dto("Zwangerschap", price: "€ 175", note: "") with { Description = "" };
var html = OfferingsIndexMarkers.Html([empty]);
html.Should().Be("<section id=\"packages-static\" aria-label=\"Pakketten\"><ul><li><h3>Zwangerschap</h3><p>€ 175</p></li></ul></section>");
html.Should().NotContain("<p></p>");
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("<li><h3>Maatwerk</h3></li>");
}
[Fact]
public async Task WithoutOfferings_BothMarkersAreEmpty()
{
var service = Substitute.For<IOfferingsService>();
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();
}
}