diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 26d7deb..e89ea49 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -14,17 +14,11 @@ jobs: steps: - uses: actions/checkout@v4 - - name: Lint build script - run: | - bash -n build.sh - command -v shellcheck >/dev/null 2>&1 || brew install shellcheck - shellcheck -S error build.sh - - - name: Validate Info.plist - run: plutil -lint Info.plist + - name: Ensure shellcheck is installed + run: command -v shellcheck >/dev/null 2>&1 || brew install shellcheck - - name: Typecheck make_icon.swift - run: swiftc -typecheck -swift-version 5 -warn-concurrency make_icon.swift + - name: Run test suite and strict checks + run: ./scripts/test.sh - name: Build and verify app bundle run: | diff --git a/scripts/test.sh b/scripts/test.sh new file mode 100755 index 0000000..60948e9 --- /dev/null +++ b/scripts/test.sh @@ -0,0 +1,26 @@ +#!/usr/bin/env bash +set -euo pipefail + +cd "$(dirname "${BASH_SOURCE[0]}")/.." + +echo "==> Checking shell scripts" +bash -n build.sh scripts/test.sh +if command -v shellcheck >/dev/null 2>&1; then + shellcheck -S error build.sh scripts/test.sh +fi + +echo "==> Validating Info.plist" +plutil -lint Info.plist + +echo "==> Typechecking Swift sources (strict concurrency)" +swiftc -typecheck -swift-version 5 -warn-concurrency -warnings-as-errors Core.swift main.swift +swiftc -typecheck -swift-version 5 -warn-concurrency -warnings-as-errors make_icon.swift + +echo "==> Running Swift CoreTests" +TMP_BIN="$(mktemp "${TMPDIR:-/tmp}/twoup-core-tests.XXXXXX")" +trap 'rm -f "$TMP_BIN"' EXIT +swiftc -swift-version 5 -warn-concurrency -warnings-as-errors Core.swift tests/CoreTests.swift -o "$TMP_BIN" +"$TMP_BIN" + +echo "==> Running Node reader tests" +node --test tests/reader.test.mjs diff --git a/tests/CoreTests.swift b/tests/CoreTests.swift new file mode 100644 index 0000000..5958828 --- /dev/null +++ b/tests/CoreTests.swift @@ -0,0 +1,353 @@ +import Foundation + +final class TestRunner { + private(set) var passed = 0 + private(set) var failed = 0 + + func expect(_ condition: @autoclosure () -> Bool, _ message: String, file: StaticString = #filePath, line: UInt = #line) { + if condition() { + passed += 1 + } else { + failed += 1 + fputs("FAIL [\(file):\(line)] \(message)\n", stderr) + } + } + + func expectEqual(_ actual: T, _ expected: T, _ message: String, file: StaticString = #filePath, line: UInt = #line) { + if actual == expected { + passed += 1 + } else { + failed += 1 + fputs("FAIL [\(file):\(line)] \(message): expected \(expected), got \(actual)\n", stderr) + } + } + + func expectNil(_ value: T?, _ message: String, file: StaticString = #filePath, line: UInt = #line) { + if value == nil { + passed += 1 + } else { + failed += 1 + fputs("FAIL [\(file):\(line)] \(message): expected nil, got \(String(describing: value!))\n", stderr) + } + } +} + +@main +enum CoreTests { + static func main() { + let t = TestRunner() + + testNormalizeURL(t) + testParseLaunchURL(t) + testIsAllowedExternalURL(t) + testMatchesExpectedReaderURL(t) + testReadwiseURLs(t) + testSlimDoc(t) + testJsonLiteral(t) + testThemeWindowAppearance(t) + + print("CoreTests: \(t.passed) passed, \(t.failed) failed") + if t.failed > 0 { + exit(1) + } + } + + static func testNormalizeURL(_ t: TestRunner) { + t.expectEqual(normalizeURL("https://example.com")?.absoluteString, "https://example.com", "accepts https URL") + t.expectEqual(normalizeURL("http://example.com/a?x=1#top")?.absoluteString, "http://example.com/a?x=1#top", "accepts http URL with path/query/hash") + t.expectEqual(normalizeURL(" https://example.com/trimmed ")?.absoluteString, "https://example.com/trimmed", "trims surrounding whitespace") + t.expectEqual(normalizeURL("example.com/a")?.absoluteString, "https://example.com/a", "prepends https to bare domain with path") + t.expectEqual(normalizeURL("sub.example.co.uk/post?id=42")?.absoluteString, "https://sub.example.co.uk/post?id=42", "accepts multi-label bare domain") + t.expectEqual(normalizeURL("example.xn--p1ai")?.absoluteString, "https://example.xn--p1ai", "accepts punycode TLD") + t.expectEqual(normalizeURL("localhost")?.absoluteString, "https://localhost", "accepts bare localhost") + t.expectEqual(normalizeURL("localhost:3000")?.absoluteString, "https://localhost:3000", "accepts localhost with port") + t.expectEqual(normalizeURL("http://localhost:8080/page")?.absoluteString, "http://localhost:8080/page", "accepts http localhost with port") + t.expectEqual(normalizeURL("127.0.0.1")?.absoluteString, "https://127.0.0.1", "accepts IPv4 literal") + t.expectEqual(normalizeURL("192.168.1.10:8080/article")?.absoluteString, "https://192.168.1.10:8080/article", "accepts IPv4 literal with port and path") + t.expectEqual(normalizeURL("[::1]")?.absoluteString, "https://[::1]", "accepts bracketed IPv6 literal") + t.expectEqual(normalizeURL("[::1]:8080/test")?.absoluteString, "https://[::1]:8080/test", "accepts bracketed IPv6 literal with port") + t.expectEqual(normalizeURL("http://[2001:db8::1]/doc")?.absoluteString, "http://[2001:db8::1]/doc", "accepts full IPv6 URL") + + // Rejections: typos, empty, non-HTTP schemes, invalid hosts/ports/IPs + let rejected = [ + "", + " ", + "hello", + "en", + "not a url", + "https://example.com/has space", + "javascript:alert(1)", + "file:///etc/passwd", + "data:text/html,", + "mailto:user@example.com", + "ftp://example.com/file", + ".example.com", + "example.com.", + "a..b.com", + "-bad.com", + "bad-.com", + "example.123", + "999.1.1.1", + "1.2.3.256", + "1.2.3", + "localhost:0", + "localhost:65536", + "localhost:abc", + "example.com:", + ] + for raw in rejected { + t.expectNil(normalizeURL(raw), "rejects invalid URL '\(raw)'") + } + } + + static func testParseLaunchURL(_ t: TestRunner) { + t.expectNil(parseLaunchURL(arguments: []), "empty args returns nil") + t.expectNil(parseLaunchURL(arguments: ["-AppleLanguages", "en"]), "ignores -AppleLanguages flag pair") + t.expectNil(parseLaunchURL(arguments: ["-NSDocumentRevisionsDebugMode", "YES"]), "ignores -NS* flag pair") + t.expectNil(parseLaunchURL(arguments: ["-psn_0_12345"]), "ignores -psn_ launch flag") + t.expectEqual( + parseLaunchURL(arguments: [ + "-AppleLanguages", "en", + "-NSDocumentRevisionsDebugMode", "YES", + "-psn_0_12345", + "https://example.com/article", + ])?.absoluteString, + "https://example.com/article", + "extracts URL after macOS launch flags" + ) + t.expectEqual( + parseLaunchURL(arguments: ["-foo=bar", "example.com/post"])?.absoluteString, + "https://example.com/post", + "ignores key=value flag and extracts bare domain URL" + ) + t.expectEqual( + parseLaunchURL(arguments: ["-NSOpen", "https://example.com/direct"])?.absoluteString, + "https://example.com/direct", + "does not consume https URL as flag value" + ) + t.expectEqual( + parseLaunchURL(arguments: ["--", "example.com/after-dash"])?.absoluteString, + "https://example.com/after-dash", + "treats arguments after -- as positional" + ) + t.expectEqual( + parseLaunchURL(arguments: ["not-a-url", "example.com/second"])?.absoluteString, + "https://example.com/second", + "skips invalid positional args to find first valid URL" + ) + } + + static func testIsAllowedExternalURL(_ t: TestRunner) { + let allowed = [ + "http://example.com", + "https://example.com/path?q=1", + "HTTPS://EXAMPLE.COM", + "mailto:reader@example.com", + ] + for raw in allowed { + guard let url = URL(string: raw) else { + t.expect(false, "failed to construct URL for '\(raw)'") + continue + } + t.expect(isAllowedExternalURL(url), "allows external URL '\(raw)'") + } + + let disallowed = [ + "file:///etc/passwd", + "shortcuts://run-shortcut?name=Test", + "smb://fileserver.local/share", + "ssh://user@example.com", + "javascript:alert(1)", + "data:text/html,

