damusTests.swift (9589B)
1 // 2 // damusTests.swift 3 // damusTests 4 // 5 // Created by William Casarin on 2022-04-01. 6 // 7 8 import XCTest 9 @testable import damus 10 11 12 class damusTests: XCTestCase { 13 14 override func setUpWithError() throws { 15 // Put setup code here. This method is called before the invocation of each test method in the class. 16 } 17 18 override func tearDownWithError() throws { 19 // Put teardown code here. This method is called after the invocation of each test method in the class. 20 } 21 22 func testIdEquality() throws { 23 let pubkey = test_pubkey 24 let ev = test_note 25 26 let pubkey_same = Pubkey(Data([0xf7, 0xda, 0xc4, 0x6a, 0xa2, 0x70, 0xf7, 0x28, 0x76, 0x06, 0xa2, 0x2b, 0xeb, 0x4d, 0x77, 0x25, 0x57, 0x3a, 0xfa, 0x0e, 0x02, 0x8c, 0xdf, 0xac, 0x39, 0xa4, 0xcb, 0x23, 0x31, 0x53, 0x7f, 0x66])) 27 28 XCTAssertEqual(pubkey.hashValue, pubkey_same.hashValue) 29 XCTAssertEqual(pubkey, pubkey_same) 30 } 31 32 func testRandomBytes() { 33 let bytes = random_bytes(count: 32) 34 35 print("testRandomBytes \(hex_encode(bytes))") 36 XCTAssertEqual(bytes.count, 32) 37 } 38 39 func testTrimmingFunctions() { 40 let txt = " bobs " 41 42 XCTAssertEqual(trim_prefix(txt), "bobs ") 43 XCTAssertEqual(trim_suffix(txt), " bobs") 44 } 45 46 func testParseMentionWithMarkdown() { 47 let md = """ 48 Testing markdown in damus 49 50 **bold** 51 52 _italics_ 53 54 `monospace` 55 56 # h1 57 58 ## h2 59 60 ### h3 61 62 * list1 63 * list2 64 65 > some awesome quote 66 67 [my website](https://jb55.com) 68 """ 69 70 let parsed = parse_note_content(content: .content(md, nil)).blocks 71 72 XCTAssertNotNil(parsed) 73 XCTAssertEqual(parsed.count, 3) 74 75 XCTAssertTrue(parsed[0].isText) 76 XCTAssertFalse(parsed[0].isURL) 77 78 XCTAssertTrue(parsed[1].isURL) 79 XCTAssertFalse(parsed[1].isText) 80 81 XCTAssertTrue(parsed[2].isText) 82 XCTAssertFalse(parsed[2].isURL) 83 } 84 85 func testStringArrayStorage() { 86 let key = "test_key_string_values" 87 let scoped_key = setting_property_key(key: key) 88 89 let res = setting_set_property_value(scoped_key: scoped_key, old_value: [], new_value: ["a"]) 90 XCTAssertEqual(res, ["a"]) 91 92 let got = setting_get_property_value(key: key, scoped_key: scoped_key, default_value: [String]()) 93 XCTAssertEqual(got, ["a"]) 94 95 _ = setting_set_property_value(scoped_key: scoped_key, old_value: got, new_value: ["a", "b", "c"]) 96 let got2 = setting_get_property_value(key: key, scoped_key: scoped_key, default_value: [String]()) 97 XCTAssertEqual(got2, ["a", "b", "c"]) 98 } 99 100 func testBech32Url() { 101 let parsed = decode_nostr_uri("nostr:npub1xtscya34g58tk0z605fvr788k263gsu6cy9x0mhnm87echrgufzsevkk5s") 102 103 let pk = Pubkey(hex:"32e1827635450ebb3c5a7d12c1f8e7b2b514439ac10a67eef3d9fd9c5c68e245")! 104 XCTAssertEqual(parsed, .ref(.pubkey(pk))) 105 } 106 107 func testSaveRelayFilters() { 108 var filters = Set<RelayFilter>() 109 110 let filter1 = RelayFilter(timeline: .search, relay_id: RelayURL("wss://abc.com")!) 111 let filter2 = RelayFilter(timeline: .home, relay_id: RelayURL("wss://abc.com")!) 112 filters.insert(filter1) 113 filters.insert(filter2) 114 115 save_relay_filters(test_pubkey, filters: filters) 116 let loaded_filters = load_relay_filters(test_pubkey)! 117 118 XCTAssertEqual(loaded_filters.count, 2) 119 XCTAssertTrue(loaded_filters.contains(filter1)) 120 XCTAssertTrue(loaded_filters.contains(filter2)) 121 XCTAssertEqual(filters, loaded_filters) 122 } 123 124 func testParseUrl() { 125 let parsed = parse_note_content(content: .content("a https://jb55.com b", nil)).blocks 126 127 XCTAssertNotNil(parsed) 128 XCTAssertEqual(parsed.count, 3) 129 130 let url = URL(string: "https://jb55.com") 131 XCTAssertNotNil(url) 132 133 XCTAssertEqual(parsed[1].asURL, url) 134 } 135 136 func testParseUrlEnd() { 137 let parsed = parse_note_content(content: .content("a https://jb55.com", nil)).blocks 138 139 XCTAssertNotNil(parsed) 140 XCTAssertEqual(parsed.count, 2) 141 142 XCTAssertEqual(parsed[0].asString, "a ") 143 144 let url = URL(string: "https://jb55.com") 145 XCTAssertNotNil(url) 146 147 XCTAssertEqual(parsed[1].asURL, url) 148 } 149 150 func testParseUrlStart() { 151 let parsed = parse_note_content(content: .content("https://jb55.com br",nil)).blocks 152 153 XCTAssertNotNil(parsed) 154 XCTAssertEqual(parsed.count, 2) 155 156 let testURL = URL(string: "https://jb55.com") 157 XCTAssertNotNil(testURL) 158 159 XCTAssertEqual(parsed[0].asURL, testURL) 160 161 XCTAssertEqual(parsed[1].asText, " br") 162 } 163 164 func testNoParseUrlWithOnlyWhitespace() { 165 let testString = "https:// " 166 let parsed = parse_note_content(content: .content(testString,nil)).blocks 167 168 XCTAssertNotNil(parsed) 169 XCTAssertFalse(parsed[0].isURL) 170 XCTAssertEqual(parsed[0].asText, testString) 171 } 172 173 func testNoParseUrlTrailingCharacters() { 174 let testString = "https://foo.bar, " 175 let parsed = parse_note_content(content: .content(testString,nil)).blocks 176 177 let testURL = URL(string: "https://foo.bar") 178 XCTAssertNotNil(testURL) 179 180 XCTAssertNotNil(parsed) 181 XCTAssertEqual(parsed[0].asURL, testURL) 182 } 183 184 185 /* 186 func testParseMentionBlank() { 187 let parsed = parse_note_content(content: "", tags: [["e", "event_id"]]).blocks 188 189 XCTAssertNotNil(parsed) 190 XCTAssertEqual(parsed.count, 0) 191 } 192 */ 193 194 func testMakeHashtagPost() { 195 let post = NostrPost(content: "#damus some content #bitcoin derp #かっこいい wow", tags: []) 196 let ev = post.to_event(keypair: test_keypair_full)! 197 198 XCTAssertEqual(ev.tags.count, 3) 199 XCTAssertEqual(ev.content, "#damus some content #bitcoin derp #かっこいい wow") 200 XCTAssertEqual(ev.tags[0][0].string(), "t") 201 XCTAssertEqual(ev.tags[0][1].string(), "damus") 202 XCTAssertEqual(ev.tags[1][0].string(), "t") 203 XCTAssertEqual(ev.tags[1][1].string(), "bitcoin") 204 XCTAssertEqual(ev.tags[2][0].string(), "t") 205 XCTAssertEqual(ev.tags[2][1].string(), "かっこいい") 206 } 207 208 func testParseMentionOnlyText() { 209 let tags = [["e", "event_id"]] 210 let ev = NostrEvent(content: "there is no mention here", keypair: test_keypair, tags: tags)! 211 let parsed = parse_note_content(content: .init(note: ev, keypair: test_keypair)).blocks 212 213 XCTAssertNotNil(parsed) 214 XCTAssertEqual(parsed.count, 1) 215 XCTAssertEqual(parsed[0].asText, "there is no mention here") 216 217 guard case .text(let txt) = parsed[0] else { 218 XCTAssertTrue(false) 219 return 220 } 221 222 XCTAssertEqual(txt, "there is no mention here") 223 } 224 225 func testTagGeneration_Note_ContainsNoTags() { 226 let ev = createEventFromContentString("note1h865g8j9egu30yequqp3e7ccudq8seeaes7nuw3m82vpwc9226tqtudlvp") 227 228 XCTAssertEqual(ev.tags.count, 0) 229 } 230 231 func testTagGeneration_Nevent_ContainsNoTags() { 232 let ev = createEventFromContentString("nevent1qqstna2yrezu5wghjvswqqculvvwxsrcvu7uc0f78gan4xqhvz49d9spr3mhxue69uhkummnw3ez6un9d3shjtn4de6x2argwghx6egpr4mhxue69uhkummnw3ez6ur4vgh8wetvd3hhyer9wghxuet5nxnepm") 233 234 XCTAssertEqual(ev.tags.count, 0) 235 } 236 237 func testTagGeneration_Npub_ContainsPTag() { 238 let ev = createEventFromContentString("npub180cvv07tjdrrgpa0j7j7tmnyl2yr6yr7l8j4s3evf6u64th6gkwsyjh6w6") 239 240 XCTAssertEqual(ev.tags.count, 1) 241 XCTAssertEqual(ev.tags[0][0].string(), "p") 242 XCTAssertEqual(ev.tags[0][1].string(), "3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459d") 243 } 244 245 func testTagGeneration_Nprofile_ContainsPTag() { 246 let ev = createEventFromContentString("nprofile1qqsrhuxx8l9ex335q7he0f09aej04zpazpl0ne2cgukyawd24mayt8gpp4mhxue69uhhytnc9e3k7mgpz4mhxue69uhkg6nzv9ejuumpv34kytnrdaksjlyr9p") 247 248 XCTAssertEqual(ev.tags.count, 1) 249 XCTAssertEqual(ev.tags[0][0].string(), "p") 250 XCTAssertEqual(ev.tags[0][1].string(), "3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459d") 251 } 252 253 func testTagGeneration_Nrelay_ContainsRTag() { 254 let ev = createEventFromContentString("nrelay1qqt8wumn8ghj7un9d3shjtnwdaehgu3wvfskueq4r295t") 255 256 XCTAssertEqual(ev.tags.count, 1) 257 XCTAssertEqual(ev.tags[0][0].string(), "r") 258 XCTAssertEqual(ev.tags[0][1].string(), "wss://relay.nostr.band") 259 } 260 261 func testTagGeneration_Naddr_ContainsATag(){ 262 let ev = createEventFromContentString("naddr1qqxnzdesxqmnxvpexqunzvpcqyt8wumn8ghj7un9d3shjtnwdaehgu3wvfskueqzypve7elhmamff3sr5mgxxms4a0rppkmhmn7504h96pfcdkpplvl2jqcyqqq823cnmhuld") 263 264 XCTAssertEqual(ev.tags.count, 1) 265 XCTAssertEqual(ev.tags[0][0].string(), "a") 266 XCTAssertEqual(ev.tags[0][1].string(), "30023:599f67f7df7694c603a6d0636e15ebc610db77dcfd47d6e5d05386d821fb3ea9:1700730909108") 267 } 268 269 } 270 271 private func createEventFromContentString(_ content: String) -> NostrEvent { 272 let post = NostrPost(content: content, tags: []) 273 guard let ev = post.to_event(keypair: test_keypair_full) else { 274 XCTFail("Could not create event") 275 return test_note 276 } 277 278 return ev 279 }