testutil

package
v0.0.0-...-bf34452 Latest Latest
Warning

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

Go to latest
Published: Feb 25, 2023 License: MIT Imports: 23 Imported by: 0

Documentation

Index

Constants

View Source
const (
	MainChannelID   = model.Snowflake(8675309)
	SecondChannelID = model.Snowflake(9000000)
	DirectMessageID = model.Snowflake(1)
)

IDs used for testing

View Source
const (
	// GistSuccessURL is the fake URL for success
	GistSuccessURL = "https://www.example.com/success"
)

Variables

This section is empty.

Functions

func NewUser

func NewUser(name string, id model.Snowflake, bot bool) *discordgo.User

NewUser creates a new user for testing

Types

type FakeUTCClock

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

FakeUTCClock is a mockable UTC clock for testing.

func NewFakeUTCClock

func NewFakeUTCClock() *FakeUTCClock

NewFakeUTCClock works as advertised.

func (*FakeUTCClock) Advance

func (c *FakeUTCClock) Advance(d time.Duration)

Advance advances the internal clock by the given duration.

func (*FakeUTCClock) Now

func (c *FakeUTCClock) Now() time.Time

Now returns the mocked UTC time.

type FakeUTCTimer

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

FakeUTCTimer is a faked UTC timer for testing.

func NewFakeUTCTimer

func NewFakeUTCTimer() *FakeUTCTimer

NewFakeUTCTimer works as advertised.

func (*FakeUTCTimer) ElapseTime

func (c *FakeUTCTimer) ElapseTime(duration time.Duration)

ElapseTime subtracts duration from each stored callback. For each duration that becomes negative, it is removed from the list and the associated callback is called.

func (*FakeUTCTimer) ExecuteAfter

func (c *FakeUTCTimer) ExecuteAfter(d time.Duration, cb func())

ExecuteAfter stores the callback and the duration. If the duration is 0 or negative, it will fire immediately.

type InMemoryDiscordSession

type InMemoryDiscordSession struct {
	Messages []*Message
	Users    map[string]*discordgo.User
	Channels map[string]*discordgo.Channel
	// contains filtered or unexported fields
}

InMemoryDiscordSession is a fake for the discord session.

func NewInMemoryDiscordSession

func NewInMemoryDiscordSession() *InMemoryDiscordSession

NewInMemoryDiscordSession works as advertised.

func (*InMemoryDiscordSession) Channel

func (s *InMemoryDiscordSession) Channel(channelID string, options ...discordgo.RequestOption) (*discordgo.Channel, error)

Channel returns the Channel struct of the given channel ID. Can be used to determine attributes such as the channel name, topic, etc.

func (*InMemoryDiscordSession) ChannelMessageSend

func (s *InMemoryDiscordSession) ChannelMessageSend(channel string, message string, options ...discordgo.RequestOption) (*discordgo.Message, error)

ChannelMessageSend records a new message delivery.

func (*InMemoryDiscordSession) SetChannel

func (s *InMemoryDiscordSession) SetChannel(channel *discordgo.Channel)

SetChannel adds a channel to the map of channels that InMemoryDiscordSession holds. Can be used to test features that only work under certain channel conditions.

func (*InMemoryDiscordSession) User

func (s *InMemoryDiscordSession) User(userID string, options ...discordgo.RequestOption) (*discordgo.User, error)

User returns the user struct of the given user ID.

type InMemoryGist

type InMemoryGist struct {
	Messages []string
	FailNext bool
}

InMemoryGist is a fake for the Gist interface.

func NewInMemoryGist

func NewInMemoryGist() *InMemoryGist

NewInMemoryGist works as advertised.

func (*InMemoryGist) Upload

func (g *InMemoryGist) Upload(content string) (string, error)

Upload stores the message, or returns an error if FailNext is set. Resets FailNext.

type LearnData

type LearnData struct {
	Call     string
	Response string
}

LearnData represents the information needed to reconstruct a Learn for testing.

func NewLearnData

func NewLearnData(call, response string) *LearnData

NewLearnData stores a duplicate representation of the expected learn data for testing

type Message

type Message struct {
	Channel string
	Message string
}

Message encapsulates a channel/message pair.

func NewMessage

func NewMessage(channel, message string) *Message

NewMessage works as advertised.

type Runner