hi

", + "tel:+15551234567", + "https://", + "http://", + "mailto:", + ] + for raw in disallowed { + guard let url = URL(string: raw) else { + t.expect(false, "failed to construct URL for '\(raw)'") + continue + } + t.expect(!isAllowedExternalURL(url), "rejects external URL '\(raw)'") + } + } + + static func testMatchesExpectedReaderURL(_ t: TestRunner) { + let expected = URL(string: "https://example.com/post?a=1")! + let rootExpected = URL(string: "https://example.com/")! + + t.expect(matchesExpectedReaderURL(nil, expected: nil), "nil matches nil") + t.expect(!matchesExpectedReaderURL(nil, expected: expected), "nil does not match non-nil expected") + t.expect(matchesExpectedReaderURL(URL(string: "about:blank"), expected: nil), "about:blank matches nil") + t.expect(matchesExpectedReaderURL(URL(string: "about:blank"), expected: expected), "about:blank matches expected URL") + t.expect(!matchesExpectedReaderURL(expected, expected: nil), "external URL rejected when expected is nil") + + t.expect(matchesExpectedReaderURL(expected, expected: expected), "exact URL matches") + t.expect(matchesExpectedReaderURL(URL(string: "https://example.com"), expected: rootExpected), "empty path matches root slash") + t.expect(matchesExpectedReaderURL(URL(string: "HTTPS://EXAMPLE.COM/post?a=1"), expected: expected), "scheme/host comparison is case-insensitive") + t.expect(matchesExpectedReaderURL(URL(string: "https://example.com/post?a=1#section"), expected: expected), "same URL with fragment matches") + + t.expect(!matchesExpectedReaderURL(URL(string: "https://evil.test/post?a=1"), expected: expected), "rejects different host") + t.expect(!matchesExpectedReaderURL(URL(string: "http://example.com/post?a=1"), expected: expected), "rejects different scheme") + t.expect(!matchesExpectedReaderURL(URL(string: "https://example.com:8443/post?a=1"), expected: expected), "rejects different port") + t.expect(!matchesExpectedReaderURL(URL(string: "https://example.com/other?a=1"), expected: expected), "rejects different path") + t.expect(!matchesExpectedReaderURL(URL(string: "https://example.com/post?a=2"), expected: expected), "rejects different query") + } + + static func testReadwiseURLs(_ t: TestRunner) { + let listURL = readwiseEndpointURL( + path: "/api/v3/list/", + queryItems: [ + URLQueryItem(name: "location", value: "later"), + URLQueryItem(name: "withHtmlContent", value: "false"), + ] + ) + t.expectEqual( + listURL?.absoluteString, + "https://readwise.io/api/v3/list/?location=later&withHtmlContent=false", + "builds Readwise list endpoint URL with query params" + ) + + let noLeadingSlash = readwiseEndpointURL(path: "api/v2/auth/") + t.expectEqual(noLeadingSlash?.absoluteString, "https://readwise.io/api/v2/auth/", "prepends leading slash when omitted") + + let encodedQuery = readwiseEndpointURL( + path: "/api/v3/list/", + queryItems: [URLQueryItem(name: "tag", value: "a&b=c d")] + ) + t.expectEqual(encodedQuery?.host, "readwise.io", "keeps readwise.io host with encoded query") + t.expect(encodedQuery?.absoluteString.contains("tag=a%26b%3Dc%20d") == true, "percent-encodes special query characters") + + for badPath in ["", "/api/v3/../v2/auth/", "/api/v3/list/?foo=bar", "/api/v3/list/#frag"] { + t.expectNil(readwiseEndpointURL(path: badPath), "rejects unsafe Readwise path '\(badPath)'") + } + + let updateNormal = readwiseUpdateURL(docID: "01abc-DEF_99.1~") + t.expectEqual(updateNormal?.absoluteString, "https://readwise.io/api/v3/update/01abc-DEF_99.1~/", "builds Readwise update URL for normal docID") + + let updateEncoded = readwiseUpdateURL(docID: "doc id?x=1#h") + t.expectEqual(updateEncoded?.absoluteString, "https://readwise.io/api/v3/update/doc%20id%3Fx%3D1%23h/", "percent-encodes special chars in docID") + + for badID in ["", ".", "..", "a..b", "a/b", "../v2/auth", "a\\b"] { + t.expectNil(readwiseUpdateURL(docID: badID), "rejects unsafe docID '\(badID)'") + } + + t.expectEqual(readwiseReaderBaseURL()?.absoluteString, "https://read.readwise.io", "returns Readwise Reader base URL") + } + + static func testSlimDoc(_ t: TestRunner) { + let article: [String: Any] = [ + "id": "doc-1", + "parent_id": NSNull(), + "category": "article", + "title": "Two-Up Reading", + "author": "Jane Doe", + "site_name": "Example Press", + "source_url": "https://example.com/two-up", + "word_count": 1200, + "reading_progress": 0.45, + "location": "later", + "html_content": "

