// // HTMLImageParserTests.swift // RSParserTests // // Created by Brent Simmons on 10/25/26. // import Foundation import Testing import RSParser @Suite struct HTMLImageParserTests { @Test func smileysAreSkipped() throws { let parsedFeed = try #require(try FeedParser.parse(parserData("natasha", "xml", "https://www.natashatherobot.com/feed/"))) var foundImageCount = 0 for item in parsedFeed.items { guard let html = item.contentHTML else { continue } let image = HTMLImageParser.significantImages(with: ParserData(url: item.url ?? "", data: Data(html.utf8)), maximumCount: 1).first if let image { foundImageCount += 1 #expect(!image.urlString.contains("s.w.org")) } if item.url == "https://www.natashatherobot.com/url-apple-wallet-passkit-pass/" { // The first image is 503×88, too wide to stand for the article. #expect(image?.urlString == "https://www.natashatherobot.com/wp-content/uploads/Screen-Shot-2017-07-07-at-12.35.13-PM.png") } } #expect(foundImageCount > 0) } @Test func feedBurnerFlaresAndTrackingPixelAreSkipped() { let html = """
Text.
"""
#expect(firstSignificantImage(html)?.urlString == "https://example.com/photo.jpg")
}
@Test func trackingPixelWithDeclaredDimensionsIsSkipped() {
let html = """
"""
#expect(firstSignificantImage(html) == HTMLImage(urlString: "https://example.com/photo.jpg", width: 640, height: 480))
}
@Test func imageWithUndeclaredDimensionsIsAccepted() {
// Real dimensions are checked at download time.
let html = #"
"""
#expect(firstSignificantImage(html)?.urlString == "https://example.com/photo.jpg")
}
@Test func srcsetIsUsedWhenSrcIsPlaceholder() {
let html = """
"""
#expect(firstSignificantImage(html)?.urlString == "https://example.com/a-1024.jpg")
}
@Test func lazySourceIsUsedWhenSrcIsPlaceholder() {
let html = """
"""
#expect(firstSignificantImage(html)?.urlString == "https://example.com/uploads/photo.jpg")
}
@Test func significantImagesAreFoundUpToMaximumCount() {
let html = """



"""
let images = HTMLImageParser.significantImages(with: ParserData(url: "https://example.com/posts/1", data: Data(html.utf8)), maximumCount: 2)
#expect(images.map(\.urlString) == ["https://example.com/a.jpg", "https://example.com/b.jpg"])
}
@Test func relativeURLsAreResolved() {
#expect(firstSignificantImage(#"
"#)?.urlString == "https://example.com/posts/images/a.jpg")
#expect(firstSignificantImage(#"
"#)?.urlString == "https://cdn.example.net/a.jpg")
}
@Test func dimensionsInPixelsAreParsed() {
let html = #"
"#
#expect(firstSignificantImage(html) == HTMLImage(urlString: "https://example.com/a.jpg", width: nil, height: 300))
}
@Test func decorativeURLsAreRejected() {
#expect(!SignificantImageFilter.isSignificant(urlString: "https://example.com/logo.svg", width: nil, height: nil))
#expect(!SignificantImageFilter.isSignificant(urlString: "https://s0.wp.com/i/blank.jpg", width: nil, height: nil))
#expect(!SignificantImageFilter.isSignificant(urlString: "https://pixel.wp.com/b.gif?v=1", width: nil, height: nil))
#expect(!SignificantImageFilter.isSignificant(urlString: "ftp://example.com/a.jpg", width: nil, height: nil))
#expect(!SignificantImageFilter.isSignificant(urlString: "https://example.com/banner.jpg", width: 1200, height: 250))
}
}
// MARK: - Helpers
private func firstSignificantImage(_ html: String) -> HTMLImage? {
HTMLImageParser.significantImages(with: ParserData(url: "https://example.com/posts/1", data: Data(html.utf8)), maximumCount: 1).first
}