NoteContentViewTests.swift (7059B)
1 // 2 // NoteContentViewTests.swift 3 // damusTests 4 // 5 // Created by Daniel D’Aquino on 2023-08-02. 6 // 7 8 import XCTest 9 import SwiftUI 10 @testable import damus 11 12 class NoteContentViewTests: XCTestCase { 13 func testRenderBlocksWithNonLatinHashtags() { 14 let content = "Damusはかっこいいです #cool #かっこいい" 15 let note = NostrEvent(content: content, keypair: test_keypair, tags: [["t", "かっこいい"]])! 16 let parsed: Blocks = parse_note_content(content: .init(note: note, keypair: test_keypair)) 17 18 let testState = test_damus_state 19 20 let text: NoteArtifactsSeparated = render_blocks(blocks: parsed, profiles: testState.profiles) 21 let attributedText: AttributedString = text.content.attributed 22 23 let runs: AttributedString.Runs = attributedText.runs 24 let runArray: [AttributedString.Runs.Run] = Array(runs) 25 print(runArray.description) 26 XCTAssertEqual(runArray[1].link?.absoluteString, "damus:t:cool", "Latin-character hashtag is missing. Runs description :\(runArray.description)") 27 XCTAssertEqual(runArray[3].link?.absoluteString.removingPercentEncoding!, "damus:t:かっこいい", "Non-latin-character hashtag is missing. Runs description :\(runArray.description)") 28 } 29 30 /// Based on https://github.com/damus-io/damus/issues/1468 31 /// Tests whether a note content view correctly parses an image block when url in JSON content contains optional escaped slashes 32 func testParseImageBlockInContentWithEscapedSlashes() { 33 let testJSONWithEscapedSlashes = "{\"tags\":[],\"pubkey\":\"f8e6c64342f1e052480630e27e1016dce35fc3a614e60434fef4aa2503328ca9\",\"content\":\"https:\\/\\/cdn.nostr.build\\/i\\/5c1d3296f66c2630131bf123106486aeaf051ed8466031c0e0532d70b33cddb2.jpg\",\"created_at\":1691864981,\"kind\":1,\"sig\":\"fc0033aa3d4df50b692a5b346fa816fdded698de2045e36e0642a021391468c44ca69c2471adc7e92088131872d4aaa1e90ea6e1ad97f3cc748f4aed96dfae18\",\"id\":\"e8f6eca3b161abba034dac9a02bb6930ecde9fd2fb5d6c5f22a05526e11382cb\"}" 34 let testNote = NostrEvent.owned_from_json(json: testJSONWithEscapedSlashes)! 35 let parsed = parse_note_content(content: .init(note: testNote, keypair: test_keypair)) 36 37 XCTAssertTrue((parsed.blocks[0].asURL != nil), "NoteContentView does not correctly parse an image block when url in JSON content contains optional escaped slashes.") 38 } 39 40 func testMentionStr_Pubkey_ContainsAbbreviated() throws { 41 let compatibleText = createCompatibleText(test_pubkey.npub) 42 43 assertCompatibleTextHasExpectedString(compatibleText: compatibleText, expected: "17ldvg64:nq5mhr77") 44 } 45 46 func testMentionStr_Pubkey_ContainsFullBech32() { 47 let compatableText = createCompatibleText(test_pubkey.npub) 48 49 assertCompatibleTextHasExpectedString(compatibleText: compatableText, expected: test_pubkey.npub) 50 } 51 52 func testMentionStr_Nprofile_ContainsAbbreviated() throws { 53 let compatibleText = createCompatibleText("nprofile1qqsrhuxx8l9ex335q7he0f09aej04zpazpl0ne2cgukyawd24mayt8gpp4mhxue69uhhytnc9e3k7mgpz4mhxue69uhkg6nzv9ejuumpv34kytnrdaksjlyr9p") 54 55 assertCompatibleTextHasExpectedString(compatibleText: compatibleText, expected: "180cvv07:wsyjh6w6") 56 } 57 58 func testMentionStr_Nprofile_ContainsFullBech32() throws { 59 let bech = "nprofile1qqsrhuxx8l9ex335q7he0f09aej04zpazpl0ne2cgukyawd24mayt8gpp4mhxue69uhhytnc9e3k7mgpz4mhxue69uhkg6nzv9ejuumpv34kytnrdaksjlyr9p" 60 let compatibleText = createCompatibleText(bech) 61 62 assertCompatibleTextHasExpectedString(compatibleText: compatibleText, expected: bech) 63 } 64 65 func testMentionStr_Note_ContainsAbbreviated() { 66 let compatibleText = createCompatibleText(test_note.id.bech32) 67 68 assertCompatibleTextHasExpectedString(compatibleText: compatibleText, expected: "note1qqq:qqn2l0z3") 69 } 70 71 func testMentionStr_Note_ContainsFullBech32() { 72 let compatableText = createCompatibleText(test_note.id.bech32) 73 74 assertCompatibleTextHasExpectedString(compatibleText: compatableText, expected: test_note.id.bech32) 75 } 76 77 func testMentionStr_Nevent_ContainsAbbreviated() { 78 let bech = "nevent1qqstna2yrezu5wghjvswqqculvvwxsrcvu7uc0f78gan4xqhvz49d9spr3mhxue69uhkummnw3ez6un9d3shjtn4de6x2argwghx6egpr4mhxue69uhkummnw3ez6ur4vgh8wetvd3hhyer9wghxuet5nxnepm" 79 let compatibleText = createCompatibleText(bech) 80 81 assertCompatibleTextHasExpectedString(compatibleText: compatibleText, expected: "nevent1q:t5nxnepm") 82 } 83 84 func testMentionStr_Nevent_ContainsFullBech32() throws { 85 let bech = "nevent1qqstna2yrezu5wghjvswqqculvvwxsrcvu7uc0f78gan4xqhvz49d9spr3mhxue69uhkummnw3ez6un9d3shjtn4de6x2argwghx6egpr4mhxue69uhkummnw3ez6ur4vgh8wetvd3hhyer9wghxuet5nxnepm" 86 let compatibleText = createCompatibleText(bech) 87 88 assertCompatibleTextHasExpectedString(compatibleText: compatibleText, expected: bech) 89 } 90 91 func testMentionStr_Nrelay_ContainsAbbreviated() { 92 let bech = "nrelay1qqt8wumn8ghj7un9d3shjtnwdaehgu3wvfskueq4r295t" 93 let compatibleText = createCompatibleText(bech) 94 95 assertCompatibleTextHasExpectedString(compatibleText: compatibleText, expected: "wss://relay.nostr.band") 96 } 97 98 func testMentionStr_Nrelay_ContainsFullBech32() { 99 let bech = "nrelay1qqt8wumn8ghj7un9d3shjtnwdaehgu3wvfskueq4r295t" 100 let compatibleText = createCompatibleText(bech) 101 102 assertCompatibleTextHasExpectedString(compatibleText: compatibleText, expected: bech) 103 } 104 105 func testMentionStr_Naddr_ContainsAbbreviated() { 106 let bech = "naddr1qqxnzdesxqmnxvpexqunzvpcqyt8wumn8ghj7un9d3shjtnwdaehgu3wvfskueqzypve7elhmamff3sr5mgxxms4a0rppkmhmn7504h96pfcdkpplvl2jqcyqqq823cnmhuld" 107 let compatibleText = createCompatibleText(bech) 108 109 assertCompatibleTextHasExpectedString(compatibleText: compatibleText, expected: "naddr1qq:3cnmhuld") 110 } 111 112 func testMentionStr_Naddr_ContainsFullBech32() { 113 let bech = "naddr1qqxnzdesxqmnxvpexqunzvpcqyt8wumn8ghj7un9d3shjtnwdaehgu3wvfskueqzypve7elhmamff3sr5mgxxms4a0rppkmhmn7504h96pfcdkpplvl2jqcyqqq823cnmhuld" 114 let compatibleText = createCompatibleText(bech) 115 116 assertCompatibleTextHasExpectedString(compatibleText: compatibleText, expected: bech) 117 } 118 119 } 120 121 private func assertCompatibleTextHasExpectedString(compatibleText: CompatibleText, expected: String) { 122 guard let hasExpected = compatibleText.items.first?.attributed_string()?.description.contains(expected) else { 123 XCTFail() 124 return 125 } 126 127 XCTAssertTrue(hasExpected) 128 } 129 130 private func createCompatibleText(_ bechString: String) -> CompatibleText { 131 guard let mentionRef = Bech32Object.parse(bechString)?.toMentionRef() else { 132 XCTFail("Failed to create MentionRef from Bech32 string") 133 return CompatibleText() 134 } 135 return mention_str(.any(mentionRef), profiles: test_damus_state.profiles) 136 }