package com.cottondb.messages.warm import android.app.Application import androidx.compose.ui.test.assertTextEquals import androidx.compose.ui.test.junit4.createComposeRule import androidx.compose.ui.test.onNodeWithTag import androidx.compose.ui.test.onNodeWithText import androidx.compose.ui.test.performClick import androidx.compose.ui.test.performScrollTo import androidx.compose.ui.test.SemanticsMatcher import androidx.compose.ui.test.assert import androidx.compose.ui.semantics.SemanticsProperties import androidx.compose.ui.semantics.getOrNull 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.Rule import org.junit.Test import org.junit.runner.RunWith import org.robolectric.RobolectricTestRunner import org.robolectric.annotation.Config /** * STORAGE BY CHAT (owner, 23 Nov 2026, item 4; CONTRACT.md "Storage by chat"): under Storage's totals, what each chat * keeps on this phone, largest first, a tap opening its breakdown and Free this chat. The answers are invented, shaped * as the core's `chat.storage` (Asha 19 MB, the Hill trip 5 MB, Meera 2.6 MB with a voice message not synced yet). */ @RunWith(RobolectricTestRunner::class) @Config(sdk = [35], qualifiers = "w360dp-h1600dp-mdpi", application = Application::class) class StorageByChatTest { @get:Rule val compose = createComposeRule() private fun j(text: String): JsonObject = Json.parseToJsonElement(text).jsonObject private val chatRows = SemanticsMatcher("a chat's row") { it.config.getOrNull(SemanticsProperties.TestTag)?.startsWith("you-storage-chat-t-") == true } private val storage = j( """{"title":"Storage","said":"190 MB","parts":[{"id":"data","label":"Your data","bytes":140000000,"said":"140 MB"}, {"id":"files","label":"Photos and files","bytes":26600000,"said":"27 MB"}],"free_up":{"label":"Free up space","bytes":26300000,"line":"Frees 26 MB."}}""", ) private val chats = j( """{"title":"By chat","chats":[ {"thread":"t-asha","name":"Asha Verma","picture":{"initials":"AV","tint":1},"bytes":19300000,"said":"19 MB", "parts":[{"id":"photos","label":"Photos","bytes":5300000,"said":"5.3 MB"},{"id":"videos","label":"Videos","bytes":14000000,"said":"14 MB"}], "free":{"label":"Free this chat","bytes":19300000,"line":"Previews stay, and anything not synced yet.","ask":"Free 19 MB from Asha Verma?","ask_body":"They come back when you open them.","confirm":"Free up"}}, {"thread":"t-hill","name":"The Hill trip","picture":{"initials":"HT","tint":3},"bytes":5000000,"said":"5 MB", "parts":[{"id":"photos","label":"Photos","bytes":4400000,"said":"4.4 MB"},{"id":"files","label":"Files","bytes":600000,"said":"0.6 MB"}], "free":{"label":"Free this chat","bytes":5000000,"line":"Previews stay, and anything not synced yet.","ask":"Free 5 MB from The Hill trip?","confirm":"Free up"}}, {"thread":"t-meera","name":"Meera Iyer","picture":{"initials":"MI","tint":2},"bytes":300000,"said":"0.3 MB", "parts":[{"id":"voice","label":"Voice messages","bytes":300000,"said":"0.3 MB"}], "free":{"label":"Free this chat","bytes":0,"line":"Previews stay, and anything not synced yet."}}]}""", ) @Test fun under_storage_each_chat_stands_in_the_cores_order_with_its_size_and_opens() { val by = StorageByChat.of(chats)!! var opened: String? = null compose.setContent { WarmTheme(night = false) { StoragePage(storage, "Storage", "Back", "Comms keeps this much on this phone", "Each number is measured on this phone.", {}, {}) { StorageChats(by) { opened = it } } } } compose.onNodeWithText("BY CHAT").performScrollTo() val rows = compose.onAllNodes(chatRows).fetchSemanticsNodes().map { it.config.getOrNull(SemanticsProperties.TestTag) } assertEquals(listOf("you-storage-chat-t-asha", "you-storage-chat-t-hill", "you-storage-chat-t-meera"), rows) // Read aloud as one: the name and what it keeps. compose.onNodeWithTag("you-storage-chat-t-asha").assert(SemanticsMatcher.expectValue(SemanticsProperties.ContentDescription, listOf("Asha Verma, 19 MB"))) compose.onNodeWithTag("you-storage-chat-t-hill").performScrollTo().performClick() assertEquals("t-hill", opened) } @Test fun with_nothing_kept_the_core_says_so() { val by = StorageByChat.of(j("""{"title":"By chat","none":"No chat keeps photos or files on this phone yet.","chats":[]}"""))!! compose.setContent { WarmTheme(night = false) { StorageChats(by) {} } } compose.onNodeWithTag("you-storage-chats-none", useUnmergedTree = true).assertTextEquals("No chat keeps photos or files on this phone yet.") assertEquals(0, compose.onAllNodes(chatRows).fetchSemanticsNodes().size) } @Test fun a_chats_breakdown_shows_only_its_kinds_and_frees_what_was_agreed() { val asha = StorageByChat.of(chats)!!.chat("t-asha")!! var freed: Long? = null var back = 0 compose.setContent { WarmTheme(night = false) { ChatStoragePage(asha, "Back", "Each number is measured on this phone.", { freed = it }, { back++ }) } } compose.onNodeWithText("Asha Verma").assertExists() compose.onNodeWithTag("you-storage-chat-total", useUnmergedTree = true).assertTextEquals("19 MB") compose.onNodeWithTag("you-storage-chat-photos").assertExists() compose.onNodeWithTag("you-storage-chat-videos").assertExists() compose.onNodeWithTag("you-storage-chat-voice").assertDoesNotExist() compose.onNodeWithTag("you-storage-chat-files").assertDoesNotExist() compose.onNodeWithText("Previews stay, and anything not synced yet.").assertExists() compose.onNodeWithTag("you-storage-chat-free").performScrollTo().performClick() assertEquals(19300000L, freed) compose.onNodeWithTag("you-storage-chat-page-back").performClick() assertEquals(1, back) } @Test fun a_chat_with_nothing_safe_to_free_has_free_this_chat_dimmed_and_still_says_what_stays() { val meera = StorageByChat.of(chats)!!.chat("t-meera")!! assertNull("no ask with nothing to free", meera.free!!.ask) var freed: Long? = null compose.setContent { WarmTheme(night = false) { ChatStoragePage(meera, "Back", "Each number is measured on this phone.", { freed = it }, {}) } } compose.onNodeWithTag("you-storage-chat-voice").assertExists() compose.onNodeWithText("Previews stay, and anything not synced yet.").assertExists() compose.onNodeWithTag("you-storage-chat-free").performScrollTo().assert(SemanticsMatcher.keyNotDefined(androidx.compose.ui.semantics.SemanticsActions.OnClick)) assertNull(freed) } }