using AIStudio.Tools;
namespace AIStudio.Tests.Tools;
///
/// Checks the Markdown which the HTML of a web page or a mail becomes before a model reads it.
///
///
/// The trim smoke test converts the same HTML in a trimmed build and expects the same Markdown, see MarkdownChecks
/// in app/TrimSmokeTest. A change here belongs there as well.
///
[TestFixture]
public sealed class HTMLParserConversionTests
{
///
/// A teaser wrapped in a link keeps the words of its blocks apart, while the text of pre stays as it is.
///
///
/// Without the compatibility shim in HTMLParser, the first item reads "Probe lands on the asteroid22 minutes
/// agoExample News", see documentation/compatibility-shims/2026-11-reversemarkdown-blocks-in-inline-elements.md.
///
[Test]
public void BlocksInsideALinkKeepTheirWordsApart()
{
const string HTML =
"""
Code in a link: line one
line two
""";
const string EXPECTED =
"""
- [Probe lands on the asteroid 22 minutes ago Example News](https://example.org/news/1)
- [Second headline A summary of it.](https://example.org/news/2)
- [Plain link text](https://example.org/news/3)
Code in a link: [](https://example.org/code)
```
line one
line two
```
""";
Assert.That(HTMLParser.ParseToMarkdown(HTML).ReplaceLineEndings("\n"), Is.EqualTo(EXPECTED.ReplaceLineEndings("\n")));
}
///
/// The conversion keeps the text of unknown tags, drops comments and scripts, writes a link whose text is its
/// address as that address, and decodes the HTML entities.
///
[Test]
public void TheConversionFollowsItsConfiguration()
{
const string HTML =
"""
Heading & more
Text with an unknown tag, and a https://example.org link.
- First
- Second
""";
const string EXPECTED =
"""
## Heading & more
Text with an unknown tag, and a https://example.org link.
1. First
- Nested
2. Second
| Name | Value |
| --- | --- |
| A < B | 1 |
""";
Assert.That(HTMLParser.ParseToMarkdown(HTML).ReplaceLineEndings("\n"), Is.EqualTo(EXPECTED.ReplaceLineEndings("\n")));
}
}