You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
96 lines
3.4 KiB
96 lines
3.4 KiB
package net.aiterp.git.ykonsole2.infrastructure.drivers
|
|
|
|
import net.aiterp.git.ykonsole2.domain.models.*
|
|
import net.aiterp.git.ykonsole2.domain.runtime.*
|
|
import io.mockk.coEvery
|
|
import io.mockk.every
|
|
import io.mockk.mockk
|
|
import kotlinx.coroutines.cancelAndJoin
|
|
import kotlinx.coroutines.flow.*
|
|
import kotlinx.coroutines.launch
|
|
import kotlinx.coroutines.test.runTest
|
|
import org.junit.jupiter.api.Assertions.*
|
|
import org.junit.jupiter.api.Test
|
|
|
|
internal class ProgramEnforcerTest {
|
|
private val programRepo = mockk<ProgramRepository>()
|
|
private val workoutRepo = mockk<WorkoutRepository>()
|
|
private val enforcer = ProgramEnforcer(programRepo, workoutRepo)
|
|
private val input = CommandBus()
|
|
private val output = mockk<EventBus>()
|
|
|
|
@Test
|
|
fun `program end warmup step`() = validProgramTest(listOf(
|
|
ValuesReceived(listOf(Time(0), Calories(0), Distance(0))),
|
|
ValuesReceived(listOf(Time(599), Calories(234), Distance(3715))),
|
|
ValuesReceived(listOf(Time(600), Calories(234), Distance(3716))),
|
|
ValuesReceived(listOf(Time(601), Calories(235), Distance(3718))),
|
|
), listOf(
|
|
SetValueCommand(Level(15)),
|
|
SetValueCommand(Level(18)),
|
|
))
|
|
|
|
@Test
|
|
fun `program end warmup and main step`() = validProgramTest(listOf(
|
|
ValuesReceived(listOf(Time(0), Calories(0), Distance(0))),
|
|
ValuesReceived(listOf(Time(600), Calories(234), Distance(3716))),
|
|
ValuesReceived(listOf(Time(601), Calories(235), Distance(3718))),
|
|
ValuesReceived(listOf(Time(1753), Calories(934), Distance(9199))),
|
|
ValuesReceived(listOf(Time(1754), Calories(935), Distance(9201))),
|
|
ValuesReceived(listOf(Time(1755), Calories(935), Distance(9202))),
|
|
), listOf(
|
|
SetValueCommand(Level(15)),
|
|
SetValueCommand(Level(18)),
|
|
SetValueCommand(Level(14)),
|
|
))
|
|
|
|
@Test
|
|
fun `program goes through`() = validProgramTest(listOf(
|
|
ValuesReceived(listOf(Time(0), Calories(0), Distance(0))),
|
|
ValuesReceived(listOf(Time(600), Calories(234), Distance(3716))),
|
|
ValuesReceived(listOf(Time(601), Calories(235), Distance(3718))),
|
|
ValuesReceived(listOf(Time(1754), Calories(935), Distance(9201))),
|
|
ValuesReceived(listOf(Time(1822), Calories(844), Distance(10200))),
|
|
ValuesReceived(listOf(Time(1822), Calories(844), Distance(10201))),
|
|
), listOf(
|
|
SetValueCommand(Level(15)),
|
|
SetValueCommand(Level(18)),
|
|
SetValueCommand(Level(14)),
|
|
StopCommand,
|
|
))
|
|
|
|
private fun validProgramTest(events: List<Event>, expectedCommands: List<Command>) = runTest {
|
|
val program = Program("test123", "Test program", listOf(
|
|
ProgramStep(listOf(Level(15)), Time(600)),
|
|
ProgramStep(listOf(Level(18)), Calories(700)),
|
|
ProgramStep(listOf(Level(14)), Distance(1000)),
|
|
))
|
|
val workout = Workout(id = "1234", programId = program.id)
|
|
|
|
every { workoutRepo.findActive() } returns workout
|
|
every { programRepo.findById(program.id) } returns program
|
|
|
|
coEvery { output.collect(any(), any()) } coAnswers {
|
|
val func = arg<suspend (value: Event) -> Unit>(1)
|
|
|
|
func.invoke(Connected(initial = true))
|
|
func.invoke(Started)
|
|
events.forEach { func.invoke(it) }
|
|
}
|
|
|
|
val listener = launch {
|
|
val actualCommands = input.asSharedFlow()
|
|
.take(expectedCommands.size)
|
|
.toList()
|
|
|
|
assertEquals(expectedCommands, actualCommands)
|
|
}
|
|
|
|
val starter = launch {
|
|
enforcer.start(input, output)
|
|
}
|
|
|
|
listener.join()
|
|
starter.cancelAndJoin()
|
|
}
|
|
}
|