1
1
package org.xmtp.android.library
2
2
3
3
import androidx.test.ext.junit.runners.AndroidJUnit4
4
+ import com.google.protobuf.kotlin.toByteStringUtf8
4
5
import org.junit.Assert.assertEquals
5
6
import org.junit.Test
6
7
import org.junit.runner.RunWith
7
8
import org.xmtp.android.library.codecs.ContentTypeReaction
9
+ import org.xmtp.android.library.codecs.EncodedContent
8
10
import org.xmtp.android.library.codecs.Reaction
9
11
import org.xmtp.android.library.codecs.ReactionAction
10
12
import org.xmtp.android.library.codecs.ReactionCodec
@@ -14,6 +16,50 @@ import org.xmtp.android.library.messages.walletAddress
14
16
@RunWith(AndroidJUnit4 ::class )
15
17
class ReactionTest {
16
18
19
+ @Test
20
+ fun testCanDecodeLegacyForm () {
21
+ val codec = ReactionCodec ()
22
+
23
+ // This is how clients send reactions now.
24
+ val canonicalEncoded = EncodedContent .newBuilder().also {
25
+ it.type = ContentTypeReaction
26
+ it.content = """
27
+ {
28
+ "action": "added",
29
+ "content": "smile",
30
+ "reference": "abc123",
31
+ "schema": "shortcode"
32
+ }
33
+ """ .trimIndent().toByteStringUtf8()
34
+ }.build()
35
+
36
+ // Previously, some clients sent reactions like this.
37
+ // So we test here to make sure we can still decode them.
38
+ val legacyEncoded = EncodedContent .newBuilder().also {
39
+ it.type = ContentTypeReaction
40
+ it.putAllParameters(
41
+ mapOf (
42
+ " action" to " added" ,
43
+ " reference" to " abc123" ,
44
+ " schema" to " shortcode" ,
45
+ )
46
+ )
47
+ it.content = " smile" .toByteStringUtf8()
48
+ }.build()
49
+
50
+ val canonical = codec.decode(canonicalEncoded)
51
+ val legacy = codec.decode(legacyEncoded)
52
+
53
+ assertEquals(ReactionAction .added, canonical.action)
54
+ assertEquals(ReactionAction .added, legacy.action)
55
+ assertEquals(" smile" , canonical.content)
56
+ assertEquals(" smile" , legacy.content)
57
+ assertEquals(" abc123" , canonical.reference)
58
+ assertEquals(" abc123" , legacy.reference)
59
+ assertEquals(ReactionSchema .shortcode, canonical.schema)
60
+ assertEquals(ReactionSchema .shortcode, legacy.schema)
61
+ }
62
+
17
63
@Test
18
64
fun testCanUseReactionCodec () {
19
65
Client .register(codec = ReactionCodec ())
0 commit comments