damus

nostr ios client
git clone git://jb55.com/damus
Log | Files | Refs | README | LICENSE

UserSearchCacheTests.swift (6048B)


      1 //
      2 //  UserSearchCacheTests.swift
      3 //  damusTests
      4 //
      5 //  Created by Terry Yiu on 6/30/23.
      6 //
      7 
      8 import XCTest
      9 @testable import damus
     10 
     11 // TODO: Update these tests to work with NostrDB Profile changes (https://github.com/damus-io/damus/issues/1586)
     12 final class UserSearchCacheTests: XCTestCase {
     13     /*
     14     var keypair: FullKeypair? = nil
     15     let damusState = test_damus_state
     16     let nip05 = "_@somedomain.com"
     17 
     18     @MainActor
     19     override func setUpWithError() throws {
     20         keypair = try XCTUnwrap(generate_new_keypair())
     21 
     22         if let keypair {
     23             let pubkey = keypair.pubkey
     24             let validatedNip05 = try XCTUnwrap(NIP05.parse(nip05))
     25 
     26             damusState.profiles.set_validated(pubkey, nip05: validatedNip05)
     27 
     28             let profile = Profile(name: "tyiu", display_name: "Terry Yiu", about: nil, picture: nil, banner: nil, website: nil, lud06: nil, lud16: nil, nip05: nip05, damus_donation: nil)
     29 
     30             // Lookup to synchronize access on profiles dictionary to avoid race conditions.
     31             let _ = damusState.profiles.lookup(id: pubkey)
     32         }
     33     }
     34 
     35     override func tearDown() {
     36         keypair = nil
     37     }
     38 
     39     func testSearch() throws {
     40         let keypair = try XCTUnwrap(keypair)
     41         XCTAssertEqual(damusState.user_search_cache.search(key: "tyiu"), [keypair.pubkey])
     42         XCTAssertEqual(damusState.user_search_cache.search(key: "ty"), [keypair.pubkey])
     43         XCTAssertEqual(damusState.user_search_cache.search(key: "terry yiu"), [keypair.pubkey])
     44         XCTAssertEqual(damusState.user_search_cache.search(key: "rry"), [keypair.pubkey])
     45         XCTAssertEqual(damusState.user_search_cache.search(key: "somedomain"), [keypair.pubkey])
     46         XCTAssertEqual(damusState.user_search_cache.search(key: "dom"), [keypair.pubkey])
     47         XCTAssertEqual(damusState.user_search_cache.search(key: "i"), [keypair.pubkey])
     48     }
     49 
     50     @MainActor
     51     func testUpdateProfile() throws {
     52         let keypair = try XCTUnwrap(keypair)
     53 
     54         let newNip05 = "_@other.xyz"
     55         _ = try XCTUnwrap(NIP05.parse(newNip05))
     56 
     57         damusState.profiles.set_validated(keypair.pubkey, nip05: NIP05.parse(newNip05))
     58 
     59         // Old profile attributes are removed from cache.
     60         XCTAssertEqual(damusState.user_search_cache.search(key: "tyiu"), [])
     61         XCTAssertEqual(damusState.user_search_cache.search(key: "ty"), [])
     62         XCTAssertEqual(damusState.user_search_cache.search(key: "Terry Yiu"), [])
     63         XCTAssertEqual(damusState.user_search_cache.search(key: "rry"), [])
     64         XCTAssertEqual(damusState.user_search_cache.search(key: "somedomain"), [])
     65         XCTAssertEqual(damusState.user_search_cache.search(key: "dom"), [])
     66 
     67         // New profile attributes are added to cache.
     68         XCTAssertEqual(damusState.user_search_cache.search(key: "whoami"), [keypair.pubkey])
     69         XCTAssertEqual(damusState.user_search_cache.search(key: "hoa"), [keypair.pubkey])
     70         XCTAssertEqual(damusState.user_search_cache.search(key: "t-dawg"), [keypair.pubkey])
     71         XCTAssertEqual(damusState.user_search_cache.search(key: "daw"), [keypair.pubkey])
     72         XCTAssertEqual(damusState.user_search_cache.search(key: "other"), [keypair.pubkey])
     73         XCTAssertEqual(damusState.user_search_cache.search(key: "xyz"), [keypair.pubkey])
     74         XCTAssertEqual(damusState.user_search_cache.search(key: "the"), [keypair.pubkey])
     75         XCTAssertEqual(damusState.user_search_cache.search(key: "y"), [keypair.pubkey])
     76     }
     77     */
     78 
     79     /*
     80     func testUpdateOwnContactsPetnames() throws {
     81         let keypair = try XCTUnwrap(keypair)
     82         let damus = Pubkey(hex: "3efdaebb1d8923ebd99c9e7ace3b4194ab45512e2be79c1b7d68d9243e0d2681")!
     83         let jb55 = Pubkey(hex: "32e1827635450ebb3c5a7d12c1f8e7b2b514439ac10a67eef3d9fd9c5c68e245")!
     84 
     85         var pubkeysToPetnames = [Pubkey: String]()
     86         pubkeysToPetnames[damus] = "damus"
     87         pubkeysToPetnames[jb55] = "jb55"
     88 
     89         let contactsEvent = try createContactsEventWithPetnames(pubkeysToPetnames: pubkeysToPetnames)
     90 
     91         // Initial own contacts event caching on searchable petnames.
     92         damusState.user_search_cache.updateOwnContactsPetnames(id: keypair.pubkey, oldEvent: nil, newEvent: contactsEvent)
     93 
     94         XCTAssertEqual(damusState.user_search_cache.search(key: "damus"), [damus])
     95         XCTAssertEqual(damusState.user_search_cache.search(key: "jb55"), [jb55])
     96         XCTAssertEqual(damusState.user_search_cache.search(key: "5"), [jb55])
     97 
     98         // Replace one of the petnames and verify if the cache updates accordingly.
     99 
    100         pubkeysToPetnames.removeValue(forKey: jb55)
    101         pubkeysToPetnames[jb55] = "bill"
    102         let newContactsEvent = try createContactsEventWithPetnames(pubkeysToPetnames: pubkeysToPetnames)
    103 
    104         damusState.user_search_cache.updateOwnContactsPetnames(id: keypair.pubkey, oldEvent: contactsEvent, newEvent: newContactsEvent)
    105 
    106         XCTAssertEqual(damusState.user_search_cache.search(key: "damus"), [damus])
    107         XCTAssertEqual(damusState.user_search_cache.search(key: "jb55"), [])
    108         XCTAssertEqual(damusState.user_search_cache.search(key: "5"), [])
    109         XCTAssertEqual(damusState.user_search_cache.search(key: "bill"), [jb55])
    110         XCTAssertEqual(damusState.user_search_cache.search(key: "l"), [jb55])
    111     }
    112     */
    113 
    114     /*
    115     private func createContactsEventWithPetnames(pubkeysToPetnames: [Pubkey: String]) throws -> NostrEvent {
    116         let keypair = try XCTUnwrap(keypair)
    117 
    118         let bootstrapRelays = load_bootstrap_relays(pubkey: keypair.pubkey)
    119         let relayInfo = RelayInfo(read: true, write: true)
    120         var relays: [String: RelayInfo] = [:]
    121 
    122         for relay in bootstrapRelays {
    123             relays[relay] = relayInfo
    124         }
    125 
    126         let relayJson = encode_json(relays)!
    127 
    128         let tags: [[String]] = pubkeysToPetnames.enumerated().map {
    129             ["p", $0.element.key.description, "", $0.element.value]
    130         }
    131 
    132         return NostrEvent(content: relayJson, keypair: keypair.to_keypair(), kind: NostrKind.contacts.rawValue, tags: tags)!
    133     }
    134      */
    135 
    136 }