Skip to content

Commit

Permalink
More SceneEditorRepository tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Wavesonics committed Oct 24, 2024
1 parent db4dba3 commit d209513
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,6 @@ abstract class SceneEditorRepository(
abstract suspend fun deleteGroup(scene: SceneItem): Boolean
abstract fun getScenes(): List<SceneItem>
abstract fun getSceneTree(): ImmutableTree<SceneItem>
abstract fun getScenes(root: HPath): List<SceneItem>
abstract fun getSceneTempBufferContents(): List<SceneContent>
abstract fun getSceneFromPath(path: HPath): SceneItem
abstract fun exportStory(path: HPath): HPath
Expand Down Expand Up @@ -299,7 +298,7 @@ abstract class SceneEditorRepository(
)
}

protected fun reloadScenes(summary: SceneSummary? = null) {
fun reloadScenes(summary: SceneSummary? = null) {
val scenes = summary ?: getSceneSummaries()
_sceneListChannel.tryEmit(scenes)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -663,15 +663,6 @@ class SceneEditorRepositoryOkio(
return sceneTree.toImmutableTree()
}

override fun getScenes(root: HPath): List<SceneItem> {
val rootOkia = root.toOkioPath()
return getScenePathsOkio(rootOkia)
.filterScenePathsOkio()
.map { path ->
getSceneFromFilename(path)
}
}

override fun getSceneTempBufferContents(): List<SceneContent> {
val bufferDirectory = getSceneBufferDirectory().toOkioPath()
return fileSystem.list(bufferDirectory)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.darkrockstudios.apps.hammer.common.data.ProjectDef
import com.darkrockstudios.apps.hammer.common.data.SceneBuffer
import com.darkrockstudios.apps.hammer.common.data.SceneContent
import com.darkrockstudios.apps.hammer.common.data.SceneItem
import com.darkrockstudios.apps.hammer.common.data.SceneSummary
import com.darkrockstudios.apps.hammer.common.data.UpdateSource
import com.darkrockstudios.apps.hammer.common.data.id.IdRepository
import com.darkrockstudios.apps.hammer.common.data.projectmetadata.ProjectMetadataDatasource
Expand Down Expand Up @@ -37,6 +38,7 @@ import org.junit.jupiter.api.Test
import utils.BaseTest
import kotlin.test.assertEquals
import kotlin.test.assertFalse
import kotlin.test.assertNotEquals
import kotlin.test.assertNotNull
import kotlin.test.assertTrue

Expand Down Expand Up @@ -168,6 +170,23 @@ class SceneEditorRepositoryBufferTest : BaseTest() {
coVerify(exactly = 0) { onBufferUpdate(any()) }
}

@Test
fun `Subscribe to Scene Updates`() = runTest(mainTestDispatcher) {
val projDef = getProject1Def()
createProject(ffs, PROJECT_1_NAME)

val repo = createRepository(projDef)
repo.initializeSceneEditor()

val onSceneUpdate: ((SceneSummary) -> Unit) = mockk()
coEvery { onSceneUpdate(any()) } just Runs

val subJob = repo.subscribeToSceneUpdates(scope, onSceneUpdate)
advanceUntilIdle()
subJob.cancelAndJoin()
coVerify(exactly = 1) { onSceneUpdate(any()) }
}

@Test
fun `Store Scene Buffer when no buffer is loaded`() = runTest(mainTestDispatcher) {
val projDef = getProject1Def()
Expand All @@ -188,6 +207,59 @@ class SceneEditorRepositoryBufferTest : BaseTest() {
assertFalse(stored)
}

@Test
fun `Store Scene Buffer raw`() = runTest(mainTestDispatcher) {
val projDef = getProject1Def()
createProject(ffs, PROJECT_1_NAME)

val repo = createRepository(projDef)
repo.initializeSceneEditor()

val sceneItem = SceneItem(
projectDef = getProject1Def(),
type = SceneItem.Type.Scene,
id = 3,
name = "Scene ID 3",
order = 0
)
val content = SceneContent(
scene = sceneItem,
markdown = "Updated scene content ID 3"
)

val stored = repo.storeSceneMarkdownRaw(content)
assertTrue(stored)

val scene3Path = repo.getSceneFilePath(3).toOkioPath()
ffs.read(scene3Path) {
val scene2Content = readUtf8()
assertEquals(content.markdown, scene2Content)
}
}

@Test
fun `Get scene path from filesystem`() = runTest(mainTestDispatcher) {
val projDef = getProject1Def()
createProject(ffs, PROJECT_1_NAME)

val sceneItem = SceneItem(
projectDef = getProject1Def(),
type = SceneItem.Type.Scene,
id = 3,
name = "Scene ID 3",
order = 0
)

val repo = createRepository(projDef)
val scene3Path = repo.getPathFromFilesystem(sceneItem)?.toOkioPath()
assertNotNull(scene3Path)

val pathSegments = scene3Path.segments.reversed()
assertEquals("0-Scene ID 3-3.md", pathSegments[0])
assertEquals("1-Chapter ID 2-2", pathSegments[1])
assertEquals("scenes", pathSegments[2])
}

@Test
fun `Load Scene Buffer, then store it`() = runTest(mainTestDispatcher) {
val projDef = getProject1Def()
Expand Down Expand Up @@ -416,4 +488,39 @@ class SceneEditorRepositoryBufferTest : BaseTest() {
order = order
)
}

@Test
fun `Rationalize Tree`() = runTest(mainTestDispatcher) {
val projDef = getProject1Def()
createProject(ffs, PROJECT_1_NAME)

val repo = createRepository(projDef)
repo.initializeSceneEditor()

val oldPath1 = repo.getSceneFilePath(1).toOkioPath()
val oldPath6 = repo.getSceneFilePath(6).toOkioPath()

assertTrue(ffs.exists(oldPath1))
assertTrue(ffs.exists(oldPath6))

val node1 = repo.rawTree.find { it.id == 1 }
val node6 = repo.rawTree.find { it.id == 6 }

node1.value = node1.value.copy(order = 2)
node6.value = node6.value.copy(order = 0)

repo.rationalizeTree()

assertFalse(ffs.exists(oldPath1))
assertFalse(ffs.exists(oldPath6))

val newPath1 = repo.getSceneFilePath(1).toOkioPath()
val newPath6 = repo.getSceneFilePath(6).toOkioPath()

assertTrue(ffs.exists(newPath1))
assertTrue(ffs.exists(newPath6))

assertNotEquals(oldPath1, newPath1)
assertNotEquals(oldPath6, newPath6)
}
}

0 comments on commit d209513

Please sign in to comment.