Modern Apps git
git clone https://git.maproj.org/Modern-Apps.git
dependencies ▾

Modern-Apps.git / communicate-dev / src / test / java / com / vayunmathur / communicate / whatsapp / W5Slice1PollStoreTest.kt

W5Slice1PollStoreTest.kt119 lines · 4.6 KiBRaw
1package com.vayunmathur.communicate.whatsapp
2
3import androidx.room3.Room
4import androidx.test.core.app.ApplicationProvider
5import androidx.test.ext.junit.runners.AndroidJUnit4
6import com.vayunmathur.communicate.data.whatsapp.WhatsAppClient
7import com.vayunmathur.communicate.data.whatsapp.WhatsAppDatabase
8import com.vayunmathur.communicate.data.whatsapp.loadPollSecret
9import com.vayunmathur.communicate.data.whatsapp.rememberInboundPoll
10import com.vayunmathur.communicate.data.whatsapp.proto.WhatsAppE2EProto
11import com.vayunmathur.communicate.data.whatsapp.storePollOptions
12import com.vayunmathur.communicate.data.whatsapp.storePollSecret
13import kotlinx.coroutines.test.runTest
14import org.junit.runner.RunWith
15import kotlin.test.AfterTest
16import kotlin.test.BeforeTest
17import kotlin.test.Test
18import kotlin.test.assertEquals
19import kotlin.test.assertNull
20import kotlin.test.assertTrue
21
22/**
23 * Slice 1 (w5): WhatsAppClient poll persistence (WhatsAppClientSync.kt) against a
24 * real in-memory database wired to the client singleton's [db] slot.
25 * Robolectric (Room + Android Log). Asserts option-hash rows, secret hex
26 * round-trip through DB + memory cache, and invalid-hex tolerance.
27 */
28@RunWith(AndroidJUnit4::class)
29class W5Slice1PollStoreTest {
30
31 private lateinit var db: WhatsAppDatabase
32
33 @BeforeTest
34 fun open() {
35 db = Room.inMemoryDatabaseBuilder(
36 ApplicationProvider.getApplicationContext(),
37 WhatsAppDatabase::class.java,
38 ).allowMainThreadQueries().build()
39 WhatsAppClient.db = db
40 WhatsAppClient.pollSecrets.clear()
41 }
42
43 @AfterTest
44 fun close() {
45 WhatsAppClient.db = null
46 WhatsAppClient.pollSecrets.clear()
47 db.close()
48 }
49
50 @Test
51 fun storePollOptions_writesSha256Rows() = runTest {
52 WhatsAppClient.storePollOptions("P1", listOf("Alpha", "Beta"))
53 val rows = db.pollOptionDao().getByMessageId("P1")
54 assertEquals(2, rows.size)
55 val byName = rows.associateBy { it.optionName }
56 assertEquals(
57 com.vayunmathur.communicate.data.whatsapp.WhatsAppProtocol
58 .sha256("Alpha".toByteArray()).joinToString("") { "%02x".format(it) },
59 byName["Alpha"]!!.optionHash,
60 )
61 assertEquals("Beta", db.pollOptionDao().getByHash("P1", byName["Beta"]!!.optionHash)!!.optionName)
62 }
63
64 @Test
65 fun pollSecret_memoryAndDbRoundTrip() = runTest {
66 val secret = ByteArray(32) { it.toByte() }
67 WhatsAppClient.storePollSecret("P2", secret)
68 // memory cache hit
69 assertTrue(WhatsAppClient.loadPollSecret("P2")!!.contentEquals(secret))
70 // evict memory -> DB hex fallback repopulates memory
71 WhatsAppClient.pollSecrets.clear()
72 assertTrue(WhatsAppClient.loadPollSecret("P2")!!.contentEquals(secret))
73 assertEquals(
74 secret.joinToString("") { "%02x".format(it) },
75 db.pollSecretDao().get("P2"),
76 )
77 }
78
79 @Test
80 fun loadPollSecret_missing_returnsNull() = runTest {
81 assertNull(WhatsAppClient.loadPollSecret("nope"))
82 }
83
84 @Test
85 fun storePollOptions_noDb_isNoOp() = runTest {
86 WhatsAppClient.db = null
87 WhatsAppClient.storePollOptions("PX", listOf("a"))
88 WhatsAppClient.storePollSecret("PX", byteArrayOf(1))
89 // memory path still works without db
90 assertTrue(WhatsAppClient.loadPollSecret("PX")!!.contentEquals(byteArrayOf(1)))
91 WhatsAppClient.db = db
92 }
93
94 @Test
95 fun rememberInboundPoll_storesOptionsAndSecret() = runTest {
96 val secret = ByteArray(32) { 9 }
97 val poll = WhatsAppE2EProto.PollCreationMessage.newBuilder()
98 .setName("Q?")
99 .addOptions(WhatsAppE2EProto.PollCreationMessage.Option.newBuilder().setOptionName("x"))
100 .build()
101 val e2e = WhatsAppE2EProto.Message.newBuilder()
102 .setPollCreationMessageV3(poll)
103 .setMessageContextInfo(
104 WhatsAppE2EProto.MessageContextInfo.newBuilder()
105 .setMessageSecret(com.google.protobuf.ByteString.copyFrom(secret)),
106 )
107 .build()
108 val msg = com.vayunmathur.communicate.data.whatsapp.WhatsAppMessage(
109 id = "RP1", from = "[email protected]", to = "", body = "", timestamp = 1, type = "text",
110 pollData = com.vayunmathur.communicate.data.whatsapp.PollData(
111 question = "Q?", options = listOf("x"), selectableOptionCount = 1,
112 ),
113 e2eMessage = e2e,
114 )
115 WhatsAppClient.rememberInboundPoll(msg)
116 assertEquals(1, db.pollOptionDao().getByMessageId("RP1").size)
117 assertTrue(WhatsAppClient.loadPollSecret("RP1")!!.contentEquals(secret))
118 }
119}