Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / communicate-dev / src / test / java / com / vayunmathur / communicate / whatsapp / W5Slice1MexOpsBuildersTest.kt
W5Slice1MexOpsBuildersTest.kt150 lines · 6.3 KiBRaw
| 1 | package com.vayunmathur.communicate.whatsapp |
| 2 | |
| 3 | import com.vayunmathur.communicate.data.whatsapp.mex.WhatsAppMexOps |
| 4 | import com.vayunmathur.communicate.data.whatsapp.mex.WhatsAppMexVariables |
| 5 | import java.util.Base64 |
| 6 | import kotlin.test.Test |
| 7 | import kotlin.test.assertEquals |
| 8 | import kotlin.test.assertTrue |
| 9 | |
| 10 | /** |
| 11 | * Slice 1 (w5): all remaining JVM-pure `variables` builders in WhatsAppMexOps |
| 12 | * (groups batch, contacts sync, blocklist, presence, newsletter, messaging keys). |
| 13 | * The builders use java.util.Base64 only — no Android runtime needed. |
| 14 | */ |
| 15 | class W5Slice1MexOpsBuildersTest { |
| 16 | |
| 17 | @Test |
| 18 | fun participatingGroupsVariables_withAndWithoutUser() { |
| 19 | assertEquals( |
| 20 | """{"participating_groups_input":{"id":"[email protected]"}}""", |
| 21 | WhatsAppMexVariables.buildParticipatingGroupsVariables("[email protected]"), |
| 22 | ) |
| 23 | assertEquals( |
| 24 | """{"participating_groups_input":{}}""", |
| 25 | WhatsAppMexVariables.buildParticipatingGroupsVariables(null), |
| 26 | ) |
| 27 | } |
| 28 | |
| 29 | @Test |
| 30 | fun groupBatchQueryVariables_listsGroups() { |
| 31 | assertEquals( |
| 32 | """{"batch_query_input":{"groups":[{"group_jid":"[email protected]","query_context":"INTERACTIVE"},{"group_jid":"[email protected]","query_context":"INTERACTIVE"}]}}""", |
| 33 | WhatsAppMexVariables.buildGroupBatchQueryVariables(listOf("[email protected]", "[email protected]"), "INTERACTIVE"), |
| 34 | ) |
| 35 | } |
| 36 | |
| 37 | @Test |
| 38 | fun contactDiscoveryVariables_shape() { |
| 39 | assertEquals( |
| 40 | """{"input":{"contacts":[{"phone":{"raw_pn":"+15550001"}},{"phone":{"raw_pn":"+15550002"}}],"context":"SEARCH"}}""", |
| 41 | WhatsAppMexVariables.buildContactDiscoveryVariables(listOf("+15550001", "+15550002"), "SEARCH"), |
| 42 | ) |
| 43 | } |
| 44 | |
| 45 | @Test |
| 46 | fun primaryContactsFullSyncVariables_shape() { |
| 47 | val out = WhatsAppMexVariables.buildPrimaryContactsFullSyncVariables( |
| 48 | listOf("+15550001"), "sess-1", 0, true, "REGISTRATION", |
| 49 | ) |
| 50 | assertTrue(out.contains("\"session_id\":\"sess-1\""), out) |
| 51 | assertTrue(out.contains("\"page_index\":0"), out) |
| 52 | assertTrue(out.contains("\"last\":true,\"page_index\":0") || out.contains("\"last\":true"), out) |
| 53 | assertTrue(out.contains("\"context\":\"REGISTRATION\""), out) |
| 54 | assertTrue(out.contains("\"raw_pn\":\"+15550001\""), out) |
| 55 | } |
| 56 | |
| 57 | @Test |
| 58 | fun usernameCheckVariables_withAndWithoutSession() { |
| 59 | assertEquals( |
| 60 | """{"username":"jo","include_suggestions":true,"source":"USER_INPUT","session_id":"s1"}""", |
| 61 | WhatsAppMexVariables.buildUsernameCheckVariables("jo", true, "USER_INPUT", "s1"), |
| 62 | ) |
| 63 | assertEquals( |
| 64 | """{"username":"jo","include_suggestions":false,"source":"USER_INPUT"}""", |
| 65 | WhatsAppMexVariables.buildUsernameCheckVariables("jo", false, "USER_INPUT", null), |
| 66 | ) |
| 67 | } |
| 68 | |
| 69 | @Test |
| 70 | fun usernameSetVariables_withAndWithoutSession() { |
| 71 | assertEquals( |
| 72 | """{"username":"johndoe","source":"USER_INPUT","pin":"ABC123","session_id":"s9"}""", |
| 73 | WhatsAppMexVariables.buildUsernameSetVariables("johndoe", "ABC123", "USER_INPUT", "s9"), |
| 74 | ) |
| 75 | } |
| 76 | |
| 77 | @Test |
| 78 | fun blocklistVariables_shapes() { |
| 79 | assertEquals("{}", WhatsAppMexVariables.buildBlocklistGetVariables(null)) |
| 80 | assertEquals("""{"dhash":"h1"}""", WhatsAppMexVariables.buildBlocklistGetVariables("h1")) |
| 81 | val update = WhatsAppMexVariables.buildUpdateBlocklistLidVariables( |
| 82 | "lid1", "pn_jid", "[email protected]", "BLOCK", "d2", |
| 83 | ) |
| 84 | assertEquals( |
| 85 | """{"block_input":{"user":"lid1","identifier":{"pn_jid":"[email protected]"},"action":"BLOCK","dhash":"d2"}}""", |
| 86 | update, |
| 87 | ) |
| 88 | val updateNoDhash = WhatsAppMexVariables.buildUpdateBlocklistLidVariables( |
| 89 | "lid1", "username", "jo", "UNBLOCK", null, |
| 90 | ) |
| 91 | assertTrue(!updateNoDhash.contains("dhash"), updateNoDhash) |
| 92 | } |
| 93 | |
| 94 | @Test |
| 95 | fun presenceVariables_withAndWithoutFilter() { |
| 96 | assertEquals( |
| 97 | """{"get_online_or_last_status_input":{"online_or_last_status_input":[{"jid":"a@lid"}],"last_active_filter":"LAST_HOUR"}}""", |
| 98 | WhatsAppMexVariables.buildOnlineOrLastStatusVariables(listOf("a@lid"), "LAST_HOUR"), |
| 99 | ) |
| 100 | assertTrue( |
| 101 | !WhatsAppMexVariables.buildOnlineOrLastStatusVariables(listOf("a@lid"), null).contains("last_active_filter"), |
| 102 | ) |
| 103 | } |
| 104 | |
| 105 | @Test |
| 106 | fun newsletterVariables_shapes() { |
| 107 | assertEquals( |
| 108 | """{"input":{"key":"k1","type":"JID"}}""", |
| 109 | WhatsAppMexVariables.buildNewsletterVariables("k1", "JID", null), |
| 110 | ) |
| 111 | assertEquals( |
| 112 | """{"input":{"key":"k1","type":"INVITE","view_role":"GUEST"}}""", |
| 113 | WhatsAppMexVariables.buildNewsletterVariables("k1", "INVITE", "GUEST"), |
| 114 | ) |
| 115 | assertEquals( |
| 116 | """{"newsletter_id":"n1"}""", |
| 117 | WhatsAppMexVariables.buildNewsletterIdVariables("n1"), |
| 118 | ) |
| 119 | } |
| 120 | |
| 121 | @Test |
| 122 | fun messagingKeysVariables_shapes() { |
| 123 | assertEquals( |
| 124 | """{"input":{"users":["[email protected]","[email protected]"]}}""", |
| 125 | WhatsAppMexVariables.buildGetMessagingKeysVariables(listOf("[email protected]", "[email protected]")), |
| 126 | ) |
| 127 | val setVars = WhatsAppMexVariables.buildSetMessagingKeysVariables( |
| 128 | identity = byteArrayOf(1, 2, 3), |
| 129 | skeyId = 1, |
| 130 | skeyValue = byteArrayOf(4), |
| 131 | skeySignature = byteArrayOf(5, 6), |
| 132 | prekeys = listOf(2 to byteArrayOf(7)), |
| 133 | ) |
| 134 | // type = base64(0x05), ids are 3-byte big-endian base64 |
| 135 | assertTrue(setVars.contains("\"type\":\"" + Base64.getEncoder().encodeToString(byteArrayOf(0x05)) + "\""), setVars) |
| 136 | assertTrue(setVars.contains("\"identity\":\"" + Base64.getEncoder().encodeToString(byteArrayOf(1, 2, 3)) + "\""), setVars) |
| 137 | // ids are 3-byte big-endian base64: 1 -> AAAB, 2 -> AAAC |
| 138 | assertTrue(setVars.contains("\"id\":\"AAAB\""), setVars) |
| 139 | assertTrue(setVars.contains("\"id\":\"AAAC\""), setVars) |
| 140 | } |
| 141 | |
| 142 | @Test |
| 143 | fun opNames_areDocumentedConstants() { |
| 144 | assertEquals("QueryGroupInfo", WhatsAppMexOps.OP_GROUP_QUERY) |
| 145 | assertEquals("ContactUserInfoQuery", WhatsAppMexOps.OP_CONTACT_DISCOVERY) |
| 146 | assertEquals("UsernameSetMutation", WhatsAppMexOps.OP_USERNAME_SET) |
| 147 | assertEquals("GetMessagingKeys", WhatsAppMexOps.OP_GET_MESSAGING_KEYS) |
| 148 | assertEquals("SetMessagingKeys", WhatsAppMexOps.OP_SET_MESSAGING_KEYS) |
| 149 | } |
| 150 | } |