feat(scene): add scene editor, executor and fakes (phase 1)
Introduce the scene module skeleton that satisfies phase 1 exit conditions for tzh's responsibility in project_plan.md section 6.5: scene list, editor and Fake-driven execution that can be tested independently. Domain layer (domain/): - Scene, SceneAction (SocketPower / InfraredKey), SceneIcon, SceneColor - SceneExecutionResult with per-action outcome aggregation - SceneRepository port (observe/get/create/update/delete, provider-isolated) - SceneExecutor port (sequential execution, per-action failure continues) Fakes (testing/fakes/): - FakeSceneRepository: in-memory, provider-isolated, seeds local-experience catalog - FakeSceneExecutor: drives existing SocketGateway/InfraredGateway fakes, rejects concurrent execution of the same scene Android app (android/app/): - SceneListScreen / SceneListViewModel: list, empty state, delete confirm - SceneEditorScreen / SceneEditorViewModel: name/icon/color, action add/remove/reorder - SceneExecutionScreen / SceneExecutionViewModel: run, progress, per-action summary - SceneRoutes + NavGraphBuilder extensions wired into ABoxManagerApp - SceneModule (debug) binds FakeSceneRepository, FakeSceneExecutor and a LocalProfileId qualifier that feat/provider-login will retire later Build (settings.gradle.kts): - Apply foojay-resolver-convention so the JVM 17 toolchain is auto-provisioned Resources: zh-rCN / en strings for all scene surfaces Tests: 20 unit tests covering repository isolation, CRUD, executor ordering, failure continuation and concurrent-execution rejection
This commit is contained in:
+245
@@ -0,0 +1,245 @@
|
||||
package com.flagship.abox.manager.testing.fakes
|
||||
|
||||
import com.flagship.abox.manager.domain.DeviceName
|
||||
import com.flagship.abox.manager.domain.DomainError
|
||||
import com.flagship.abox.manager.domain.DomainResult
|
||||
import com.flagship.abox.manager.domain.InfraredGateway
|
||||
import com.flagship.abox.manager.domain.ProviderProfileId
|
||||
import com.flagship.abox.manager.domain.Scene
|
||||
import com.flagship.abox.manager.domain.SceneAction
|
||||
import com.flagship.abox.manager.domain.SceneActionExecutionResult
|
||||
import com.flagship.abox.manager.domain.SceneActionId
|
||||
import com.flagship.abox.manager.domain.SceneActionOutcome
|
||||
import com.flagship.abox.manager.domain.SceneColor
|
||||
import com.flagship.abox.manager.domain.SceneExecutionResult
|
||||
import com.flagship.abox.manager.domain.SceneExecutor
|
||||
import com.flagship.abox.manager.domain.SceneIcon
|
||||
import com.flagship.abox.manager.domain.SceneId
|
||||
import com.flagship.abox.manager.domain.SceneRepository
|
||||
import com.flagship.abox.manager.domain.SocketGateway
|
||||
import com.flagship.abox.manager.domain.SocketPowerState
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import java.util.concurrent.ConcurrentHashMap
|
||||
|
||||
/**
|
||||
* In-memory [SceneRepository] used by the scene UI and ViewModel tests. Scenes are isolated by
|
||||
* [ProviderProfileId]; mutating scenes for one profile never touches another profile's flow.
|
||||
*
|
||||
* The repository is intentionally deterministic: [defaultScenes] seeds the same catalog every
|
||||
* run so UI snapshots and tests stay stable. Mutations emit a new list through the profile's
|
||||
* StateFlow.
|
||||
*/
|
||||
class FakeSceneRepository : SceneRepository {
|
||||
private val flows = ConcurrentHashMap<
|
||||
ProviderProfileId,
|
||||
MutableStateFlow<DomainResult<List<Scene>>>,
|
||||
>()
|
||||
|
||||
override fun observeScenes(
|
||||
profileId: ProviderProfileId,
|
||||
): Flow<DomainResult<List<Scene>>> = flowFor(profileId)
|
||||
|
||||
override suspend fun getScene(
|
||||
profileId: ProviderProfileId,
|
||||
sceneId: SceneId,
|
||||
): DomainResult<Scene?> {
|
||||
val scenes = currentScenes(profileId) ?: return notFound(sceneId)
|
||||
return DomainResult.Success(scenes.firstOrNull { it.id == sceneId })
|
||||
}
|
||||
|
||||
override suspend fun createScene(
|
||||
profileId: ProviderProfileId,
|
||||
scene: Scene,
|
||||
): DomainResult<Scene> {
|
||||
require(scene.profileId == profileId) {
|
||||
"Scene profileId must match the supplied profileId"
|
||||
}
|
||||
val scenes = currentScenes(profileId).orEmpty()
|
||||
if (scenes.any { it.id == scene.id }) {
|
||||
return DomainResult.Failure(
|
||||
DomainError.InvalidRequest(
|
||||
detail = "Scene id ${scene.id.value} already exists in profile ${profileId.value}",
|
||||
),
|
||||
)
|
||||
}
|
||||
val updated = scenes + scene
|
||||
publish(profileId, updated)
|
||||
return DomainResult.Success(scene)
|
||||
}
|
||||
|
||||
override suspend fun updateScene(
|
||||
profileId: ProviderProfileId,
|
||||
scene: Scene,
|
||||
): DomainResult<Scene> {
|
||||
require(scene.profileId == profileId) {
|
||||
"Scene profileId must match the supplied profileId"
|
||||
}
|
||||
val scenes = currentScenes(profileId).orEmpty()
|
||||
val index = scenes.indexOfFirst { it.id == scene.id }
|
||||
if (index < 0) return notFound(scene.id)
|
||||
val updated = scenes.toMutableList().apply { set(index, scene) }
|
||||
publish(profileId, updated)
|
||||
return DomainResult.Success(scene)
|
||||
}
|
||||
|
||||
override suspend fun deleteScene(
|
||||
profileId: ProviderProfileId,
|
||||
sceneId: SceneId,
|
||||
): DomainResult<Unit> {
|
||||
val scenes = currentScenes(profileId).orEmpty()
|
||||
val updated = scenes.filterNot { it.id == sceneId }
|
||||
if (updated.size == scenes.size) return notFound(sceneId)
|
||||
publish(profileId, updated)
|
||||
return DomainResult.Success(Unit)
|
||||
}
|
||||
|
||||
/**
|
||||
* Replaces the catalog for [profileId] in tests. Returns the same repository so callers can
|
||||
* chain `repo.setScenes(profile, listOf(...))`.
|
||||
*/
|
||||
fun setScenes(profileId: ProviderProfileId, scenes: List<Scene>): FakeSceneRepository {
|
||||
require(scenes.all { it.profileId == profileId }) {
|
||||
"All scenes must belong to the supplied provider profile"
|
||||
}
|
||||
publish(profileId, scenes)
|
||||
return this
|
||||
}
|
||||
|
||||
fun setError(profileId: ProviderProfileId, error: DomainError) {
|
||||
flowFor(profileId).value = DomainResult.Failure(error)
|
||||
}
|
||||
|
||||
private fun flowFor(
|
||||
profileId: ProviderProfileId,
|
||||
): MutableStateFlow<DomainResult<List<Scene>>> =
|
||||
flows.getOrPut(profileId) {
|
||||
val initial = if (profileId == localProfile) defaultScenes(profileId) else emptyList()
|
||||
MutableStateFlow(DomainResult.Success(initial))
|
||||
}
|
||||
|
||||
private fun currentScenes(profileId: ProviderProfileId): List<Scene>? =
|
||||
(flowFor(profileId).value as? DomainResult.Success)?.value
|
||||
|
||||
private fun publish(profileId: ProviderProfileId, scenes: List<Scene>) {
|
||||
flowFor(profileId).value = DomainResult.Success(scenes)
|
||||
}
|
||||
|
||||
private fun notFound(sceneId: SceneId): DomainResult<Nothing> = DomainResult.Failure(
|
||||
DomainError.InvalidRequest(
|
||||
detail = "Scene ${sceneId.value} not found",
|
||||
),
|
||||
)
|
||||
|
||||
private fun defaultScenes(profileId: ProviderProfileId): List<Scene> = listOf(
|
||||
Scene(
|
||||
id = SceneId("${profileId.value}:scene-leaving-home"),
|
||||
profileId = profileId,
|
||||
name = "Leaving home",
|
||||
icon = SceneIcon.AWAY,
|
||||
color = SceneColor("#FF8A65"),
|
||||
actions = listOf(
|
||||
SceneAction.SocketPower(
|
||||
id = SceneActionId("${profileId.value}:scene-leaving-home:socket-1"),
|
||||
profileId = profileId,
|
||||
deviceName = DeviceName("socket-1"),
|
||||
powerState = SocketPowerState.OFF,
|
||||
),
|
||||
SceneAction.InfraredKey(
|
||||
id = SceneActionId("${profileId.value}:scene-leaving-home:ir-1"),
|
||||
profileId = profileId,
|
||||
deviceName = DeviceName("ir-1"),
|
||||
key = "power",
|
||||
),
|
||||
),
|
||||
),
|
||||
Scene(
|
||||
id = SceneId("${profileId.value}:scene-movie-night"),
|
||||
profileId = profileId,
|
||||
name = "Movie night",
|
||||
icon = SceneIcon.MOVIE,
|
||||
color = SceneColor("#7986CB"),
|
||||
actions = listOf(
|
||||
SceneAction.SocketPower(
|
||||
id = SceneActionId("${profileId.value}:scene-movie-night:socket-1"),
|
||||
profileId = profileId,
|
||||
deviceName = DeviceName("socket-1"),
|
||||
powerState = SocketPowerState.OFF,
|
||||
),
|
||||
),
|
||||
),
|
||||
)
|
||||
|
||||
companion object {
|
||||
/**
|
||||
* The canonical profile id used by the in-app local experience provider. UI code that
|
||||
* hard-codes a profile in phase 1 should use this constant so the Fake shows data.
|
||||
*/
|
||||
val localProfile: ProviderProfileId = ProviderProfileId("local-experience")
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Fake [SceneExecutor] that drives the existing [SocketGateway] and [InfraredGateway] fakes.
|
||||
*
|
||||
* Behaviour matches the project_plan.md §4.7 contract:
|
||||
* - actions run sequentially in [Scene.actions] order;
|
||||
* - a single action failure does not abort subsequent actions;
|
||||
* - the returned [SceneExecutionResult] contains one entry per action;
|
||||
* - re-entering execute() while a scene is running returns [DomainError.OperationFailed].
|
||||
*/
|
||||
class FakeSceneExecutor(
|
||||
private val socketGateway: SocketGateway,
|
||||
private val infraredGateway: InfraredGateway,
|
||||
) : SceneExecutor {
|
||||
private val running = ConcurrentHashMap<SceneId, Boolean>()
|
||||
|
||||
override suspend fun execute(scene: Scene): DomainResult<SceneExecutionResult> {
|
||||
val alreadyRunning = running.putIfAbsent(scene.id, true) != null
|
||||
if (alreadyRunning) {
|
||||
return DomainResult.Failure(
|
||||
DomainError.OperationFailed(
|
||||
detail = "Scene ${scene.id.value} is already running",
|
||||
),
|
||||
)
|
||||
}
|
||||
return try {
|
||||
val results = scene.actions.map { action -> executeAction(scene.profileId, action) }
|
||||
DomainResult.Success(SceneExecutionResult(scene = scene, results = results))
|
||||
} finally {
|
||||
running.remove(scene.id)
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun executeAction(
|
||||
profileId: ProviderProfileId,
|
||||
action: SceneAction,
|
||||
): SceneActionExecutionResult {
|
||||
val result = when (action) {
|
||||
is SceneAction.SocketPower -> socketGateway.setPower(
|
||||
profileId = profileId,
|
||||
deviceName = action.deviceName,
|
||||
powerState = action.powerState,
|
||||
)
|
||||
|
||||
is SceneAction.InfraredKey -> infraredGateway.sendKey(
|
||||
profileId = profileId,
|
||||
deviceName = action.deviceName,
|
||||
key = action.key,
|
||||
)
|
||||
}
|
||||
return when (result) {
|
||||
is DomainResult.Success -> SceneActionExecutionResult(
|
||||
action = action,
|
||||
outcome = SceneActionOutcome.SUCCESS,
|
||||
error = null,
|
||||
)
|
||||
|
||||
is DomainResult.Failure -> SceneActionExecutionResult(
|
||||
action = action,
|
||||
outcome = SceneActionOutcome.FAILURE,
|
||||
error = result.error,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
+167
@@ -0,0 +1,167 @@
|
||||
package com.flagship.abox.manager.testing.fakes
|
||||
|
||||
import com.flagship.abox.manager.domain.DeviceName
|
||||
import com.flagship.abox.manager.domain.DomainError
|
||||
import com.flagship.abox.manager.domain.DomainResult
|
||||
import com.flagship.abox.manager.domain.ProviderProfileId
|
||||
import com.flagship.abox.manager.domain.Scene
|
||||
import com.flagship.abox.manager.domain.SceneAction
|
||||
import com.flagship.abox.manager.domain.SceneActionId
|
||||
import com.flagship.abox.manager.domain.SceneActionOutcome
|
||||
import com.flagship.abox.manager.domain.SceneColor
|
||||
import com.flagship.abox.manager.domain.SceneIcon
|
||||
import com.flagship.abox.manager.domain.SceneId
|
||||
import com.flagship.abox.manager.domain.SocketGateway
|
||||
import com.flagship.abox.manager.domain.SocketPowerState
|
||||
import com.flagship.abox.manager.domain.SocketStatus
|
||||
import kotlinx.coroutines.CompletableDeferred
|
||||
import kotlinx.coroutines.async
|
||||
import kotlinx.coroutines.test.runTest
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertFalse
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Test
|
||||
|
||||
class FakeSceneExecutorTest {
|
||||
private val profileId = ProviderProfileId("abox")
|
||||
|
||||
@Test
|
||||
fun executesActionsInOrder() = runTest {
|
||||
val socketGateway = FakeSocketGateway()
|
||||
val infraredGateway = FakeInfraredGateway()
|
||||
val executor = FakeSceneExecutor(socketGateway, infraredGateway)
|
||||
val scene = scene(
|
||||
actions = listOf(
|
||||
socketAction("socket-1", SocketPowerState.ON),
|
||||
infraredAction("ir-1", "power"),
|
||||
socketAction("socket-2", SocketPowerState.OFF),
|
||||
),
|
||||
)
|
||||
|
||||
val result = executor.execute(scene)
|
||||
|
||||
assertTrue(result is DomainResult.Success)
|
||||
val executionResult = (result as DomainResult.Success).value
|
||||
assertEquals(3, executionResult.results.size)
|
||||
executionResult.results.forEach {
|
||||
assertEquals(SceneActionOutcome.SUCCESS, it.outcome)
|
||||
}
|
||||
assertEquals(1, infraredGateway.sentKeys.size)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun continuesAfterActionFailure() = runTest {
|
||||
val socketGateway = FakeSocketGateway()
|
||||
socketGateway.setError(
|
||||
profileId,
|
||||
DeviceName("socket-1"),
|
||||
DomainError.DeviceOffline(detail = "offline"),
|
||||
)
|
||||
val infraredGateway = FakeInfraredGateway()
|
||||
val executor = FakeSceneExecutor(socketGateway, infraredGateway)
|
||||
val scene = scene(
|
||||
actions = listOf(
|
||||
socketAction("socket-1", SocketPowerState.ON),
|
||||
infraredAction("ir-1", "power"),
|
||||
),
|
||||
)
|
||||
|
||||
val result = executor.execute(scene)
|
||||
|
||||
assertTrue(result is DomainResult.Success)
|
||||
val executionResult = (result as DomainResult.Success).value
|
||||
assertEquals(2, executionResult.results.size)
|
||||
assertEquals(SceneActionOutcome.FAILURE, executionResult.results[0].outcome)
|
||||
assertEquals(SceneActionOutcome.SUCCESS, executionResult.results[1].outcome)
|
||||
assertEquals(1, executionResult.succeededCount)
|
||||
assertEquals(1, executionResult.failedCount)
|
||||
assertFalse(executionResult.allSucceeded)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun runningSceneRejectsConcurrentExecution() = runTest {
|
||||
val gate = CompletableDeferred<Unit>()
|
||||
val socketGateway = BlockingSocketGateway(gate)
|
||||
val executor = FakeSceneExecutor(socketGateway, FakeInfraredGateway())
|
||||
val scene = scene(
|
||||
actions = listOf(socketAction("socket-1", SocketPowerState.ON)),
|
||||
)
|
||||
|
||||
val first = async { executor.execute(scene) }
|
||||
// Let the first coroutine enter the executor and block on the socket gateway.
|
||||
testScheduler.advanceUntilIdle()
|
||||
val second = async { executor.execute(scene) }
|
||||
val secondResult = second.await()
|
||||
|
||||
assertTrue(secondResult is DomainResult.Failure)
|
||||
assertTrue((secondResult as DomainResult.Failure).error is DomainError.OperationFailed)
|
||||
|
||||
gate.complete(Unit)
|
||||
val firstResult = first.await()
|
||||
assertTrue(firstResult is DomainResult.Success)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun emptySceneSucceedsWithNoResults() = runTest {
|
||||
val executor = FakeSceneExecutor(FakeSocketGateway(), FakeInfraredGateway())
|
||||
val scene = scene(actions = emptyList())
|
||||
|
||||
val result = executor.execute(scene)
|
||||
|
||||
assertTrue(result is DomainResult.Success)
|
||||
val executionResult = (result as DomainResult.Success).value
|
||||
assertEquals(0, executionResult.results.size)
|
||||
assertEquals(0, executionResult.succeededCount)
|
||||
assertEquals(0, executionResult.failedCount)
|
||||
assertFalse(executionResult.allSucceeded)
|
||||
}
|
||||
|
||||
private fun scene(actions: List<SceneAction>): Scene = Scene(
|
||||
id = SceneId("${profileId.value}:scene-test"),
|
||||
profileId = profileId,
|
||||
name = "Test scene",
|
||||
icon = SceneIcon.HOME,
|
||||
color = SceneColor("#FF8A65"),
|
||||
actions = actions,
|
||||
)
|
||||
|
||||
private fun socketAction(
|
||||
deviceName: String,
|
||||
powerState: SocketPowerState,
|
||||
): SceneAction.SocketPower = SceneAction.SocketPower(
|
||||
id = SceneActionId("${profileId.value}:action-${deviceName}-${powerState.name}"),
|
||||
profileId = profileId,
|
||||
deviceName = DeviceName(deviceName),
|
||||
powerState = powerState,
|
||||
)
|
||||
|
||||
private fun infraredAction(
|
||||
deviceName: String,
|
||||
key: String,
|
||||
): SceneAction.InfraredKey = SceneAction.InfraredKey(
|
||||
id = SceneActionId("${profileId.value}:action-${deviceName}-$key"),
|
||||
profileId = profileId,
|
||||
deviceName = DeviceName(deviceName),
|
||||
key = key,
|
||||
)
|
||||
|
||||
private class BlockingSocketGateway(
|
||||
private val gate: CompletableDeferred<Unit>,
|
||||
) : SocketGateway {
|
||||
override suspend fun getStatus(
|
||||
profileId: ProviderProfileId,
|
||||
deviceName: DeviceName,
|
||||
): DomainResult<SocketStatus> = DomainResult.Success(
|
||||
SocketStatus(profileId, deviceName, SocketPowerState.OFF),
|
||||
)
|
||||
|
||||
override suspend fun setPower(
|
||||
profileId: ProviderProfileId,
|
||||
deviceName: DeviceName,
|
||||
powerState: SocketPowerState,
|
||||
): DomainResult<SocketStatus> {
|
||||
gate.await()
|
||||
return DomainResult.Success(SocketStatus(profileId, deviceName, powerState))
|
||||
}
|
||||
}
|
||||
}
|
||||
+149
@@ -0,0 +1,149 @@
|
||||
package com.flagship.abox.manager.testing.fakes
|
||||
|
||||
import com.flagship.abox.manager.domain.DeviceName
|
||||
import com.flagship.abox.manager.domain.DomainError
|
||||
import com.flagship.abox.manager.domain.DomainResult
|
||||
import com.flagship.abox.manager.domain.ProviderProfileId
|
||||
import com.flagship.abox.manager.domain.Scene
|
||||
import com.flagship.abox.manager.domain.SceneAction
|
||||
import com.flagship.abox.manager.domain.SceneActionId
|
||||
import com.flagship.abox.manager.domain.SceneColor
|
||||
import com.flagship.abox.manager.domain.SceneIcon
|
||||
import com.flagship.abox.manager.domain.SceneId
|
||||
import com.flagship.abox.manager.domain.SocketPowerState
|
||||
import kotlinx.coroutines.flow.first
|
||||
import kotlinx.coroutines.test.runTest
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertNotNull
|
||||
import org.junit.Assert.assertNull
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Test
|
||||
|
||||
class FakeSceneRepositoryTest {
|
||||
private val aboxProfile = ProviderProfileId("abox")
|
||||
private val localProfile = FakeSceneRepository.localProfile
|
||||
|
||||
@Test
|
||||
fun defaultScenesAreIsolatedByProviderProfile() = runTest {
|
||||
val repo = FakeSceneRepository()
|
||||
|
||||
val aboxScenes = scenesFor(repo, aboxProfile)
|
||||
val localScenes = scenesFor(repo, localProfile)
|
||||
|
||||
assertEquals(0, aboxScenes.size)
|
||||
assertEquals(2, localScenes.size)
|
||||
assertTrue(localScenes.all { it.profileId == localProfile })
|
||||
}
|
||||
|
||||
@Test
|
||||
fun createScenePublishesSceneToFlow() = runTest {
|
||||
val repo = FakeSceneRepository()
|
||||
val scene = sampleScene(localProfile, "scene-1")
|
||||
|
||||
val result = repo.createScene(localProfile, scene)
|
||||
|
||||
assertTrue(result is DomainResult.Success)
|
||||
assertEquals(scene, (result as DomainResult.Success).value)
|
||||
val list = scenesFor(repo, localProfile)
|
||||
assertEquals(3, list.size)
|
||||
assertEquals(scene, list.last())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun createSceneWithDuplicateIdFails() = runTest {
|
||||
val repo = FakeSceneRepository()
|
||||
val scene = sampleScene(localProfile, "dup")
|
||||
|
||||
repo.createScene(localProfile, scene)
|
||||
val second = repo.createScene(localProfile, scene)
|
||||
|
||||
assertTrue(second is DomainResult.Failure)
|
||||
assertTrue((second as DomainResult.Failure).error is DomainError.InvalidRequest)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun updateSceneReplacesExistingEntry() = runTest {
|
||||
val repo = FakeSceneRepository()
|
||||
val original = sampleScene(localProfile, "scene-2", "Original")
|
||||
repo.createScene(localProfile, original)
|
||||
|
||||
val updated = original.copy(name = "Renamed")
|
||||
val result = repo.updateScene(localProfile, updated)
|
||||
|
||||
assertTrue(result is DomainResult.Success)
|
||||
val list = scenesFor(repo, localProfile)
|
||||
assertEquals(3, list.size)
|
||||
val stored = list.first { it.id == original.id }
|
||||
assertEquals("Renamed", stored.name)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun deleteSceneRemovesEntry() = runTest {
|
||||
val repo = FakeSceneRepository()
|
||||
val scene = sampleScene(localProfile, "scene-3")
|
||||
repo.createScene(localProfile, scene)
|
||||
|
||||
val result = repo.deleteScene(localProfile, scene.id)
|
||||
|
||||
assertTrue(result is DomainResult.Success)
|
||||
val list = scenesFor(repo, localProfile)
|
||||
assertEquals(2, list.size)
|
||||
assertNull(list.firstOrNull { it.id == scene.id })
|
||||
}
|
||||
|
||||
@Test
|
||||
fun deleteUnknownSceneFails() = runTest {
|
||||
val repo = FakeSceneRepository()
|
||||
|
||||
val result = repo.deleteScene(localProfile, SceneId("missing"))
|
||||
|
||||
assertTrue(result is DomainResult.Failure)
|
||||
assertTrue((result as DomainResult.Failure).error is DomainError.InvalidRequest)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun getSceneReturnsNullForUnknownId() = runTest {
|
||||
val repo = FakeSceneRepository()
|
||||
|
||||
val result = repo.getScene(localProfile, SceneId("missing"))
|
||||
|
||||
assertTrue(result is DomainResult.Success)
|
||||
assertNull((result as DomainResult.Success).value)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun setErrorStopsPublishingSuccess() = runTest {
|
||||
val repo = FakeSceneRepository()
|
||||
repo.setError(localProfile, DomainError.Unknown(detail = "boom"))
|
||||
|
||||
val value = repo.observeScenes(localProfile).first()
|
||||
|
||||
assertTrue(value is DomainResult.Failure)
|
||||
assertNotNull((value as DomainResult.Failure).error)
|
||||
}
|
||||
|
||||
private suspend fun scenesFor(
|
||||
repo: FakeSceneRepository,
|
||||
profileId: ProviderProfileId,
|
||||
): List<Scene> = (repo.observeScenes(profileId).first() as DomainResult.Success).value
|
||||
|
||||
private fun sampleScene(
|
||||
profileId: ProviderProfileId,
|
||||
idSuffix: String,
|
||||
name: String = "Test scene",
|
||||
): Scene = Scene(
|
||||
id = SceneId("${profileId.value}:scene-$idSuffix"),
|
||||
profileId = profileId,
|
||||
name = name,
|
||||
icon = SceneIcon.HOME,
|
||||
color = SceneColor("#FF8A65"),
|
||||
actions = listOf(
|
||||
SceneAction.SocketPower(
|
||||
id = SceneActionId("${profileId.value}:scene-$idSuffix:1"),
|
||||
profileId = profileId,
|
||||
deviceName = DeviceName("socket-1"),
|
||||
powerState = SocketPowerState.ON,
|
||||
),
|
||||
),
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user