// The seal a file's web page is read under in Words (NotesFileViewer.swift; // the owner, 22 Nov 2026; CONTRACT.md section 15): the page's JavaScript off, // nothing kept, no handler a script could post to, its words handed in at a // host that is no host, every load it asks for blocked, and the pane kept on // it. A page that would rewrite itself by script is shown as it was written. // Every page is invented. import WebKit import XCTest @testable import CottonUI #if os(iOS) @MainActor final class NotesFileSealTests: XCTestCase { func testThePagesScriptsAreOffAndNothingIsKept() { let configuration = NotesFileSeal.configuration(rules: nil) XCTAssertFalse(configuration.defaultWebpagePreferences.allowsContentJavaScript, "the page's own JavaScript is off") XCTAssertFalse(configuration.preferences.javaScriptCanOpenWindowsAutomatically, "no window opens by itself") XCTAssertFalse(configuration.websiteDataStore.isPersistent, "nothing is kept between openings") XCTAssertTrue(configuration.userContentController.userScripts.isEmpty, "no script is laid on the page") XCTAssertEqual(configuration.dataDetectorTypes, [], "no words become actions") XCTAssertEqual(configuration.mediaTypesRequiringUserActionForPlayback, .all, "nothing plays by itself") } func testEveryLoadThePageAsksForIsBlocked() throws { let rules = try XCTUnwrap(try JSONSerialization.jsonObject(with: Data(NotesFileSeal.ruleList.utf8)) as? [[String: [String: Any]]]) XCTAssertEqual(rules.count, 1, "one rule, and no way back in: \(rules)") XCTAssertEqual(rules[0]["trigger"]?["url-filter"] as? String, ".*", "every address") XCTAssertEqual(rules[0]["action"]?["type"] as? String, "block") let types = Set(rules[0]["trigger"]?["resource-type"] as? [String] ?? []) for type in ["image", "style-sheet", "script", "font", "raw", "media", "fetch", "websocket", "ping", "popup", "other"] { XCTAssertTrue(types.contains(type), "a \(type) load is blocked") } XCTAssertFalse(NotesFileSeal.ruleList.contains("ignore-previous-rules"), "no host or scheme is let back in") XCTAssertEqual(NotesFileSeal.base.host, "file.words.invalid", "the page stands at a host that never resolves") } func testThePaneStaysOnItsPage() { XCTAssertTrue(NotesFileSeal.stays(NotesFileSeal.base)) XCTAssertTrue(NotesFileSeal.stays(URL(string: "https://file.words.invalid/#day-two")), "an anchor in it is the same page") XCTAssertFalse(NotesFileSeal.stays(URL(string: "https://file.words.invalid/other.html")), "another address there is not") XCTAssertFalse(NotesFileSeal.stays(URL(string: "https://example.com/")), "the network is not") XCTAssertFalse(NotesFileSeal.stays(URL(fileURLWithPath: "/tmp/invented.html")), "a file on the phone is not") XCTAssertFalse(NotesFileSeal.stays(URL(string: "cotton://open")), "the app is not") XCTAssertFalse(NotesFileSeal.stays(URL(string: "data:text/html,

x

"))) } /// A page whose script would retitle it and fetch, loaded under the seal as its words: the words it was /// written with stand. func testAPagesScriptDoesNotRun() async throws { let html = "Written

As written

" let rules = await NotesFileSeal.rules() XCTAssertNotNil(rules, "the rule list compiles") let view = WKWebView(frame: CGRect(x: 0, y: 0, width: 320, height: 480), configuration: NotesFileSeal.configuration(rules: rules)) let loaded = Loaded() view.navigationDelegate = loaded view.loadHTMLString(html, baseURL: NotesFileSeal.base) for _ in 0..<100 where !loaded.done { try await Task.sleep(for: .milliseconds(100)) } XCTAssertTrue(loaded.done, "the page loaded") // The document's own title, asked of the page once it has loaded. `view.title` is WebKit's // copy, updated after didFinish, and swift-main 17668 read it empty on a loaded Mac. let title = try await view.evaluateJavaScript("document.title") as? String XCTAssertEqual(title, "Written", "the page's script did not run") let words = try await view.evaluateJavaScript("document.getElementById('p').textContent") as? String XCTAssertEqual(words, "As written", "the page's words stand as written") let width = try await view.evaluateJavaScript("document.images[0].naturalWidth") as? Double XCTAssertEqual(width, 0, "the picture it asked the network for never came") } private final class Loaded: NSObject, WKNavigationDelegate { var done = false func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) { done = true } } } #endif