ignored

", + ] + if let slim = slimDoc(article) { + t.expectEqual(slim["id"] as? String, "doc-1", "slimDoc preserves id") + t.expectEqual(slim["title"] as? String, "Two-Up Reading", "slimDoc preserves title") + t.expectEqual(slim["author"] as? String, "Jane Doe", "slimDoc preserves author") + t.expectEqual(slim["site_name"] as? String, "Example Press", "slimDoc preserves site_name") + t.expectEqual(slim["source_url"] as? String, "https://example.com/two-up", "slimDoc preserves source_url") + t.expectEqual(slim["word_count"] as? Int, 1200, "slimDoc preserves word_count") + t.expectEqual(slim["reading_progress"] as? Double, 0.45, "slimDoc preserves reading_progress") + t.expectNil(slim["html_content"], "slimDoc strips html_content") + t.expectNil(slim["location"], "slimDoc strips location") + } else { + t.expect(false, "slimDoc should accept valid article") + } + + // Drops NSNull optional fields and accepts email/rss categories + let rss: [String: Any] = [ + "id": "doc-2", + "category": "rss", + "title": "RSS Item", + "author": NSNull(), + ] + if let slim = slimDoc(rss) { + t.expectEqual(slim["id"] as? String, "doc-2", "slimDoc accepts rss category") + t.expectNil(slim["author"], "slimDoc omits NSNull optional fields") + } else { + t.expect(false, "slimDoc should accept rss doc") + } + + let email: [String: Any] = ["id": "doc-3", "category": "email"] + t.expect(slimDoc(email) != nil, "slimDoc accepts email category") + + // Rejections + t.expectNil(slimDoc(["category": "article"]), "rejects missing id") + t.expectNil(slimDoc(["id": "", "category": "article"]), "rejects empty id") + t.expectNil(slimDoc(["id": "h1", "parent_id": "doc-1", "category": "article"]), "rejects non-null parent_id") + for badCategory in ["pdf", "video", "epub", "highlight", "note", "tweet", ""] { + t.expectNil(slimDoc(["id": "x", "category": badCategory]), "rejects category '\(badCategory)'") + } + t.expectNil(slimDoc(["id": "x"]), "rejects missing category") + } + + static func testJsonLiteral(_ t: TestRunner) { + let scriptEscape = jsonLiteral("") + t.expect(!scriptEscape.contains(""), "escapes closing tags") + t.expect(scriptEscape.contains("<\\/script>"), "replaces \u{2028}\u{2029}

