Repository Public
1import CryptoKit2import XCTest3@testable import SPBrowserCore45/// End-to-end tests against a real SP Browser backend process (scripts/test-backend.sh up), exercising the same6/// client code the apps use: passkey ceremonies, sessions, key enrollment, encrypted sync between several devices.7/// The only substitute is the authenticator (software ES256 instead of Touch ID / Face ID).8/// Skipped when SPB_TEST_BACKEND is not set — never replaced by mocked responses.9@MainActor10final class BackendIntegrationTests: XCTestCase {11 private var configuration: BackendConfiguration!12 private var origin = ""1314 override func setUp() async throws {15 let env = ProcessInfo.processInfo.environment16 guard let base = env["SPB_TEST_BACKEND"], let url = URL(string: base) else { throw XCTSkip("SPB_TEST_BACKEND not set: run scripts/test-backend.sh up") }17 configuration = BackendConfiguration(baseURL: url, relyingParty: env["SPB_TEST_RP"] ?? "browser.test")18 origin = env["SPB_TEST_ORIGIN"] ?? "https://browser.test"19 }2021 /// One simulated device: its own keychain, its own local databases, its own engine.22 @MainActor23 final class Device {24 let name: String25 let secrets = InMemorySecretStore()26 let account: AccountService27 let mirror: SyncDatabase28 let library: SyncedLibrary29 let history: HistoryStore30 let bookmarks: BookmarkStore31 let coordinator: SyncCoordinator32 var enabled = true3334 init(_ name: String, platform: String, configuration: BackendConfiguration, authenticator: SoftwareAuthenticator) throws {35 self.name = name36 account = AccountService(configuration: configuration, secrets: secrets, authorizer: authenticator,37 device: { DeviceDescription(name: name, platform: platform, model: "Test", appVersion: "2.0.0") })38 mirror = try SyncDatabase(url: nil)39 library = SyncedLibrary(mirror: mirror)40 let profile = try ProfileSchema.open(at: nil)41 history = HistoryStore(db: profile)42 bookmarks = BookmarkStore(db: profile)43 var flag: () -> Bool = { true }44 coordinator = SyncCoordinator(account: account, mirror: mirror, isEnabled: { flag() })45 flag = { [unowned self] in self.enabled }46 let historyBridge = HistorySyncBridge(history: history, mirror: mirror), bookmarkBridge = BookmarkSyncBridge(bookmarks: bookmarks, mirror: mirror)47 coordinator.captureLocalChanges = { historyBridge.captureLocalChanges(); bookmarkBridge.captureLocalChanges() }48 coordinator.applyRemoteChanges = { changed in49 if changed.contains(SyncCollection.history) { historyBridge.applyRemoteChanges() }50 if changed.contains(SyncCollection.bookmarks) { bookmarkBridge.applyRemoteChanges() }51 }52 account.onStateChange = { [weak coordinator] in coordinator?.refreshPhase() }53 }5455 @discardableResult func sync() async -> SyncReport? { await coordinator.syncNow() }56 func mustSync() async throws -> SyncReport {57 let report = await coordinator.syncNow()58 return try XCTUnwrap(report, "\(name): a sync cycle was expected to run (phase \(coordinator.phase))")59 }60 func expectNoSync() async throws {61 let report = await coordinator.syncNow()62 XCTAssertNil(report, "\(name): no sync cycle should have run")63 }64 func firstPending() async throws -> PendingDevice {65 let pending = try await account.pendingDevices()66 return try XCTUnwrap(pending.first)67 }68 }6970 private func device(_ name: String, platform: String = "macos", _ authenticator: SoftwareAuthenticator) throws -> Device {71 try Device(name, platform: platform, configuration: configuration, authenticator: authenticator)72 }7374 private func serverDump() throws -> String {75 guard let path = ProcessInfo.processInfo.environment["SPB_TEST_DB"] else { throw XCTSkip("SPB_TEST_DB not set") }76 let db = try SQLiteDatabase(path: path)77 var text = ""78 for table in ["sync_records", "account_keys", "devices", "users", "sessions"] {79 for row in try db.query("SELECT * FROM \(table)") { text += row.values.map { $0.string ?? "" }.joined(separator: " ") + "\n" }80 }81 return text82 }8384 func testAccountCreationSecondDeviceEnrollmentAndFullSync() async throws {85 let passkey = SoftwareAuthenticator(origin: origin)86 let mac = try device("Mac A", passkey)87 XCTAssertEqual(mac.coordinator.phase, .signedOut)88 let kit = try await mac.account.createAccount(displayName: "Simon", inviteCode: nil)89 XCTAssertEqual(mac.account.keyStatus, .ready)90 XCTAssertEqual(kit.userID, mac.account.session?.user.id)9192 // Browsing on the Mac.93 mac.history.record(url: URL(string: "https://www.apple.com/mac-studio/")!, title: "Mac Studio", at: Date().addingTimeInterval(-120))94 mac.history.record(url: URL(string: "https://secret-clinic.example/appointment?id=42")!, title: "Appointment", at: Date().addingTimeInterval(-60))95 let folder = try XCTUnwrap(mac.bookmarks.addFolder("Research"))96 mac.bookmarks.add(title: "WebKit", url: "https://webkit.org", parentID: folder.id, tags: ["engine"])97 mac.library.addFavorite(title: "Apple", url: "https://www.apple.com")98 mac.library.addToReadingList(title: "Long article", url: "https://example.org/long")99 mac.library.recordSearch("best apple silicon computers for local ai")100 var prefs = SyncedPreferences(); prefs.search.language = "fr"; prefs.search.region = "ca"101 mac.library.setPreferences(prefs)102 mac.library.publishTabs([RemoteTab(title: "Mac Studio", url: "https://www.apple.com/mac-studio/"), RemoteTab(title: "WebKit", url: "https://webkit.org")],103 closed: [RemoteTab(title: "Closed", url: "https://closed.example")],104 deviceID: try XCTUnwrap(mac.account.currentDeviceID), deviceName: "Mac A", platform: "macos")105 let first = try await mac.mustSync()106 XCTAssertEqual(first.pushed, 10)107 XCTAssertEqual(mac.mirror.dirtyCount, 0)108109 // The server holds only ciphertext and opaque identifiers.110 let dump = try serverDump()111 for secret in ["secret-clinic", "appointment", "apple.com", "webkit", "Research", "best apple silicon", "Mac Studio", "Long article", kit.text, mac.account.session!.token] {112 XCTAssertNil(dump.range(of: secret, options: .caseInsensitive), "the server database must not contain “\(secret)”")113 }114115 // iPhone signs in with the same (platform-synchronized) passkey: it has a session but not the key yet.116 let phone = try device("iPhone", platform: "ios", passkey.clone())117 try await phone.account.signIn()118 guard case .waitingForApproval(let codeOnPhone) = phone.account.keyStatus else { return XCTFail("the phone must wait for the key") }119 try await phone.expectNoSync()120 XCTAssertEqual(phone.coordinator.phase, .waitingForKey)121 XCTAssertTrue(phone.history.allURLs().isEmpty)122123 // The Mac lists the waiting device, the user compares the codes, the Mac approves.124 let pending = try await mac.account.pendingDevices()125 XCTAssertEqual(pending.map(\.name), ["iPhone"])126 XCTAssertEqual(pending[0].verificationCode, codeOnPhone)127 try await mac.account.approve(pending[0])128 let pulled = try await phone.mustSync()129 XCTAssertEqual(phone.account.keyStatus, .ready)130 XCTAssertEqual(pulled.pulled, 10)131132 XCTAssertEqual(phone.history.allURLs(), ["https://www.apple.com/mac-studio/", "https://secret-clinic.example/appointment?id=42"])133 XCTAssertEqual(phone.bookmarks.all().map(\.title).sorted(), ["Research", "WebKit"])134 XCTAssertEqual(phone.bookmarks.all().first { $0.title == "WebKit" }?.parentID, folder.id)135 XCTAssertEqual(phone.library.favorites().map(\.url), ["https://www.apple.com"])136 XCTAssertEqual(phone.library.readingList().map(\.title), ["Long article"])137 XCTAssertEqual(phone.library.recentSearches().map(\.query), ["best apple silicon computers for local ai"])138 XCTAssertEqual(phone.library.preferences().search.region, "ca")139 let tabs = phone.library.otherDevicesTabs(excluding: phone.account.currentDeviceID)140 XCTAssertEqual(tabs.map(\.deviceName), ["Mac A"])141 XCTAssertEqual(tabs.first?.tabs.map(\.url), ["https://www.apple.com/mac-studio/", "https://webkit.org"])142 XCTAssertEqual(phone.library.otherDevicesClosedTabs(excluding: phone.account.currentDeviceID).first?.tabs.first?.url, "https://closed.example")143 XCTAssertEqual(phone.mirror.dirtyCount, 0, "pulled data is not pushed back")144145 // A page opened on the phone reaches the Mac.146 phone.history.record(url: URL(string: "https://developer.apple.com/")!, title: "Developer")147 phone.library.publishTabs([RemoteTab(title: "Developer", url: "https://developer.apple.com/")], closed: [],148 deviceID: try XCTUnwrap(phone.account.currentDeviceID), deviceName: "iPhone", platform: "ios")149 await phone.sync()150 await mac.sync()151 XCTAssertTrue(mac.history.allURLs().contains("https://developer.apple.com/"))152 XCTAssertEqual(mac.library.otherDevicesTabs(excluding: mac.account.currentDeviceID).first?.tabs.first?.title, "Developer")153154 // Account screen data.155 let details = try await mac.account.details()156 XCTAssertEqual(Set(details.devices.map(\.name)), ["Mac A", "iPhone"])157 XCTAssertTrue(details.devices.allSatisfy(\.hasSyncKey))158 XCTAssertEqual(details.passkeys.count, 1)159 XCTAssertEqual(details.sessions.count, 2)160 XCTAssertTrue(details.user.hasRecovery)161 XCTAssertEqual(details.sync.records, 12)162 }163164 func testSameDataOnTwoDevicesNeverDuplicatesAndDeletionsPropagate() async throws {165 let passkey = SoftwareAuthenticator(origin: origin)166 let a = try device("Mac A", passkey), b = try device("Mac B", passkey.clone())167 try await a.account.createAccount(displayName: "Dup", inviteCode: nil)168 try await b.account.signIn()169 try await a.account.approve(try await a.firstPending())170 await b.sync()171172 // Both visit the same page and search the same query while apart.173 let page = URL(string: "https://news.example/article")!174 a.history.record(url: page, title: "Article", at: Date().addingTimeInterval(-500))175 b.history.record(url: page, title: "Article (updated)")176 a.library.recordSearch("Swift concurrency"); b.library.recordSearch("swift concurrency")177 a.library.addToReadingList(title: "Read me", url: "https://e.example/read")178 b.library.addToReadingList(title: "Read me", url: "https://e.example/read")179 await a.sync()180 let report = try await b.mustSync()181 XCTAssertGreaterThanOrEqual(report.conflictsResolved, 3, "the same records written by two devices are merged, not duplicated")182 await a.sync()183 var details = try await a.account.details()184 XCTAssertEqual(details.sync.records, 3, "one page, one query, one reading-list entry")185 for device in [a, b] {186 XCTAssertEqual(Set(device.history.search("news.example").map(\.url)), [page.absoluteString])187 let item: HistorySyncItem = try XCTUnwrap(device.mirror.value("history", key: page.absoluteString))188 XCTAssertEqual(item.title, "Article (updated)")189 XCTAssertEqual(device.library.recentSearches().count, 1)190 XCTAssertEqual(device.library.readingList().count, 1)191 }192193 // Deleting history on one device removes it on the other, and from the server's live records.194 a.history.delete(url: page.absoluteString)195 a.library.removeFromReadingList(url: "https://e.example/read")196 a.library.clearSearches()197 await a.sync()198 await b.sync()199 XCTAssertTrue(b.history.allURLs().isEmpty)200 XCTAssertTrue(b.library.readingList().isEmpty)201 XCTAssertTrue(b.library.recentSearches().isEmpty)202 details = try await a.account.details()203 XCTAssertEqual(details.sync.records, 0)204 // And it does not come back from the device that still had it queued.205 await b.sync(); await a.sync()206 XCTAssertTrue(a.history.allURLs().isEmpty)207 }208209 func testConcurrentBookmarkEditsResolveToTheLatest() async throws {210 let passkey = SoftwareAuthenticator(origin: origin)211 let a = try device("Mac A", passkey), b = try device("Mac B", passkey.clone())212 try await a.account.createAccount(displayName: "Conflict", inviteCode: nil)213 try await b.account.signIn()214 try await a.account.approve(try await a.firstPending())215 let bookmark = try XCTUnwrap(a.bookmarks.add(title: "Original", url: "https://e.example"))216 await a.sync(); await b.sync()217 XCTAssertEqual(b.bookmarks.bookmark(id: bookmark.id)?.title, "Original")218219 var onA = bookmark; onA.title = "Edited on A"220 a.bookmarks.update(onA)221 await a.sync()222 try await Task.sleep(for: .milliseconds(30))223 var onB = try XCTUnwrap(b.bookmarks.bookmark(id: bookmark.id)); onB.title = "Edited on B, later"224 b.bookmarks.update(onB)225 let report = try await b.mustSync()226 XCTAssertEqual(report.conflictsResolved, 1)227 await a.sync()228 XCTAssertEqual(a.bookmarks.bookmark(id: bookmark.id)?.title, "Edited on B, later")229 XCTAssertEqual(b.bookmarks.bookmark(id: bookmark.id)?.title, "Edited on B, later")230 XCTAssertEqual(a.bookmarks.all().count, 1)231 let details = try await a.account.details()232 XCTAssertEqual(details.sync.records, 1)233 }234235 func testOfflineChangesAreQueuedAndDeliveredLater() async throws {236 let passkey = SoftwareAuthenticator(origin: origin)237 let a = try device("Mac A", passkey), b = try device("Mac B", passkey.clone())238 try await a.account.createAccount(displayName: "Offline", inviteCode: nil)239 try await b.account.signIn()240 try await a.account.approve(try await a.firstPending())241 await b.sync()242243 // An engine whose server is unreachable: the cycle fails as "offline" and nothing is lost.244 a.library.addToReadingList(title: "Written offline", url: "https://offline.example/1")245 a.library.addFavorite(title: "Offline fav", url: "https://offline.example")246 let dead = BackendClient(configuration: BackendConfiguration(baseURL: URL(string: "http://127.0.0.1:9")!, relyingParty: "x"), token: { "t" })247 let offlineEngine = SyncEngine(database: a.mirror, backend: dead, accountKey: a.account.accountKeyProvider)248 do { try await offlineEngine.sync(); XCTFail("must fail offline") } catch let error as BackendError {249 XCTAssertEqual(error.kind, .offline)250 XCTAssertTrue(error.isTransient)251 }252 XCTAssertEqual(a.mirror.dirtyCount, 2)253 XCTAssertEqual(a.library.readingList().count, 1, "local data stays usable offline")254255 // Back online: the queued changes go out in the next cycle.256 let report = try await a.mustSync()257 XCTAssertEqual(report.pushed, 2)258 await b.sync()259 XCTAssertEqual(b.library.readingList().map(\.title), ["Written offline"])260 XCTAssertEqual(b.library.favorites().map(\.title), ["Offline fav"])261 }262263 func testSyncCanBeTurnedOffAndNothingLeavesTheDevice() async throws {264 let passkey = SoftwareAuthenticator(origin: origin)265 let a = try device("Mac A", passkey)266 try await a.account.createAccount(displayName: "Off", inviteCode: nil)267 a.enabled = false268 a.history.record(url: URL(string: "https://not-synced.example/")!, title: "Local only")269 try await a.expectNoSync()270 XCTAssertEqual(a.coordinator.phase, .off)271 let details = try await a.account.details()272 XCTAssertEqual(details.sync.records, 0)273 a.enabled = true274 do { let pushed = try await a.mustSync().pushed; XCTAssertEqual(pushed, 1) }275 }276277 func testRevocationRecoveryAdditionalPasskeyAndDeletion() async throws {278 let passkey = SoftwareAuthenticator(origin: origin)279 let mac = try device("Mac A", passkey)280 let kit = try await mac.account.createAccount(displayName: "Recover", inviteCode: nil)281 mac.library.addToReadingList(title: "Keep me", url: "https://keep.example")282 await mac.sync()283284 // A second passkey (e.g. a hardware key) can be registered and is listed.285 try await mac.account.addPasskey(label: "Security key")286 var details = try await mac.account.details()287 XCTAssertEqual(details.passkeys.map(\.label).sorted(), ["Mac A", "Security key"])288289 // A cancelled system sheet surfaces as a cancellation, not as a sign-in.290 let stranger = try device("Stranger", SoftwareAuthenticator(origin: origin))291 do { try await stranger.account.signIn(); XCTFail("no passkey, no session") } catch {}292 XCTAssertFalse(stranger.account.isSignedIn)293294 // Every passkey is lost; the recovery kit opens the account and the data on a new device.295 let replacement = SoftwareAuthenticator(origin: origin)296 let newMac = try device("New Mac", replacement)297 let wrong = SyncCrypto.RecoveryKit(userID: kit.userID)298 do { try await newMac.account.recover(with: wrong); XCTFail("a wrong kit must be refused") } catch let error as BackendError { XCTAssertEqual(error.code, "recovery_refused") }299 try await newMac.account.recover(with: try XCTUnwrap(SyncCrypto.RecoveryKit(text: kit.text)))300 XCTAssertEqual(newMac.account.keyStatus, .ready)301 await newMac.sync()302 XCTAssertEqual(newMac.library.readingList().map(\.title), ["Keep me"])303304 // Removing the old Mac from the account ends its session: its next cycle signs it out locally.305 details = try await newMac.account.details()306 let old = try XCTUnwrap(details.devices.first { $0.name == "Mac A" })307 try await newMac.account.removeDevice(id: old.id)308 try await mac.expectNoSync()309 XCTAssertFalse(mac.account.isSignedIn)310 XCTAssertEqual(mac.coordinator.phase, .signedOut)311 XCTAssertEqual(mac.library.readingList().count, 1, "local data stays on the device")312313 // A new recovery kit replaces the old one.314 let newKit = try await newMac.account.newRecoveryKit()315 let third = try device("Third", SoftwareAuthenticator(origin: origin))316 do { try await third.account.recover(with: kit); XCTFail("the old kit no longer works") } catch {}317 try await third.account.recover(with: newKit)318 XCTAssertEqual(third.account.keyStatus, .ready)319320 // Sign out everywhere, then delete the account (needs a passkey confirmation).321 try await third.account.signOutEverywhere()322 XCTAssertFalse(third.account.isSignedIn)323 try await newMac.expectNoSync()324 XCTAssertFalse(newMac.account.isSignedIn)325 try await newMac.account.signIn()326 try await newMac.account.deleteAccount()327 XCTAssertFalse(newMac.account.isSignedIn)328 do { try await newMac.account.signIn(); XCTFail("the account no longer exists") } catch let error as BackendError { XCTAssertEqual(error.code, "unknown_passkey") }329 }330331 func testErasingSyncedDataThenSettingUpAgainReuploadsEverything() async throws {332 let passkey = SoftwareAuthenticator(origin: origin)333 let a = try device("Mac A", passkey), b = try device("Mac B", passkey.clone())334 try await a.account.createAccount(displayName: "Erase", inviteCode: nil)335 try await b.account.signIn()336 try await a.account.approve(try await a.firstPending())337 a.library.addToReadingList(title: "Survives locally", url: "https://survive.example")338 await a.sync(); await b.sync()339 try await a.account.eraseSyncedData()340 XCTAssertEqual(a.account.keyStatus, .notSetUp)341 try await b.expectNoSync()342 XCTAssertEqual(b.account.keyStatus, .notSetUp, "the other device notices that the key is gone")343 // Setting sync up again creates a new key; local data is uploaded afresh under it.344 _ = try await a.account.setUpSync()345 do { let pushed = try await a.mustSync().pushed; XCTAssertEqual(pushed, 1) }346 let details = try await a.account.details()347 XCTAssertEqual(details.sync.records, 1)348 }349350 func testSearchWithoutAProviderKeyFailsCleanly() async throws {351 let client = SearchClient(backend: BackendClient(configuration: configuration))352 do { _ = try await client.search(SearchQuery("anything")); XCTFail("the test backend has no provider key") } catch let error as BackendError {353 XCTAssertEqual(error.code, "provider_not_configured")354 XCTAssertEqual(error.status, 503)355 }356 do { _ = try await client.search(SearchQuery("")) ; XCTFail("empty") } catch let error as BackendError { XCTAssertEqual(error.code, "empty_query") }357 }358}359360/// Real provider round-trip through the production SP Search endpoint (spends a few provider credits).361/// Run with SPB_LIVE_SEARCH=1; skipped otherwise.362final class LiveSearchTests: XCTestCase {363 func testWebImageAndVideoSearchThroughProduction() async throws {364 guard ProcessInfo.processInfo.environment["SPB_LIVE_SEARCH"] == "1" else { throw XCTSkip("SPB_LIVE_SEARCH not set") }365 let client = SearchClient(backend: BackendClient(configuration: .production))366 var prefs = SearchPreferences(); prefs.language = "en"367 let web = try await client.search(SearchQuery("Best Apple Silicon computers for local AI", kind: .web, preferences: prefs))368 guard case .web(let results) = web.results else { return XCTFail("web results expected") }369 XCTAssertEqual(results.count, 10)370 XCTAssertTrue(results.allSatisfy { URL(string: $0.url)?.scheme?.hasPrefix("http") == true && !$0.title.isEmpty })371 XCTAssertNotNil(web.answer)372 let again = try await client.search(SearchQuery("Best Apple Silicon computers for local AI", kind: .web, preferences: prefs))373 XCTAssertEqual(again, web, "the second identical search is served from the client cache")374375 guard case .images(let images) = try await client.search(SearchQuery("aurora borealis Yukon", kind: .images)).results else { return XCTFail("images expected") }376 XCTAssertGreaterThanOrEqual(images.count, 5)377 XCTAssertTrue(images.allSatisfy { $0.displayURL != nil })378379 guard case .videos(let videos) = try await client.search(SearchQuery("Apple M4 Max local LLM benchmark", kind: .videos)).results else { return XCTFail("videos expected") }380 XCTAssertGreaterThanOrEqual(videos.count, 2)381 XCTAssertTrue(videos.allSatisfy { $0.verifiedBy == "oembed" || $0.verifiedBy == "youtube-data-api" })382 }383}384