Get started

Testing

Every part of the SDK that reaches the outside world can be replaced in a test: the HTTP exchange, the clock, the map's renderer and the navigation.

Test code that calls the API

Every attempt leaves through an HttpExchange. Answer it yourself:

import com.mountaya.client.Credential
import com.mountaya.client.ExchangeBody
import com.mountaya.client.ExchangeRequest
import com.mountaya.client.ExchangeResponse
import com.mountaya.client.HttpExchange
import com.mountaya.client.Mountaya
import kotlinx.coroutines.test.runTest
import kotlin.test.Test
import kotlin.test.assertTrue

class ActivitiesTest {
    @Test
    fun listsNoActivity() =
        runTest {
            val answered =
                object : HttpExchange {
                    override suspend fun send(request: ExchangeRequest): ExchangeResponse =
                        ExchangeResponse(200, body = ExchangeBody("""{"data": []}""".encodeToByteArray()))
                }
            val mountaya = Mountaya(Credential.Publishable("pk_your_publishable_key"), exchange = answered)
            assertTrue(mountaya.studio.activities.getActivities().data.isEmpty())
        }
}

Control time

Hand the client the test's dispatcher, and every wait and retry runs on virtual time:

import com.mountaya.client.Credential
import com.mountaya.client.ExchangeBody
import com.mountaya.client.ExchangeRequest
import com.mountaya.client.ExchangeResponse
import com.mountaya.client.HttpExchange
import com.mountaya.client.Mountaya
import kotlinx.coroutines.test.StandardTestDispatcher
import kotlinx.coroutines.test.runTest
import kotlin.test.Test
import kotlin.test.assertEquals

class RetryTest {
    @Test
    fun retriesAfterAnOutage() =
        runTest {
            var attempts = 0
            val flaky =
                object : HttpExchange {
                    override suspend fun send(request: ExchangeRequest): ExchangeResponse {
                        attempts += 1
                        return if (attempts == 1) ExchangeResponse(503) else ExchangeResponse(200, body = ExchangeBody("""{"data": []}""".encodeToByteArray()))
                    }
                }
            // The test's dispatcher: the backoff between the 503 and the 200 waits on virtual time.
            val mountaya =
                Mountaya(
                    Credential.Publishable("pk_your_publishable_key"),
                    exchange = flaky,
                    dispatcher = StandardTestDispatcher(testScheduler),
                )
            mountaya.studio.activities.getActivities()
            assertEquals(2, attempts)
        }
}

Script the map

ScriptedMapSurface stands in for the renderer: it records each camera move and plays clicks, hovers and failed requests:

import com.mountaya.client.Credential
import com.mountaya.client.Mountaya
import com.mountaya.elements.CameraTarget
import com.mountaya.elements.MapEngine
import com.mountaya.elements.ScriptedMapSurface
import kotlin.test.Test
import kotlin.test.assertTrue

class MapTest {
    @Test
    fun movesAreRecorded() {
        val map = MapEngine(Mountaya(Credential.Publishable("pk_your_publishable_key")))
        val surface = ScriptedMapSurface(map, canvas = listOf(390.0, 844.0))
        map.ease(CameraTarget(center = listOf(6.8712, 45.9231), zoom = 12.0))
        // A click at a point of the view and a place, told to the map's events as MapView tells it.
        surface.click(listOf(120.0, 80.0), listOf(6.8712, 45.9231))
        assertTrue(surface.moves.isNotEmpty())
    }
}

ScriptedNavigationEngine is the navigation in your tests: you answer each route request and hand each position:

import com.mountaya.geo.NavigationRoute
import com.mountaya.geo.NavigationStatus
import com.mountaya.geo.ScriptedNavigationEngine
import kotlin.test.Test
import kotlin.test.assertEquals

class NavigationTest {
    @Test
    fun arrivesAtArgentiere() {
        val line = listOf(listOf(6.8712, 45.9231), listOf(6.9271, 45.9822))
        val navigation = ScriptedNavigationEngine<String>()
        navigation.start("0b6d1a4e-6d3f-4f7a-9c2e-1f0a5b8c3d21")
        navigation.sent[0].answer(NavigationRoute(line = line, duration = 5_400.0))
        navigation.setPosition(line.last())
        assertEquals(NavigationStatus.Arrived, navigation.state.value.status)
        navigation.close()
    }
}

Build errors

Every exception has a public constructor, so a test builds the failure its code handles:

import com.mountaya.client.ErrorEntry
import com.mountaya.client.MountayaException
import com.mountaya.client.NotFoundException
import com.mountaya.client.generated.ApiErrorCode
import kotlin.test.Test
import kotlin.test.assertEquals

class ErrorScreenTest {
    // Your app's own words for a failure.
    private fun messageFor(failure: MountayaException): String =
        when (failure) {
            is NotFoundException -> "This itinerary is gone."
            else -> "Something went wrong."
        }

    @Test
    fun aMissingItineraryReadsAsGone() {
        val failure = NotFoundException(errors = listOf(ErrorEntry("Gone.", code = ApiErrorCode.NotFound)), requestId = "req_test")
        assertEquals("This itinerary is gone.", messageFor(failure))
    }
}

Next steps

Last updated