| 1 | import XCTest |
| 2 | @testable import LChatCore |
| 3 | |
| 4 | // 🤖 The two keyboard message verbs' targets (issue #392): ⌘R replies to the |
| 5 | // latest message, ⌘T reacts to the latest one that ISN'T yours. Web twin: |
| 6 | // chat.js's `latest(othersOnly)` behind its cmd/ctrl-R and -T handlers. |
| 7 | final class LatestMessageHotkeyTests: XCTestCase { |
| 8 | typealias Message = ChatFeedModel.Message |
| 9 | |
| 10 | private func message(_ id: Int64, _ text: String, mine: Bool, |
| 11 | action: Bool = false, pending: Bool = false) -> Message { |
| 12 | var m = Message(id: id, name: mine ? "Me" : "Alice", text: text, mine: mine, |
| 13 | at: 1_700_000_000 + id, action: action, edited: false) |
| 14 | m.pending = pending |
| 15 | return m |
| 16 | } |
| 17 | |
| 18 | func testReplyTargetsTheNewestMessageEvenWhenItsYours() { |
| 19 | let list = [ |
| 20 | message(1, "Morning", mine: false), |
| 21 | message(2, "Morning!", mine: true), |
| 22 | ] |
| 23 | // The issue's wording: "even if it's from you". |
| 24 | XCTAssertEqual(ChatFeedModel.latestReplyTarget(in: list)?.id, 2) |
| 25 | } |
| 26 | |
| 27 | func testReplySkipsActionLinesAndOptimisticCopies() { |
| 28 | let list = [ |
| 29 | message(1, "Real message", mine: false), |
| 30 | message(2, "added the To-do app", mine: true, action: true), |
| 31 | message(3, "not acknowledged yet", mine: true, pending: true), |
| 32 | ] |
| 33 | // An action line has no bubble to quote, and a pending copy's id is a |
| 34 | // synthetic one the server has never seen. |
| 35 | XCTAssertEqual(ChatFeedModel.latestReplyTarget(in: list)?.id, 1) |
| 36 | } |
| 37 | |
| 38 | func testReplyTargetIsNilWithNothingToReplyTo() { |
| 39 | XCTAssertNil(ChatFeedModel.latestReplyTarget(in: [])) |
| 40 | XCTAssertNil(ChatFeedModel.latestReplyTarget(in: [ |
| 41 | message(1, "set the group photo", mine: false, action: true), |
| 42 | ])) |
| 43 | } |
| 44 | |
| 45 | func testReactionTargetsTheNewestMessageThatIsntYours() { |
| 46 | let list = [ |
| 47 | message(1, "Morning", mine: false), |
| 48 | message(2, "Standup in five", mine: false), |
| 49 | message(3, "On my way", mine: true), |
| 50 | ] |
| 51 | XCTAssertEqual(ChatFeedModel.latestReactionTarget(in: list)?.id, 2) |
| 52 | } |
| 53 | |
| 54 | func testReactionTargetIsNilWhenEveryMessageIsYours() { |
| 55 | let list = [ |
| 56 | message(1, "Anyone about?", mine: true), |
| 57 | message(2, "Never mind", mine: true), |
| 58 | ] |
| 59 | XCTAssertNil(ChatFeedModel.latestReactionTarget(in: list)) |
| 60 | } |
| 61 | } |
| 62 | |