testkit

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Jun 11, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

testkit 包提供插件单元测试用的轻量 AnyBot 运行时。

Index

Constants

This section is empty.

Variables

View Source
var ErrAppUnavailable = errors.New("anybot testkit: app is unavailable")

ErrAppUnavailable 表示测试运行时不可用。

Functions

func InstallConfig

func InstallConfig[T any](app *App, definition sdk.DefinitionOf[T], config T) error

InstallConfig 使用给定 typed config 安装插件定义。

func Message

func Message(text string, opts ...EventOption) *sdk.Event

Message 创建一条测试消息事件。

Types

type App

type App struct {
	// contains filtered or unexported fields
}

App 是插件测试用运行时,记录插件通过 Reply 或 Send 发出的消息。

func NewApp

func NewApp(opts ...Option) *App

NewApp 创建带记录客户端的测试运行时。

func (*App) Client

func (app *App) Client() *Client

Client 返回记录客户端。

func (*App) Dispatch

func (app *App) Dispatch(ctx context.Context, event *sdk.Event) error

Dispatch 投递自定义事件。

func (*App) DispatchText

func (app *App) DispatchText(text string, opts ...EventOption) error

DispatchText 投递一条消息事件。

func (*App) Install

func (app *App) Install(plugins ...sdk.Plugin) error

Install 安装已按测试配置构建好的插件对象。

func (*App) InstallDefault

func (app *App) InstallDefault(definitions ...sdk.Definition) error

InstallDefault 用插件默认配置安装插件定义。

func (*App) LastReplyText

func (app *App) LastReplyText() string

LastReplyText 返回最后一条发送消息的纯文本内容。

func (*App) Runtime

func (app *App) Runtime() *sdk.App

Runtime 返回底层运行时,供少数需要直接调度或检查 App 状态的测试使用。

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client 是记录发送消息的测试动作客户端。

func (*Client) Last

func (c *Client) Last() (SentMessage, bool)

Last 返回最后一条发送消息。

func (*Client) LastText

func (c *Client) LastText() string

LastText 返回最后一条发送消息的纯文本内容。

func (*Client) Send

func (c *Client) Send(_ context.Context, target sdk.ReplyTarget, chain message.Chain) (sdk.MessageReceipt, error)

Send 记录一条发送消息。

func (*Client) Sent

func (c *Client) Sent() []SentMessage

Sent 返回所有已发送消息的副本。

type EventOption

type EventOption func(*sdk.Event)

EventOption 调整测试事件。

func FromUser

func FromUser(id string) EventOption

FromUser 设置消息发送者 ID。

func InGroup

func InGroup(id string) EventOption

InGroup 设置群 ID,使事件成为群消息。

func Mention

func Mention(id string) EventOption

Mention 追加一个 at 消息段,便于测试 Mentioned 和 ToMe 规则。

func MentionSelf

func MentionSelf() EventOption

MentionSelf 追加一个指向当前机器人账号的 at 消息段。

func WithGroupRole added in v1.2.0

func WithGroupRole(role string) EventOption

WithGroupRole 设置群消息发送者的标准群角色。

func WithMessage

func WithMessage(chain message.Chain) EventOption

WithMessage 设置完整消息链,并同步 Text 为消息链中的纯文本内容。

func WithProtocol

func WithProtocol(protocol sdk.Protocol) EventOption

WithProtocol 设置协议标识。

func WithSelfID

func WithSelfID(id string) EventOption

WithSelfID 设置当前机器人账号 ID。

type Option

type Option func(*options)

Option 调整测试运行时。

func WithConfigStore

func WithConfigStore(store sdk.ConfigStore) Option

WithConfigStore 配置测试插件配置写回存储。

func WithDataDir

func WithDataDir(root string) Option

WithDataDir 配置测试插件数据根目录。

func WithPluginID

func WithPluginID(id string) Option

WithPluginID 配置测试运行时使用的 PluginID。

func WithSuperUsers

func WithSuperUsers(ids ...string) Option

WithSuperUsers 配置测试运行时的超级用户。

type SentMessage

type SentMessage struct {
	Target sdk.ReplyTarget
	Chain  message.Chain
}

SentMessage 是测试客户端记录到的一条发送消息。

Jump to

Keyboard shortcuts

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