telega/testing/mock

Mock client and assertion utilities for testing bot API interactions.

import telega/testing/mock

let #(client, calls) = mock.client()
// ... use client in your bot setup ...
mock.assert_call_count(from: calls, expected: 1)

Types

Represents a recorded API call.

pub type ApiCall {
  ApiCall(request: request.Request(String))
}

Constructors

A route that matches API requests by path and returns a custom response.

pub type MockRoute {
  MockRoute(
    path_contains: String,
    handler: fn(request.Request(String)) -> Result(
      response.Response(String),
      error.TelegaError,
    ),
  )
}

Constructors

Values

pub fn assert_call_count(
  from subject: process.Subject(ApiCall),
  expected expected: Int,
) -> List(ApiCall)

Asserts the number of recorded API calls equals the expected count.

pub fn assert_called_with_body(
  from subject: process.Subject(ApiCall),
  path_contains path_contains: String,
  body_contains body_contains: String,
) -> ApiCall

Asserts at least one API call was made to a path containing path_contains and whose request body contains body_contains.

pub fn assert_called_with_path(
  from subject: process.Subject(ApiCall),
  path_contains path_contains: String,
) -> ApiCall

Asserts at least one API call was made to a path containing the given string.

pub fn assert_no_calls(
  from subject: process.Subject(ApiCall),
) -> Nil

Asserts no API calls were recorded.

pub fn bool_response() -> String

Returns a {"ok":true,"result":true} response string. Use for endpoints that return a boolean (e.g. answerCallbackQuery, deleteMessage).

pub fn client() -> #(
  client.TelegramClient,
  process.Subject(ApiCall),
)

Creates a mock Telegram client that records all API calls and returns 200 OK with an empty successful response. Note: This response will NOT decode as a valid Message. Use message_client() if your handlers call reply functions.

pub fn client_with(
  handler handler: fn(request.Request(String)) -> Result(
    response.Response(String),
    error.TelegaError,
  ),
) -> #(client.TelegramClient, process.Subject(ApiCall))

Creates a mock client with a custom response handler.

pub fn get_calls(
  from subject: process.Subject(ApiCall),
) -> List(ApiCall)

Drains all recorded API calls from the subject.

pub fn message_client() -> #(
  client.TelegramClient,
  process.Subject(ApiCall),
)

Creates a mock Telegram client that records all API calls and returns 200 OK with a valid Message response. Use this when your handlers call reply.with_text or other reply functions.

pub fn message_response() -> String

Returns a JSON string representing a valid {"ok":true,"result":{...message...}} response. Useful for building custom mock clients that need to return valid Message responses.

pub fn ok_response(result result: json.Json) -> String

Wraps a Json value into {"ok":true,"result":...} Telegram API response format.

pub fn route(
  path_contains path_contains: String,
  handler handler: fn(request.Request(String)) -> Result(
    response.Response(String),
    error.TelegaError,
  ),
) -> MockRoute

Creates a route that matches requests whose path contains path_contains and delegates to the given handler function.

pub fn route_with_body(
  path_contains path_contains: String,
  body body: String,
) -> MockRoute

Creates a route that matches requests whose path contains path_contains and returns a 200 OK with the given body string.

pub fn route_with_response(
  path_contains path_contains: String,
  response response_body: String,
) -> MockRoute

Creates a route that matches requests whose path contains path_contains and returns a 200 OK with the given response string (from ok_response, bool_response, etc.). Prefer this over route_with_body for type-safe response building.

pub fn routed_client(
  routes routes: List(MockRoute),
) -> #(client.TelegramClient, process.Subject(ApiCall))

Creates a mock client that routes requests through a list of MockRoutes. Routes are checked in order; first match wins. Unmatched requests fall back to a default message_response().

pub fn stateful_client(
  handler handler: fn(request.Request(String), Int) -> Result(
    response.Response(String),
    error.TelegaError,
  ),
) -> #(client.TelegramClient, process.Subject(ApiCall))

Creates a mock client that passes a 0-based call counter to the handler. Useful for returning different responses based on call order.

Search Document