botsfwtest

package
v0.77.9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 14, 2026 License: Apache-2.0 Imports: 10 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

View Source
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

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) BotChatID

func (m *FakeTextMessage) BotChatID() (string, error)

func (*FakeTextMessage) Chat

func (m *FakeTextMessage) Chat() botinput.Chat

func (*FakeTextMessage) GetRecipient

func (m *FakeTextMessage) GetRecipient() botinput.Recipient

func (*FakeTextMessage) GetSender

func (m *FakeTextMessage) GetSender() botinput.User

func (*FakeTextMessage) GetTime

func (m *FakeTextMessage) GetTime() time.Time

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

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 (*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 (*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

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

func WithBotCode(code string) Option

WithBotCode sets the bot code (default: "testbot").

func WithCallbackQuery

func WithCallbackQuery(data string) Option

WithCallbackQuery configures the context to present a callback query as input.

func WithChatID

func WithChatID(id string) Option

WithChatID sets the fake chat ID (default: "chat1").

func WithLocale

func WithLocale(code5 string) Option

WithLocale sets the translator locale (default: en-US).

func WithTextMessage

func WithTextMessage(text string) Option

WithTextMessage configures the context to present a text message as input.

func WithUserID

func WithUserID(id string) Option

WithUserID sets the fake sender's user ID (default: "user1").

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL