-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/4, context_with/2, context/1, 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", 69). ?DOC(" Creates a `Context` with full customization.\n"). -spec context_with_all( AVAH, telega@update:update(), binary(), telega@model@types:user() ) -> telega@bot:context(AVAH, any()). context_with_all(Session, Update, Key, Bot_info) -> Chat_subject = gleam@erlang@process:new_subject(), {context, Key, Update, config(), Session, Chat_subject, none, none, Bot_info}. -file("src/telega/testing/context.gleam", 56). ?DOC(" Creates a `Context` with the given session and update.\n"). -spec context_with(AVAD, telega@update:update()) -> telega@bot:context(AVAD, any()). context_with(Session, Update) -> context_with_all( Session, Update, <<"test_chat:123"/utf8>>, telega@testing@factory:bot_user() ). -file("src/telega/testing/context.gleam", 51). ?DOC(" Creates a `Context` with the given session and default update/config.\n"). -spec context(AUZZ) -> telega@bot:context(AUZZ, any()). context(Session) -> context_with(Session, telega@testing@factory:text_update(<<"Hello"/utf8>>)). -file("src/telega/testing/context.gleam", 89). ?DOC(" Creates `SessionSettings` with a no-op persist and get returning None.\n"). -spec session_settings(fun(() -> AVAL)) -> telega@bot:session_settings(AVAL, any()). session_settings(Default) -> {session_settings, fun(_, Session) -> {ok, Session} end, fun(_) -> {ok, none} end, Default}. -file("src/telega/testing/context.gleam", 100). ?DOC(" Creates `SessionSettings` where get returns `Some(initial)`.\n"). -spec session_settings_with(fun(() -> AVAP), AVAP) -> telega@bot:session_settings(AVAP, 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", 112). ?DOC(" Creates a no-op catch handler.\n"). -spec catch_handler() -> fun((telega@bot:context(any(), AVAU), AVAU) -> {ok, nil} | {error, AVAU}). catch_handler() -> fun(_, _) -> {ok, nil} end.