package com.cottondb.people import kotlinx.serialization.json.Json import kotlinx.serialization.json.JsonObject import kotlinx.serialization.json.jsonObject import org.junit.Assert.assertEquals import org.junit.Assert.assertNull import org.junit.Test class PeopleImportTest { private fun obj(text: String) = Json.parseToJsonElement(text).jsonObject /** A birthday is Android's own two forms (with a year, vCard's --MM-DD without); anything else is no birthday, never guessed. */ @Test fun a_birthday_is_read_in_androids_two_forms_only() { assertEquals(Triple(4, 17, 1990), DeviceContact.birthday("1990-04-17")) assertEquals(Triple(4, 17, null), DeviceContact.birthday("--04-17")) assertEquals(Triple(12, 1, 1984), DeviceContact.birthday("1984-12-01T00:00:00Z")) assertNull(DeviceContact.birthday("April 17")) assertNull(DeviceContact.birthday("1990-13-01")) assertNull(DeviceContact.birthday(null)) } /** The row filed is the card's fields only (PeopleImportSheet.syncEntry): empty lists left out, half a birthday none. */ @Test fun the_sync_entry_carries_only_what_the_card_holds() { val full = DeviceContact("key-1", 7, "Asha Example", listOf("+15550100001"), listOf("asha@example.com"), "Tin Works", "Maker", "Pune", 4, 17, 1990).syncEntry() assertEquals(obj("""{"deviceContactId":"key-1","fullName":"Asha Example","phones":["+15550100001"],"emails":["asha@example.com"],"org":"Tin Works","roleTitle":"Maker","city":"Pune","birthday":{"month":4,"day":17,"year":1990}}"""), full) val bare = DeviceContact("key-2", 8, "Ravi Example", birthMonth = 4).syncEntry() assertEquals(obj("""{"deviceContactId":"key-2","fullName":"Ravi Example"}"""), bare) } /** `write`'s answer nests the tool's own: how many it made and who, beside the contact each follows. */ @Test fun the_outcome_is_read_from_the_nested_answer() { val answer = obj("""{"answer":{"inserted":1,"skipped":2,"people":[{"id":"p1","name":"Asha","deviceContactId":"key-1"}]},"rail":"client","queued":1}""") assertEquals(ImportOutcome(1, listOf("p1" to "key-1")), ImportOutcome.of(answer)) assertEquals(ImportOutcome(0, emptyList()), ImportOutcome.of(obj("""{"answer":{"inserted":0,"skipped":3,"people":[]}}"""))) } /** Already-people are `read.peopleImport`'s linked rows by their deviceContactId. */ @Test fun linked_contacts_are_the_reads_device_ids() { val read = obj("""{"linked":[{"id":"p1","deviceContactId":"key-1"},{"id":"p2"}],"linkedCount":2}""") assertEquals(setOf("key-1"), linkedContacts(read)) assertEquals(emptySet(), linkedContacts(JsonObject(emptyMap()))) } /** The search keeps a contact holding every word in its name, phones, emails or company. */ @Test fun the_search_keeps_every_word() { val a = DeviceContact("a", 1, "Asha Example", listOf("+15550100001"), org = "Tin Works") val b = DeviceContact("b", 2, "Ravi Sample", emails = listOf("ravi@example.com")) assertEquals(listOf(a, b), searched(listOf(a, b), " ")) assertEquals(listOf(a), searched(listOf(a, b), "asha tin")) assertEquals(listOf(b), searched(listOf(a, b), "RAVI@")) assertEquals(listOf(a), searched(listOf(a, b), "0100001")) } }