|
| 1 | +package com.squareup.workflow1.testing |
| 2 | + |
| 3 | +import app.cash.turbine.ReceiveTurbine |
| 4 | +import app.cash.turbine.test |
| 5 | +import com.squareup.workflow1.RuntimeConfig |
| 6 | +import com.squareup.workflow1.RuntimeConfigOptions |
| 7 | +import com.squareup.workflow1.Workflow |
| 8 | +import com.squareup.workflow1.WorkflowInterceptor |
| 9 | +import com.squareup.workflow1.renderWorkflowIn |
| 10 | +import com.squareup.workflow1.testing.WorkflowTurbine.Companion.WORKFLOW_TEST_DEFAULT_TIMEOUT_MS |
| 11 | +import kotlinx.coroutines.CoroutineScope |
| 12 | +import kotlinx.coroutines.Dispatchers |
| 13 | +import kotlinx.coroutines.ExperimentalCoroutinesApi |
| 14 | +import kotlinx.coroutines.cancel |
| 15 | +import kotlinx.coroutines.flow.MutableStateFlow |
| 16 | +import kotlinx.coroutines.flow.StateFlow |
| 17 | +import kotlinx.coroutines.flow.asStateFlow |
| 18 | +import kotlinx.coroutines.flow.drop |
| 19 | +import kotlinx.coroutines.flow.map |
| 20 | +import kotlinx.coroutines.test.UnconfinedTestDispatcher |
| 21 | +import kotlinx.coroutines.test.runTest |
| 22 | +import kotlin.coroutines.CoroutineContext |
| 23 | +import kotlin.time.Duration.Companion.milliseconds |
| 24 | + |
| 25 | +/** |
| 26 | + * This is a test harness to run integration tests for a Workflow tree. The parameters passed here are |
| 27 | + * the same as those to start a Workflow runtime with [renderWorkflowIn] except for ignoring |
| 28 | + * state persistence as that is not needed for this style of test. |
| 29 | + * |
| 30 | + * The [coroutineContext] rather than a [CoroutineScope] is passed so that this harness handles the |
| 31 | + * scope for the Workflow runtime for you but you can still specify context for it. It defaults to |
| 32 | + * [Dispatchers.Main.immediate]. If you wish to use a dispatcher that skips delays, use a |
| 33 | + * [StandardTestDispatcher][kotlinx.coroutines.test.StandardTestDispatcher], so that the dispatcher |
| 34 | + * will still guarantee ordering. |
| 35 | + * |
| 36 | + * A [testTimeout] may be specified to override the default [WORKFLOW_TEST_DEFAULT_TIMEOUT_MS] for |
| 37 | + * any particular test. This is the max amount of time the test could spend waiting on a rendering. |
| 38 | + * |
| 39 | + * This will start the Workflow runtime (with params as passed) rooted at whatever Workflow |
| 40 | + * it is called on and then create a [WorkflowTurbine] for its renderings and run [testCase] on that. |
| 41 | + * [testCase] can thus drive the test scenario and assert against renderings. |
| 42 | + */ |
| 43 | +public fun <PropsT, OutputT, RenderingT> Workflow<PropsT, OutputT, RenderingT>.headlessIntegrationTest( |
| 44 | + props: StateFlow<PropsT>, |
| 45 | + coroutineContext: CoroutineContext = Dispatchers.Main.immediate, |
| 46 | + interceptors: List<WorkflowInterceptor> = emptyList(), |
| 47 | + runtimeConfig: RuntimeConfig = RuntimeConfigOptions.DEFAULT_CONFIG, |
| 48 | + onOutput: suspend (OutputT) -> Unit = {}, |
| 49 | + testTimeout: Long = WORKFLOW_TEST_DEFAULT_TIMEOUT_MS, |
| 50 | + testCase: suspend WorkflowTurbine<RenderingT>.() -> Unit |
| 51 | +) { |
| 52 | + val workflow = this |
| 53 | + |
| 54 | + runTest( |
| 55 | + context = coroutineContext, |
| 56 | + timeout = testTimeout.milliseconds |
| 57 | + ) { |
| 58 | + // We use a sub-scope so that we can cancel the Workflow runtime when we are done with it so that |
| 59 | + // tests don't all have to do that themselves. |
| 60 | + val workflowRuntimeScope = CoroutineScope(coroutineContext) |
| 61 | + val renderings = renderWorkflowIn( |
| 62 | + workflow = workflow, |
| 63 | + props = props, |
| 64 | + scope = workflowRuntimeScope, |
| 65 | + interceptors = interceptors, |
| 66 | + runtimeConfig = runtimeConfig, |
| 67 | + onOutput = onOutput |
| 68 | + ) |
| 69 | + |
| 70 | + val firstRendering = renderings.value.rendering |
| 71 | + |
| 72 | + // Drop one as its provided separately via `firstRendering`. |
| 73 | + renderings.drop(1).map { |
| 74 | + it.rendering |
| 75 | + }.test { |
| 76 | + val workflowTurbine = WorkflowTurbine( |
| 77 | + firstRendering, |
| 78 | + this |
| 79 | + ) |
| 80 | + workflowTurbine.testCase() |
| 81 | + cancelAndIgnoreRemainingEvents() |
| 82 | + } |
| 83 | + workflowRuntimeScope.cancel() |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +/** |
| 88 | + * Version of [headlessIntegrationTest] that does not require props. For Workflows that have [Unit] |
| 89 | + * props type. |
| 90 | + */ |
| 91 | +@OptIn(ExperimentalCoroutinesApi::class) |
| 92 | +public fun <OutputT, RenderingT> Workflow<Unit, OutputT, RenderingT>.headlessIntegrationTest( |
| 93 | + coroutineContext: CoroutineContext = UnconfinedTestDispatcher(), |
| 94 | + interceptors: List<WorkflowInterceptor> = emptyList(), |
| 95 | + runtimeConfig: RuntimeConfig = RuntimeConfigOptions.DEFAULT_CONFIG, |
| 96 | + onOutput: suspend (OutputT) -> Unit = {}, |
| 97 | + testTimeout: Long = WORKFLOW_TEST_DEFAULT_TIMEOUT_MS, |
| 98 | + testCase: suspend WorkflowTurbine<RenderingT>.() -> Unit |
| 99 | +): Unit = headlessIntegrationTest( |
| 100 | + props = MutableStateFlow(Unit).asStateFlow(), |
| 101 | + coroutineContext = coroutineContext, |
| 102 | + interceptors = interceptors, |
| 103 | + runtimeConfig = runtimeConfig, |
| 104 | + onOutput = onOutput, |
| 105 | + testTimeout = testTimeout, |
| 106 | + testCase = testCase |
| 107 | +) |
| 108 | + |
| 109 | +/** |
| 110 | + * Simple wrapper around a [ReceiveTurbine] of [RenderingT] to provide convenience helper methods specific |
| 111 | + * to Workflow renderings. |
| 112 | + * |
| 113 | + * @property firstRendering The first rendering of the Workflow runtime is made synchronously. This is |
| 114 | + * provided separately if any assertions or operations are needed from it. |
| 115 | + */ |
| 116 | +public class WorkflowTurbine<RenderingT>( |
| 117 | + public val firstRendering: RenderingT, |
| 118 | + private val receiveTurbine: ReceiveTurbine<RenderingT> |
| 119 | +) { |
| 120 | + private var usedFirst = false |
| 121 | + |
| 122 | + /** |
| 123 | + * Suspend waiting for the next rendering to be produced by the Workflow runtime. Note this includes |
| 124 | + * the first (synchronously made) rendering. |
| 125 | + * |
| 126 | + * @return the rendering. |
| 127 | + */ |
| 128 | + public suspend fun awaitNextRendering(): RenderingT { |
| 129 | + if (!usedFirst) { |
| 130 | + usedFirst = true |
| 131 | + return firstRendering |
| 132 | + } |
| 133 | + return receiveTurbine.awaitItem() |
| 134 | + } |
| 135 | + |
| 136 | + public suspend fun skipRenderings(count: Int) { |
| 137 | + val skippedCount = if (!usedFirst) { |
| 138 | + usedFirst = true |
| 139 | + count - 1 |
| 140 | + } else { |
| 141 | + count |
| 142 | + } |
| 143 | + |
| 144 | + if (skippedCount > 0) { |
| 145 | + receiveTurbine.skipItems(skippedCount) |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + /** |
| 150 | + * Suspend waiting for the next rendering to be produced by the Workflow runtime that satisfies the |
| 151 | + * [predicate]. |
| 152 | + * |
| 153 | + * @return the rendering. |
| 154 | + */ |
| 155 | + public suspend fun awaitNextRenderingSatisfying( |
| 156 | + predicate: (RenderingT) -> Boolean |
| 157 | + ): RenderingT { |
| 158 | + var rendering = awaitNextRendering() |
| 159 | + while (!predicate(rendering)) { |
| 160 | + rendering = awaitNextRendering() |
| 161 | + } |
| 162 | + return rendering |
| 163 | + } |
| 164 | + |
| 165 | + /** |
| 166 | + * Suspend waiting for the next rendering which satisfies [precondition], can successfully be mapped |
| 167 | + * using [map] and satisfies the [satisfying] predicate when called on the [T] rendering after it |
| 168 | + * has been mapped. |
| 169 | + * |
| 170 | + * @return the mapped rendering as [T] |
| 171 | + */ |
| 172 | + public suspend fun <T> awaitNext( |
| 173 | + precondition: (RenderingT) -> Boolean = { true }, |
| 174 | + map: (RenderingT) -> T, |
| 175 | + satisfying: T.() -> Boolean = { true } |
| 176 | + ): T = |
| 177 | + map( |
| 178 | + awaitNextRenderingSatisfying { |
| 179 | + precondition(it) && |
| 180 | + with(map(it)) { |
| 181 | + this.satisfying() |
| 182 | + } |
| 183 | + } |
| 184 | + ) |
| 185 | + |
| 186 | + public companion object { |
| 187 | + /** |
| 188 | + * Default timeout to use while waiting for renderings. |
| 189 | + */ |
| 190 | + public const val WORKFLOW_TEST_DEFAULT_TIMEOUT_MS: Long = 60_000L |
| 191 | + } |
| 192 | +} |
0 commit comments