package com.cottondb.story import android.app.Application import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableIntStateOf import androidx.compose.runtime.setValue import androidx.compose.ui.test.junit4.createComposeRule import java.io.File import org.junit.Assert.assertEquals import org.junit.Assert.assertTrue import org.junit.Rule import org.junit.Test import org.junit.runner.RunWith import org.robolectric.RobolectricTestRunner import 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() 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(), 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() 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(), 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) }")) } }