ContactMergeTest.kt 137 lignes · 4272 octets
package fr.ebii.card2vcf.crm

import fr.ebii.card2vcf.data.CrmContactEntity
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertNull

class ContactMergeTest {
    private fun contact(
        id: Long,
        fullName: String? = null,
        firstName: String? = null,
        lastName: String? = null,
        company: String? = null,
        jobTitle: String? = null,
        website: String? = null,
        address: String? = null,
        notes: String? = null,
        phones: List<String> = emptyList(),
        emails: List<String> = emptyList(),
    ) = CrmContactEntity(
        id = id,
        fullName = fullName,
        firstName = firstName,
        lastName = lastName,
        company = company,
        jobTitle = jobTitle,
        website = website,
        address = address,
        notes = notes,
        phones = phones,
        emails = emails,
        phonesText = phones.joinToString(" "),
        emailsText = emails.joinToString(" "),
        createdAt = id,
        updatedAt = id,
    )

    private fun choices(
        keepId: Long,
        notesMode: NotesMode = NotesMode.KEEP_PRIMARY,
        fullNameFromId: Long = keepId,
        firstNameFromId: Long = keepId,
        lastNameFromId: Long = keepId,
        companyFromId: Long = keepId,
        jobTitleFromId: Long = keepId,
        websiteFromId: Long = keepId,
        addressFromId: Long = keepId,
    ) = MergeFieldChoices(
        fullNameFromId = fullNameFromId,
        firstNameFromId = firstNameFromId,
        lastNameFromId = lastNameFromId,
        companyFromId = companyFromId,
        jobTitleFromId = jobTitleFromId,
        websiteFromId = websiteFromId,
        addressFromId = addressFromId,
        notesMode = notesMode,
        cardImageFromId = null,
        profileImageFromId = null,
        keepId = keepId,
    )

    @Test
    fun picksScalarsAndUnionsLists() {
        val a = contact(
            1,
            fullName = "A",
            firstName = "Ada",
            company = "CoA",
            notes = "note A",
            phones = listOf("111"),
            emails = listOf("a@ex.com"),
        )
        val b = contact(
            2,
            fullName = "B",
            lastName = "Lovelace",
            jobTitle = "Eng",
            website = "https://b",
            address = "Paris",
            notes = "note B",
            phones = listOf("111", "222"),
            emails = listOf("b@ex.com"),
        )
        val merged = ContactMerge.merge(
            mapOf(1L to a, 2L to b),
            choices(
                keepId = 1,
                fullNameFromId = 2,
                lastNameFromId = 2,
                jobTitleFromId = 2,
                websiteFromId = 2,
                addressFromId = 2,
            ),
            now = 99L,
        )
        assertEquals(1L, merged.id)
        assertEquals("B", merged.fullName)
        assertEquals("Ada", merged.firstName)
        assertEquals("Lovelace", merged.lastName)
        assertEquals("CoA", merged.company)
        assertEquals("Eng", merged.jobTitle)
        assertEquals("https://b", merged.website)
        assertEquals("Paris", merged.address)
        assertEquals("note A", merged.notes)
        assertEquals(listOf("111", "222"), merged.phones)
        assertEquals(listOf("a@ex.com", "b@ex.com"), merged.emails)
        assertEquals("111 222", merged.phonesText)
        assertEquals("a@ex.com b@ex.com", merged.emailsText)
        assertEquals(99L, merged.updatedAt)
        assertEquals(1L, merged.createdAt)
    }

    @Test
    fun concatNotes() {
        val a = contact(1, notes = "alpha")
        val b = contact(2, notes = "beta")
        val merged = ContactMerge.merge(
            mapOf(1L to a, 2L to b),
            choices(keepId = 1, notesMode = NotesMode.CONCAT),
            now = 1L,
        )
        assertEquals("alpha\n---\nbeta", merged.notes)
    }

    @Test
    fun concatNotesBlankBecomesNull() {
        val a = contact(1, notes = "  ")
        val b = contact(2, notes = null)
        val merged = ContactMerge.merge(
            mapOf(1L to a, 2L to b),
            choices(keepId = 1, notesMode = NotesMode.CONCAT),
            now = 1L,
        )
        assertNull(merged.notes)
    }
}