// // HTMLRelativeURLResolverTests.swift // RSParser // // Created by Brent Simmons on 7/28/26. // import Foundation import Testing import RSParser @Suite struct HTMLRelativeURLResolverTests { private let base = URL(string: "https://example.com/blog/post1/")! private func resolved(_ html: String) -> String { HTMLRelativeURLResolver.resolvingRelativeURLs(in: html, baseURL: base) } // MARK: - Values that resolve @Test func relativeValuesResolve() { let cases: [(input: String, expected: String)] = [ ("x", "x"), ("", ""), ("", ""), ("x", "x"), ("x", "x"), ("x", "x"), ("x", "x") ] for c in cases { #expect(resolved(c.input) == c.expected, "mismatch for \(c.input)") } } @Test func ampersandEntityInRelativeURLSurvives() { let html = "x" #expect(resolved(html) == "x") } @Test func attributeNamesAreCaseInsensitive() { #expect(resolved("y") == "y") #expect(resolved("") == "") } @Test func unquotedAndSingleQuotedValuesResolve() { #expect(resolved("x") == "x") #expect(resolved("y") == "y") } // MARK: - Values left alone @Test func untouchedValues() { let cases = [ "x", "x", "x", "x", "", "x", "x", "not URL attributes" ] for html in cases { #expect(resolved(html) == html, "expected no change for \(html)") } } @Test func scriptStyleCommentsAndCDATAUntouched() { let cases = [ "", "", "", "]]>" ] for html in cases { #expect(resolved(html) == html, "expected no change for \(html)") } } @Test func scriptSrcAttributeStillResolves() { // The script element's own attributes are scanned — only its text content is skipped. let html = "" #expect(resolved(html) == "") } // MARK: - srcset @Test func srcsetCandidatesResolve() { let cases: [(input: String, expected: String)] = [ ("", ""), ("", ""), ("", ""), ("", ""), ("", "") ] for c in cases { #expect(resolved(c.input) == c.expected, "mismatch for \(c.input)") } } @Test func surroundingWhitespaceInValuesIsIgnoredAndPreserved() { // Whitespace can't hide a fragment-only href from the skip rule. #expect(resolved("x") == "x") // A padded relative value resolves — the padding stays in place. #expect(resolved("x") == "x") } @Test func similarlyNamedCloseTagDoesNotEndRawText() { // is not — everything inside stays untouched. let html = "" #expect(resolved(html) == html) } // MARK: - Malformed input @Test func malformedInputIsSafe() { // Unterminated quote: scanning stops, input preserved. let unterminated = "y" #expect(resolved(strayLessThan) == "1 < 2 and y") } // MARK: - No-op guarantee @Test func allAbsoluteDocumentIsUnchanged() { let html = """

Intro text with an absolute link.

More text, an footnote, and an empty href.

""" #expect(resolved(html) == html) } @Test func mixedDocumentIsSurgical() { // Everything outside the two rewritten values is byte-identical — // whitespace, attribute order, quoting, the non-URL attributes. let html = """
A screenshot
Caption & more
""" let expected = """
A screenshot
Caption & more
""" #expect(resolved(html) == expected) } // MARK: - Bytes entry point @Test func bytesEntryPointMatchesStringEntryPoint() { let html = "" let bytes = Array(html.utf8)[...] let fromBytes = HTMLRelativeURLResolver.resolvingRelativeURLs(inBytes: bytes, baseURL: base) #expect(fromBytes == resolved(html)) let absolute = "" let absoluteBytes = Array(absolute.utf8)[...] #expect(HTMLRelativeURLResolver.resolvingRelativeURLs(inBytes: absoluteBytes, baseURL: base) == absolute) } }