damus

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

commit 60a0c2127201c29985de8ce14ce1186313669c4b
parent 8242ca27d2ef3a0d28e2cf4a2e01b4215a4c72c2
Author: William Casarin <jb55@jb55.com>
Date:   Mon, 17 Jul 2023 10:20:21 -0700

test: add post composition tests

This adds post composition tests so that we can avoid composition bugs.
This still does not capture all of the dynamics of post composition,
because it ignores much of the mutable cursor position and related state
when editing posts.

We will need to make post editing more pure and less mutable in the
future to get test coverage on those.

Diffstat:
MdamusTests/ReplyTests.swift | 72+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---
1 file changed, 69 insertions(+), 3 deletions(-)

diff --git a/damusTests/ReplyTests.swift b/damusTests/ReplyTests.swift @@ -137,8 +137,7 @@ class ReplyTests: XCTestCase { return } - let profile = Profile(name: "jb55", display_name: "Will", about: nil, picture: nil, banner: nil, website: nil, lud06: nil, lud16: nil, nip05: nil, damus_donation: nil) - + let profile = Profile(name: "jb55") let post = user_tag_attr_string(profile: profile, pubkey: pk) post.append(.init(string: "\n")) post.append(user_tag_attr_string(profile: profile, pubkey: pk)) @@ -190,7 +189,74 @@ class ReplyTests: XCTestCase { XCTAssertEqual(r.is_thread_id!.ref_id, "thread_id") XCTAssertNil(r.is_mention) } - + + func testAdjacentComposedMention() throws { + let content = "cc@jb55" + + let profile = Profile(name: "jb55") + let tag = user_tag_attr_string(profile: profile, pubkey: "pk") + let appended = append_user_tag(tag: tag, post: .init(string: content), word_range: .init(2...6)) + let new_post = appended.post + + try new_post.testAttributes(conditions: [ + { let link = $0[.link] as? String; XCTAssertNil(link) }, + { let link = $0[.link] as! String; XCTAssertEqual(link, "damus:nostr:pk") }, + { let link = $0[.link] as? String; XCTAssertNil(link) } + ]) + + XCTAssertEqual(new_post.string, "cc @jb55 ") + } + + func testAdjacentEmojiComposedMention() throws { + let content = "😎@jb55" + + let profile = Profile(name: "jb55") + let tag = user_tag_attr_string(profile: profile, pubkey: "pk") + let appended = append_user_tag(tag: tag, post: .init(string: content), word_range: .init(2...6)) + let new_post = appended.post + + try new_post.testAttributes(conditions: [ + { let link = $0[.link] as? String; XCTAssertNil(link) }, + { let link = $0[.link] as! String; XCTAssertEqual(link, "damus:nostr:pk") }, + { let link = $0[.link] as? String; XCTAssertNil(link) } + ]) + + XCTAssertEqual(new_post.string, "😎 @jb55 ") + } + + func testComposedMention() throws { + let content = "@jb55" + + let profile = Profile(name: "jb55") + let tag = user_tag_attr_string(profile: profile, pubkey: "pk") + let appended = append_user_tag(tag: tag, post: .init(string: content), word_range: .init(0...4)) + let new_post = appended.post + + try new_post.testAttributes(conditions: [ + { let link = $0[.link] as! String; XCTAssertEqual(link, "damus:nostr:pk") }, + { let link = $0[.link] as? String; XCTAssertNil(link) }, + ]) + + XCTAssertEqual(new_post.string, "@jb55 ") + } + + func testAdjacentSpaceComposedMention() throws { + let content = "cc @jb55" + + let profile = Profile(name: "jb55") + let tag = user_tag_attr_string(profile: profile, pubkey: "pk") + let appended = append_user_tag(tag: tag, post: .init(string: content), word_range: .init(3...7)) + let new_post = appended.post + + try new_post.testAttributes(conditions: [ + { let link = $0[.link] as? String; XCTAssertNil(link) }, + { let link = $0[.link] as! String; XCTAssertEqual(link, "damus:nostr:pk") }, + { let link = $0[.link] as? String; XCTAssertNil(link) } + ]) + + XCTAssertEqual(new_post.string, "cc @jb55 ") + } + func testNoReply() throws { let content = "this is a #[0] reply" let blocks = parse_note_content(content: content, tags: []).blocks