package org.terst.feedreader.data
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNull
import org.junit.Test
/**
* Finding the feed when what you were given is a site.
*
* Reported live: "Can't subscribe to https://heathercoxrichardson.substack.com/". Pasting a
* site's home page was the only thing to hand -- a site does not advertise its feed URL
* anywhere a reader can see, it advertises it in a that every other
* reader looks for and this one did not.
*/
class FeedDiscoveryTest {
/** Verbatim from the reported page, 2026-10-21. */
private val substackHead =
"""
Letters from an American""" +
"""..."""
@Test
fun `finds the feed a Substack home page advertises, resolved against the site`() {
assertEquals(
"https://heathercoxrichardson.substack.com/feed",
FeedDiscovery.findFeedUrl(substackHead, "https://heathercoxrichardson.substack.com/")
)
}
@Test
fun `finds an Atom feed too`() {
val html = """"""
assertEquals("https://example.com/atom.xml", FeedDiscovery.findFeedUrl(html, "https://example.com/"))
}
@Test
fun `keeps an absolute href as it stands`() {
val html = """"""
assertEquals("https://feeds.example.net/x.xml", FeedDiscovery.findFeedUrl(html, "https://example.com/blog/"))
}
@Test
fun `resolves a relative href against the page, not the site root`() {
val html = """"""
assertEquals("https://example.com/blog/feed.xml", FeedDiscovery.findFeedUrl(html, "https://example.com/blog/"))
}
@Test
fun `copes with the attributes in any order and single quotes`() {
val html = """"""
assertEquals("https://example.com/rss", FeedDiscovery.findFeedUrl(html, "https://example.com"))
}
@Test
fun `prefers the first feed when a page advertises several`() {
val html =
"""""" +
""""""
assertEquals("https://example.com/feed", FeedDiscovery.findFeedUrl(html, "https://example.com/"))
}
@Test
fun `ignores an alternate link that is not a feed`() {
val html =
"""""" +
""""""
assertNull(FeedDiscovery.findFeedUrl(html, "https://example.com/"))
}
@Test
fun `a page with no feed at all reports nothing rather than guessing a path`() {
assertNull(FeedDiscovery.findFeedUrl("hi", "https://example.com/"))
}
@Test
fun `an http feed link is upgraded, since the app cannot fetch cleartext`() {
val html = """"""
assertEquals("https://example.com/feed", FeedDiscovery.findFeedUrl(html, "https://example.com/"))
}
@Test
fun `xml declaration means this was already a feed, so there is nothing to discover`() {
assertNull(FeedDiscovery.findFeedUrl("", "https://x/f"))
}
}