package com.cottondb.days import android.app.Application import androidx.compose.runtime.CompositionLocalProvider import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.test.assertIsDisplayed 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.performImeAction import androidx.compose.ui.test.performTextClearance import androidx.compose.ui.test.performTextInput import com.cottondb.shell.DeleteAccount import com.cottondb.shell.Deleting import com.cottondb.shell.LocalCardLook import com.cottondb.shell.core.CoreException import com.cottondb.shell.core.CottonCore import com.cottondb.days.pen.DaysCardLook import com.cottondb.days.pen.penFonts import kotlinx.serialization.json.JsonNull import kotlinx.serialization.json.JsonObject import kotlinx.serialization.json.JsonPrimitive 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 /** * 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() 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(), core.asked) } } }