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(
" Beschrijving € 300 eenmalig € 175A <b>& B</b>
" +
"Zwangerschap