package com.cottondb.android.ui.day import androidx.compose.ui.test.hasSetTextAction import androidx.compose.ui.test.junit4.createComposeRule import androidx.compose.ui.test.onAllNodesWithText import androidx.compose.ui.test.performTextInput import androidx.compose.runtime.mutableIntStateOf import androidx.test.ext.junit.runners.AndroidJUnit4 import androidx.test.platform.app.InstrumentationRegistry import com.cottondb.android.core.CottonCore import com.cottondb.android.day.notes.NoteEditorModel import com.cottondb.android.day.notes.NotesJournalRepository import com.cottondb.android.day.todos.CreateTodo import com.cottondb.android.day.todos.TodoRepository import com.cottondb.android.design.CottonTheme import com.cottondb.android.navigation.Route import com.cottondb.android.ui.day.notes.NotesJournalScreen import com.cottondb.android.ui.day.todos.TodoScreen import java.io.File import java.time.LocalDate import java.util.Base64 import java.util.UUID import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.SupervisorJob import kotlinx.coroutines.cancel import kotlinx.coroutines.runBlocking import kotlinx.serialization.json.buildJsonObject import kotlinx.serialization.json.put import org.junit.After import org.junit.Rule import org.junit.Test import org.junit.runner.RunWith /** Real JNI screen proof: the UI renders rows written to an isolated synthetic workspace. */ @RunWith(AndroidJUnit4::class) class DayScreensTest { @get:Rule val compose = createComposeRule() private val context = InstrumentationRegistry.getInstrumentation().targetContext private val container = File(context.noBackupFilesDir, "day-ui-test-${UUID.randomUUID()}") @After fun removeSyntheticContainer() { container.deleteRecursively() } @Test fun realJniTodoAndNotesScreensRenderSavedSyntheticRecords() = runBlocking { val core = CottonCore.open(config()) val writerScope = CoroutineScope(SupervisorJob() + Dispatchers.Main.immediate) try { core.call("workspace.startLocal", buildJsonObject { put("deviceName", "Android Day UI test") put("appVersion", "0.0.0-instrumented") }) val todos = TodoRepository(core) todos.create(CreateTodo("JNI UI todo")) val todoId = todos.refresh()!!.todos.single { it.title == "JNI UI todo" }.id val notes = NotesJournalRepository(core) val editors = mutableMapOf() val editorFor = { key: String, journalDate: String?, id: String? -> editors.getOrPut(key) { NoteEditorModel(notes, journalDate, id) } } val phase = mutableIntStateOf(0) compose.setContent { CottonTheme { when (phase.intValue) { 0 -> TodoScreen(Route.Todos(false), todos, "Etc/UTC", LocalDate.of(2026, 9, 11), {}, {}) 1 -> TodoScreen(Route.TodosTodo(todoId), todos, "Etc/UTC", LocalDate.of(2026, 9, 11), {}, {}) 2 -> NotesJournalScreen( route = Route.NotesNote("new"), repository = notes, today = LocalDate.of(2026, 9, 11), nowMs = 1_789_128_000_000L, timezone = "Etc/UTC", writerScope = writerScope, editorFor = editorFor, onNavigate = {}, onBack = {}, ) else -> NotesJournalScreen( route = Route.Notes, repository = notes, today = LocalDate.of(2026, 9, 11), nowMs = 1_789_128_000_000L, timezone = "Etc/UTC", writerScope = writerScope, editorFor = editorFor, onNavigate = {}, onBack = {}, ) } } } compose.waitUntil(timeoutMillis = 5_000) { compose.onAllNodesWithText("JNI UI todo").fetchSemanticsNodes().isNotEmpty() } compose.runOnIdle { phase.intValue = 1 } compose.waitUntil(timeoutMillis = 5_000) { compose.onAllNodesWithText("JNI UI todo").fetchSemanticsNodes().isNotEmpty() } compose.runOnIdle { phase.intValue = 2 } compose.awaitIdle() val fields = compose.onAllNodes(hasSetTextAction()) fields[0].performTextInput("JNI UI note") fields[1].performTextInput("saved through Compose") compose.awaitIdle() editorFor("note:new", null, null).flushPending(writerScope)?.await() compose.runOnIdle { phase.intValue = 3 } compose.waitUntil(timeoutMillis = 5_000) { compose.onAllNodesWithText("JNI UI note").fetchSemanticsNodes().isNotEmpty() } } finally { writerScope.cancel() core.close() } } private fun config() = buildJsonObject { put("containerPath", container.absolutePath) put("appVersion", "0.0.0-instrumented") put("timeZone", "Etc/UTC") put("deviceSecret", Base64.getUrlEncoder().withoutPadding().encodeToString(ByteArray(32) { 0x5b })) } }