Current section

Files

Jump to
telega src telega@testing@context.erl
Raw

src/telega@testing@context.erl

-module(telega@testing@context).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/telega/testing/context.gleam").
-export([config/0, config_with_client/1, context_with_all/5, context_with/2, context/1, context_with_dependencies/2, session_settings/1, session_settings_with/2, catch_handler/0]).
-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.
?MODULEDOC(
" Context and config builders for testing.\n"
"\n"
" Provides functions to create `bot.Context`, `config.Config`,\n"
" and `bot.SessionSettings` with sensible test defaults.\n"
"\n"
" ```gleam\n"
" import telega/testing/context\n"
" import telega/testing/factory\n"
"\n"
" let ctx = context.context(session: MySession(count: 0))\n"
" let cfg = context.config()\n"
" ```\n"
).
-file("src/telega/testing/context.gleam", 27).
-spec test_fetch_client(any()) -> {ok, gleam@http@response:response(binary())} |
{error, telega@error:telega_error()}.
test_fetch_client(_) ->
{ok,
begin
_pipe = gleam@http@response:new(200),
gleam@http@response:set_body(
_pipe,
<<"{\"ok\":true,\"result\":{}}"/utf8>>
)
end}.
-file("src/telega/testing/context.gleam", 34).
?DOC(" Creates a test `Config` with a test token and URL.\n").
-spec config() -> telega@internal@config:config().
config() ->
telega@internal@config:new(
telega@client:new(<<"test_token"/utf8>>, fun test_fetch_client/1),
<<"https://test.example.com"/utf8>>,
<<"test_webhook"/utf8>>,
none
).
-file("src/telega/testing/context.gleam", 45).
?DOC(
" Creates a test `Config` with the given Telegram client.\n"
" Use this with `mock.message_client()` to get a config that returns valid API responses.\n"
).
-spec config_with_client(telega@client:telegram_client()) -> telega@internal@config:config().
config_with_client(Client) ->
Cfg = config(),
{config,
erlang:element(2, Cfg),
erlang:element(3, Cfg),
erlang:element(4, Cfg),
Client}.
-file("src/telega/testing/context.gleam", 90).
?DOC(" Creates a `Context` with full customization, including injected `dependencies`.\n").
-spec context_with_all(
BLMN,
telega@update:update(),
binary(),
telega@model@types:user(),
BLMO
) -> telega@bot:context(BLMN, any(), BLMO).
context_with_all(Session, Update, Key, Bot_info, Dependencies) ->
Chat_subject = gleam@erlang@process:new_subject(),
{context,
Key,
Update,
config(),
Session,
Dependencies,
Chat_subject,
none,
none,
Bot_info}.
-file("src/telega/testing/context.gleam", 59).
?DOC(" Creates a `Context` with the given session and update. `dependencies` defaults to `Nil`.\n").
-spec context_with(BLMC, telega@update:update()) -> telega@bot:context(BLMC, any(), nil).
context_with(Session, Update) ->
context_with_all(
Session,
Update,
<<"test_chat:123"/utf8>>,
telega@testing@factory:bot_user(),
nil
).
-file("src/telega/testing/context.gleam", 54).
?DOC(
" Creates a `Context` with the given session and default update/config.\n"
"\n"
" `dependencies` defaults to `Nil`. To inject mock services, use `context_with_dependencies`\n"
" (or `context_with_all`).\n"
).
-spec context(BLLX) -> telega@bot:context(BLLX, any(), nil).
context(Session) ->
context_with(Session, telega@testing@factory:text_update(<<"Hello"/utf8>>)).
-file("src/telega/testing/context.gleam", 76).
?DOC(
" Creates a `Context` with the given session and mock dependencies (services).\n"
"\n"
" Use this to substitute mocked services in handler tests:\n"
" `context.context_with_dependencies(session: MySession(..), dependencies: MockDependencies(db:, http:))`.\n"
).
-spec context_with_dependencies(BLMH, BLMI) -> telega@bot:context(BLMH, any(), BLMI).
context_with_dependencies(Session, Dependencies) ->
context_with_all(
Session,
telega@testing@factory:text_update(<<"Hello"/utf8>>),
<<"test_chat:123"/utf8>>,
telega@testing@factory:bot_user(),
Dependencies
).
-file("src/telega/testing/context.gleam", 112).
?DOC(" Creates `SessionSettings` with a no-op persist and get returning None.\n").
-spec session_settings(fun(() -> BLMT)) -> telega@bot:session_settings(BLMT, any()).
session_settings(Default) ->
{session_settings,
fun(_, Session) -> {ok, Session} end,
fun(_) -> {ok, none} end,
Default}.
-file("src/telega/testing/context.gleam", 123).
?DOC(" Creates `SessionSettings` where get returns `Some(initial)`.\n").
-spec session_settings_with(fun(() -> BLMX), BLMX) -> telega@bot:session_settings(BLMX, any()).
session_settings_with(Default, Initial) ->
{session_settings,
fun(_, Session) -> {ok, Session} end,
fun(_) -> {ok, {some, Initial}} end,
Default}.
-file("src/telega/testing/context.gleam", 135).
?DOC(" Creates a no-op catch handler.\n").
-spec catch_handler() -> fun((telega@bot:context(any(), BLNC, any()), BLNC) -> {ok,
nil} |
{error, BLNC}).
catch_handler() ->
fun(_, _) -> {ok, nil} end.