Cotton code
72 lines · 3.3 KB · KotlinRaw
package com.cottondb.messages.chatimport kotlinx.coroutines.CoroutineScopeimport kotlinx.coroutines.Dispatchersimport kotlinx.coroutines.SupervisorJobimport kotlinx.coroutines.runBlockingimport org.junit.Assert.assertEqualsimport org.junit.Assert.assertFalseimport org.junit.Assert.assertNullimport org.junit.Assert.assertTrueimport org.junit.Ruleimport org.junit.Testimport org.junit.rules.TemporaryFolder/** * ONE COPY OF A PICTURE ON THE PHONE (owner, 23 Nov 2026, item 3: Storage showed very little kept). On emulator-5554, * after 32 invented pictures were opened, Storage said Photos and files 48 MB while files/chat-pictures held another * 22.7 MB of second copies that Storage counted nowhere and Free up space never freed. Now the picture is drawn from * the copy the core keeps in the person's media and the opened copy goes; the old folder is emptied once. */class OnePictureCopyTest {    @get:Rule val tmp = TemporaryFolder()    @Test    fun a_fetched_picture_is_drawn_from_the_kept_copy_and_nothing_stays_in_the_folder() = runBlocking {        val dir = tmp.newFolder("chat-pictures")        val kept = tmp.newFolder("media").resolve("ab12.jpg").apply { writeText("invented picture") }        var asked = 0        // The core's answers: opened into the folder the first time (and kept), the kept copy when asked again.        val drawn = ChatPictures.keptOnly(dir) {            asked++            if (asked == 1) dir.resolve("ab12.jpg").apply { writeText("invented picture") } else kept        }        assertEquals(kept, drawn)        assertEquals(2, asked)        assertEquals(emptyList<String>(), dir.list()!!.toList())    }    @Test    fun a_picture_seen_before_opens_from_the_phone_and_writes_nothing() = runBlocking {        // Seen before, then offline: the core answers the kept copy straight from the phone; no second ask, no copy.        val dir = tmp.newFolder("chat-pictures")        val kept = tmp.newFolder("media").resolve("cd34.jpg").apply { writeText("invented picture") }        var asked = 0        val drawn = ChatPictures.keptOnly(dir) { asked++; kept }        assertEquals(kept, drawn)        assertEquals(1, asked)        assertEquals(emptyList<String>(), dir.list()!!.toList())    }    @Test    fun where_nothing_was_kept_the_opened_copy_stands() = runBlocking {        val dir = tmp.newFolder("chat-pictures")        val opened = dir.resolve("ef56.jpg").apply { writeText("invented picture") }        assertEquals(opened, ChatPictures.keptOnly(dir) { opened })        assertTrue(opened.isFile)        // Not there at all: nothing.        assertNull(ChatPictures.keptOnly(dir) { null })    }    @Test    fun the_old_folder_of_second_copies_is_emptied_once_on_start() {        val files = tmp.newFolder("files")        val old = files.resolve(ChatPictures.OLD_FOLDER).apply { mkdirs(); resolve("old.jpg").writeText("invented picture") }        files.resolve("chat-list-core.json").writeText("{}")        ChatData(null, null, Words { _, k -> k }, CoroutineScope(SupervisorJob() + Dispatchers.Unconfined), files, pictures = tmp.newFolder("cache"))        val end = System.currentTimeMillis() + 5_000        while (old.exists() && System.currentTimeMillis() < end) Thread.sleep(20)        assertFalse(old.exists())        assertTrue("the rest of the app's files stay", files.resolve("chat-list-core.json").isFile)    }}