", + "count": 3, + "ok": true, + ] + let encoded = jsonLiteral(payload) + t.expect(!encoded.contains("\u{2028}\u{2029}

", "round-trips escaped payload via JSONSerialization") + t.expectEqual(decoded["count"] as? Int, 3, "round-trips integer") + t.expectEqual(decoded["ok"] as? Bool, true, "round-trips boolean") + } else { + t.expect(false, "encoded dictionary must be valid JSON") + } + + t.expectEqual(jsonLiteral(URL(string: "https://example.com")!), "{}", "returns {} for non-JSON Swift object") + } + + static func testThemeWindowAppearance(_ t: TestRunner) { + let paper = themeWindowAppearance(theme: "paper") + t.expectEqual(paper.bgHex, "#fbf9f4", "paper bgHex") + t.expectEqual(paper.isDark, false, "paper isDark") + + let paperUpper = themeWindowAppearance(theme: "PAPER") + t.expectEqual(paperUpper.bgHex, "#fbf9f4", "paper is case-insensitive") + t.expectEqual(paperUpper.isDark, false, "paper uppercase isDark") + + let sepia = themeWindowAppearance(theme: "sepia") + t.expectEqual(sepia.bgHex, "#f3ead6", "sepia bgHex") + t.expectEqual(sepia.isDark, false, "sepia isDark") + + let dark = themeWindowAppearance(theme: "dark") + t.expectEqual(dark.bgHex, "#161616", "dark bgHex") + t.expectEqual(dark.isDark, true, "dark isDark") + + let nilTheme = themeWindowAppearance(theme: nil) + t.expectEqual(nilTheme.bgHex, "#fbf9f4", "nil theme defaults to paper bgHex") + t.expectNil(nilTheme.isDark, "nil theme preserves nil isDark (follow system)") + + let unknown = themeWindowAppearance(theme: "solarized") + t.expectEqual(unknown.bgHex, "#fbf9f4", "unknown theme defaults to paper bgHex") + t.expectNil(unknown.isDark, "unknown theme preserves nil isDark") + } +} + diff --git a/tests/reader.test.mjs b/tests/reader.test.mjs new file mode 100644 index 0000000..18e47ac --- /dev/null +++ b/tests/reader.test.mjs @@ -0,0 +1,705 @@ +import test from "node:test"; +import assert from "node:assert/strict"; +import fs from "node:fs"; +import path from "node:path"; +import vm from "node:vm"; +import { fileURLToPath } from "node:url"; + +const __dirname = path.dirname(fileURLToPath(import.meta.url)); +const ROOT = path.resolve(__dirname, ".."); + +const readerHtml = fs.readFileSync(path.join(ROOT, "Resources/reader.html"), "utf8"); +const extractJs = fs.readFileSync(path.join(ROOT, "Resources/extract.js"), "utf8"); +const readabilityJs = fs.readFileSync(path.join(ROOT, "Resources/Readability.js"), "utf8"); + +function extractInlineScript(html) { + const match = html.match(/'); + assert.equal(scriptFrag.querySelectorAll("script").length, 0); + assert.equal(scriptFrag.innerHTML, "

