Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / communicate-dev / src / test / java / com / vayunmathur / communicate / whatsapp / W5Slice1GroupBodiesTest.kt
W5Slice1GroupBodiesTest.kt130 lines · 4.4 KiBRaw
| 1 | package com.vayunmathur.communicate.whatsapp |
| 2 | |
| 3 | import com.vayunmathur.communicate.data.whatsapp.WhatsAppProtocol |
| 4 | import com.vayunmathur.communicate.data.whatsapp.ephemeralBody |
| 5 | import com.vayunmathur.communicate.data.whatsapp.groupChangeBody |
| 6 | import kotlin.test.Test |
| 7 | import kotlin.test.assertEquals |
| 8 | import kotlin.test.assertNull |
| 9 | import kotlin.test.assertTrue |
| 10 | |
| 11 | /** |
| 12 | * Slice 1 (w5): group-notification bodies (WhatsAppGroupBodies.kt). Pure JVM. |
| 13 | */ |
| 14 | class W5Slice1GroupBodiesTest { |
| 15 | |
| 16 | private fun node(tag: String, attrs: Map<String, String> = emptyMap(), data: String? = null) = |
| 17 | WhatsAppProtocol.Node( |
| 18 | tag = tag, |
| 19 | attrs = attrs, |
| 20 | data = data?.toByteArray(Charsets.UTF_8), |
| 21 | ) |
| 22 | |
| 23 | private fun memberNode(tag: String, vararg jids: String) = WhatsAppProtocol.Node( |
| 24 | tag = tag, |
| 25 | content = jids.map { WhatsAppProtocol.Node(tag = "participant", attrs = mapOf("jid" to it)) }, |
| 26 | ) |
| 27 | |
| 28 | @Test |
| 29 | fun subject_fromAttrAndFromData() { |
| 30 | assertEquals( |
| 31 | "[Group name changed to: New Name]", |
| 32 | groupChangeBody(node("subject", mapOf("subject" to "New Name"))), |
| 33 | ) |
| 34 | assertEquals( |
| 35 | "[Group name changed to: Data Name]", |
| 36 | groupChangeBody(node("subject", data = "Data Name")), |
| 37 | ) |
| 38 | assertNull(groupChangeBody(node("subject"))) |
| 39 | } |
| 40 | |
| 41 | @Test |
| 42 | fun description_usesDataOrEmpty() { |
| 43 | assertEquals( |
| 44 | "[Group description changed: hello]", |
| 45 | groupChangeBody(node("description", data = "hello")), |
| 46 | ) |
| 47 | assertEquals( |
| 48 | "[Group description changed: ]", |
| 49 | groupChangeBody(node("description")), |
| 50 | ) |
| 51 | } |
| 52 | |
| 53 | @Test |
| 54 | fun membership_listsParticipantsAndAction() { |
| 55 | val body = groupChangeBody( |
| 56 | memberNode("add", "[email protected]", "[email protected]"), |
| 57 | )!! |
| 58 | assertTrue(body.contains("[email protected]"), body) |
| 59 | assertTrue(body.contains("added"), body) |
| 60 | val removed = groupChangeBody(memberNode("remove", "[email protected]"))!! |
| 61 | assertTrue(removed.contains("removeed"), removed) |
| 62 | val promoted = groupChangeBody(memberNode("promote", "[email protected]"))!! |
| 63 | assertTrue(promoted.contains("promoteed"), promoted) |
| 64 | } |
| 65 | |
| 66 | @Test |
| 67 | fun announce_locked_branches() { |
| 68 | assertEquals( |
| 69 | "[Only admins can send messages now]", |
| 70 | groupChangeBody(node("announce", mapOf("announce" to "true"))), |
| 71 | ) |
| 72 | assertEquals( |
| 73 | "[Only admins can send messages now]", |
| 74 | groupChangeBody(node("announce", mapOf("value" to "on"))), |
| 75 | ) |
| 76 | assertEquals( |
| 77 | "[All participants can send messages now]", |
| 78 | groupChangeBody(node("announce")), |
| 79 | ) |
| 80 | assertEquals( |
| 81 | "[Only admins can edit group info now]", |
| 82 | groupChangeBody(node("locked", mapOf("locked" to "true"))), |
| 83 | ) |
| 84 | assertEquals( |
| 85 | "[All participants can edit group info now]", |
| 86 | groupChangeBody(node("locked", mapOf("locked" to "false"))), |
| 87 | ) |
| 88 | } |
| 89 | |
| 90 | @Test |
| 91 | fun link_unlink_andUnknown() { |
| 92 | assertEquals( |
| 93 | "[Group linked: community]", |
| 94 | groupChangeBody(node("link", mapOf("link_type" to "community"))), |
| 95 | ) |
| 96 | assertEquals( |
| 97 | "[Group unlinked: subgroup]", |
| 98 | groupChangeBody(node("unlink", mapOf("unlink_type" to "subgroup"))), |
| 99 | ) |
| 100 | assertNull(groupChangeBody(node("bogus_tag"))) |
| 101 | } |
| 102 | |
| 103 | @Test |
| 104 | fun ephemeral_offAndDurations() { |
| 105 | assertEquals( |
| 106 | "[Disappearing messages turned off]", |
| 107 | ephemeralBody(node("ephemeral")), |
| 108 | ) |
| 109 | assertEquals( |
| 110 | "[Disappearing messages turned off]", |
| 111 | ephemeralBody(node("ephemeral", mapOf("expiration" to "0"))), |
| 112 | ) |
| 113 | assertEquals( |
| 114 | "[Disappearing messages set to 90 seconds]", |
| 115 | ephemeralBody(node("ephemeral", mapOf("expiration" to "90"))), |
| 116 | ) |
| 117 | assertEquals( |
| 118 | "[Disappearing messages set to 5 hour(s)]", |
| 119 | ephemeralBody(node("ephemeral", mapOf("expiration" to "18000"))), |
| 120 | ) |
| 121 | assertEquals( |
| 122 | "[Disappearing messages set to 3 day(s)]", |
| 123 | ephemeralBody(node("ephemeral", mapOf("expiration" to "259200"))), |
| 124 | ) |
| 125 | assertEquals( |
| 126 | "[Disappearing messages set to 1 week(s)]", |
| 127 | ephemeralBody(node("ephemeral", mapOf("expiration" to "604800"))), |
| 128 | ) |
| 129 | } |
| 130 | } |