Yokan / rebuild
Course 1 8624561

DeclarativeRecipeRuntimeTest.kt

composeApp/src/commonTest/kotlin/eu/friedcutlet/yokan/services/extensions/recipe/DeclarativeRecipeRuntimeTest.kt

test / fixture41 lines
1package eu.friedcutlet.yokan.services.extensions.recipe23import kotlinx.coroutines.CancellationException4import kotlinx.coroutines.test.runTest5import kotlin.test.Test6import kotlin.test.assertEquals7import kotlin.test.assertFailsWith8import kotlin.test.assertIs910class DeclarativeRecipeRuntimeTest {11    private val policy = RecipeExecutionPolicy(mapOf("primary" to "https://example.test"))12    private val inputs = mapOf("pageIndex" to RecipeEntrypointInput.PageIndex(0))13    private val bundle = assertIs<RecipeBundleDecodeResult.Accepted>(StrictRecipeBundleDecoder.decode(validBundle().encodeToByteArray())).bundle1415    @Test16    fun entrypointProjectsJsonFromTheGrantedOrigin() = runTest {17        var requested: RecipeBrokerRequest? = null18        val broker = broker { requested = it; RecipeBrokerResult.Document("""{"data":[{"id":"a","title":"A"}]}""".encodeToByteArray()) }19        val completed = assertIs<RecipeEntrypointExecutionResult.Completed>(DeclarativeRecipeRuntime(broker).executeEntrypoint(bundle, "popular", inputs, policy))20        assertEquals(listOf(RecipeEntrypointResult.Item("a", "A", null)), assertIs<RecipeEntrypointResult.Listing>(completed.result).items)21        assertEquals("https://example.test/items", requested?.url)22    }2324    @Test25    fun brokerExceptionsBecomeBrokerFailureButCancellationPropagates() = runTest {26        val failing = DeclarativeRecipeRuntime(broker { throw IllegalStateException("boom") })27        assertEquals(RecipeExecutionErrorCode.BrokerFailure, assertIs<RecipeEntrypointExecutionResult.Rejected>(failing.executeEntrypoint(bundle, "popular", inputs, policy)).error.code)28        val cancelling = DeclarativeRecipeRuntime(broker { throw CancellationException("cancel") })29        assertFailsWith<CancellationException> { cancelling.executeEntrypoint(bundle, "popular", inputs, policy) }30    }3132    @Test33    fun malformedJsonResponseIsRejected() = runTest {34        val runtime = DeclarativeRecipeRuntime(broker { RecipeBrokerResult.Document("{".encodeToByteArray()) })35        assertEquals(RecipeExecutionErrorCode.InvalidJsonDocument, assertIs<RecipeEntrypointExecutionResult.Rejected>(runtime.executeEntrypoint(bundle, "popular", inputs, policy)).error.code)36    }3738    private fun broker(respond: (RecipeBrokerRequest) -> RecipeBrokerResult) = object : RecipeCapabilityBroker {39        override suspend fun fetch(request: RecipeBrokerRequest): RecipeBrokerResult = respond(request)40    }41}42

Search chapters, reference and source filenames.