using StellaOps.Feedser.Source.Common.Html;

namespace StellaOps.Feedser.Source.Common.Tests;

public sealed class HtmlContentSanitizerTests
{
    [Fact]
    public void Sanitize_RemovesScriptAndDangerousAttributes()
    {
        var sanitizer = new HtmlContentSanitizer();
        var input = "<div onclick=\"alert(1)\"><script>alert('bad')</script><a href='/foo' target='_blank'>link</a></div>";

        var sanitized = sanitizer.Sanitize(input, new Uri("https://example.test/base/"));

        Assert.DoesNotContain("script", sanitized, StringComparison.OrdinalIgnoreCase);
        Assert.DoesNotContain("onclick", sanitized, StringComparison.OrdinalIgnoreCase);
        Assert.Contains("https://example.test/foo", sanitized, StringComparison.Ordinal);
        Assert.Contains("rel=\"noopener nofollow noreferrer\"", sanitized, StringComparison.Ordinal);
    }

    [Fact]
    public void Sanitize_PreservesBasicFormatting()
    {
        var sanitizer = new HtmlContentSanitizer();
        var input = "<p><strong>Hello</strong> <em>world</em></p>";

        var sanitized = sanitizer.Sanitize(input);

        Assert.Equal("<p><strong>Hello</strong> <em>world</em></p>", sanitized);
    }

    [Fact]
    public void Sanitize_PreservesHeadingsAndLists()
    {
        var sanitizer = new HtmlContentSanitizer();
        var input = "<section><h2>Affected Products</h2><ul><li>Example One</li></ul></section>";

        var sanitized = sanitizer.Sanitize(input);

        Assert.Contains("<h2>Affected Products</h2>", sanitized, StringComparison.Ordinal);
        Assert.Contains("<ul><li>Example One</li></ul>", sanitized, StringComparison.Ordinal);
    }
}
