Cotton code
94 lines · 5.3 KB · KotlinRaw
package com.cottondb.daysimport android.app.Applicationimport androidx.compose.runtime.CompositionLocalProviderimport androidx.compose.ui.platform.LocalContextimport androidx.compose.ui.test.assertIsDisplayedimport androidx.compose.ui.test.junit4.createComposeRuleimport androidx.compose.ui.test.onNodeWithTagimport androidx.compose.ui.test.onNodeWithTextimport androidx.compose.ui.test.performClickimport androidx.compose.ui.test.performImeActionimport androidx.compose.ui.test.performTextClearanceimport androidx.compose.ui.test.performTextInputimport com.cottondb.shell.DeleteAccountimport com.cottondb.shell.Deletingimport com.cottondb.shell.LocalCardLookimport com.cottondb.shell.core.CoreExceptionimport com.cottondb.shell.core.CottonCoreimport com.cottondb.days.pen.DaysCardLookimport com.cottondb.days.pen.penFontsimport kotlinx.serialization.json.JsonNullimport kotlinx.serialization.json.JsonObjectimport kotlinx.serialization.json.JsonPrimitiveimport kotlinx.serialization.json.jsonObjectimport org.junit.Assert.assertEqualsimport org.junit.Assert.assertNullimport org.junit.Ruleimport org.junit.Testimport org.junit.runner.RunWithimport org.robolectric.RobolectricTestRunnerimport org.robolectric.annotation.Config/** * Days's Delete account (Settings › Account, last), drawn in Days's own look against an invented core: the row, the * page's warning, Send code by text, a wrong code said in the core's words with nothing deleted, then the code typed * back deletes and the app is told which data key to forget. */@RunWith(RobolectricTestRunner::class)@Config(sdk = [35], qualifiers = "w400dp-h800dp-mdpi", application = Application::class)class DaysDeleteAccountTest {    @get:Rule val compose = createComposeRule()    private class Invented : Deleting {        val asked = mutableListOf<String>()        override suspend fun page(app: String) = obj("""{"row":"Delete account","heading":"Delete your Cotton account","whatGoes":"Everything goes.","whatStays":"Messages you sent stay.","when":"Your account is deleted 30 days after you ask.","otherApps":"Every Cotton app, not only in $app.","send":"Send code by text","codeLabel":"Code from text","confirm":"Delete my account","cancel":"Go back","working":"Deleting your account"}""")        override suspend fun code(): JsonObject { asked += "code"; return obj("""{"challengeId":"invented-challenge","sent":"We sent a code by text message to +919000000009."}""") }        override suspend fun delete(challengeId: String, code: String, app: String): JsonObject {            asked += "delete $challengeId $code $app"            if (code != "123456") throw CoreException("invalid_code", "That code is wrong or too old. Please try again.", JsonNull)            return obj("""{"deleted":true,"line":"Your account is deleted.","databaseId":"invented-database"}""")        }        fun obj(text: String) = CottonCore.json.parseToJsonElement(text).jsonObject    }    @Test fun a_code_typed_back_deletes_the_account_and_a_wrong_one_deletes_nothing() {        val core = Invented()        var deleted: JsonObject? = null        compose.setContent {            CompositionLocalProvider(LocalCardLook provides DaysCardLook(penFonts(LocalContext.current))) { DeleteAccount(core, "Days") { deleted = it } }        }        compose.onNodeWithTag("account-delete").assertIsDisplayed().performClick()        compose.onNodeWithText("Your account is deleted 30 days after you ask.").assertIsDisplayed()        compose.onNodeWithText("Every Cotton app, not only in Days.").assertIsDisplayed()        compose.onNodeWithTag("account-delete-send").performClick()        compose.onNodeWithText("We sent a code by text message to +919000000009.").assertIsDisplayed()        compose.onNodeWithTag("account-delete-code").performTextInput("000000")        compose.onNodeWithTag("account-delete-confirm").performClick()        compose.onNodeWithText("That code is wrong or too old. Please try again.").assertIsDisplayed()        compose.runOnIdle { assertNull("a wrong code deletes nothing", deleted) }        compose.onNodeWithTag("account-delete-code").performTextClearance()        compose.onNodeWithTag("account-delete-code").performTextInput("123456")        // Editing the code clears the refusal; the keyboard's Done deletes, as the button does (hand try, 22 Nov 2026).        compose.onNodeWithTag("account-delete-problem").assertDoesNotExist()        compose.onNodeWithTag("account-delete-code").performImeAction()        compose.runOnIdle {            assertEquals("invented-database", (deleted?.get("databaseId") as? JsonPrimitive)?.content)            assertEquals(listOf("code", "delete invented-challenge 000000 Days", "delete invented-challenge 123456 Days"), core.asked)        }        compose.onNodeWithTag("account-delete-page").assertDoesNotExist()    }    @Test fun go_back_closes_the_page_and_nothing_is_asked() {        val core = Invented()        compose.setContent {            CompositionLocalProvider(LocalCardLook provides DaysCardLook(penFonts(LocalContext.current))) { DeleteAccount(core, "Days") {} }        }        compose.onNodeWithTag("account-delete").performClick()        compose.onNodeWithTag("account-delete-cancel").performClick()        compose.onNodeWithTag("account-delete-page").assertDoesNotExist()        compose.runOnIdle { assertEquals(emptyList<String>(), core.asked) }    }}