Cotton code
75 lines · 4.0 KB · KotlinRaw
package com.cottondb.storyimport android.app.Applicationimport androidx.compose.runtime.getValueimport androidx.compose.runtime.mutableIntStateOfimport androidx.compose.runtime.setValueimport androidx.compose.ui.test.junit4.createComposeRuleimport java.io.Fileimport org.junit.Assert.assertEqualsimport org.junit.Assert.assertTrueimport org.junit.Ruleimport org.junit.Testimport org.junit.runner.RunWithimport org.robolectric.RobolectricTestRunnerimport org.robolectric.annotation.Config/** * An open script's home is read again when a change lands. On the emulator (big invented account, 21 Nov 2026) a new * phone opened the 300-scene script as its first rows came and kept "No scenes yet" over it after the loading line * went, until Story was closed and opened. */@RunWith(RobolectricTestRunner::class)@Config(sdk = [35], qualifiers = "w400dp-h800dp-mdpi", application = Application::class)class StoryHomeFollowsTest {    @get:Rule val compose = createComposeRule()    @Test fun the_home_is_read_again_on_each_change_that_lands_and_not_before() {        val read = mutableListOf<String>()        var revision by mutableIntStateOf(0)        compose.setContent { HomeFollowsChanges(revision, { "invented-night" }) { read += it } }        compose.waitForIdle()        assertEquals("the home the script stood with is not read twice", emptyList<String>(), read)        compose.runOnIdle { revision++ }        compose.waitForIdle()        assertEquals(listOf("invented-night"), read)        compose.runOnIdle { revision++ }        compose.waitForIdle()        assertEquals(listOf("invented-night", "invented-night"), read)    }    /** A script gone from this copy (signed out, signed in): the home becomes what `story.current` names, with no raw refusal said. */    @Test fun a_script_gone_from_this_copy_gives_way_to_the_current_one() {        val state = com.cottondb.story.ink.InkState()        state.home("invented-gone")        val said = mutableListOf<Throwable>()        kotlinx.coroutines.runBlocking {            state.readHome("invented-gone", { throw IllegalStateException("There is no story with that id.") }, { com.cottondb.story.ink.Current(null, null) }) { said += it }        }        assertEquals(null, state.story)        assertTrue(state.known)        assertEquals(emptyList<Throwable>(), said)        state.home("invented-other")        kotlinx.coroutines.runBlocking {            state.readHome("invented-other", { throw IllegalStateException("io") }, { com.cottondb.story.ink.Current("invented-other", null) }) { said += it }        }        assertEquals("a script still named that fails says why", 1, said.size)    }    /** An open scene is read again as changes land, keeping who speaks next (two emulators, 22 Nov 2026). */    @Test fun an_open_scene_follows_the_changes() {        val app = generateSequence(File("").absoluteFile) { it.parentFile }.first { File(it, "src/story/kotlin/com/cottondb/story").isDirectory }        val screen = File(app, "src/story/kotlin/com/cottondb/story/ink/SceneScreen.kt").readText()        val body = screen.substring(screen.indexOf("fun SceneScreen("))        assertTrue(body.contains("HomeFollowsChanges(shell.revision, { scene }) { id -> runCatching { book.scene(id) }.getOrNull()?.let(w::follow) }"))        val follow = screen.substring(screen.indexOf("fun follow(next: Scene)")).substringBefore("\n    }")        assertTrue("following keeps who speaks next", !follow.contains("speaker =") && !follow.contains("kind ="))    }    @Test fun the_surface_follows_the_changes() {        val app = generateSequence(File("").absoluteFile) { it.parentFile }.first { File(it, "src/story/kotlin/com/cottondb/story").isDirectory }        val surface = File(app, "src/story/kotlin/com/cottondb/story/StorySurface.kt").readText()        val body = surface.substring(surface.indexOf("fun StorySurface("))        assertTrue(body.contains("HomeFollowsChanges(shell.revision, { state.story }) { story -> state.readHome(story, { home = book.home(it) }, { book.now() }, shell::failed) }"))    }}