type Runner struct {
	// Test object
	T *testing.T

	// State
	GistsCount           int
	DiscordMessagesCount int
	LearnDataMap         map[string]*LearnData
	ActiveVoteDataMap    map[model.Snowflake]*VoteData // channel->vote. May be nil

	// Fakes
	CustomMap      *stringmap.InMemoryStringMap
	KarmaMap       *stringmap.InMemoryStringMap
	VoteMap        *stringmap.InMemoryStringMap
	Gist           *InMemoryGist
	DiscordSession *InMemoryDiscordSession
	UTCClock       *FakeUTCClock
	UTCTimer       *FakeUTCTimer

	// Real objects
	FeatureRegistry *feature.Registry

	// Controllers under test
	Handler func(api.DiscordSession, *discordgo.MessageCreate)
}

Runner is a helper that executes messages incrementally, and asserts that the global state is always what is expected.

func NewRunner

func NewRunner(t *testing.T) *Runner

NewRunner works as advertised

func (*Runner) AddUser

func (r *Runner) AddUser(user *discordgo.User)

AddUser adds a user in the test session

func (*Runner) AssertState

func (r *Runner) AssertState()

AssertState asserts all of the features against the test runner's state

func (*Runner) CastBallotAs

func (r *Runner) CastBallotAs(author *discordgo.User, channel model.Snowflake, inFavor bool)

CastBallotAs casts a ballot as the given user

func (*Runner) CastDuplicateBallotAs

func (r *Runner) CastDuplicateBallotAs(author *discordgo.User, channel model.Snowflake, inFavor bool)

CastDuplicateBallotAs casts a ballot as the user and expects no state change as a result

func (*Runner) ExpireVote

func (r *Runner) ExpireVote(channel model.Snowflake)

ExpireVote advances the clock enough that the vote expires, and fires the trigger.

func (*Runner) SendKarmaListMessage

func (r *Runner) SendKarmaListMessage(channel model.Snowflake)

SendKarmaListMessage senda a ?karmalist message to the bot

func (*Runner) SendLearnMessage

func (r *Runner) SendLearnMessage(channel model.Snowflake, message string, learnData *LearnData)

SendLearnMessage sends a learn message to the bot

func (*Runner) SendLearnMessageAs

func (r *Runner) SendLearnMessageAs(author *discordgo.User, channel model.Snowflake, message string, learnData *LearnData)

SendLearnMessageAs sends a ?learn message as the given user

func (*Runner) SendListMessage

func (r *Runner) SendListMessage(channel model.Snowflake)

SendListMessage sends a ?list message to the bot

func (*Runner) SendMessage

func (r *Runner) SendMessage(channel model.Snowflake, message, expectedResponse string)

SendMessage sends the message to the bot as the standard test user

func (*Runner) SendMessageAs

func (r *Runner) SendMessageAs(author *discordgo.User, channel model.Snowflake, message, expectedResponse string)

SendMessageAs sends a message to the bot as the given user

func (*Runner) SendMessageIgnoringResponse

func (r *Runner) SendMessageIgnoringResponse(channel model.Snowflake, message string)

SendMessageIgnoringResponse sends a message to the bot without checking the output

func (*Runner) SendMessageWithoutResponse

func (r *Runner) SendMessageWithoutResponse(channel model.Snowflake, message string)

SendMessageWithoutResponse sends a message without a response

func (*Runner) SendUnlearnMessage

func (r *Runner) SendUnlearnMessage(channel model.Snowflake, message string, call string)

SendUnlearnMessage sends an ?unlearn message

func (*Runner) SendVoteMessageAs

func (r *Runner) SendVoteMessageAs(author *discordgo.User, channel model.Snowflake)

SendVoteMessageAs sends a ?vote message to the bot as the given user

func (*Runner) SendVoteStatusMessage

func (r *Runner) SendVoteStatusMessage(channel model.Snowflake)

SendVoteStatusMessage sends a vote status message to the bot

type VoteData

type VoteData struct {
	Channel      model.Snowflake
	Author       *discordgo.User
	Message      string
	VotesFor     []model.Snowflake
	VotesAgainst []model.Snowflake
	TimestampEnd time.Time
}

VoteData contains enough information to reconstruct the status message.

func (*VoteData) Reconstruct

func (v *VoteData) Reconstruct() *model.Vote

Reconstruct creates a vote.Vote out of the local storage vote. This isn't meant to be a complete reconstruction, but rather all the info necessary for testing (mostly reproducing status lines).

Jump to

Keyboard shortcuts

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