Cotton code
70 lines · 4.0 KB · KotlinRaw
package com.cottondb.messagesimport kotlinx.serialization.json.Jsonimport kotlinx.serialization.json.jsonObjectimport org.junit.Assert.assertEqualsimport org.junit.Assert.assertFalseimport org.junit.Assert.assertNullimport org.junit.Assert.assertTrueimport org.junit.Testimport java.io.Fileimport java.nio.file.Files/** WhatsApp's shared export, carried to the core as it reads it. Every name is invented. */class WhatsAppExportTest {    /** The core reads a .txt or a .zip by name: the staged copy takes the shared name's kind; anything else is not Messages'. */    @Test    fun the_staged_copy_takes_its_kind() {        assertEquals("txt", WhatsAppExport.kind("WhatsApp Chat with Asha.txt"))        assertEquals("zip", WhatsAppExport.kind("WhatsApp Chat - Family.ZIP"))        assertNull(WhatsAppExport.kind("photo.jpg"))        assertNull(WhatsAppExport.kind(null))        val dir = Files.createTempDirectory("share").toFile()        val staged = File(dir, "3b1c").apply { writeText("[12/11/2026, 10:01:02] Asha: hello") }        val named = WhatsAppExport.staged(staged, "WhatsApp Chat with Asha.txt")!!        assertEquals("3b1c.txt", named.name)        assertTrue(named.isFile)        assertFalse(staged.exists())        assertNull(WhatsAppExport.staged(named, "photo.jpg"))    }    /** The request names the file, its shared name and the owner when known; a blank owner is not sent. */    @Test    fun the_request_names_file_and_owner() {        val file = File("/tmp/x.txt")        val asked = WhatsAppExport.request(file, "WhatsApp Chat with Asha.txt", "Walk b")        assertEquals("\"Walk b\"", asked["ownerName"].toString())        assertNull(WhatsAppExport.request(file, null, " ")["ownerName"])        assertNull(WhatsAppExport.request(file, null, null)["fileName"])    }    /**     * A group export the person is not named in under the account's name (another spelling, another phone's name):     * the core refused the guess with its command-line sentence, and the import was lost. Now it is asked again with     * no owner, so the core asks whose side the person is on; the person's own pick, or another refusal, stands.     */    @Test    fun a_guessed_owner_no_sender_carries_asks_the_core_again_with_none() = kotlinx.coroutines.runBlocking {        val refused = com.cottondb.shell.core.CoreException("bad_request", "--owner-name \"Noor Example\" matches nobody in this export.", kotlinx.serialization.json.JsonNull)        val asked = mutableListOf<String?>()        val needs = Json.parseToJsonElement("""{"needs":"ownerName","senders":["Asha Elsewhere","Aarav Example"]}""").jsonObject        val answer = WhatsAppExport.answer("Noor Example") { owner -> asked += owner; if (owner != null) throw refused else needs }        assertEquals(listOf("Noor Example", null), asked)        assertEquals(listOf("Asha Elsewhere", "Aarav Example"), WhatsAppExport.senders(answer))        // Named by the guess, it imports at once.        val imported = Json.parseToJsonElement("""{"thread":"t1","imported":3}""").jsonObject        assertEquals(imported, WhatsAppExport.answer("Noor Example") { imported })        // No guess, or a refusal of another kind: it stands.        val unreadable = com.cottondb.shell.core.CoreException("unreadable", "The file could not be read.", kotlinx.serialization.json.JsonNull)        assertTrue(runCatching { WhatsAppExport.answer(null) { throw refused } }.exceptionOrNull() === refused)        assertTrue(runCatching { WhatsAppExport.answer("Noor Example") { throw unreadable } }.exceptionOrNull() === unreadable)    }    /** `needs: ownerName` answers the senders to pick from; an import answers none. */    @Test    fun the_owner_is_asked_only_when_the_core_needs_it() {        val needs = Json.parseToJsonElement("""{"needs":"ownerName","senders":["Asha","Bimal"],"fileName":"x.txt"}""").jsonObject        assertEquals(listOf("Asha", "Bimal"), WhatsAppExport.senders(needs))        assertNull(WhatsAppExport.senders(Json.parseToJsonElement("""{"thread":"t1","imported":12,"skipped":0}""").jsonObject))    }}