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.
///
///
/// ReverseMarkdown separates such blocks itself since version 6.3.1, see
/// https://github.com/mysticmind/reversemarkdown-net/issues/440. Up to version 6.3.0, the first item read "Probe
/// lands on the asteroid22 minutes agoExample News". Since version 6.3.1 as well, it drops the empty link which
/// the HTML parser leaves behind when the pre closes the paragraph.
///
[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:
```
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")));
}
}