Get started

Testing

Every part of the SDK your code talks to can be answered by your test: the network, the clocks, the map's renderer and navigation.

Test code that calls the API

Every request leaves through an HTTPExchange. Hand the client your own, and it answers with no network:

import Foundation
import MountayaClient
import Testing

struct Answered: HTTPExchange {
  func send(_ request: ExchangeRequest) async throws -> ExchangeResponse {
    ExchangeResponse(status: 200, body: ExchangeBody(Data(#"{"data": []}"#.utf8)))
  }
}

@Test func listsNoActivities() async throws {
  let mountaya = try Mountaya(
    credential: .publishable(key: "pk_your_publishable_key"), exchange: Answered())
  let activities = try await mountaya.studio.activities.getActivities()
  #expect(activities.data.isEmpty)
}

Control time

The client measures every wait on the clock you hand it. A test that answers 503 and then 200 sees the call retried with no real wait:

import Foundation
import MountayaClient
import Testing

// Skips every wait under a second, a retry's backoff; an attempt's deadline still runs.
struct SkippingClock: Clock {
  var now: ContinuousClock.Instant { ContinuousClock.now }
  var minimumResolution: Duration { .zero }

  func sleep(until deadline: ContinuousClock.Instant, tolerance: Duration?) async throws {
    guard now.duration(to: deadline) >= .seconds(1) else { return }
    try await ContinuousClock().sleep(until: deadline, tolerance: tolerance)
  }
}

actor Attempts {
  private(set) var count = 0
  func next() -> Int {
    count += 1
    return count
  }
}

struct UnavailableOnce: HTTPExchange {
  let attempts = Attempts()

  func send(_ request: ExchangeRequest) async throws -> ExchangeResponse {
    if await attempts.next() == 1 { return ExchangeResponse(status: 503) }
    return ExchangeResponse(status: 200, body: ExchangeBody(Data(#"{"data": []}"#.utf8)))
  }
}

@Test func retriesAfterA503() async throws {
  let exchange = UnavailableOnce()
  let mountaya = try Mountaya(
    credential: .publishable(key: "pk_your_publishable_key"), exchange: exchange,
    clock: SkippingClock())
  let activities = try await mountaya.studio.activities.getActivities()
  #expect(activities.data.isEmpty)
  #expect(await exchange.attempts.count == 2)
}

Script the map

ScriptedMapSurface stands in for the renderer, so a test drives a MapEngine with no MapLibre:

import MountayaClient
import MountayaElements
import Testing

@MainActor
@Test func aTapReachesTheMap() async throws {
  let map = MapEngine(client: try Mountaya(credential: .publishable(key: "pk_your_publishable_key")))
  let surface = ScriptedMapSurface(drawing: map)
  var events = map.events().makeAsyncIterator()

  surface.tap(at: [400, 300], position: [6.8712, 45.9231])

  guard case .tap(let tap) = await events.next() else {
    Issue.record("no tap")
    return
  }
  #expect(tap.position == [6.8712, 45.9231])
}

ScriptedNavigationEngine is a navigation engine whose route requests your test answers:

import MountayaGeo
import Testing

@MainActor
@Test func arrivesInArgentiere() throws {
  let navigation = ScriptedNavigationEngine<String>()
  navigation.start("0b6d1a4e-6d3f-4f7a-9c2e-1f0a5b8c3d21")
  try navigation.sent[0].answer(
    NavigationRoute(line: [[6.8712, 45.9231], [6.9271, 45.9822]], duration: 9_600))

  try navigation.setPosition([6.9271, 45.9822])
  #expect(navigation.state.status == .arrived)
}

Code that takes any NavigationEngine runs the same over this engine and over DeviceNavigationEngine.

Build errors

Build the failure your code handles through its public initializer:

import MountayaClient
import Testing

func message(for error: MountayaError) -> String {
  if case .api(let failure) = error, failure.kind == .notFound { return "Gone" }
  return "Try again"
}

@Test func saysGoneOnA404() {
  #expect(message(for: .api(APIError(status: 404))) == "Gone")
  #expect(message(for: .connection(ConnectionError())) == "Try again")
}

Next steps

Last updated