damus

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

Bech32ObjectTests.swift (9790B)


      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"]
     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"]
     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"]
     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"]
     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_NRelayExample_ValidContent() throws {
     93         let content = "nrelay1qqt8wumn8ghj7un9d3shjtnwdaehgu3wvfskueq4r295t"
     94         let relay = "wss://relay.nostr.band"
     95         
     96         let expectedObject = Bech32Object.nrelay(relay)
     97         let actualObject = Bech32Object.parse(content)
     98         
     99         XCTAssertEqual(expectedObject, actualObject)
    100     }
    101     
    102     func testTLVParsing_NaddrExample_ValidContent() throws {
    103         let content = "naddr1qqxnzdesxqmnxvpexqunzvpcqyt8wumn8ghj7un9d3shjtnwdaehgu3wvfskueqzypve7elhmamff3sr5mgxxms4a0rppkmhmn7504h96pfcdkpplvl2jqcyqqq823cnmhuld"
    104         
    105         guard let author = try bech32_decode("npub1tx0k0a7lw62vvqax6p3ku90tccgdka7ul4radews2wrdsg0m865szf9fw6") else {
    106             XCTFail("Can't decode npub")
    107             return
    108         }
    109         let relays = ["wss://relay.nostr.band"]
    110         let identifier = "1700730909108"
    111         let kind: UInt32 = 30023
    112         
    113         let expectedObject = Bech32Object.naddr(NAddr(identifier: identifier, author: Pubkey(author.data), relays: relays, kind: kind))
    114         let actualObject = Bech32Object.parse(content)
    115         
    116         XCTAssertEqual(expectedObject, actualObject)
    117     }
    118     
    119     func testTLVEncoding_NeventHasRelaysNoAuthorNoKind_ValidContent() throws {
    120         guard let noteid = hex_decode_noteid("b9f5441e45ca39179320e0031cfb18e34078673dcc3d3e3a3b3a981760aa5696") else {
    121             XCTFail("Parsing note ID failed")
    122             return
    123         }
    124         
    125         let relays = ["wss://nostr-relay.untethr.me", "wss://nostr-pub.wellorder.net"]
    126         
    127         let expectedEncoding = "nevent1qqstna2yrezu5wghjvswqqculvvwxsrcvu7uc0f78gan4xqhvz49d9spr3mhxue69uhkummnw3ez6un9d3shjtn4de6x2argwghx6egpr4mhxue69uhkummnw3ez6ur4vgh8wetvd3hhyer9wghxuet5nxnepm"
    128         
    129         let actualEncoding = Bech32Object.encode(.nevent(NEvent(noteid: noteid, relays: relays)))
    130         
    131         XCTAssertEqual(expectedEncoding, actualEncoding)
    132     }
    133     
    134     func testTLVEncoding_NeventHasRelaysNoAuthorHasKind_ValidContent() throws {
    135         guard let noteid = hex_decode_noteid("b9f5441e45ca39179320e0031cfb18e34078673dcc3d3e3a3b3a981760aa5696") else {
    136             XCTFail()
    137             return
    138         }
    139         
    140         let relays = [
    141             "wss://nostr-relay.untethr.me",
    142             "wss://nostr-pub.wellorder.net"
    143         ]
    144         
    145         let expectedEncoding = "nevent1qqstna2yrezu5wghjvswqqculvvwxsrcvu7uc0f78gan4xqhvz49d9spr3mhxue69uhkummnw3ez6un9d3shjtn4de6x2argwghx6egpr4mhxue69uhkummnw3ez6ur4vgh8wetvd3hhyer9wghxuet5qvzqqqqqqyjyqz7d"
    146         
    147         let actualEncoding = Bech32Object.encode(.nevent(NEvent(noteid: noteid, relays: relays, kind: 1)))
    148         
    149         XCTAssertEqual(expectedEncoding, actualEncoding)
    150     }
    151     
    152     func testTLVEncoding_NeventHasRelaysHasAuthorHasKind_ValidContent() throws {
    153         guard let noteid = hex_decode_noteid("b9f5441e45ca39179320e0031cfb18e34078673dcc3d3e3a3b3a981760aa5696") else {
    154             XCTFail("Parsing note ID failed")
    155             return
    156         }
    157         guard let author = try bech32_decode("npub180cvv07tjdrrgpa0j7j7tmnyl2yr6yr7l8j4s3evf6u64th6gkwsyjh6w6") else {
    158             XCTFail()
    159             return
    160         }
    161         
    162         let relays = ["wss://nostr-relay.untethr.me", "wss://nostr-pub.wellorder.net"]
    163         
    164         let expectedEncoding = "nevent1qqstna2yrezu5wghjvswqqculvvwxsrcvu7uc0f78gan4xqhvz49d9spr3mhxue69uhkummnw3ez6un9d3shjtn4de6x2argwghx6egpr4mhxue69uhkummnw3ez6ur4vgh8wetvd3hhyer9wghxuet5qgsrhuxx8l9ex335q7he0f09aej04zpazpl0ne2cgukyawd24mayt8grqsqqqqqpw4032x"
    165 
    166         let actualEncoding = Bech32Object.encode(.nevent(NEvent(noteid: noteid, relays: relays, author: Pubkey(author.data), kind: 1)))
    167         
    168         XCTAssertEqual(expectedEncoding, actualEncoding)
    169     }
    170     
    171     func testTLVEncoding_NProfileExample_ValidContent() throws {
    172         guard let author = try bech32_decode("npub180cvv07tjdrrgpa0j7j7tmnyl2yr6yr7l8j4s3evf6u64th6gkwsyjh6w6") else {
    173             XCTFail()
    174             return
    175         }
    176         
    177         let relays = [
    178             "wss://r.x.com",
    179             "wss://djbas.sadkb.com"
    180         ]
    181         
    182         let expectedEncoding = "nprofile1qqsrhuxx8l9ex335q7he0f09aej04zpazpl0ne2cgukyawd24mayt8gpp4mhxue69uhhytnc9e3k7mgpz4mhxue69uhkg6nzv9ejuumpv34kytnrdaksjlyr9p"
    183         
    184         let actualEncoding = Bech32Object.encode(.nprofile(NProfile(author: Pubkey(author.data), relays: relays)))
    185         
    186         XCTAssertEqual(expectedEncoding, actualEncoding)
    187     }
    188     
    189     func testTLVEncoding_NRelayExample_ValidContent() throws {
    190         let relay = "wss://relay.nostr.band"
    191         
    192         let expectedEncoding = "nrelay1qqt8wumn8ghj7un9d3shjtnwdaehgu3wvfskueq4r295t"
    193         
    194         let actualEncoding = Bech32Object.encode(.nrelay(relay))
    195         
    196         XCTAssertEqual(expectedEncoding, actualEncoding)
    197     }
    198     
    199     func testTLVEncoding_NaddrExample_ValidContent() throws {
    200         guard let author = try bech32_decode("npub1tx0k0a7lw62vvqax6p3ku90tccgdka7ul4radews2wrdsg0m865szf9fw6") else {
    201             XCTFail()
    202             return
    203         }
    204         
    205         let relays = ["wss://relay.nostr.band"]
    206         let identifier = "1700730909108"
    207         let kind: UInt32 = 30023
    208         
    209         let expectedEncoding = "naddr1qqxnzdesxqmnxvpexqunzvpcqyt8wumn8ghj7un9d3shjtnwdaehgu3wvfskueqzypve7elhmamff3sr5mgxxms4a0rppkmhmn7504h96pfcdkpplvl2jqcyqqq823cnmhuld"
    210         
    211         let actualEncoding = Bech32Object.encode(.naddr(NAddr(identifier: identifier, author: Pubkey(author.data), relays: relays, kind: kind)))
    212         
    213         XCTAssertEqual(expectedEncoding, actualEncoding)
    214     }
    215 }