telega/testing/handler
Utilities for testing individual handlers in isolation and setting up full bot instances with minimal boilerplate.
import telega/testing/handler
import telega/testing/factory
import telega/testing/mock
// Test a single handler without a router
pub fn my_handler_test() {
let #(ctx, calls) = handler.test_handler(
session: MySession(count: 0),
update: factory.text_update(text: "hello"),
handler: fn(ctx, _update) { reply.with_text(ctx, "hi") |> result.map(fn(_) { ctx }) },
)
mock.assert_call_count(from: calls, expected: 1)
}
// Test with a full bot (router + actors)
pub fn my_bot_test() {
handler.with_test_bot(
router: build_router(),
session: fn() { MySession(count: 0) },
handler: fn(bot_subject, calls) {
let update = factory.command_update("start")
bot.handle_update(bot_subject:, update:)
mock.assert_call_count(from: calls, expected: 1)
},
)
}
Values
pub fn test_handler(
session session: session,
update update: update.Update,
handler handler: fn(bot.Context(session, error), update.Update) -> Result(
bot.Context(session, error),
error,
),
) -> #(
Result(bot.Context(session, error), error),
process.Subject(mock.ApiCall),
)
Runs a handler function in isolation with a mock client. Returns the resulting context and the recorded API calls subject.
Use this to test a single handler without involving the router or actor system.
pub fn with_test_bot(
router router: router.Router(session, error),
session default_session: fn() -> session,
handler handler: fn(
process.Subject(bot.BotMessage),
process.Subject(mock.ApiCall),
) -> Nil,
) -> Nil
Starts a full bot with router, registry, and actors using a mock client.
Calls handler with the bot subject and API calls subject, then cleans up.
Use this to test end-to-end update handling with minimal boilerplate.
pub fn with_test_bot_advanced(
router_handler router_handler: fn(
bot.Context(session, error),
update.Update,
) -> Result(bot.Context(session, error), error),
session_settings session_settings: bot.SessionSettings(
session,
error,
),
handler handler: fn(
process.Subject(bot.BotMessage),
process.Subject(mock.ApiCall),
) -> Nil,
) -> Nil
Lower-level variant with custom router handler and session settings.