Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / communicate-dev / src / test / java / com / vayunmathur / communicate / whatsapp / WhatsAppSendProtoTest.kt
WhatsAppSendProtoTest.kt166 lines · 6.6 KiBRaw
| 1 | package com.vayunmathur.communicate.whatsapp |
| 2 | |
| 3 | import com.vayunmathur.communicate.data.whatsapp.WhatsAppProtocol |
| 4 | import com.vayunmathur.communicate.data.whatsapp.buildContactProto |
| 5 | import com.vayunmathur.communicate.data.whatsapp.buildContextInfo |
| 6 | import com.vayunmathur.communicate.data.whatsapp.buildConversationMessage |
| 7 | import com.vayunmathur.communicate.data.whatsapp.buildDisappearingTimerProto |
| 8 | import com.vayunmathur.communicate.data.whatsapp.buildEditProto |
| 9 | import com.vayunmathur.communicate.data.whatsapp.buildRevokeProto |
| 10 | import com.vayunmathur.communicate.data.whatsapp.buildTextProto |
| 11 | import com.vayunmathur.communicate.data.whatsapp.getMessageType |
| 12 | import com.vayunmathur.communicate.data.whatsapp.isRevokeFromMe |
| 13 | import com.vayunmathur.communicate.data.whatsapp.proto.WhatsAppE2EProto |
| 14 | import com.vayunmathur.communicate.data.whatsapp.unwrapViewOnce |
| 15 | import kotlin.test.Test |
| 16 | import kotlin.test.assertEquals |
| 17 | import kotlin.test.assertFalse |
| 18 | import kotlin.test.assertNull |
| 19 | import kotlin.test.assertTrue |
| 20 | |
| 21 | /** |
| 22 | * Wire-shape assertions for the messaging-hardening send builders (Phase A 1a-1c). These are the |
| 23 | * proto builders that were previously emitting plaintext `<enc>` nodes; they now return E2E |
| 24 | * [WhatsAppE2EProto.Message] protos so the caller routes them through the Signal fan-out. Pure |
| 25 | * (protobuf-java only), so no Android runtime is required. |
| 26 | */ |
| 27 | class WhatsAppSendProtoTest { |
| 28 | |
| 29 | @Test |
| 30 | fun editProto_setsMessageEditProtocolMessage() { |
| 31 | val msg = WhatsAppProtocol.buildEditProto("[email protected]", "TARGET1", "new text") |
| 32 | assertTrue(msg.hasProtocolMessage()) |
| 33 | val pm = msg.protocolMessage |
| 34 | assertEquals(WhatsAppE2EProto.ProtocolMessage.Type.MESSAGE_EDIT, pm.type) |
| 35 | assertEquals("TARGET1", pm.key.id) |
| 36 | assertTrue(pm.key.fromMe) |
| 37 | assertEquals("[email protected]", pm.key.remoteJid) |
| 38 | assertEquals("new text", pm.editedMessage.conversation) |
| 39 | assertTrue(pm.timestampMs > 0) |
| 40 | } |
| 41 | |
| 42 | @Test |
| 43 | fun revokeProto_ownMessage_fromMeNoParticipant() { |
| 44 | val msg = WhatsAppProtocol.buildRevokeProto( |
| 45 | chatJid = "[email protected]", |
| 46 | senderJid = "[email protected]", |
| 47 | targetMessageId = "T1", |
| 48 | ownJid = "[email protected]", |
| 49 | ) |
| 50 | val pm = msg.protocolMessage |
| 51 | assertEquals(WhatsAppE2EProto.ProtocolMessage.Type.REVOKE, pm.type) |
| 52 | assertTrue(pm.key.fromMe) |
| 53 | assertFalse(pm.key.hasParticipant()) |
| 54 | assertTrue(WhatsAppProtocol.isRevokeFromMe("[email protected]", "[email protected]")) |
| 55 | assertTrue(WhatsAppProtocol.isRevokeFromMe("", "[email protected]")) |
| 56 | } |
| 57 | |
| 58 | @Test |
| 59 | fun revokeProto_groupOtherSender_setsParticipant() { |
| 60 | val msg = WhatsAppProtocol.buildRevokeProto( |
| 61 | chatJid = "[email protected]", |
| 62 | senderJid = "[email protected]", |
| 63 | targetMessageId = "T2", |
| 64 | ownJid = "[email protected]", |
| 65 | ) |
| 66 | val pm = msg.protocolMessage |
| 67 | assertFalse(pm.key.fromMe) |
| 68 | assertEquals("[email protected]", pm.key.participant) |
| 69 | assertFalse(WhatsAppProtocol.isRevokeFromMe("[email protected]", "[email protected]")) |
| 70 | } |
| 71 | |
| 72 | @Test |
| 73 | fun contactProto_setsDisplayNameAndVcard() { |
| 74 | val msg = WhatsAppProtocol.buildContactProto("Alice", "BEGIN:VCARD\nEND:VCARD") |
| 75 | assertTrue(msg.hasContactMessage()) |
| 76 | assertEquals("Alice", msg.contactMessage.displayName) |
| 77 | assertEquals("BEGIN:VCARD\nEND:VCARD", msg.contactMessage.vcard) |
| 78 | } |
| 79 | |
| 80 | @Test |
| 81 | fun disappearingTimerProto_setsEphemeralSetting() { |
| 82 | val msg = WhatsAppProtocol.buildDisappearingTimerProto(604800L) |
| 83 | val pm = msg.protocolMessage |
| 84 | assertEquals(WhatsAppE2EProto.ProtocolMessage.Type.EPHEMERAL_SETTING, pm.type) |
| 85 | assertEquals(604800, pm.ephemeralExpiration) |
| 86 | } |
| 87 | |
| 88 | @Test |
| 89 | fun textProto_plain_isConversation() { |
| 90 | val msg = WhatsAppProtocol.buildTextProto("hello") |
| 91 | assertTrue(msg.hasConversation()) |
| 92 | assertEquals("hello", msg.conversation) |
| 93 | assertFalse(msg.hasExtendedTextMessage()) |
| 94 | } |
| 95 | |
| 96 | @Test |
| 97 | fun textProto_withMentions_usesExtendedTextWithContextInfo() { |
| 98 | val msg = WhatsAppProtocol.buildTextProto( |
| 99 | "hi @alice", |
| 100 | mentionedJids = listOf("[email protected]"), |
| 101 | ) |
| 102 | assertTrue(msg.hasExtendedTextMessage()) |
| 103 | val ext = msg.extendedTextMessage |
| 104 | assertEquals("hi @alice", ext.text) |
| 105 | assertEquals(listOf("[email protected]"), ext.contextInfo.mentionedJidList) |
| 106 | } |
| 107 | |
| 108 | @Test |
| 109 | fun textProto_withQuotedReply_setsStanzaIdAndParticipant() { |
| 110 | val quoted = WhatsAppProtocol.QuotedContext( |
| 111 | stanzaId = "QID", |
| 112 | participant = "[email protected]", |
| 113 | quotedMessage = WhatsAppProtocol.buildConversationMessage("original"), |
| 114 | ) |
| 115 | val msg = WhatsAppProtocol.buildTextProto("reply", quoted = quoted) |
| 116 | val ctx = msg.extendedTextMessage.contextInfo |
| 117 | assertEquals("QID", ctx.stanzaId) |
| 118 | assertEquals("[email protected]", ctx.participant) |
| 119 | assertEquals("original", ctx.quotedMessage.conversation) |
| 120 | } |
| 121 | |
| 122 | @Test |
| 123 | fun textProto_withLinkPreview_setsPreviewFields() { |
| 124 | val preview = WhatsAppProtocol.LinkPreview( |
| 125 | matchedText = "https://example.com", |
| 126 | canonicalUrl = "https://example.com/", |
| 127 | title = "Example", |
| 128 | description = "An example", |
| 129 | ) |
| 130 | val msg = WhatsAppProtocol.buildTextProto("see https://example.com", linkPreview = preview) |
| 131 | val ext = msg.extendedTextMessage |
| 132 | assertEquals("https://example.com", ext.matchedText) |
| 133 | assertEquals("https://example.com/", ext.canonicalUrl) |
| 134 | assertEquals("Example", ext.title) |
| 135 | assertEquals("An example", ext.description) |
| 136 | } |
| 137 | |
| 138 | @Test |
| 139 | fun buildContextInfo_returnsNullWhenEmpty() { |
| 140 | assertNull(WhatsAppProtocol.buildContextInfo()) |
| 141 | } |
| 142 | |
| 143 | @Test |
| 144 | fun unwrapViewOnce_v2_returnsInnerAndTypesAsImage() { |
| 145 | val image = WhatsAppE2EProto.Message.newBuilder() |
| 146 | .setImageMessage( |
| 147 | WhatsAppE2EProto.ImageMessage.newBuilder().setMimetype("image/jpeg"), |
| 148 | ) |
| 149 | .build() |
| 150 | val wrapped = WhatsAppE2EProto.Message.newBuilder() |
| 151 | .setViewOnceMessageV2( |
| 152 | WhatsAppE2EProto.FutureProofMessage.newBuilder().setMessage(image), |
| 153 | ) |
| 154 | .build() |
| 155 | |
| 156 | val inner = WhatsAppProtocol.unwrapViewOnce(wrapped) |
| 157 | assertTrue(inner != null && inner.hasImageMessage()) |
| 158 | assertEquals("image image/jpeg", WhatsAppProtocol.getMessageType(wrapped)) |
| 159 | } |
| 160 | |
| 161 | @Test |
| 162 | fun unwrapViewOnce_nonWrapper_returnsNull() { |
| 163 | val plain = WhatsAppProtocol.buildConversationMessage("hi") |
| 164 | assertNull(WhatsAppProtocol.unwrapViewOnce(plain)) |
| 165 | } |
| 166 | } |