SPB Git

Repository Public

SearchAndSyncTests.swift 21.0 KB 304 lines swift

1import CryptoKit2import XCTest3@testable import SPBrowserCore45final class SPSearchModelTests: XCTestCase {6    func testQueriesTypedInTheAddressBarBecomeNativeSearchPages() {7        let settings = AppSettings()8        XCTAssertEqual(settings.activeSearchEngine().id, "spsearch")9        let resolved = AddressResolver.resolve("Best Apple Silicon computers for local AI", engine: settings.activeSearchEngine(), engines: settings.allSearchEngines())10        XCTAssertEqual(resolved?.isSearch, true)11        XCTAssertEqual(resolved?.url.scheme, "spsearch")12        XCTAssertNil(resolved?.url.absoluteString.range(of: "duckduckgo"))13        let parsed = SPSearchURL.parse(resolved?.url)14        XCTAssertEqual(parsed?.query, "Best Apple Silicon computers for local AI")15        XCTAssertEqual(parsed?.kind, .web)16    }1718    func testURLVersusQueryClassification() {19        let engine = SearchEngine.spSearch20        let urls = ["apple.com", "https://example.org/a?b=c", "localhost:3000", "192.168.1.10/admin", "sub.domain.co.uk/path", "[::1]:8080"]21        let queries = ["what is webkit", "swift", "javascript:alert(1)", "3.14", "hello.", "c++ tutorial", "is example.com down", "data:text/html,x", "a b.com"]22        for text in urls { XCTAssertEqual(AddressResolver.resolve(text, engine: engine)?.isSearch, false, text) }23        for text in queries {24            let resolved = AddressResolver.resolve(text, engine: engine)25            XCTAssertEqual(resolved?.isSearch, true, text)26            XCTAssertEqual(SPSearchURL.parse(resolved?.url)?.query, SearchQuery.normalize(text), text)27        }28        // Other engines remain reachable by their shortcut.29        XCTAssertEqual(AddressResolver.resolve("w montréal", engine: engine)?.url.host, "www.wikipedia.org")30    }3132    func testSearchURLRoundTripAndKinds() {33        let url = SPSearchURL.make(query: "  aurora   borealis & co ", kind: .images)34        XCTAssertEqual(SPSearchURL.parse(url)?.query, "aurora borealis & co")35        XCTAssertEqual(SPSearchURL.parse(url)?.kind, .images)36        XCTAssertNil(SPSearchURL.make(query: "   "))37        XCTAssertNil(SPSearchURL.parse(URL(string: "https://example.com/?q=x")))38        XCTAssertNil(SPSearchURL.parse(URL(string: "spsearch://results?q=")))39        XCTAssertFalse(HistoryStore.isRecordable(url!), "search pages are not browsing history")40    }4142    func testSettingsMigrationReplacesTheOldDefaultOnly() {43        var old = AppSettings(); old.schemaVersion = 2; old.searchEngineID = "duckduckgo"44        SettingsStore.migrate(&old)45        XCTAssertEqual(old.searchEngineID, "spsearch")46        var chosen = AppSettings(); chosen.schemaVersion = 2; chosen.searchEngineID = "google"47        SettingsStore.migrate(&chosen)48        XCTAssertEqual(chosen.searchEngineID, "google")49    }5051    func testResponseDecodingForTheThreeModes() throws {52        let web = #"{"query":"q","type":"web","page":1,"pageSize":10,"hasMore":true,"provider":"tavily","cached":false,"tookMs":12,"answer":{"text":"Sum","generatedBy":"tavily"},"notices":[],"results":[{"id":"a","title":"T","url":"https://e.com/x","domain":"e.com","favicon":null,"snippet":"S","publishedDate":"Wed, 30 Sep 2026 00:00:00 GMT","score":0.8}]}"#53        let page = try JSONDecoder().decode(SearchPage.self, from: Data(web.utf8))54        guard case .web(let results) = page.results else { return XCTFail("web expected") }55        XCTAssertEqual(results.first?.domain, "e.com")56        XCTAssertEqual(page.answer?.text, "Sum")57        XCTAssertNotNil(SearchDate.parse(results.first?.publishedDate))5859        let images = #"{"query":"q","type":"images","page":1,"pageSize":40,"hasMore":false,"provider":"tavily+wikimedia-commons","results":[{"id":"i","imageURL":"https://c.org/a.jpg","imageDomain":"c.org","description":null,"sourceURL":null,"sourceTitle":null,"sourceDomain":null,"thumbnailURL":"https://c.org/t.jpg","width":4000,"height":3000,"author":"A","license":"CC BY 4.0","provider":"wikimedia-commons"}]}"#60        guard case .images(let pictures) = try JSONDecoder().decode(SearchPage.self, from: Data(images.utf8)).results else { return XCTFail("images expected") }61        XCTAssertEqual(pictures.first?.aspectRatio ?? 0, 4.0 / 3.0, accuracy: 0.001)62        XCTAssertEqual(pictures.first?.displayURL?.absoluteString, "https://c.org/t.jpg")6364        let videos = #"{"query":"q","type":"videos","page":1,"pageSize":20,"hasMore":false,"provider":"tavily+oembed","notices":["video-duration-unavailable-without-youtube-data-api"],"results":[{"id":"v","url":"https://vimeo.com/1","title":"V","platform":"vimeo","channel":"C","channelURL":null,"thumbnail":null,"durationSeconds":3723,"publishedDate":"2024-05-01T10:00:00","description":null,"embedURL":null,"verifiedBy":"oembed"}]}"#65        guard case .videos(let clips) = try JSONDecoder().decode(SearchPage.self, from: Data(videos.utf8)).results else { return XCTFail("videos expected") }66        XCTAssertEqual(clips.first?.durationText, "1:02:03")67        XCTAssertEqual(clips.first?.platformName, "Vimeo")68        XCTAssertNotNil(SearchDate.parse(clips.first?.publishedDate))69    }7071    func testRequestEncodingCarriesPreferences() throws {72        var prefs = SearchPreferences(); prefs.language = "fr"; prefs.region = "ca"; prefs.safeSearch = false73        let query = SearchQuery("  météo  montréal ", kind: .videos, preferences: prefs, timeRange: .week, source: .youtube, page: 2)74        let json = try JSONSerialization.jsonObject(with: JSONEncoder().encode(query)) as? [String: Any]75        XCTAssertEqual(json?["query"] as? String, "météo montréal")76        XCTAssertEqual(json?["type"] as? String, "videos")77        XCTAssertEqual(json?["language"] as? String, "fr")78        XCTAssertEqual(json?["region"] as? String, "ca")79        XCTAssertEqual(json?["timeRange"] as? String, "week")80        XCTAssertEqual(json?["source"] as? String, "youtube")81        XCTAssertEqual(json?["safeSearch"] as? Bool, false)82        XCTAssertEqual(json?["summary"] as? Bool, false, "summaries are only requested for web results")83        XCTAssertEqual(json?["page"] as? Int, 2)84    }8586    func testHighlightingMatchesTermsIgnoringCaseAndAccents() {87        let segments = SearchHighlighter.segments(of: "Le Québec et la Silicon Valley: benchmarks", query: "quebec silicon benchmark the")88        XCTAssertEqual(segments.filter(\.isMatch).map(\.text), ["Québec", "Silicon", "benchmarks"])89        XCTAssertEqual(segments.map(\.text).joined(), "Le Québec et la Silicon Valley: benchmarks")90        XCTAssertTrue(SearchHighlighter.segments(of: "nothing here", query: "zebra").allSatisfy { !$0.isMatch })91    }92}9394final class SyncCryptoTests: XCTestCase {95    func testRecordsAreBoundToTheirIdentityAndKey() throws {96        let key = SyncCrypto.newAccountKey()97        let sealed = try SyncCrypto.seal(Data("secret visit".utf8), collection: "history", id: "abc", accountKey: key)98        XCTAssertNil(Data(base64Encoded: sealed).flatMap { String(data: $0, encoding: .utf8) }?.range(of: "secret"))99        XCTAssertEqual(try SyncCrypto.open(sealed, collection: "history", id: "abc", accountKey: key), Data("secret visit".utf8))100        XCTAssertThrowsError(try SyncCrypto.open(sealed, collection: "history", id: "other", accountKey: key))101        XCTAssertThrowsError(try SyncCrypto.open(sealed, collection: "bookmarks", id: "abc", accountKey: key))102        XCTAssertThrowsError(try SyncCrypto.open(sealed, collection: "history", id: "abc", accountKey: SyncCrypto.newAccountKey()))103        XCTAssertThrowsError(try SyncCrypto.open("not base64", collection: "history", id: "abc", accountKey: key))104        XCTAssertNotEqual(sealed, try SyncCrypto.seal(Data("secret visit".utf8), collection: "history", id: "abc", accountKey: key), "fresh nonce every time")105    }106107    func testRecordIdentifiersAreStableOpaqueAndKeyed() {108        let key = SyncCrypto.newAccountKey()109        let id = SyncCrypto.recordID(for: "https://example.com/private", collection: "history", accountKey: key)110        XCTAssertEqual(id, SyncCrypto.recordID(for: "https://example.com/private", collection: "history", accountKey: key))111        XCTAssertNotEqual(id, SyncCrypto.recordID(for: "https://example.com/private", collection: "reading", accountKey: key))112        XCTAssertNotEqual(id, SyncCrypto.recordID(for: "https://example.com/private", collection: "history", accountKey: SyncCrypto.newAccountKey()))113        XCTAssertNil(id.range(of: "example"))114        XCTAssertNotNil(id.range(of: "^[A-Za-z0-9_-]{1,64}$", options: .regularExpression))115    }116117    func testDeviceEnvelopeOpensOnlyForTheIntendedDevice() throws {118        let key = SyncCrypto.newAccountKey()119        let phone = SyncCrypto.newDeviceKey(), other = SyncCrypto.newDeviceKey()120        let envelope = try SyncCrypto.sealEnvelope(accountKey: key, to: SyncCrypto.publicKeyString(phone), userID: "u1", deviceID: "d1")121        XCTAssertEqual(SyncCrypto.data(try SyncCrypto.openEnvelope(envelope, with: phone, userID: "u1", deviceID: "d1")), SyncCrypto.data(key))122        XCTAssertThrowsError(try SyncCrypto.openEnvelope(envelope, with: other, userID: "u1", deviceID: "d1"))123        XCTAssertThrowsError(try SyncCrypto.openEnvelope(envelope, with: phone, userID: "u1", deviceID: "d2"), "an envelope cannot be replayed to another device")124        XCTAssertThrowsError(try SyncCrypto.openEnvelope(envelope, with: phone, userID: "u2", deviceID: "d1"))125        let code = SyncCrypto.verificationCode(forPublicKey: SyncCrypto.publicKeyString(phone))126        XCTAssertNotNil(code.range(of: "^\\d{3} \\d{3}$", options: .regularExpression))127        XCTAssertNotEqual(code, SyncCrypto.verificationCode(forPublicKey: SyncCrypto.publicKeyString(other)))128    }129130    func testRecoveryKitRoundTripAndTypingTolerance() throws {131        let key = SyncCrypto.newAccountKey()132        let kit = SyncCrypto.RecoveryKit(userID: "Zm9vYmFyLWlkXzEyMw")133        let blob = try kit.seal(key)134        let typed = SyncCrypto.RecoveryKit(text: kit.text.lowercased().replacingOccurrences(of: "-", with: " "))135        XCTAssertEqual(typed, kit)136        XCTAssertEqual(SyncCrypto.data(try XCTUnwrap(typed).open(blob)), SyncCrypto.data(key))137        XCTAssertTrue(kit.text.hasPrefix("SPB1-"))138        XCTAssertNil(SyncCrypto.RecoveryKit(text: "SPB1-AAAA-BBBB"))139        XCTAssertNil(SyncCrypto.RecoveryKit(text: "hello"))140        XCTAssertThrowsError(try SyncCrypto.RecoveryKit(userID: "Zm9vYmFyLWlkXzEyMw").open(blob), "another kit cannot open the key")141        XCTAssertNotEqual(kit.authenticator, SyncCrypto.data(SymmetricKey(data: kit.secret)).base64EncodedString(), "the server never receives the kit secret itself")142    }143144    func testKeyCheckMatchesWhatTheServerComputes() {145        let key = SyncCrypto.newAccountKey()146        let expected = SHA256.hash(data: Data(SyncCrypto.keyProof(key).utf8)).map { String(format: "%02x", $0) }.joined()147        XCTAssertEqual(SyncCrypto.keyCheck(key), expected)148        XCTAssertEqual(Base32.decode(Base32.encode(Data([1, 2, 3, 4, 5, 6, 7]))), Data([1, 2, 3, 4, 5, 6, 7]))149    }150}151152final class SyncLocalTests: XCTestCase {153    private func mirror() throws -> SyncDatabase { try SyncDatabase(url: nil) }154155    func testMirrorQueuesChangesAndKeepsTombstonesUntilPushed() throws {156        let db = try mirror()157        db.put("reading", key: "https://a.example", value: ReadingItem(url: "https://a.example", title: "A"))158        XCTAssertEqual(db.dirtyCount, 1)159        // Never pushed: deleting simply forgets it.160        db.remove("reading", key: "https://a.example")161        XCTAssertEqual(db.dirtyCount, 0)162        XCTAssertNil(db.record("reading", key: "https://a.example"))163        // Pushed, then deleted: a tombstone waits for upload.164        db.put("reading", key: "https://b.example", value: ReadingItem(url: "https://b.example", title: "B"))165        db.markPushed(db.record("reading", key: "https://b.example")!, remoteID: "rb", seq: 7)166        XCTAssertEqual(db.dirtyCount, 0)167        db.remove("reading", key: "https://b.example")168        let tombstone = db.dirty(limit: 10).first169        XCTAssertEqual(tombstone?.deleted, true)170        XCTAssertEqual(tombstone?.serverSeq, 7)171        XCTAssertTrue(db.records("reading").isEmpty)172        db.markPushed(tombstone!, remoteID: "rb", seq: 8)173        XCTAssertNil(db.record("reading", key: "https://b.example"))174    }175176    func testAChangeMadeDuringAPushStaysQueued() throws {177        let db = try mirror()178        db.put("prefs", key: "prefs", json: Data("{\"v\":1}".utf8), modifiedAt: Date(timeIntervalSince1970: 100))179        let sent = db.record("prefs", key: "prefs")!180        db.put("prefs", key: "prefs", json: Data("{\"v\":2}".utf8), modifiedAt: Date(timeIntervalSince1970: 200))181        db.markPushed(sent, remoteID: "r", seq: 3)182        let after = db.record("prefs", key: "prefs")!183        XCTAssertTrue(after.dirty)184        XCTAssertEqual(after.serverSeq, 3)185    }186187    func testMergeRules() throws {188        let older = Date(timeIntervalSince1970: 1000), newer = Date(timeIntervalSince1970: 2000)189        func record(_ collection: String, _ value: some Encodable, at date: Date) -> SyncRecord {190            SyncRecord(collection: collection, key: "k", json: try! SyncDatabase.encoder.encode(value), modifiedAt: date, deleted: false, serverSeq: 1, dirty: true, remoteID: nil)191        }192        // History: both devices' visits count.193        let local = HistorySyncItem(url: "https://e.com", title: "Local title", lastVisit: newer, visitCount: 3)194        let remote = HistorySyncItem(url: "https://e.com", title: "Remote", lastVisit: older, visitCount: 9)195        let merged = SyncMerge.merge(collection: "history", local: record("history", local, at: newer), remoteJSON: try SyncDatabase.encoder.encode(remote), remoteModifiedAt: older)196        let item = try SyncDatabase.decoder.decode(HistorySyncItem.self, from: merged.json)197        XCTAssertEqual(item, HistorySyncItem(url: "https://e.com", title: "Local title", lastVisit: newer, visitCount: 9))198        XCTAssertTrue(merged.differsFromRemote)199        // Bookmarks: last writer wins; ties go to the server.200        let a = BookmarkSyncItem(id: "b", parentID: nil, isFolder: false, title: "Mine", url: "https://e.com", tags: [], position: 0, createdAt: older)201        let b = BookmarkSyncItem(id: "b", parentID: nil, isFolder: false, title: "Theirs", url: "https://e.com", tags: [], position: 0, createdAt: older)202        let mineWins = SyncMerge.merge(collection: "bookmarks", local: record("bookmarks", a, at: newer), remoteJSON: try SyncDatabase.encoder.encode(b), remoteModifiedAt: older)203        XCTAssertEqual(try SyncDatabase.decoder.decode(BookmarkSyncItem.self, from: mineWins.json).title, "Mine")204        XCTAssertTrue(mineWins.differsFromRemote)205        let theirsWins = SyncMerge.merge(collection: "bookmarks", local: record("bookmarks", a, at: older), remoteJSON: try SyncDatabase.encoder.encode(b), remoteModifiedAt: older)206        XCTAssertEqual(try SyncDatabase.decoder.decode(BookmarkSyncItem.self, from: theirsWins.json).title, "Theirs")207        XCTAssertFalse(theirsWins.differsFromRemote)208    }209210    func testHistoryBridgeCapturesVisitsAndDeletionsAndAppliesRemoteOnes() throws {211        let profile = try ProfileSchema.open(at: nil)212        let history = HistoryStore(db: profile)213        let db = try mirror()214        let bridge = HistorySyncBridge(history: history, mirror: db)215        history.record(url: URL(string: "https://a.example/")!, title: "A", at: Date().addingTimeInterval(-600))216        history.record(url: URL(string: "https://a.example/")!, title: "A2", at: Date().addingTimeInterval(-300))217        history.record(url: URL(string: "https://b.example/")!, title: "B", at: Date().addingTimeInterval(-200))218        bridge.captureLocalChanges()219        let a: HistorySyncItem = try XCTUnwrap(db.value("history", key: "https://a.example/"))220        XCTAssertEqual([a.title, String(a.visitCount)], ["A2", "2"])221        XCTAssertEqual(db.count("history"), 2)222        bridge.captureLocalChanges()223        XCTAssertEqual(db.dirtyCount, 2, "nothing new to capture")224        // Deleting locally produces a deletion to propagate (the record was already on the server).225        db.dirty(limit: 10).forEach { db.markPushed($0, remoteID: "r-" + $0.key, seq: 1) }226        history.delete(url: "https://b.example/")227        bridge.captureLocalChanges()228        XCTAssertEqual(db.dirty(limit: 10).map { [$0.key, String($0.deleted)] }, [["https://b.example/", "true"]])229        // A visit from another device appears here once, and a remote deletion removes the page here.230        bridge.rememberKnown()231        db.applyRemote("history", key: "https://c.example/", json: try SyncDatabase.encoder.encode(HistorySyncItem(url: "https://c.example/", title: "C", lastVisit: Date(), visitCount: 5)),232                       modifiedAt: Date(), seq: 9, remoteID: "rc")233        db.applyRemoteDeletion("history", key: "https://a.example/")234        bridge.applyRemoteChanges()235        bridge.applyRemoteChanges()236        XCTAssertEqual(history.allURLs(), ["https://c.example/"])237        XCTAssertEqual(history.search("c.example").count, 1, "applying twice must not duplicate the visit")238        bridge.captureLocalChanges()239        XCTAssertFalse(db.record("history", key: "https://c.example/")!.dirty, "a remote visit is not echoed back")240    }241242    func testBookmarkBridgeBothWaysIncludingFoldersAndOrphans() throws {243        let store = BookmarkStore(db: try ProfileSchema.open(at: nil))244        let db = try mirror()245        let bridge = BookmarkSyncBridge(bookmarks: store, mirror: db)246        let folder = try XCTUnwrap(store.addFolder("Dev"))247        let page = try XCTUnwrap(store.add(title: "Swift", url: "https://swift.org", parentID: folder.id, tags: ["lang"]))248        bridge.captureLocalChanges()249        XCTAssertEqual(db.count("bookmarks"), 2)250        var edited = page; edited.title = "Swift.org"251        store.update(edited)252        store.delete(id: folder.id) // cascades to the page253        bridge.captureLocalChanges()254        XCTAssertEqual(db.count("bookmarks"), 0)255256        // Remote → local: child listed before its parent, plus an orphan whose folder no longer exists.257        let when = Date()258        func put(_ item: BookmarkSyncItem) { db.applyRemote("bookmarks", key: item.id, json: try! SyncDatabase.encoder.encode(item), modifiedAt: when, seq: 1, remoteID: "r" + item.id) }259        put(BookmarkSyncItem(id: "child", parentID: "folder", isFolder: false, title: "Child", url: "https://c.example", tags: ["x"], position: 0, createdAt: when))260        put(BookmarkSyncItem(id: "folder", parentID: nil, isFolder: true, title: "Folder", url: nil, tags: [], position: 0, createdAt: when))261        put(BookmarkSyncItem(id: "orphan", parentID: "gone", isFolder: false, title: "Orphan", url: "https://o.example", tags: [], position: 1, createdAt: when))262        bridge.applyRemoteChanges()263        XCTAssertEqual(store.bookmark(id: "child")?.parentID, "folder")264        XCTAssertNil(store.bookmark(id: "orphan")?.parentID)265        XCTAssertEqual(store.all().count, 3)266        bridge.applyRemoteChanges()267        XCTAssertEqual(store.all().count, 3)268    }269270    func testSyncedLibraryFavoritesReadingListSearchesAndTabs() throws {271        let library = SyncedLibrary(mirror: try mirror())272        library.addFavorite(title: "", url: "https://apple.com")273        library.addFavorite(title: "Dup", url: "https://apple.com")274        XCTAssertEqual(library.favorites().map(\.title), ["apple.com"])275        library.removeFavorite(url: "https://apple.com")276        XCTAssertTrue(library.favorites().isEmpty)277278        library.addToReadingList(title: "Long read", url: "https://e.com/long")279        XCTAssertTrue(library.isInReadingList("https://e.com/long"))280        library.setRead(library.readingList()[0], true)281        XCTAssertEqual(library.readingList().first?.isRead, true)282283        library.recordSearch("Apple Silicon benchmarks"); library.recordSearch("apple silicon  benchmarks"); library.recordSearch("swift concurrency"); library.recordSearch("apple pie")284        XCTAssertEqual(library.recentSearches().count, 3, "the same query in another case is one entry")285        XCTAssertEqual(library.searchSuggestions(for: "app").map(\.query), ["apple silicon benchmarks", "apple pie"])286        XCTAssertEqual(library.relatedSearches(to: "apple watch"), ["apple pie", "apple silicon benchmarks"])287        XCTAssertTrue(library.searchSuggestions(for: "zzz").isEmpty)288        library.clearSearches()289        XCTAssertTrue(library.recentSearches().isEmpty)290291        library.publishTabs([RemoteTab(title: "A", url: "https://a.example")], closed: [], deviceID: "me", deviceName: "Mac", platform: "macos")292        XCTAssertTrue(library.otherDevicesTabs(excluding: "me").isEmpty)293        XCTAssertEqual(library.otherDevicesTabs(excluding: "phone").first?.tabs.first?.url, "https://a.example")294        let before = library.mirror.record("tabs", key: "me")!.modifiedAt295        library.publishTabs([RemoteTab(title: "A", url: "https://a.example")], closed: [], deviceID: "me", deviceName: "Mac", platform: "macos")296        XCTAssertEqual(library.mirror.record("tabs", key: "me")!.modifiedAt, before, "unchanged tabs are not rewritten")297    }298299    func testRetryDelaysGrowAndAreCapped() {300        let delays = (1...8).map { SyncCoordinator.retryDelay(afterFailures: $0) }301        XCTAssertEqual(delays, [5, 15, 60, 300, 900, 900, 900, 900])302    }303}304
Files main
Menu

Theme