|
| 1 | +package com.flipcash.app.contacts |
| 2 | + |
| 3 | +import com.flipcash.app.contacts.device.DeviceContactLookup |
| 4 | +import com.flipcash.app.persistence.sources.ContactDataSource |
| 5 | +import com.flipcash.app.phone.PhoneUtils |
| 6 | +import io.mockk.coEvery |
| 7 | +import io.mockk.every |
| 8 | +import io.mockk.mockk |
| 9 | +import kotlinx.coroutines.test.runTest |
| 10 | +import org.junit.Test |
| 11 | +import kotlin.test.assertEquals |
| 12 | +import kotlin.test.assertNull |
| 13 | + |
| 14 | +class ContactResolverTest { |
| 15 | + |
| 16 | + private val e164 = "+15551234567" |
| 17 | + |
| 18 | + private fun resolver( |
| 19 | + dbName: String? = null, |
| 20 | + deviceName: String? = null, |
| 21 | + formatted: String? = null, |
| 22 | + formatThrows: Boolean = false, |
| 23 | + photoUri: String? = null, |
| 24 | + ): ContactResolver { |
| 25 | + val dataSource = mockk<ContactDataSource> { |
| 26 | + coEvery { getDisplayName(e164) } returns dbName |
| 27 | + coEvery { getPhotoUri(e164) } returns photoUri |
| 28 | + } |
| 29 | + val deviceLookup = mockk<DeviceContactLookup> { |
| 30 | + every { lookupDisplayName(e164) } returns deviceName |
| 31 | + } |
| 32 | + val phoneUtils = mockk<PhoneUtils> { |
| 33 | + if (formatThrows) { |
| 34 | + every { formatNumber(any<String>()) } throws RuntimeException("parse error") |
| 35 | + } else { |
| 36 | + every { formatNumber(any<String>()) } returns (formatted ?: e164) |
| 37 | + } |
| 38 | + } |
| 39 | + return ContactResolver(dataSource, deviceLookup, phoneUtils) |
| 40 | + } |
| 41 | + |
| 42 | + // region resolveName |
| 43 | + |
| 44 | + @Test |
| 45 | + fun `DB display name is returned when present`() = runTest { |
| 46 | + val result = resolver(dbName = "Alice").resolveName(e164) |
| 47 | + assertEquals("Alice", result) |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + fun `device lookup is used when DB returns null`() = runTest { |
| 52 | + val result = resolver(deviceName = "Bob Device").resolveName(e164) |
| 53 | + assertEquals("Bob Device", result) |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + fun `formatted number is used when DB and device both return null`() = runTest { |
| 58 | + val result = resolver(formatted = "+1 (555) 123-4567").resolveName(e164) |
| 59 | + assertEquals("+1 (555) 123-4567", result) |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + fun `fallback is returned when all sources fail`() = runTest { |
| 64 | + val result = resolver(formatThrows = true).resolveName(e164, fallback = "unknown") |
| 65 | + assertEquals("unknown", result) |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + fun `raw e164 is default fallback`() = runTest { |
| 70 | + val result = resolver(formatThrows = true).resolveName(e164) |
| 71 | + assertEquals(e164, result) |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + fun `DB takes priority over device lookup`() = runTest { |
| 76 | + val result = resolver(dbName = "Alice DB", deviceName = "Alice Device").resolveName(e164) |
| 77 | + assertEquals("Alice DB", result) |
| 78 | + } |
| 79 | + |
| 80 | + // endregion |
| 81 | + |
| 82 | + // region resolvePhotoUri |
| 83 | + |
| 84 | + @Test |
| 85 | + fun `photo URI is returned from data source`() = runTest { |
| 86 | + val result = resolver(photoUri = "content://photo/1").resolvePhotoUri(e164) |
| 87 | + assertEquals("content://photo/1", result) |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + fun `null photo URI when data source has none`() = runTest { |
| 92 | + val result = resolver().resolvePhotoUri(e164) |
| 93 | + assertNull(result) |
| 94 | + } |
| 95 | + |
| 96 | + // endregion |
| 97 | +} |
0 commit comments