Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / communicate-dev / src / test / java / com / vayunmathur / communicate / whatsapp / W5Slice1ServiceDataTest.kt
W5Slice1ServiceDataTest.kt68 lines · 2.3 KiBRaw
| 1 | package com.vayunmathur.communicate.whatsapp |
| 2 | |
| 3 | import com.vayunmathur.communicate.data.whatsapp.PollOptionData |
| 4 | import com.vayunmathur.communicate.data.whatsapp.Reaction |
| 5 | import com.vayunmathur.communicate.data.whatsapp.WhatsAppServiceData |
| 6 | import kotlin.test.Test |
| 7 | import kotlin.test.assertEquals |
| 8 | import kotlin.test.assertFalse |
| 9 | import kotlin.test.assertNull |
| 10 | import kotlin.test.assertTrue |
| 11 | |
| 12 | /** |
| 13 | * Slice 1 (w5): WhatsAppServiceData serialize/parse round-trip. Pure kotlinx.serialization. |
| 14 | */ |
| 15 | class W5Slice1ServiceDataTest { |
| 16 | |
| 17 | @Test |
| 18 | fun roundTrip_fullPayload() { |
| 19 | val original = WhatsAppServiceData( |
| 20 | senderName = "Alice", |
| 21 | senderJid = "[email protected]", |
| 22 | reactions = listOf(Reaction("👍", 2), Reaction("❤️", 1)), |
| 23 | isEdited = true, |
| 24 | pollQuestion = "Lunch?", |
| 25 | pollOptions = listOf(PollOptionData("pizza", 3, listOf("a", "b"))), |
| 26 | quotedMessageId = "Q1", |
| 27 | quotedBody = "quoted", |
| 28 | quotedSender = "bob", |
| 29 | isGroup = true, |
| 30 | groupParticipantCount = 7, |
| 31 | mediaUrl = "https://x/y.jpg", |
| 32 | mediaMime = "image/jpeg", |
| 33 | mediaName = "photo.jpg", |
| 34 | ) |
| 35 | val back = WhatsAppServiceData.parse(original.serialize())!! |
| 36 | assertEquals(original, back) |
| 37 | } |
| 38 | |
| 39 | @Test |
| 40 | fun roundTrip_defaultsSurvive() { |
| 41 | val back = WhatsAppServiceData.parse(WhatsAppServiceData().serialize())!! |
| 42 | assertEquals(WhatsAppServiceData(), back) |
| 43 | assertFalse(back.isEdited) |
| 44 | assertFalse(back.isRevoked) |
| 45 | } |
| 46 | |
| 47 | @Test |
| 48 | fun parse_nullBlankOrGarbage_returnsNull() { |
| 49 | assertNull(WhatsAppServiceData.parse(null)) |
| 50 | assertNull(WhatsAppServiceData.parse("")) |
| 51 | assertNull(WhatsAppServiceData.parse(" ")) |
| 52 | assertNull(WhatsAppServiceData.parse("not json")) |
| 53 | } |
| 54 | |
| 55 | @Test |
| 56 | fun parse_unknownKeys_ignored() { |
| 57 | val back = WhatsAppServiceData.parse("""{"senderName":"Al","futureField":123}""")!! |
| 58 | assertEquals("Al", back.senderName) |
| 59 | } |
| 60 | |
| 61 | @Test |
| 62 | fun parse_partialJson_usesDefaults() { |
| 63 | val back = WhatsAppServiceData.parse("""{"isRevoked":true,"groupParticipantCount":4}""")!! |
| 64 | assertTrue(back.isRevoked) |
| 65 | assertEquals(4, back.groupParticipantCount) |
| 66 | assertEquals(emptyList(), back.reactions) |
| 67 | } |
| 68 | } |