Documentation
¶
Overview ¶
Package slackmock is a stand-in for Slack, for tests and the local dev stacks. It does three things:
- It serves the part of the Slack Web API the kōjō Slack adapter calls (auth.test, chat.postMessage, chat.update, users.info, conversations.open, apps.connections.open) under /api/, and records every message kōjō posts.
- It plays the users: it sends mentions, direct messages and button clicks to kōjō, as signed HTTP requests to /slack/events and /slack/interactive, or over Socket Mode when the adapter has opened a connection.
- It exposes a small control API under /mock/ so that shell scripts and end-to-end tests can drive it over HTTP.
Point the adapter at it with chat.slack.api_url: <mock URL>/api/.
Index ¶
- Constants
- func DMChannel(userID string) string
- func Sign(secret, timestamp string, body []byte) string
- type Button
- type Message
- type Mock
- func (m *Mock) APICalls(method string) int
- func (m *Mock) APIURL() string
- func (m *Mock) AddUser(u User)
- func (m *Mock) ClickButton(ref chat.MessageRef, value, user string) error
- func (m *Mock) Close()
- func (m *Mock) DropSockets()
- func (m *Mock) Message(channel, ts string) (Message, bool)
- func (m *Mock) Messages() []Message
- func (m *Mock) Redeliver(eventID string) error
- func (m *Mock) Reset()
- func (m *Mock) SendDM(user, text string) (Sent, error)
- func (m *Mock) SendMention(channel, user, text string) (Sent, error)
- func (m *Mock) SendReply(channel, threadTS, user, text string) (Sent, error)
- func (m *Mock) ServeHTTP(w http.ResponseWriter, r *http.Request)
- func (m *Mock) SetKojoURL(u string)
- func (m *Mock) SocketConnections() int
- func (m *Mock) Start() string
- func (m *Mock) URL() string
- func (m *Mock) VerifyURL() error
- func (m *Mock) WaitForSocket(ctx context.Context) error
- type Options
- type Sent
- type User
Constants ¶
const ( // BotUserID is the user ID of the kōjō bot, as auth.test reports it. BotUserID = "U0KOJOBOT" // BotID is the bot ID of the kōjō bot. BotID = "B0KOJOBOT" // TeamID is the workspace. TeamID = "T0SLACKMOCK" // AppID is the Slack app. AppID = "A0SLACKMOCK" )
The mock's fixed identities.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Button ¶
type Button struct {
BlockID string `json:"block_id"`
ActionID string `json:"action_id"`
Value string `json:"value"`
Label string `json:"label"`
Style string `json:"style,omitempty"`
}
Button is a button on a message.
type Message ¶
type Message struct {
Channel string `json:"channel"`
TS string `json:"ts"`
// ThreadTS is the root of the thread the message is in; empty for a
// top-level message.
ThreadTS string `json:"thread_ts,omitempty"`
// Text is the top-level text, mrkdwn.
Text string `json:"text"`
// Blocks is the Block Kit JSON as kōjō sent it.
Blocks json.RawMessage `json:"blocks,omitempty"`
// BlockText is the text of the header, section and context blocks, one
// per line: what a person reads.
BlockText string `json:"block_text,omitempty"`
// Buttons are the buttons of the message's actions blocks.
Buttons []Button `json:"buttons,omitempty"`
// Updates counts the chat.update calls on the message.
Updates int `json:"updates"`
}
Message is a message kōjō posted, in its current state: chat.update replaces Text and Blocks and counts in Updates.
type Mock ¶
type Mock struct {
// contains filtered or unexported fields
}
Mock is the Slack mock. It is an http.Handler; Start serves it on a local port for tests.
func (*Mock) APICalls ¶
APICalls is how many times kōjō called a Web API method, such as "users.info".
func (*Mock) ClickButton ¶
func (m *Mock) ClickButton(ref chat.MessageRef, value, user string) error
ClickButton clicks the button with the given value on a message kōjō posted, as the given user, and delivers the block_actions payload to kōjō. It fails when the message has no such button.
func (*Mock) Close ¶
func (m *Mock) Close()
Close drops the Socket Mode connections and stops the server Start began.
func (*Mock) DropSockets ¶
func (m *Mock) DropSockets()
DropSockets closes every Socket Mode connection without notice, the way a network failure does. The adapter is expected to connect again.
func (*Mock) Messages ¶
Messages returns the messages kōjō posted, oldest first, each in its current state.
func (*Mock) Redeliver ¶
Redeliver sends an event again, the way Slack retries one it believes was not received: same event ID, with X-Slack-Retry-Num set.
func (*Mock) Reset ¶
func (m *Mock) Reset()
Reset forgets every message, event and call count. Users and connections stay.
func (*Mock) SendDM ¶
SendDM posts a direct message to the bot as the given user and delivers the message event to kōjō.
func (*Mock) SendMention ¶
SendMention posts "@kojo <text>" in a channel as the given user and delivers the app_mention event to kōjō. It fails when kōjō does not acknowledge the event.
func (*Mock) SendReply ¶
SendReply posts a reply in an existing thread: a mention in a channel, a plain message in a DM channel (D…).
func (*Mock) ServeHTTP ¶
func (m *Mock) ServeHTTP(w http.ResponseWriter, r *http.Request)
ServeHTTP serves the Web API (/api/), the Socket Mode endpoint (/socket), the control API (/mock/) and /healthz.
func (*Mock) SetKojoURL ¶
SetKojoURL sets kōjō's base URL, for when it is not known at New.
func (*Mock) SocketConnections ¶
SocketConnections is the number of open Socket Mode connections.
func (*Mock) Start ¶
Start serves the mock on a local port and returns its base URL. Call Close when done.
type Options ¶
type Options struct {
// SigningSecret signs the requests sent to kōjō. It must be the adapter's
// signing_secret.
SigningSecret string
// KojoURL is kōjō's base URL (http://localhost:8080). Events go to
// KojoURL + /slack/events, clicks to KojoURL + /slack/interactive.
KojoURL string
// BotToken, when set, is the only bot token the Web API accepts.
BotToken string
// AppToken, when set, is the only app-level token apps.connections.open
// accepts. Any xapp- token is accepted otherwise.
AppToken string
// Users are the people of the workspace. A user who sends a message
// without being listed is added with a made-up name and email.
Users []User
// Logger receives one line per API call and delivery. Default: discard.
Logger *slog.Logger
// HTTPClient delivers events to kōjō. Default: a client with a 10 second
// timeout.
HTTPClient *http.Client
// PingInterval is how often a Socket Mode connection is pinged.
// Default: 5 seconds.
PingInterval time.Duration
// AckTimeout is how long a Socket Mode envelope may go unacknowledged
// before the delivery fails. Default: 3 seconds, as Slack.
AckTimeout time.Duration
}
Options configure a Mock. The zero value works: it accepts any token and can deliver events once SetKojoURL has been called or a Socket Mode connection is open.
type Sent ¶
type Sent struct {
// EventID is the Events API event ID; Redeliver takes it.
EventID string `json:"event_id"`
Channel string `json:"channel"`
// TS is the message's timestamp, which is also its ID.
TS string `json:"ts"`
// ThreadTS is set when the message was a reply in a thread.
ThreadTS string `json:"thread_ts,omitempty"`
}
Sent describes a message the mock sent to kōjō on behalf of a user.
type User ¶
type User struct {
ID string `json:"id"`
// Name is the handle; RealName the full name. users.info reports
// RealName as the display name too.
Name string `json:"name"`
RealName string `json:"real_name,omitempty"`
Email string `json:"email,omitempty"`
}
User is a member of the mock workspace.
Directories
¶
| Path | Synopsis |
|---|---|
|
cmd
|
|
|
slackmock
command
Command slackmock runs the Slack mock as a process, for the local dev stacks: kōjō's chat.slack.api_url points at http://<listen>/api/, and the mock delivers mentions, DMs and button clicks to kōjō when its control API (/mock/...) is called.
|
Command slackmock runs the Slack mock as a process, for the local dev stacks: kōjō's chat.slack.api_url points at http://<listen>/api/, and the mock delivers mentions, DMs and button clicks to kōjō when its control API (/mock/...) is called. |