43 lines
1.7 KiB
C#
43 lines
1.7 KiB
C#
using PrivaPub.Domain.Content;
|
|||
|
|
using PrivaPub.Models.Post;
|
||
|
|
|
||
|
|
namespace PrivaPub.Tests.Domain
|
||
|
|
{
|
||
|
|
public class LinkPreviewTests
|
||
|
|
{
|
||
|
|
[Fact]
|
||
|
|
public void Reads_open_graph_then_twitter_then_the_page_title()
|
||
|
|
{
|
||
|
|
var preview = LinkPreviews.Read("https://news.example/a?utm_source=x", new Uri("https://news.example/a"), """
|
||
|
|
<html><head><title>Fallback title</title>
|
||
|
|
<meta property="og:title" content="Rain & sun">
|
||
|
|
<meta name="twitter:description" content="A <b>long</b> story">
|
||
|
|
<meta property="og:image" content="/img/cover.jpg">
|
||
|
|
<meta property="og:site_name" content="News">
|
||
|
|
</head><body></body></html>
|
||
|
|
""");
|
||
|
|
|
||
|
|
Assert.Equal("Rain & sun", preview.Title);
|
||
|
|
Assert.Equal("A long story", preview.Description);
|
||
|
|
Assert.Equal("https://news.example/img/cover.jpg", preview.ImageURL);
|
||
|
|
Assert.Equal("News", preview.SiteName);
|
||
|
|
Assert.False(preview.Failed);
|
||
|
|
Assert.True(LinkPreviews.Read("https://x.example", new Uri("https://x.example"), "<html><body>nothing</body></html>").Failed);
|
||
|
|
}
|
||
|
|
|
||
|
|
[Fact]
|
||
|
|
public void Picks_the_first_real_link_and_skips_mentions_tags_quotes_and_ourselves() =>
|
||
|
|
Assert.Equal("https://blog.example/post", LinkPreviews.FirstLink(new Post
|
||
|
|
{
|
||
|
|
Url = "https://m.example/@a/1",
|
||
|
|
ContentHtml = """
|
||
|
|
<p><span class="h-card"><a href="https://m.example/@bob" class="u-url mention">@bob</a></span>
|
||
|
|
<a href="https://m.example/tags/x" class="mention hashtag" rel="tag">#x</a>
|
||
|
|
<span class="quote-inline">RE: <a href="https://k.example/notes/1">link</a></span>
|
||
|
|
<a href="https://privapub.test/@me">me</a>
|
||
|
|
<a href="https://m.example/@a/1">self</a>
|
||
|
|
<a href="https://blog.example/post">read this</a></p>
|
||
|
|
"""
|
||
|
|
}, "https://privapub.test"));
|
||
|
|
}
|
||
|
|
}
|