damus

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

NIP10Tests.swift (10208B)


      1 //
      2 //  NIP10Tests.swift
      3 //  damusTests
      4 //
      5 //  Created by William Casarin on 2024-04-25.
      6 //
      7 
      8 import XCTest
      9 @testable import damus
     10 
     11 final class NIP10Tests: XCTestCase {
     12 
     13     override func setUpWithError() throws {
     14         // Put setup code here. This method is called before the invocation of each test method in the class.
     15     }
     16 
     17     override func tearDownWithError() throws {
     18         // Put teardown code here. This method is called after the invocation of each test method in the class.
     19     }
     20 
     21     func testExample() throws {
     22         // This is an example of a functional test case.
     23         // Use XCTAssert and related functions to verify your tests produce the correct results.
     24         // Any test you write for XCTest can be annotated as throws and async.
     25         // Mark your test throws to produce an unexpected failure when your test encounters an uncaught error.
     26         // Mark your test async to allow awaiting for asynchronous code to complete. Check the results with assertions afterwards.
     27     }
     28 
     29     func test_root_with_mention_nip10() {
     30         let root_id_hex = "a32d70d331f4bea7a859ac71d85a9b4e0c2d1fa9aaf7237a17f85a6227f52fdb"
     31         let root_id = NoteId(hex: root_id_hex)!
     32         let mention_hex = "e47b7e156acec6881c89a53f1a9e349a982024245e2c398f8a5b4973b7a89ab3"
     33         let mention_id = NoteId(hex: mention_hex)!
     34 
     35         let tags =
     36             [["e", root_id_hex,"","root"],
     37              ["e", mention_hex,"","mention"],
     38              ["p","c4eabae1be3cf657bc1855ee05e69de9f059cb7a059227168b80b89761cbc4e0"],
     39              ["p","604e96e099936a104883958b040b47672e0f048c98ac793f37ffe4c720279eb2"],
     40              ["p","ffd375eb40eb486656a028edbc83825f58ff0d5c4a1ba22fe7745d284529ed08","","mention"],
     41              ["q","e47b7e156acec6881c89a53f1a9e349a982024245e2c398f8a5b4973b7a89ab3"]
     42             ]
     43 
     44         let note = NdbNote(content: "hi", keypair: test_keypair, kind: 1, tags: tags)!
     45         let thread = ThreadReply(tags: note.tags)
     46 
     47         XCTAssertNotNil(thread)
     48         guard let thread else { return }
     49 
     50         XCTAssertEqual(thread.root.note_id, root_id)
     51         XCTAssertEqual(thread.reply.note_id, root_id)
     52         XCTAssertEqual(thread.mention?.ref.note_id, mention_id)
     53     }
     54 
     55     func test_new_nip10() {
     56         let root_note_id_hex = "7c7d37bc8c04d2ec65cbc7d9275253e6b5cc34b5d10439f158194a3feefa8d52"
     57         let direct_reply_hex = "7c7d37bc8c04d2ec65cbc7d9275253e6b5cc34b5d10439f158194a3feefa8d51"
     58         let reply_hex = "7c7d37bc8c04d2ec65cbc7d9275253e6b5cc34b5d10439f158194a3feefa8d53"
     59         let mention_hex = "7c7d37bc8c04d2ec65cbc7d9275253e6b5cc34b5d10439f158194a3feefa8d54"
     60 
     61         let tags = [
     62             ["e", mention_hex, "", "mention"],
     63             ["e", direct_reply_hex, "", "reply"],
     64             ["e", root_note_id_hex, "", "root"],
     65             ["e", reply_hex, "", "reply"],
     66         ]
     67 
     68         let root_note_id = NoteId(hex: root_note_id_hex)!
     69         let reply_id = NoteId(hex: reply_hex)!
     70         let mention_id = NoteId(hex: mention_hex)!
     71 
     72         let note = NdbNote(content: "hi", keypair: test_keypair, kind: 1, tags: tags)!
     73         let tr = interp_event_refs_without_mentions_ndb(note.referenced_noterefs)
     74 
     75         XCTAssertEqual(tr?.root.note_id, root_note_id)
     76         XCTAssertEqual(tr?.reply.note_id, reply_id)
     77         XCTAssertEqual(tr?.mention?.ref.note_id, mention_id)
     78     }
     79 
     80     func test_repost_root() {
     81         let mention_hex = "7c7d37bc8c04d2ec65cbc7d9275253e6b5cc34b5d10439f158194a3feefa8d52"
     82         let tags = [
     83             ["e", mention_hex, "", "mention"],
     84         ]
     85 
     86         let mention_id = NoteId(hex: mention_hex)!
     87         let note = NdbNote(content: "hi", keypair: test_keypair, kind: 1, tags: tags)!
     88         let tr = note.thread_reply()
     89 
     90         XCTAssertNil(tr)
     91     }
     92     
     93     func test_direct_reply_old_nip10() {
     94         let root_note_id_hex = "7c7d37bc8c04d2ec65cbc7d9275253e6b5cc34b5d10439f158194a3feefa8d52"
     95         let tags = [
     96             ["e", root_note_id_hex],
     97         ]
     98 
     99         let root_note_id = NoteId(hex: root_note_id_hex)!
    100 
    101         let note = NdbNote(content: "hi", keypair: test_keypair, kind: 1, tags: tags)!
    102         let tr = note.thread_reply()
    103 
    104         XCTAssertNotNil(tr)
    105         guard let tr else { return }
    106 
    107         XCTAssertEqual(tr.root.note_id, root_note_id)
    108         XCTAssertEqual(tr.reply.note_id, root_note_id)
    109         XCTAssertEqual(tr.is_reply_to_root, true)
    110     }
    111 
    112     func test_direct_reply_new_nip10() {
    113         let root_note_id_hex = "7c7d37bc8c04d2ec65cbc7d9275253e6b5cc34b5d10439f158194a3feefa8d52"
    114         let tags = [
    115             ["e", root_note_id_hex, "", "root"],
    116         ]
    117 
    118         let root_note_id = NoteId(hex: root_note_id_hex)!
    119 
    120         let note = NdbNote(content: "hi", keypair: test_keypair, kind: 1, tags: tags)!
    121         let tr = note.thread_reply()
    122         XCTAssertNotNil(tr)
    123         guard let tr else { return }
    124 
    125         XCTAssertEqual(tr.root.note_id, root_note_id)
    126         XCTAssertEqual(tr.reply.note_id, root_note_id)
    127         XCTAssertNil(tr.mention)
    128         XCTAssertEqual(tr.is_reply_to_root, true)
    129     }
    130     
    131     // seen in the wild by the gleasonator
    132     func test_single_marker() {
    133         let root_note_id_hex = "7c7d37bc8c04d2ec65cbc7d9275253e6b5cc34b5d10439f158194a3feefa8d52"
    134         let tags = [
    135             ["e", root_note_id_hex, "", "reply"],
    136         ]
    137         
    138         let root_note_id = NoteId(hex: root_note_id_hex)!
    139         let note = NdbNote(content: "hi", keypair: test_keypair, kind: 1, tags: tags)!
    140         let tr = note.thread_reply()
    141         XCTAssertNotNil(tr)
    142         guard let tr else { return }
    143 
    144         XCTAssertNil(tr.mention)
    145         XCTAssertEqual(tr.root.note_id, root_note_id)
    146         XCTAssertEqual(tr.reply.note_id, root_note_id)
    147         XCTAssertEqual(tr.is_reply_to_root, true)
    148     }
    149 
    150     func test_marker_reply() {
    151         let note_json = """
    152         {
    153           "pubkey": "5b0183ab6c3e322bf4d41c6b3aef98562a144847b7499543727c5539a114563e",
    154           "content": "Can’t zap you btw",
    155           "id": "a8dc8b74852d7ad114d5d650b2125459c0cba3c1fdcaaf527e03f24082e11ab3",
    156           "created_at": 1715275773,
    157           "sig": "4ee5d8f954c6c087ce51ad02d30dd226eea939cd9ef4e8a8ce4bfaf3aba0a852316cfda83ce3fc9a3d98392a738e7c6b036a3b2aced1392db1be3ca190835a17",
    158           "kind": 1,
    159           "tags": [
    160             [
    161               "e",
    162               "1bb940ce0ba0d4a3b2a589355d908498dcd7452f941cf520072218f7e6ede75e",
    163               "wss://relay.nostrplebs.com",
    164               "reply"
    165             ],
    166             [
    167               "p",
    168               "6e75f7972397ca3295e0f4ca0fbc6eb9cc79be85bafdd56bd378220ca8eee74e"
    169             ],
    170             [
    171               "e",
    172               "00152d2945459fb394fed2ea95af879c903c4ec42d96327a739fa27c023f20e0",
    173               "wss://nostr.mutinywallet.com/",
    174               "root"
    175             ]
    176           ]
    177         }
    178         """;
    179 
    180         let replying_to_hex = "a8dc8b74852d7ad114d5d650b2125459c0cba3c1fdcaaf527e03f24082e11ab3"
    181         let pk = Pubkey(hex: "5b0183ab6c3e322bf4d41c6b3aef98562a144847b7499543727c5539a114563e")!
    182         //let last_reply_hex = "1bb940ce0ba0d4a3b2a589355d908498dcd7452f941cf520072218f7e6ede75e"
    183         let note = decode_nostr_event_json(json: note_json)!
    184         let reply = build_post(state: test_damus_state, post: .init(string: "hello"), action: .replying_to(note), uploadedMedias: [], pubkeys: [pk] + note.referenced_pubkeys.map({pk in pk}))
    185         let root_hex = "00152d2945459fb394fed2ea95af879c903c4ec42d96327a739fa27c023f20e0"
    186 
    187         XCTAssertEqual(reply.tags,
    188             [
    189                 ["e", root_hex, "wss://nostr.mutinywallet.com/", "root"],
    190                 ["e", replying_to_hex, "", "reply"],
    191                 ["p", "5b0183ab6c3e322bf4d41c6b3aef98562a144847b7499543727c5539a114563e"],
    192                 ["p", "6e75f7972397ca3295e0f4ca0fbc6eb9cc79be85bafdd56bd378220ca8eee74e"],
    193             ])
    194     }
    195 
    196     func test_mixed_nip10() {
    197 
    198         let root_note_id_hex = "27e71cf53299dafb5dc7bcc0a078357418a4375cb1097bf5184662493f79a627"
    199         let reply_hex = "1a616998552cf76e9786f76ac68f6104cdae46377330735c68bfe0b9426d2fa8"
    200 
    201         let tags = [
    202             [ "e", root_note_id_hex, "", "root" ],
    203             [ "e", "f99046bd87be7508d55e139de48517c06ef90830d77a5d3213df858d77bb2f8f" ],
    204             [ "e", reply_hex, "", "reply" ],
    205             [ "p", "3efdaebb1d8923ebd99c9e7ace3b4194ab45512e2be79c1b7d68d9243e0d2681" ],
    206             [ "p", "8ea485266b2285463b13bf835907161c22bb3da1e652b443db14f9cee6720a43" ],
    207             [ "p", "32e1827635450ebb3c5a7d12c1f8e7b2b514439ac10a67eef3d9fd9c5c68e245" ]
    208         ]
    209         
    210 
    211         let root_note_id = NoteId(hex: root_note_id_hex)!
    212         let reply_id = NoteId(hex: reply_hex)!
    213 
    214         let note = NdbNote(content: "hi", keypair: test_keypair, kind: 1, tags: tags)!
    215         let tr = note.thread_reply()
    216         XCTAssertNotNil(tr)
    217         guard let tr else { return }
    218 
    219         XCTAssertEqual(tr.root.note_id, root_note_id)
    220         XCTAssertEqual(tr.reply.note_id, reply_id)
    221         XCTAssertEqual(tr.is_reply_to_root, false)
    222     }
    223 
    224     func test_deprecated_nip10() {
    225         let root_note_id_hex = "7c7d37bc8c04d2ec65cbc7d9275253e6b5cc34b5d10439f158194a3feefa8d52"
    226         let direct_reply_hex = "7c7d37bc8c04d2ec65cbc7d9275253e6b5cc34b5d10439f158194a3feefa8d51"
    227         let reply_hex = "7c7d37bc8c04d2ec65cbc7d9275253e6b5cc34b5d10439f158194a3feefa8d53"
    228         let tags = [
    229             ["e", root_note_id_hex],
    230             ["e", direct_reply_hex],
    231             ["e", reply_hex],
    232         ]
    233 
    234         let root_note_id = NoteId(hex: root_note_id_hex)!
    235         let direct_reply_id = NoteId(hex: direct_reply_hex)!
    236         let reply_id = NoteId(hex: reply_hex)!
    237 
    238         let note = NdbNote(content: "hi", keypair: test_keypair, kind: 1, tags: tags)!
    239         let tr = note.thread_reply()
    240         XCTAssertNotNil(tr)
    241         guard let tr else { return }
    242 
    243         XCTAssertEqual(tr.root.note_id, root_note_id)
    244         XCTAssertEqual(tr.reply.note_id, reply_id)
    245         XCTAssertEqual(tr.is_reply_to_root, false)
    246     }
    247 
    248 
    249 
    250     func testPerformanceExample() throws {
    251         // This is an example of a performance test case.
    252         self.measure {
    253             // Put the code you want to measure the time of here.
    254         }
    255     }
    256 
    257 }