damus

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

NdbTests.swift (8238B)


      1 //
      2 //  NDBIterTests.swift
      3 //  damusTests
      4 //
      5 //  Created by William Casarin on 2023-07-21.
      6 //
      7 
      8 import XCTest
      9 @testable import damus
     10 
     11 func test_ndb_dir() -> String? {
     12     do {
     13         let fileManager = FileManager.default
     14         let tempDir = fileManager.temporaryDirectory.appendingPathComponent(UUID().uuidString)
     15         try fileManager.createDirectory(at: tempDir, withIntermediateDirectories: true, attributes: nil)
     16         return remove_file_prefix(tempDir.absoluteString)
     17     } catch {
     18         return nil
     19     }
     20 }
     21 
     22 final class NdbTests: XCTestCase {
     23     var db_dir: String = ""
     24 
     25     override func setUpWithError() throws {
     26         guard let db = test_ndb_dir() else {
     27             XCTFail("Could not create temp directory")
     28             return
     29         }
     30         db_dir = db
     31     }
     32 
     33     override func tearDownWithError() throws {
     34         // Put teardown code here. This method is called after the invocation of each test method in the class.
     35     }
     36 
     37     func test_decode_eose() throws {
     38         let json = "[\"EOSE\",\"DC268DBD-55DA-458A-B967-540925AF3497\"]"
     39         let resp = decode_nostr_event(txt: json)
     40         XCTAssertNotNil(resp)
     41     }
     42 
     43     func test_decode_command_result() throws {
     44         let json = "[\"OK\",\"b1d8f68d39c07ce5c5ea10c235100d529b2ed2250140b36a35d940b712dc6eff\",true,\"\"]"
     45         let resp = decode_nostr_event(txt: json)
     46         XCTAssertNotNil(resp)
     47 
     48     }
     49 
     50     func test_profile_creation() {
     51         let profile = make_test_profile()
     52         XCTAssertEqual(profile.name, "jb55")
     53     }
     54 
     55     func test_ndb_init() {
     56 
     57         do {
     58             let ndb = Ndb(path: db_dir)!
     59             let ok = ndb.process_events(test_wire_events)
     60             XCTAssertTrue(ok)
     61         }
     62 
     63         do {
     64             let ndb = Ndb(path: db_dir)!
     65             let id = NoteId(hex: "d12c17bde3094ad32f4ab862a6cc6f5c289cfe7d5802270bdf34904df585f349")!
     66             guard let txn = NdbTxn(ndb: ndb) else { return XCTAssert(false) }
     67             let note = ndb.lookup_note_with_txn(id: id, txn: txn)
     68             XCTAssertNotNil(note)
     69             guard let note else { return }
     70             let pk = Pubkey(hex: "32e1827635450ebb3c5a7d12c1f8e7b2b514439ac10a67eef3d9fd9c5c68e245")!
     71             XCTAssertEqual(note.pubkey, pk)
     72 
     73             let profile = ndb.lookup_profile_with_txn(pk, txn: txn)
     74             XCTAssertNotNil(profile)
     75             guard let profile else { return }
     76 
     77             XCTAssertEqual(profile.profile?.name, "jb55")
     78             XCTAssertEqual(profile.lnurl, nil)
     79         }
     80 
     81 
     82     }
     83 
     84     func test_ndb_search() throws {
     85         do {
     86             let ndb = Ndb(path: db_dir)!
     87             let ok = ndb.process_events(test_wire_events)
     88             XCTAssertTrue(ok)
     89         }
     90         
     91         do {
     92             let ndb = Ndb(path: db_dir)!
     93             let note_ids = ndb.text_search(query: "barked")
     94             XCTAssertEqual(note_ids.count, 1)
     95             let expected_note_id = NoteId(hex: "b17a540710fe8495b16bfbaf31c6962c4ba8387f3284a7973ad523988095417e")!
     96             guard note_ids.count > 0 else {
     97                 XCTFail("Expected at least one note to be found")
     98                 return
     99             }
    100             let note_id = ndb.lookup_note_by_key(note_ids[0])?.map({ n in n?.id }).value
    101             XCTAssertEqual(note_id, .some(expected_note_id))
    102         }
    103     }
    104 
    105     func test_ndb_note() throws {
    106         let note = NdbNote.owned_from_json(json: test_contact_list_json)
    107         XCTAssertNotNil(note)
    108         guard let note else { return }
    109 
    110         let id = NoteId(hex: "20d0ff27d6fcb13de8366328c5b1a7af26bcac07f2e558fbebd5e9242e608c09")!
    111         let pubkey = Pubkey(hex: "32e1827635450ebb3c5a7d12c1f8e7b2b514439ac10a67eef3d9fd9c5c68e245")!
    112 
    113         XCTAssertEqual(note.id, id)
    114         XCTAssertEqual(note.pubkey, pubkey)
    115 
    116         XCTAssertEqual(note.count, 34328)
    117         XCTAssertEqual(note.kind, 3)
    118         XCTAssertEqual(note.created_at, 1689904312)
    119 
    120         let expected_count: UInt16 = 786
    121         XCTAssertEqual(note.tags.count, expected_count)
    122         XCTAssertEqual(note.tags.reduce(0, { sum, _ in sum + 1 }), expected_count)
    123 
    124         var tags = 0
    125         var total_count_stored = 0
    126         var total_count_iter = 0
    127         //let tags = note.tags()
    128         for tag in note.tags {
    129             total_count_stored += Int(tag.count)
    130 
    131             if tags == 0 || tags == 1 || tags == 2 {
    132                 XCTAssertEqual(tag.count, 3)
    133             }
    134 
    135             if tags == 6 {
    136                 XCTAssertEqual(tag.count, 2)
    137             }
    138 
    139             if tags == 7 {
    140                 XCTAssertEqual(tag[2].string(), "wss://nostr-pub.wellorder.net")
    141             }
    142 
    143             for elem in tag {
    144                 print("tag[\(tags)][\(elem.index)]")
    145                 total_count_iter += 1
    146             }
    147 
    148             tags += 1
    149         }
    150 
    151         XCTAssertEqual(tags, 786)
    152         XCTAssertEqual(total_count_stored, total_count_iter)
    153     }
    154     
    155     /// Based on https://github.com/damus-io/damus/issues/1468
    156     /// Tests whether a JSON with optional escaped slash characters is correctly unescaped (In accordance to https://datatracker.ietf.org/doc/html/rfc8259#section-7)
    157     func test_decode_json_with_escaped_slashes() {
    158         let testJSONWithEscapedSlashes = "{\"tags\":[],\"pubkey\":\"f8e6c64342f1e052480630e27e1016dce35fc3a614e60434fef4aa2503328ca9\",\"content\":\"https:\\/\\/cdn.nostr.build\\/i\\/5c1d3296f66c2630131bf123106486aeaf051ed8466031c0e0532d70b33cddb2.jpg\",\"created_at\":1691864981,\"kind\":1,\"sig\":\"fc0033aa3d4df50b692a5b346fa816fdded698de2045e36e0642a021391468c44ca69c2471adc7e92088131872d4aaa1e90ea6e1ad97f3cc748f4aed96dfae18\",\"id\":\"e8f6eca3b161abba034dac9a02bb6930ecde9fd2fb5d6c5f22a05526e11382cb\"}"
    159         let testNote = NdbNote.owned_from_json(json: testJSONWithEscapedSlashes)!
    160         XCTAssertEqual(testNote.content, "https://cdn.nostr.build/i/5c1d3296f66c2630131bf123106486aeaf051ed8466031c0e0532d70b33cddb2.jpg")
    161     }
    162     
    163     func test_inherited_transactions() throws {
    164         let ndb = Ndb(path: db_dir)!
    165         do {
    166             guard let txn1 = NdbTxn(ndb: ndb) else { return XCTAssert(false) }
    167 
    168             let ntxn = (Thread.current.threadDictionary.value(forKey: "ndb_txn") as? ndb_txn)!
    169             XCTAssertEqual(txn1.txn.lmdb, ntxn.lmdb)
    170             XCTAssertEqual(txn1.txn.mdb_txn, ntxn.mdb_txn)
    171 
    172             guard let txn2 = NdbTxn(ndb: ndb) else { return XCTAssert(false) }
    173 
    174             XCTAssertEqual(txn1.inherited, false)
    175             XCTAssertEqual(txn2.inherited, true)
    176         }
    177 
    178         let ndb_txn = Thread.current.threadDictionary.value(forKey: "ndb_txn")
    179         XCTAssertNil(ndb_txn)
    180     }
    181 
    182     func test_decode_perf() throws {
    183         // This is an example of a performance test case.
    184         self.measure {
    185             _ = NdbNote.owned_from_json(json: test_contact_list_json)
    186         }
    187     }
    188 
    189     func test_perf_old_decoding() {
    190         self.measure {
    191             let event = decode_nostr_event_json(test_contact_list_json)
    192             XCTAssertNotNil(event)
    193         }
    194     }
    195 
    196     func test_perf_old_iter()  {
    197         self.measure {
    198             let event = decode_nostr_event_json(test_contact_list_json)
    199             XCTAssertNotNil(event)
    200         }
    201     }
    202 
    203     func longer_iter(_ n: Int = 1000) -> XCTMeasureOptions {
    204         let opts = XCTMeasureOptions()
    205         opts.iterationCount = n
    206         return opts
    207     }
    208 
    209     func test_iteration_perf() throws {
    210         guard let note = NdbNote.owned_from_json(json: test_contact_list_json) else {
    211             XCTAssert(false)
    212             return
    213         }
    214 
    215 
    216         self.measure {
    217             var count = 0
    218             var char_count = 0
    219 
    220             for tag in note.tags {
    221                 for elem in tag {
    222                     print("iter_elem \(elem.string())")
    223                     for c in elem {
    224                         if char_count == 0 {
    225                             let ac = AsciiCharacter(c)
    226                             XCTAssertEqual(ac, "p")
    227                         } else if char_count == 0 {
    228                             XCTAssertEqual(c, 0x6c)
    229                         }
    230                         char_count += 1
    231                     }
    232                 }
    233                 count += 1
    234             }
    235 
    236             XCTAssertEqual(count, 786)
    237             XCTAssertEqual(char_count, 24370)
    238         }
    239 
    240     }
    241 
    242 }
    243