2023-08-03 03:38:49 +00:00
//
// N o t e C o n t e n t V i e w T e s t s . s w i f t
// d a m u s T e s t s
//
// C r e a t e d b y D a n i e l D ’ A q u i n o o n 2 0 2 3 - 0 8 - 0 2 .
//
import XCTest
2024-01-18 19:59:27 +00:00
import SwiftUI
2023-08-03 03:38:49 +00:00
@ testable import damus
class NoteContentViewTests : XCTestCase {
func testRenderBlocksWithNonLatinHashtags ( ) {
2023-07-26 15:46:44 +00:00
let content = " Damusはかっこいいです #cool #かっこいい "
let note = NostrEvent ( content : content , keypair : test_keypair , tags : [ [ " t " , " かっこいい " ] ] ) !
2023-08-30 23:22:04 +00:00
let parsed : Blocks = parse_note_content ( content : . init ( note : note , keypair : test_keypair ) )
2023-07-26 15:46:44 +00:00
2023-09-10 22:50:27 +00:00
let testState = test_damus_state
2023-08-03 03:38:49 +00:00
let text : NoteArtifactsSeparated = render_blocks ( blocks : parsed , profiles : testState . profiles )
let attributedText : AttributedString = text . content . attributed
let runs : AttributedString . Runs = attributedText . runs
let runArray : [ AttributedString . Runs . Run ] = Array ( runs )
print ( runArray . description )
XCTAssertEqual ( runArray [ 1 ] . link ? . absoluteString , " damus:t:cool " , " Latin-character hashtag is missing. Runs description : \( runArray . description ) " )
XCTAssertEqual ( runArray [ 3 ] . link ? . absoluteString . removingPercentEncoding ! , " damus:t:かっこいい " , " Non-latin-character hashtag is missing. Runs description : \( runArray . description ) " )
}
2023-08-15 21:33:47 +00:00
// / B a s e d o n h t t p s : / / g i t h u b . c o m / d a m u s - i o / d a m u s / i s s u e s / 1 4 6 8
// / T e s t s w h e t h e r a n o t e c o n t e n t v i e w c o r r e c t l y p a r s e s a n i m a g e b l o c k w h e n u r l i n J S O N c o n t e n t c o n t a i n s o p t i o n a l e s c a p e d s l a s h e s
func testParseImageBlockInContentWithEscapedSlashes ( ) {
let testJSONWithEscapedSlashes = " { \" tags \" :[], \" pubkey \" : \" f8e6c64342f1e052480630e27e1016dce35fc3a614e60434fef4aa2503328ca9 \" , \" content \" : \" https: \\ / \\ /cdn.nostr.build \\ /i \\ /5c1d3296f66c2630131bf123106486aeaf051ed8466031c0e0532d70b33cddb2.jpg \" , \" created_at \" :1691864981, \" kind \" :1, \" sig \" : \" fc0033aa3d4df50b692a5b346fa816fdded698de2045e36e0642a021391468c44ca69c2471adc7e92088131872d4aaa1e90ea6e1ad97f3cc748f4aed96dfae18 \" , \" id \" : \" e8f6eca3b161abba034dac9a02bb6930ecde9fd2fb5d6c5f22a05526e11382cb \" } "
let testNote = NostrEvent . owned_from_json ( json : testJSONWithEscapedSlashes ) !
2023-08-30 23:22:04 +00:00
let parsed = parse_note_content ( content : . init ( note : testNote , keypair : test_keypair ) )
2023-08-15 21:33:47 +00:00
2023-09-01 15:32:01 +00:00
XCTAssertTrue ( ( parsed . blocks [ 0 ] . asURL != nil ) , " NoteContentView does not correctly parse an image block when url in JSON content contains optional escaped slashes. " )
2023-08-15 21:33:47 +00:00
}
2024-01-18 19:59:27 +00:00
func testMentionStr_Pubkey_ContainsAbbreviated ( ) throws {
let compatibleText = createCompatibleText ( test_pubkey . npub )
assertCompatibleTextHasExpectedString ( compatibleText : compatibleText , expected : " 17ldvg64:nq5mhr77 " )
}
func testMentionStr_Pubkey_ContainsFullBech32 ( ) {
let compatableText = createCompatibleText ( test_pubkey . npub )
assertCompatibleTextHasExpectedString ( compatibleText : compatableText , expected : test_pubkey . npub )
}
func testMentionStr_Nprofile_ContainsAbbreviated ( ) throws {
let compatibleText = createCompatibleText ( " nprofile1qqsrhuxx8l9ex335q7he0f09aej04zpazpl0ne2cgukyawd24mayt8gpp4mhxue69uhhytnc9e3k7mgpz4mhxue69uhkg6nzv9ejuumpv34kytnrdaksjlyr9p " )
assertCompatibleTextHasExpectedString ( compatibleText : compatibleText , expected : " 180cvv07:wsyjh6w6 " )
}
func testMentionStr_Nprofile_ContainsFullBech32 ( ) throws {
let bech = " nprofile1qqsrhuxx8l9ex335q7he0f09aej04zpazpl0ne2cgukyawd24mayt8gpp4mhxue69uhhytnc9e3k7mgpz4mhxue69uhkg6nzv9ejuumpv34kytnrdaksjlyr9p "
let compatibleText = createCompatibleText ( bech )
assertCompatibleTextHasExpectedString ( compatibleText : compatibleText , expected : bech )
}
func testMentionStr_Note_ContainsAbbreviated ( ) {
let compatibleText = createCompatibleText ( test_note . id . bech32 )
assertCompatibleTextHasExpectedString ( compatibleText : compatibleText , expected : " note1qqq:qqn2l0z3 " )
}
func testMentionStr_Note_ContainsFullBech32 ( ) {
let compatableText = createCompatibleText ( test_note . id . bech32 )
assertCompatibleTextHasExpectedString ( compatibleText : compatableText , expected : test_note . id . bech32 )
}
func testMentionStr_Nevent_ContainsAbbreviated ( ) {
let bech = " nevent1qqstna2yrezu5wghjvswqqculvvwxsrcvu7uc0f78gan4xqhvz49d9spr3mhxue69uhkummnw3ez6un9d3shjtn4de6x2argwghx6egpr4mhxue69uhkummnw3ez6ur4vgh8wetvd3hhyer9wghxuet5nxnepm "
let compatibleText = createCompatibleText ( bech )
assertCompatibleTextHasExpectedString ( compatibleText : compatibleText , expected : " nevent1q:t5nxnepm " )
}
func testMentionStr_Nevent_ContainsFullBech32 ( ) throws {
let bech = " nevent1qqstna2yrezu5wghjvswqqculvvwxsrcvu7uc0f78gan4xqhvz49d9spr3mhxue69uhkummnw3ez6un9d3shjtn4de6x2argwghx6egpr4mhxue69uhkummnw3ez6ur4vgh8wetvd3hhyer9wghxuet5nxnepm "
let compatibleText = createCompatibleText ( bech )
assertCompatibleTextHasExpectedString ( compatibleText : compatibleText , expected : bech )
}
func testMentionStr_Nrelay_ContainsAbbreviated ( ) {
let bech = " nrelay1qqt8wumn8ghj7un9d3shjtnwdaehgu3wvfskueq4r295t "
let compatibleText = createCompatibleText ( bech )
assertCompatibleTextHasExpectedString ( compatibleText : compatibleText , expected : " wss://relay.nostr.band " )
}
func testMentionStr_Nrelay_ContainsFullBech32 ( ) {
let bech = " nrelay1qqt8wumn8ghj7un9d3shjtnwdaehgu3wvfskueq4r295t "
let compatibleText = createCompatibleText ( bech )
assertCompatibleTextHasExpectedString ( compatibleText : compatibleText , expected : bech )
}
func testMentionStr_Naddr_ContainsAbbreviated ( ) {
let bech = " naddr1qqxnzdesxqmnxvpexqunzvpcqyt8wumn8ghj7un9d3shjtnwdaehgu3wvfskueqzypve7elhmamff3sr5mgxxms4a0rppkmhmn7504h96pfcdkpplvl2jqcyqqq823cnmhuld "
let compatibleText = createCompatibleText ( bech )
assertCompatibleTextHasExpectedString ( compatibleText : compatibleText , expected : " naddr1qq:3cnmhuld " )
}
func testMentionStr_Naddr_ContainsFullBech32 ( ) {
let bech = " naddr1qqxnzdesxqmnxvpexqunzvpcqyt8wumn8ghj7un9d3shjtnwdaehgu3wvfskueqzypve7elhmamff3sr5mgxxms4a0rppkmhmn7504h96pfcdkpplvl2jqcyqqq823cnmhuld "
let compatibleText = createCompatibleText ( bech )
assertCompatibleTextHasExpectedString ( compatibleText : compatibleText , expected : bech )
}
}
2023-08-03 03:38:49 +00:00
2024-01-18 19:59:27 +00:00
private func assertCompatibleTextHasExpectedString ( compatibleText : CompatibleText , expected : String ) {
guard let hasExpected = compatibleText . items . first ? . attributed_string ( ) ? . description . contains ( expected ) else {
XCTFail ( )
return
}
XCTAssertTrue ( hasExpected )
}
private func createCompatibleText ( _ bechString : String ) -> CompatibleText {
guard let mentionRef = Bech32Object . parse ( bechString ) ? . toMentionRef ( ) else {
XCTFail ( " Failed to create MentionRef from Bech32 string " )
return CompatibleText ( )
}
return mention_str ( . any ( mentionRef ) , profiles : test_damus_state . profiles )
2023-08-03 03:38:49 +00:00
}