Documentation
¶
Overview ¶
Package botsfwtest provides a fake in-memory WebhookContext implementation for testing bot command handlers without network, database, or platform dependencies.
Usage:
whc := botsfwtest.NewFakeWebhookContext(
botsfwtest.WithTextMessage("hello"),
botsfwtest.WithBotCode("mybot"),
)
m, err := myCommand.Action(whc)
// inspect m, err and whc.SentMessages()
Index ¶
- Constants
- type FakeResponder
- type FakeTextMessage
- func (m *FakeTextMessage) BotChatID() (string, error)
- func (m *FakeTextMessage) Chat() botinput.Chat
- func (m *FakeTextMessage) GetRecipient() botinput.Recipient
- func (m *FakeTextMessage) GetSender() botinput.User
- func (m *FakeTextMessage) GetTime() time.Time
- func (m *FakeTextMessage) InputType() botinput.Type
- func (m *FakeTextMessage) IsEdited() bool
- func (m *FakeTextMessage) LogRequest()
- func (m *FakeTextMessage) MessageIntID() int
- func (m *FakeTextMessage) MessageStringID() string
- func (m *FakeTextMessage) Text() string
- type FakeWebhookContext
- func (f *FakeWebhookContext) Analytics() botsfw.WebhookAnalytics
- func (f *FakeWebhookContext) AppContext() botsfw.AppContext
- func (f *FakeWebhookContext) AppUserData() (botsfwmodels.AppUserData, error)
- func (f *FakeWebhookContext) AppUserID() string
- func (f *FakeWebhookContext) BotContext() botsfw.BotContext
- func (f *FakeWebhookContext) BotPlatform() botsfw.BotPlatform
- func (f *FakeWebhookContext) ChatData() botsfwmodels.BotChatData
- func (f *FakeWebhookContext) CommandText(title, icon string) string
- func (f *FakeWebhookContext) Context() context.Context
- func (f *FakeWebhookContext) Environment() string
- func (f *FakeWebhookContext) ExecutionContext() botsfw.ExecutionContext
- func (f *FakeWebhookContext) GetBotCode() string
- func (f *FakeWebhookContext) GetBotSettings() *botsfw.BotSettings
- func (f *FakeWebhookContext) GetBotUser() (botsfwstore.PlatformUser, error)
- func (f *FakeWebhookContext) GetBotUserID() string
- func (f *FakeWebhookContext) GetTranslator(locale string) i18n.SingleLocaleTranslator
- func (f *FakeWebhookContext) Input() botinput.InputMessage
- func (f *FakeWebhookContext) IsInGroup() (bool, error)
- func (f *FakeWebhookContext) IsNewerThen(chatEntity botsfwmodels.BotChatData) bool
- func (f *FakeWebhookContext) Locale() i18n.Locale
- func (f *FakeWebhookContext) MustBotChatID() string
- func (f *FakeWebhookContext) NewEditMessage(text string, format botmsg.Format) (botmsg.MessageFromBot, error)
- func (f *FakeWebhookContext) NewMessage(text string) botmsg.MessageFromBot
- func (f *FakeWebhookContext) NewMessageByCode(code string, a ...interface{}) botmsg.MessageFromBot
- func (f *FakeWebhookContext) RecordedAnalyticsMessages() []interface{ ... }
- func (f *FakeWebhookContext) RecordsFieldsSetter() botsfw.BotRecordsFieldsSetter
- func (f *FakeWebhookContext) Request() *http.Request
- func (f *FakeWebhookContext) Responder() botsfw.WebhookResponder
- func (f *FakeWebhookContext) SaveBotChat() error
- func (f *FakeWebhookContext) SentMessages() []botmsg.MessageFromBot
- func (f *FakeWebhookContext) SetBotUserAccessGranted(value bool) error
- func (f *FakeWebhookContext) SetContext(c context.Context)
- func (f *FakeWebhookContext) SetLocale(code5 string) error
- func (f *FakeWebhookContext) SetUser(id string, data botsfwmodels.AppUserData)
- func (f *FakeWebhookContext) Translate(key string, args ...interface{}) string
- func (f *FakeWebhookContext) TranslateNoWarning(key string, args ...interface{}) string
- func (f *FakeWebhookContext) TranslateWithMap(key string, args map[string]string) string
- func (f *FakeWebhookContext) UpdateLastProcessed(chatEntity botsfwmodels.BotChatData) error
- type Option
Constants ¶
const InputTypeCallbackQuery = botinput.TypeCallbackQuery
InputTypeCallbackQuery is re-exported for use in test assertions.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type FakeResponder ¶
type FakeResponder struct {
// Sent accumulates every MessageFromBot delivered to SendMessage.
Sent []botmsg.MessageFromBot
}
FakeResponder implements botsfw.WebhookResponder and records every message passed to SendMessage so tests can inspect what the command produced.
func (*FakeResponder) DeleteMessage ¶
func (r *FakeResponder) DeleteMessage(_ context.Context, _ string) error
DeleteMessage is a no-op.
func (*FakeResponder) SendMessage ¶
func (r *FakeResponder) SendMessage(_ context.Context, m botmsg.MessageFromBot, _ botmsg.BotAPISendMessageChannel) (botsfw.OnMessageSentResponse, error)
SendMessage records the message and returns a zero response with no error.
type FakeTextMessage ¶
type FakeTextMessage struct {
// contains filtered or unexported fields
}
FakeTextMessage implements botinput.TextMessage and is exported so test code can type-assert whc.Input() to access the Text() method directly.
func (*FakeTextMessage) GetRecipient ¶
func (*FakeTextMessage) InputType ¶
func (m *FakeTextMessage) InputType() botinput.Type
func (*FakeTextMessage) IsEdited ¶
func (m *FakeTextMessage) IsEdited() bool
func (*FakeTextMessage) LogRequest ¶
func (m *FakeTextMessage) LogRequest()
func (*FakeTextMessage) MessageIntID ¶
func (m *FakeTextMessage) MessageIntID() int
func (*FakeTextMessage) MessageStringID ¶
func (m *FakeTextMessage) MessageStringID() string
func (*FakeTextMessage) Text ¶
func (m *FakeTextMessage) Text() string
Text returns the configured text of the fake message.
type FakeWebhookContext ¶
type FakeWebhookContext struct {
// contains filtered or unexported fields
}
FakeWebhookContext is an in-memory implementation of botsfw.WebhookContext. It satisfies the full interface without any network, database, or platform calls.
Typical usage in a test:
whc := botsfwtest.NewFakeWebhookContext(botsfwtest.WithTextMessage("/start"))
m, err := myCommand.TextAction(whc, "/start")
assert.NoError(t, err)
assert.Contains(t, m.Text, "Welcome")
sent := whc.SentMessages() // messages sent via whc.Responder().SendMessage(...)
func NewFakeWebhookContext ¶
func NewFakeWebhookContext(opts ...Option) *FakeWebhookContext
NewFakeWebhookContext creates a FakeWebhookContext with sensible defaults. The default input is a text message with an empty string.
func (*FakeWebhookContext) Analytics ¶
func (f *FakeWebhookContext) Analytics() botsfw.WebhookAnalytics
Analytics returns the fake analytics recorder.
func (*FakeWebhookContext) AppContext ¶
func (f *FakeWebhookContext) AppContext() botsfw.AppContext
func (*FakeWebhookContext) AppUserData ¶
func (f *FakeWebhookContext) AppUserData() (botsfwmodels.AppUserData, error)
func (*FakeWebhookContext) AppUserID ¶
func (f *FakeWebhookContext) AppUserID() string
func (*FakeWebhookContext) BotContext ¶
func (f *FakeWebhookContext) BotContext() botsfw.BotContext
func (*FakeWebhookContext) BotPlatform ¶
func (f *FakeWebhookContext) BotPlatform() botsfw.BotPlatform
func (*FakeWebhookContext) ChatData ¶
func (f *FakeWebhookContext) ChatData() botsfwmodels.BotChatData
func (*FakeWebhookContext) CommandText ¶
func (f *FakeWebhookContext) CommandText(title, icon string) string
CommandText concatenates title and icon the same way the framework does.
func (*FakeWebhookContext) Context ¶
func (f *FakeWebhookContext) Context() context.Context
func (*FakeWebhookContext) Environment ¶
func (f *FakeWebhookContext) Environment() string
func (*FakeWebhookContext) ExecutionContext ¶
func (f *FakeWebhookContext) ExecutionContext() botsfw.ExecutionContext
func (*FakeWebhookContext) GetBotCode ¶
func (f *FakeWebhookContext) GetBotCode() string
func (*FakeWebhookContext) GetBotSettings ¶
func (f *FakeWebhookContext) GetBotSettings() *botsfw.BotSettings
func (*FakeWebhookContext) GetBotUser ¶
func (f *FakeWebhookContext) GetBotUser() (botsfwstore.PlatformUser, error)
func (*FakeWebhookContext) GetBotUserID ¶
func (f *FakeWebhookContext) GetBotUserID() string
func (*FakeWebhookContext) GetTranslator ¶
func (f *FakeWebhookContext) GetTranslator(locale string) i18n.SingleLocaleTranslator
GetTranslator returns a pass-through translator for the given locale.
func (*FakeWebhookContext) Input ¶
func (f *FakeWebhookContext) Input() botinput.InputMessage
func (*FakeWebhookContext) IsInGroup ¶
func (f *FakeWebhookContext) IsInGroup() (bool, error)
func (*FakeWebhookContext) IsNewerThen ¶
func (f *FakeWebhookContext) IsNewerThen(chatEntity botsfwmodels.BotChatData) bool
func (*FakeWebhookContext) Locale ¶
func (f *FakeWebhookContext) Locale() i18n.Locale
func (*FakeWebhookContext) MustBotChatID ¶
func (f *FakeWebhookContext) MustBotChatID() string
func (*FakeWebhookContext) NewEditMessage ¶
func (f *FakeWebhookContext) NewEditMessage(text string, format botmsg.Format) (botmsg.MessageFromBot, error)
func (*FakeWebhookContext) NewMessage ¶
func (f *FakeWebhookContext) NewMessage(text string) botmsg.MessageFromBot
func (*FakeWebhookContext) NewMessageByCode ¶
func (f *FakeWebhookContext) NewMessageByCode(code string, a ...interface{}) botmsg.MessageFromBot
func (*FakeWebhookContext) RecordedAnalyticsMessages ¶
func (f *FakeWebhookContext) RecordedAnalyticsMessages() []interface{ Type() string }
RecordedAnalyticsMessages returns analytics messages recorded during the test.
func (*FakeWebhookContext) RecordsFieldsSetter ¶
func (f *FakeWebhookContext) RecordsFieldsSetter() botsfw.BotRecordsFieldsSetter
func (*FakeWebhookContext) Request ¶
func (f *FakeWebhookContext) Request() *http.Request
func (*FakeWebhookContext) Responder ¶
func (f *FakeWebhookContext) Responder() botsfw.WebhookResponder
Responder returns the FakeResponder which records every SendMessage call.
func (*FakeWebhookContext) SaveBotChat ¶
func (f *FakeWebhookContext) SaveBotChat() error
func (*FakeWebhookContext) SentMessages ¶
func (f *FakeWebhookContext) SentMessages() []botmsg.MessageFromBot
SentMessages returns all messages that were sent via this context's responder.
func (*FakeWebhookContext) SetBotUserAccessGranted ¶ added in v0.76.0
func (f *FakeWebhookContext) SetBotUserAccessGranted(value bool) error
func (*FakeWebhookContext) SetContext ¶
func (f *FakeWebhookContext) SetContext(c context.Context)
func (*FakeWebhookContext) SetLocale ¶
func (f *FakeWebhookContext) SetLocale(code5 string) error
func (*FakeWebhookContext) SetUser ¶
func (f *FakeWebhookContext) SetUser(id string, data botsfwmodels.AppUserData)
func (*FakeWebhookContext) Translate ¶
func (f *FakeWebhookContext) Translate(key string, args ...interface{}) string
Translate is a pass-through translator: it returns the key unchanged. Override it in your option if you need specific translated strings.
func (*FakeWebhookContext) TranslateNoWarning ¶
func (f *FakeWebhookContext) TranslateNoWarning(key string, args ...interface{}) string
TranslateNoWarning behaves like Translate.
func (*FakeWebhookContext) TranslateWithMap ¶
func (f *FakeWebhookContext) TranslateWithMap(key string, args map[string]string) string
TranslateWithMap is a pass-through returning the key unchanged.
func (*FakeWebhookContext) UpdateLastProcessed ¶
func (f *FakeWebhookContext) UpdateLastProcessed(chatEntity botsfwmodels.BotChatData) error
type Option ¶
type Option func(*FakeWebhookContext)
Option configures a FakeWebhookContext.
func WithBotCode ¶
WithBotCode sets the bot code (default: "testbot").
func WithCallbackQuery ¶
WithCallbackQuery configures the context to present a callback query as input.
func WithChatID ¶
WithChatID sets the fake chat ID (default: "chat1").
func WithLocale ¶
WithLocale sets the translator locale (default: en-US).
func WithTextMessage ¶
WithTextMessage configures the context to present a text message as input.
func WithUserID ¶
WithUserID sets the fake sender's user ID (default: "user1").