Bech32ObjectTests.swift (10511B)
1 // 2 // Bech32ObjectTests.swift 3 // damusTests 4 // 5 // Created by KernelKind on 1/5/24. 6 // 7 // This file contains tests that are adapted from the nostr-sdk-ios project. 8 // Original source: 9 // https://github.com/nostr-sdk/nostr-sdk-ios/blob/main/Tests/NostrSDKTests/MetadataCodingTests.swift 10 // 11 12 import XCTest 13 @testable import damus 14 15 class Bech32ObjectTests: XCTestCase { 16 func testTLVParsing_NeventHasRelaysNoAuthorNoKind_ValidContent() throws { 17 let content = "nevent1qqstna2yrezu5wghjvswqqculvvwxsrcvu7uc0f78gan4xqhvz49d9spr3mhxue69uhkummnw3ez6un9d3shjtn4de6x2argwghx6egpr4mhxue69uhkummnw3ez6ur4vgh8wetvd3hhyer9wghxuet5nxnepm" 18 let expectedNoteIDHex = "b9f5441e45ca39179320e0031cfb18e34078673dcc3d3e3a3b3a981760aa5696" 19 let relays = ["wss://nostr-relay.untethr.me", "wss://nostr-pub.wellorder.net"].compactMap(RelayURL.init) 20 guard let noteid = hex_decode_noteid(expectedNoteIDHex) else { 21 XCTFail("Parsing note ID failed") 22 return 23 } 24 25 let expectedObject = Bech32Object.nevent(NEvent(noteid: noteid, relays: relays)) 26 guard let actualObject = Bech32Object.parse(content) else { 27 XCTFail("Invalid Object") 28 return 29 } 30 31 XCTAssertEqual(expectedObject, actualObject) 32 } 33 34 func testTLVParsing_NeventHasRelaysNoAuthorHasKind_ValidContent() throws { 35 let content = "nevent1qqstna2yrezu5wghjvswqqculvvwxsrcvu7uc0f78gan4xqhvz49d9spr3mhxue69uhkummnw3ez6un9d3shjtn4de6x2argwghx6egpr4mhxue69uhkummnw3ez6ur4vgh8wetvd3hhyer9wghxuet5qvzqqqqqqyjyqz7d" 36 let expectedNoteIDHex = "b9f5441e45ca39179320e0031cfb18e34078673dcc3d3e3a3b3a981760aa5696" 37 let relays = ["wss://nostr-relay.untethr.me", "wss://nostr-pub.wellorder.net"].compactMap(RelayURL.init) 38 guard let noteid = hex_decode_noteid(expectedNoteIDHex) else { 39 XCTFail("Parsing note ID failed") 40 return 41 } 42 43 let expectedObject = Bech32Object.nevent(NEvent(noteid: noteid, relays: relays, kind: 1)) 44 guard let actualObject = Bech32Object.parse(content) else { 45 XCTFail("Invalid Object") 46 return 47 } 48 49 XCTAssertEqual(expectedObject, actualObject) 50 } 51 52 func testTLVParsing_NeventHasRelaysHasAuthorHasKind_ValidContent() throws { 53 let content = "nevent1qqstna2yrezu5wghjvswqqculvvwxsrcvu7uc0f78gan4xqhvz49d9spr3mhxue69uhkummnw3ez6un9d3shjtn4de6x2argwghx6egpr4mhxue69uhkummnw3ez6ur4vgh8wetvd3hhyer9wghxuet5qgsrhuxx8l9ex335q7he0f09aej04zpazpl0ne2cgukyawd24mayt8grqsqqqqqpw4032x" 54 55 let expectedNoteIDHex = "b9f5441e45ca39179320e0031cfb18e34078673dcc3d3e3a3b3a981760aa5696" 56 let relays = ["wss://nostr-relay.untethr.me", "wss://nostr-pub.wellorder.net"].compactMap(RelayURL.init) 57 guard let noteid = hex_decode_noteid(expectedNoteIDHex) else { 58 XCTFail("Parsing note ID failed") 59 return 60 } 61 guard let author = try bech32_decode("npub180cvv07tjdrrgpa0j7j7tmnyl2yr6yr7l8j4s3evf6u64th6gkwsyjh6w6") else { 62 XCTFail() 63 return 64 } 65 66 let expectedObject = Bech32Object.nevent(NEvent(noteid: noteid, relays: relays, author: Pubkey(author.data), kind: 1)) 67 guard let actualObject = Bech32Object.parse(content) else { 68 XCTFail("Invalid Object") 69 return 70 } 71 72 XCTAssertEqual(expectedObject, actualObject) 73 } 74 75 func testTLVParsing_NProfileExample_ValidContent() throws { 76 let content = "nprofile1qqsrhuxx8l9ex335q7he0f09aej04zpazpl0ne2cgukyawd24mayt8gpp4mhxue69uhhytnc9e3k7mgpz4mhxue69uhkg6nzv9ejuumpv34kytnrdaksjlyr9p" 77 guard let author = try bech32_decode("npub180cvv07tjdrrgpa0j7j7tmnyl2yr6yr7l8j4s3evf6u64th6gkwsyjh6w6") else { 78 XCTFail() 79 return 80 } 81 let relays = ["wss://r.x.com", "wss://djbas.sadkb.com"].compactMap(RelayURL.init) 82 83 let expectedObject = Bech32Object.nprofile(NProfile(author: Pubkey(author.data), relays: relays)) 84 guard let actualObject = Bech32Object.parse(content) else { 85 XCTFail("Invalid Object") 86 return 87 } 88 89 XCTAssertEqual(expectedObject, actualObject) 90 } 91 92 func testTLVParsing_NaddrExample_ValidContent() throws { 93 let content = "naddr1qqxnzdesxqmnxvpexqunzvpcqyt8wumn8ghj7un9d3shjtnwdaehgu3wvfskueqzypve7elhmamff3sr5mgxxms4a0rppkmhmn7504h96pfcdkpplvl2jqcyqqq823cnmhuld" 94 95 guard let author = try bech32_decode("npub1tx0k0a7lw62vvqax6p3ku90tccgdka7ul4radews2wrdsg0m865szf9fw6") else { 96 XCTFail("Can't decode npub") 97 return 98 } 99 let relays = ["wss://relay.nostr.band"].compactMap(RelayURL.init) 100 let identifier = "1700730909108" 101 let kind: UInt32 = 30023 102 103 let expectedObject = Bech32Object.naddr(NAddr(identifier: identifier, author: Pubkey(author.data), relays: relays, kind: kind)) 104 let actualObject = Bech32Object.parse(content) 105 106 XCTAssertEqual(expectedObject, actualObject) 107 } 108 109 func testTLVEncoding_NeventHasRelaysNoAuthorNoKind_ValidContent() throws { 110 guard let noteid = hex_decode_noteid("b9f5441e45ca39179320e0031cfb18e34078673dcc3d3e3a3b3a981760aa5696") else { 111 XCTFail("Parsing note ID failed") 112 return 113 } 114 115 let relays = ["wss://nostr-relay.untethr.me", "wss://nostr-pub.wellorder.net"].compactMap(RelayURL.init) 116 117 let expectedEncoding = "nevent1qqstna2yrezu5wghjvswqqculvvwxsrcvu7uc0f78gan4xqhvz49d9spr3mhxue69uhkummnw3ez6un9d3shjtn4de6x2argwghx6egpr4mhxue69uhkummnw3ez6ur4vgh8wetvd3hhyer9wghxuet5nxnepm" 118 119 let actualEncoding = Bech32Object.encode(.nevent(NEvent(noteid: noteid, relays: relays))) 120 121 XCTAssertEqual(expectedEncoding, actualEncoding) 122 } 123 124 func testTLVEncoding_NeventHasRelaysNoAuthorHasKind_ValidContent() throws { 125 guard let noteid = hex_decode_noteid("b9f5441e45ca39179320e0031cfb18e34078673dcc3d3e3a3b3a981760aa5696") else { 126 XCTFail() 127 return 128 } 129 130 let relays = [ 131 "wss://nostr-relay.untethr.me", 132 "wss://nostr-pub.wellorder.net" 133 ].compactMap(RelayURL.init) 134 135 let expectedEncoding = "nevent1qqstna2yrezu5wghjvswqqculvvwxsrcvu7uc0f78gan4xqhvz49d9spr3mhxue69uhkummnw3ez6un9d3shjtn4de6x2argwghx6egpr4mhxue69uhkummnw3ez6ur4vgh8wetvd3hhyer9wghxuet5qvzqqqqqqyjyqz7d" 136 137 let actualEncoding = Bech32Object.encode(.nevent(NEvent(noteid: noteid, relays: relays, kind: 1))) 138 139 XCTAssertEqual(expectedEncoding, actualEncoding) 140 } 141 142 func testTLVEncoding_NeventHasRelaysHasAuthorHasKind_ValidContent() throws { 143 guard let noteid = hex_decode_noteid("b9f5441e45ca39179320e0031cfb18e34078673dcc3d3e3a3b3a981760aa5696") else { 144 XCTFail("Parsing note ID failed") 145 return 146 } 147 guard let author = try bech32_decode("npub180cvv07tjdrrgpa0j7j7tmnyl2yr6yr7l8j4s3evf6u64th6gkwsyjh6w6") else { 148 XCTFail() 149 return 150 } 151 152 let relays = ["wss://nostr-relay.untethr.me", "wss://nostr-pub.wellorder.net"].compactMap(RelayURL.init) 153 154 let expectedEncoding = "nevent1qqstna2yrezu5wghjvswqqculvvwxsrcvu7uc0f78gan4xqhvz49d9spr3mhxue69uhkummnw3ez6un9d3shjtn4de6x2argwghx6egpr4mhxue69uhkummnw3ez6ur4vgh8wetvd3hhyer9wghxuet5qgsrhuxx8l9ex335q7he0f09aej04zpazpl0ne2cgukyawd24mayt8grqsqqqqqpw4032x" 155 156 let actualEncoding = Bech32Object.encode(.nevent(NEvent(noteid: noteid, relays: relays, author: Pubkey(author.data), kind: 1))) 157 158 XCTAssertEqual(expectedEncoding, actualEncoding) 159 } 160 161 func testTLVEncoding_NeventFromNostrEvent_ValidContent() throws { 162 let relay_strings = ["wss://relay.damus.io", "wss://relay.nostr.band"] 163 let relays = relay_strings.map({ RelayURL($0)! }) 164 let nevent = NEvent(event: test_note, relays: relays) 165 166 XCTAssertEqual(nevent.noteid, test_note.id) 167 XCTAssertEqual(nevent.relays, relays) 168 XCTAssertEqual(nevent.author, test_note.pubkey) 169 XCTAssertEqual(nevent.kind, test_note.kind) 170 171 let expectedEncoding = "nevent1qqsqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqpz3mhxue69uhhyetvv9ujuerpd46hxtnfduq3vamnwvaz7tmjv4kxz7fwdehhxarj9e3xzmnyqgsgydql3q4ka27d9wnlrmus4tvkrnc8ftc4h8h5fgyln54gl0a7dgsrqsqqqqqpppe7n6" 172 173 let actualEncoding = Bech32Object.encode(.nevent(NEvent(event: test_note, relays: relays))) 174 175 XCTAssertEqual(expectedEncoding, actualEncoding) 176 } 177 178 func testTLVEncoding_NProfileExample_ValidContent() throws { 179 guard let author = try bech32_decode("npub180cvv07tjdrrgpa0j7j7tmnyl2yr6yr7l8j4s3evf6u64th6gkwsyjh6w6") else { 180 XCTFail() 181 return 182 } 183 184 let relays = [ 185 "wss://r.x.com", 186 "wss://djbas.sadkb.com" 187 ].compactMap(RelayURL.init) 188 189 let expectedEncoding = "nprofile1qqsrhuxx8l9ex335q7he0f09aej04zpazpl0ne2cgukyawd24mayt8gpp4mhxue69uhhytnc9e3k7mgpz4mhxue69uhkg6nzv9ejuumpv34kytnrdaksjlyr9p" 190 191 let actualEncoding = Bech32Object.encode(.nprofile(NProfile(author: Pubkey(author.data), relays: relays))) 192 193 XCTAssertEqual(expectedEncoding, actualEncoding) 194 } 195 196 func testTLVEncoding_NRelayExample_ValidContent() throws { 197 let relay = "wss://relay.nostr.band" 198 199 let expectedEncoding = "nrelay1qqt8wumn8ghj7un9d3shjtnwdaehgu3wvfskueq4r295t" 200 201 let actualEncoding = Bech32Object.encode(.nrelay(relay)) 202 203 XCTAssertEqual(expectedEncoding, actualEncoding) 204 } 205 206 func testTLVEncoding_NaddrExample_ValidContent() throws { 207 guard let author = try bech32_decode("npub1tx0k0a7lw62vvqax6p3ku90tccgdka7ul4radews2wrdsg0m865szf9fw6") else { 208 XCTFail() 209 return 210 } 211 212 let relays = ["wss://relay.nostr.band"].compactMap(RelayURL.init) 213 let identifier = "1700730909108" 214 let kind: UInt32 = 30023 215 216 let expectedEncoding = "naddr1qqxnzdesxqmnxvpexqunzvpcqyt8wumn8ghj7un9d3shjtnwdaehgu3wvfskueqzypve7elhmamff3sr5mgxxms4a0rppkmhmn7504h96pfcdkpplvl2jqcyqqq823cnmhuld" 217 218 let actualEncoding = Bech32Object.encode(.naddr(NAddr(identifier: identifier, author: Pubkey(author.data), relays: relays, kind: kind))) 219 220 XCTAssertEqual(expectedEncoding, actualEncoding) 221 } 222 }