Hello

"); + + // Clobbering IDs (#end, #status, #twoup-end) stripped + const idFrag = sanitize('
a
b
c'); + for (const el of idFrag.querySelectorAll("*")) { + assert.equal(el.getAttribute("id"), null, `id must be stripped from <${el._localName}>`); + } + + // stripped + const baseFrag = sanitize('

safe

'); + assert.equal(baseFrag.querySelectorAll("base").length, 0); + assert.equal(baseFrag.innerHTML, "

safe

"); + + // & foreign href/use/animate stripped + const svgFrag = sanitize( + 'xno' + ); + const svgLink = svgFrag.querySelector("a"); + assert.ok(svgLink, "svg remains without dangerous attributes"); + assert.equal(svgLink.getAttribute("xlink:href"), null, "xlink:href must be removed"); + assert.equal(svgLink.getAttribute("href"), null, "foreign namespace href must be removed"); + assert.equal(svgFrag.querySelectorAll("use,foreignobject").length, 0, "dangerous SVG elements must be stripped"); + + // Control-character obfuscated javascript: URLs stripped + const ctrlFrag = sanitize('bad1bad2'); + const [a1, a2] = ctrlFrag.querySelectorAll("a"); + assert.equal(a1.getAttribute("href"), null, "leading \\x01 javascript: href must be stripped"); + assert.equal(a2.getAttribute("href"), null, "whitespace-prefixed javascript: href must be stripped"); + assert.equal(ctrlFrag.querySelector("img").getAttribute("src"), null, "control-char javascript: src must be stripped"); + + // data:image/svg+xml rejected on img src + const svgDataFrag = sanitize( + '' + ); + for (const img of svgDataFrag.querySelectorAll("img")) { + assert.equal(img.getAttribute("src"), null, "data:image/svg+xml must be rejected"); + } + + // Inline event handlers, style, ping, form/iframe stripped + const attrFrag = sanitize('

txt

'); + assert.equal(attrFrag.querySelectorAll("iframe").length, 0); + const p = attrFrag.querySelector("p"); + assert.equal(p.getAttribute("onclick"), null); + assert.equal(p.getAttribute("style"), null); + assert.equal(p.getAttribute("ping"), null); + + // srcset validation: mixed unsafe candidate rejects entire srcset; safe + relative candidates preserved & resolved + const srcsetFrag = sanitize( + '' + + '' + + '' + ); + const imgs = srcsetFrag.querySelectorAll("img"); + assert.equal(imgs[0].getAttribute("srcset"), "https://cdn.example.com/a.jpg 1x, https://example.com/img/b.jpg 2x"); + assert.equal(imgs[1].getAttribute("srcset"), null, "srcset containing javascript: must be stripped"); + assert.equal(imgs[2].getAttribute("srcset"), null, "srcset containing data:image/svg+xml must be stripped"); +}); + +test("sanitize() preserves safe https:, mailto:, #hash, resolved relative URLs, and safe raster data:image/png", () => { + const { sanitize } = loadSanitizer("https://example.com/posts/1"); + + const safeFrag = sanitize( + 'ext' + + 'mail' + + 'fn' + + 'rel' + + 'pixel' + + '' + + '' + + '' + ); + + const links = safeFrag.querySelectorAll("a"); + assert.equal(links[0].getAttribute("href"), "https://example.org/ok"); + assert.equal(links[1].getAttribute("href"), "mailto:hi@example.com"); + assert.equal(links[2].getAttribute("href"), "#fn-1"); + assert.equal(links[3].getAttribute("href"), "https://example.com/about"); + + const imgs = safeFrag.querySelectorAll("img"); + assert.equal(imgs[0].getAttribute("src"), "data:image/png;base64,iVBORw0KGgo="); + assert.equal(imgs[0].getAttribute("alt"), "pixel"); + assert.equal(imgs[1].getAttribute("src"), "https://cdn.example.com/photo.webp"); + + const videos = safeFrag.querySelectorAll("video"); + assert.equal(videos[0].getAttribute("poster"), "https://cdn.example.com/poster.jpg"); + assert.equal(videos[1].getAttribute("poster"), null); +}); + +// --------------------------------------------------------------------------- +// 6. extract.js behavioral tests (#hash preservation, lazy , rtl dir) +// --------------------------------------------------------------------------- + +test("extract.js preserves #hash anchors, promotes lazy , and passes through rtl dir", () => { + const base = "https://example.com/articles/arabic-post"; + const sampleHtml = `RTL + + `; + + // Case 1: configurable documentURI -> extract.js aligns doc.documentURI to baseURI, + // promotes , falls back placeholder , and passes through dir: "rtl". + let capturedDoc = null; + const ctx1 = vm.createContext({ + URL, + DOMParser: class { + parseFromString(html) { + return createMiniDocument(html, { documentURI: "about:blank", configurableDocumentURI: true }); + } + }, + Readability: class { + constructor(doc) { + this.doc = doc; + } + parse() { + capturedDoc = this.doc; + return { + title: "RTL Essay", + byline: "Author", + siteName: "Example", + lang: "ar", + dir: "rtl", + content: '

footnote

', + }; + } + }, + }); + vm.runInContext(extractJs, ctx1); + const res1 = ctx1.__twoupExtract(sampleHtml, base); + + assert.equal(res1.dir, "rtl", "must pass through rtl dir"); + assert.equal(res1.lang, "ar"); + assert.equal(res1.title, "RTL Essay"); + assert.equal(capturedDoc.documentURI, base, "should set documentURI to baseURI when configurable"); + + const sourceEl = capturedDoc.querySelector("picture source"); + assert.equal( + sourceEl.getAttribute("srcset"), + "https://cdn.example.com/hero-800.webp 800w, https://cdn.example.com/hero-1600.webp 1600w", + "must promote to srcset" + ); + const imgs = capturedDoc.querySelectorAll("img"); + assert.equal( + imgs[0].getAttribute("src"), + "https://cdn.example.com/hero-800.webp", + "placeholder inside must fall back to promoted " + ); + assert.equal( + imgs[1].getAttribute("src"), + "https://cdn.example.com/inline.jpg", + "lazy must be promoted over about:blank" + ); + + // Case 2: non-configurable documentURI -> Readability rewrites #fn1 to full base#fn1, + // and extract.js restores the relative #fn1 anchor in returned content. + const ctx2 = vm.createContext({ + URL, + DOMParser: class { + parseFromString(html) { + return createMiniDocument(html, { documentURI: "about:blank", configurableDocumentURI: false }); + } + }, + Readability: class { + constructor(doc) { + this.doc = doc; + } + parse() { + return { + title: "Footnote Post", + byline: "", + siteName: "Example", + lang: "en", + dir: "rtl", + content: `

footnote external

`, + }; + } + }, + }); + vm.runInContext(extractJs, ctx2); + const res2 = ctx2.__twoupExtract(sampleHtml, base); + + assert.equal(res2.dir, "rtl"); + assert.match(res2.content, /href="#fn1"/, "must restore relative #fn1 anchor when documentURI != baseURI"); + assert.match(res2.content, /href="https:\/\/other\.test\/page"/, "must leave non-hash links untouched"); +});