| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598 |
- package foodgroup
- import (
- "context"
- "math"
- "testing"
- "github.com/mk6i/open-oscar-server/state"
- "github.com/mk6i/open-oscar-server/wire"
- "github.com/stretchr/testify/assert"
- "github.com/stretchr/testify/mock"
- )
- func TestChatService_ChannelMsgToHost(t *testing.T) {
- cases := []struct {
- // name is the unit test name
- name string
- // instance is the session of the user sending the chat message
- instance *state.SessionInstance
- // inputSNAC is the SNAC sent by the sender client
- inputSNAC wire.SNACMessage
- // mockParams is the list of params sent to mocks that satisfy this
- // method's dependencies
- mockParams mockParams
- // expectSNACToParticipants is the message the server broadcast to chat
- // room participants (except the sender)
- expectSNACToParticipants wire.SNACMessage
- expectOutput *wire.SNACMessage
- // randRollDie generates result of rolling a die
- randRollDie func(sides int) int
- wantErr error
- }{
- {
- name: "send chat room message, expect acknowledgement to sender client",
- instance: newTestInstance("user_sending_chat_msg", sessOptCannedSignonTime,
- sessOptChatRoomCookie("the-chat-cookie")),
- inputSNAC: wire.SNACMessage{
- Frame: wire.SNACFrame{
- RequestID: 1234,
- },
- Body: wire.SNAC_0x0E_0x05_ChatChannelMsgToHost{
- Cookie: 1234,
- Channel: 14,
- TLVRestBlock: wire.TLVRestBlock{
- TLVList: wire.TLVList{
- {
- Tag: wire.ChatTLVPublicWhisperFlag,
- Value: []byte{},
- },
- {
- Tag: wire.ChatTLVEnableReflectionFlag,
- Value: []byte{},
- },
- wire.NewTLVBE(wire.ChatTLVMessageInfo, wire.TLVRestBlock{
- TLVList: wire.TLVList{
- wire.NewTLVBE(wire.ChatTLVMessageInfoText,
- "<HTML><BODY BGCOLOR=\"#ffffff\"><FONT LANG=\"0\">Hello</FONT></BODY></HTML>"),
- },
- }),
- },
- },
- },
- },
- mockParams: mockParams{
- chatMessageRelayerParams: chatMessageRelayerParams{
- chatRelayToAllExceptParams: chatRelayToAllExceptParams{
- {
- screenName: state.NewIdentScreenName("user_sending_chat_msg"),
- cookie: "the-chat-cookie",
- message: wire.SNACMessage{
- Frame: wire.SNACFrame{
- FoodGroup: wire.Chat,
- SubGroup: wire.ChatChannelMsgToClient,
- },
- Body: wire.SNAC_0x0E_0x06_ChatChannelMsgToClient{
- Cookie: 1234,
- Channel: 14,
- TLVRestBlock: wire.TLVRestBlock{
- TLVList: wire.TLVList{
- wire.NewTLVBE(wire.ChatTLVSenderInformation,
- newTestInstance("user_sending_chat_msg", sessOptCannedSignonTime).Session().TLVUserInfo()),
- wire.NewTLVBE(wire.ChatTLVPublicWhisperFlag, []byte{}),
- wire.NewTLVBE(wire.ChatTLVMessageInfo, wire.TLVRestBlock{
- TLVList: wire.TLVList{
- wire.NewTLVBE(wire.ChatTLVMessageInfoText,
- "<HTML><BODY BGCOLOR=\"#ffffff\"><FONT LANG=\"0\">Hello</FONT></BODY></HTML>"),
- },
- }),
- },
- },
- },
- },
- },
- },
- },
- },
- expectOutput: &wire.SNACMessage{
- Frame: wire.SNACFrame{
- FoodGroup: wire.Chat,
- SubGroup: wire.ChatChannelMsgToClient,
- RequestID: 1234,
- },
- Body: wire.SNAC_0x0E_0x06_ChatChannelMsgToClient{
- Cookie: 1234,
- Channel: 14,
- TLVRestBlock: wire.TLVRestBlock{
- TLVList: wire.TLVList{
- wire.NewTLVBE(wire.ChatTLVSenderInformation,
- newTestInstance("user_sending_chat_msg", sessOptCannedSignonTime).Session().TLVUserInfo()),
- wire.NewTLVBE(wire.ChatTLVPublicWhisperFlag, []byte{}),
- wire.NewTLVBE(wire.ChatTLVMessageInfo, wire.TLVRestBlock{
- TLVList: wire.TLVList{
- wire.NewTLVBE(wire.ChatTLVMessageInfoText,
- "<HTML><BODY BGCOLOR=\"#ffffff\"><FONT LANG=\"0\">Hello</FONT></BODY></HTML>"),
- },
- }),
- },
- },
- },
- },
- },
- {
- name: "send chat whisper",
- instance: newTestInstance("user_sending_chat_msg", sessOptCannedSignonTime,
- sessOptChatRoomCookie("the-chat-cookie")),
- inputSNAC: wire.SNACMessage{
- Frame: wire.SNACFrame{
- RequestID: 1234,
- },
- Body: wire.SNAC_0x0E_0x05_ChatChannelMsgToHost{
- Cookie: 1234,
- Channel: 14,
- TLVRestBlock: wire.TLVRestBlock{
- TLVList: wire.TLVList{
- wire.NewTLVBE(wire.ChatTLVWhisperToUser, "them"),
- wire.NewTLVBE(wire.ChatTLVMessageInfo, wire.TLVRestBlock{
- TLVList: wire.TLVList{
- wire.NewTLVBE(wire.ChatTLVMessageInfoText,
- "<HTML><BODY BGCOLOR=\"#ffffff\"><FONT LANG=\"0\">Hello</FONT></BODY></HTML>"),
- },
- }),
- },
- },
- },
- },
- mockParams: mockParams{
- chatMessageRelayerParams: chatMessageRelayerParams{
- chatRelayToScreenNameParams: chatRelayToScreenNameParams{
- {
- screenName: state.NewIdentScreenName("them"),
- cookie: "the-chat-cookie",
- message: wire.SNACMessage{
- Frame: wire.SNACFrame{
- FoodGroup: wire.Chat,
- SubGroup: wire.ChatChannelMsgToClient,
- },
- Body: wire.SNAC_0x0E_0x06_ChatChannelMsgToClient{
- Cookie: 1234,
- Channel: 14,
- TLVRestBlock: wire.TLVRestBlock{
- TLVList: wire.TLVList{
- wire.NewTLVBE(wire.ChatTLVSenderInformation,
- newTestInstance("user_sending_chat_msg", sessOptCannedSignonTime).Session().TLVUserInfo()),
- wire.NewTLVBE(wire.ChatTLVMessageInfo, wire.TLVRestBlock{
- TLVList: wire.TLVList{
- wire.NewTLVBE(wire.ChatTLVMessageInfoText,
- "<HTML><BODY BGCOLOR=\"#ffffff\"><FONT LANG=\"0\">Hello</FONT></BODY></HTML>"),
- },
- }),
- },
- },
- },
- },
- },
- },
- },
- },
- expectOutput: nil,
- },
- {
- name: "send die roll",
- instance: newTestInstance("user_sending_chat_msg", sessOptCannedSignonTime,
- sessOptChatRoomCookie("the-chat-cookie")),
- inputSNAC: wire.SNACMessage{
- Frame: wire.SNACFrame{
- RequestID: 1234,
- },
- Body: wire.SNAC_0x0E_0x05_ChatChannelMsgToHost{
- Cookie: 1234,
- Channel: 14,
- TLVRestBlock: wire.TLVRestBlock{
- TLVList: wire.TLVList{
- {
- Tag: wire.ChatTLVPublicWhisperFlag,
- Value: []byte{},
- },
- {
- Tag: wire.ChatTLVEnableReflectionFlag,
- Value: []byte{},
- },
- wire.NewTLVBE(wire.ChatTLVMessageInfo, wire.TLVRestBlock{
- TLVList: wire.TLVList{
- wire.NewTLVBE(wire.ChatTLVMessageInfoText,
- "<HTML><BODY BGCOLOR=\"#ffffff\"><FONT LANG=\"0\">//roll-dice3-sides8</FONT></BODY></HTML>"),
- },
- }),
- },
- },
- },
- },
- mockParams: mockParams{
- chatMessageRelayerParams: chatMessageRelayerParams{
- chatRelayToAllExceptParams: chatRelayToAllExceptParams{
- {
- screenName: state.NewIdentScreenName("user_sending_chat_msg"),
- cookie: "the-chat-cookie",
- message: wire.SNACMessage{
- Frame: wire.SNACFrame{
- FoodGroup: wire.Chat,
- SubGroup: wire.ChatChannelMsgToClient,
- },
- Body: wire.SNAC_0x0E_0x06_ChatChannelMsgToClient{
- Cookie: 1234,
- Channel: 14,
- TLVRestBlock: wire.TLVRestBlock{
- TLVList: wire.TLVList{
- wire.NewTLVBE(wire.ChatTLVSenderInformation, sessOnlineHost.Session().TLVUserInfo()),
- wire.NewTLVBE(wire.ChatTLVPublicWhisperFlag, []byte{}),
- wire.NewTLVBE(wire.ChatTLVMessageInfo, wire.TLVRestBlock{
- TLVList: wire.TLVList{
- wire.NewTLVBE(wire.ChatTLVMessageInfoEncoding, "us-ascii"),
- wire.NewTLVBE(wire.ChatTLVMessageInfoLang, "en"),
- wire.NewTLVBE(wire.ChatTLVMessageInfoText,
- "<HTML><BODY BGCOLOR=\"#ffffff\"><FONT LANG=\"0\">user_sending_chat_msg rolled 3 8-sided dice: 2 4 8</FONT></BODY></HTML>"),
- },
- }),
- },
- },
- },
- },
- },
- },
- },
- },
- expectOutput: &wire.SNACMessage{
- Frame: wire.SNACFrame{
- FoodGroup: wire.Chat,
- SubGroup: wire.ChatChannelMsgToClient,
- RequestID: 1234,
- },
- Body: wire.SNAC_0x0E_0x06_ChatChannelMsgToClient{
- Cookie: 1234,
- Channel: 14,
- TLVRestBlock: wire.TLVRestBlock{
- TLVList: wire.TLVList{
- wire.NewTLVBE(wire.ChatTLVSenderInformation, sessOnlineHost.Session().TLVUserInfo()),
- wire.NewTLVBE(wire.ChatTLVPublicWhisperFlag, []byte{}),
- wire.NewTLVBE(wire.ChatTLVMessageInfo, wire.TLVRestBlock{
- TLVList: wire.TLVList{
- wire.NewTLVBE(wire.ChatTLVMessageInfoEncoding, "us-ascii"),
- wire.NewTLVBE(wire.ChatTLVMessageInfoLang, "en"),
- wire.NewTLVBE(wire.ChatTLVMessageInfoText,
- "<HTML><BODY BGCOLOR=\"#ffffff\"><FONT LANG=\"0\">user_sending_chat_msg rolled 3 8-sided dice: 2 4 8</FONT></BODY></HTML>"),
- },
- }),
- },
- },
- },
- },
- randRollDie: func() func(sides int) int {
- // return multiples of 2 starting with 2
- val := 2
- return func(sides int) int {
- ret := val
- val *= 2
- return ret
- }
- }(),
- },
- {
- name: "send chat room message with macOS client 4.0.9 bug containing bad channel ID, expect message to " +
- "client on MIME channel",
- instance: newTestInstance("user_sending_chat_msg", sessOptCannedSignonTime,
- sessOptChatRoomCookie("the-chat-cookie")),
- inputSNAC: wire.SNACMessage{
- Frame: wire.SNACFrame{
- RequestID: 1234,
- },
- Body: wire.SNAC_0x0E_0x05_ChatChannelMsgToHost{
- Cookie: 1234,
- Channel: math.MaxUint16,
- TLVRestBlock: wire.TLVRestBlock{
- TLVList: wire.TLVList{
- {
- Tag: wire.ChatTLVPublicWhisperFlag,
- Value: []byte{},
- },
- wire.NewTLVBE(wire.ChatTLVMessageInfo, wire.TLVRestBlock{
- TLVList: wire.TLVList{
- wire.NewTLVBE(wire.ChatTLVMessageInfoText,
- "<HTML><BODY BGCOLOR=\"#ffffff\"><FONT LANG=\"0\">Hello</FONT></BODY></HTML>"),
- },
- }),
- },
- },
- },
- },
- mockParams: mockParams{
- chatMessageRelayerParams: chatMessageRelayerParams{
- chatRelayToAllExceptParams: chatRelayToAllExceptParams{
- {
- screenName: state.NewIdentScreenName("user_sending_chat_msg"),
- cookie: "the-chat-cookie",
- message: wire.SNACMessage{
- Frame: wire.SNACFrame{
- FoodGroup: wire.Chat,
- SubGroup: wire.ChatChannelMsgToClient,
- },
- Body: wire.SNAC_0x0E_0x06_ChatChannelMsgToClient{
- Cookie: 1234,
- Channel: wire.ICBMChannelMIME,
- TLVRestBlock: wire.TLVRestBlock{
- TLVList: wire.TLVList{
- wire.NewTLVBE(wire.ChatTLVSenderInformation,
- newTestInstance("user_sending_chat_msg", sessOptCannedSignonTime).Session().TLVUserInfo()),
- wire.NewTLVBE(wire.ChatTLVPublicWhisperFlag, []byte{}),
- wire.NewTLVBE(wire.ChatTLVMessageInfo, wire.TLVRestBlock{
- TLVList: wire.TLVList{
- wire.NewTLVBE(wire.ChatTLVMessageInfoText,
- "<HTML><BODY BGCOLOR=\"#ffffff\"><FONT LANG=\"0\">Hello</FONT></BODY></HTML>"),
- },
- }),
- },
- },
- },
- },
- },
- },
- },
- },
- expectOutput: nil,
- },
- {
- name: "send chat room message, don't expect acknowledgement to sender client",
- instance: newTestInstance("user_sending_chat_msg", sessOptCannedSignonTime,
- sessOptChatRoomCookie("the-chat-cookie")),
- inputSNAC: wire.SNACMessage{
- Frame: wire.SNACFrame{
- RequestID: 1234,
- },
- Body: wire.SNAC_0x0E_0x05_ChatChannelMsgToHost{
- Cookie: 1234,
- Channel: 14,
- TLVRestBlock: wire.TLVRestBlock{
- TLVList: wire.TLVList{
- {
- Tag: wire.ChatTLVPublicWhisperFlag,
- Value: []byte{},
- },
- wire.NewTLVBE(wire.ChatTLVMessageInfo, wire.TLVRestBlock{
- TLVList: wire.TLVList{
- wire.NewTLVBE(wire.ChatTLVMessageInfoText,
- "<HTML><BODY BGCOLOR=\"#ffffff\"><FONT LANG=\"0\">Hello</FONT></BODY></HTML>"),
- },
- }),
- },
- },
- },
- },
- mockParams: mockParams{
- chatMessageRelayerParams: chatMessageRelayerParams{
- chatRelayToAllExceptParams: chatRelayToAllExceptParams{
- {
- screenName: state.NewIdentScreenName("user_sending_chat_msg"),
- cookie: "the-chat-cookie",
- message: wire.SNACMessage{
- Frame: wire.SNACFrame{
- FoodGroup: wire.Chat,
- SubGroup: wire.ChatChannelMsgToClient,
- },
- Body: wire.SNAC_0x0E_0x06_ChatChannelMsgToClient{
- Cookie: 1234,
- Channel: 14,
- TLVRestBlock: wire.TLVRestBlock{
- TLVList: wire.TLVList{
- wire.NewTLVBE(wire.ChatTLVSenderInformation,
- newTestInstance("user_sending_chat_msg", sessOptCannedSignonTime).Session().TLVUserInfo()),
- wire.NewTLVBE(wire.ChatTLVPublicWhisperFlag, []byte{}),
- wire.NewTLVBE(wire.ChatTLVMessageInfo, wire.TLVRestBlock{
- TLVList: wire.TLVList{
- wire.NewTLVBE(wire.ChatTLVMessageInfoText,
- "<HTML><BODY BGCOLOR=\"#ffffff\"><FONT LANG=\"0\">Hello</FONT></BODY></HTML>"),
- },
- }),
- },
- },
- },
- },
- },
- },
- },
- },
- },
- {
- name: "send chat room message, expect that TLVs that crash macos client",
- instance: newTestInstance("user_sending_chat_msg", sessOptCannedSignonTime,
- sessOptChatRoomCookie("the-chat-cookie")),
- inputSNAC: wire.SNACMessage{
- Frame: wire.SNACFrame{
- RequestID: 1234,
- },
- Body: wire.SNAC_0x0E_0x05_ChatChannelMsgToHost{
- Cookie: 1234,
- Channel: 14,
- TLVRestBlock: wire.TLVRestBlock{
- TLVList: wire.TLVList{
- {
- Tag: wire.ChatTLVPublicWhisperFlag,
- Value: []byte{},
- },
- wire.NewTLVBE(wire.ChatTLVMessageInfo, wire.TLVRestBlock{
- TLVList: wire.TLVList{
- wire.NewTLVBE(wire.ChatTLVMessageInfoText,
- "<HTML><BODY BGCOLOR=\"#ffffff\"><FONT LANG=\"0\">Hello</FONT></BODY></HTML>"),
- wire.NewTLVBE(0x04, "remove me"),
- wire.NewTLVBE(0x05, "remove me"),
- },
- }),
- },
- },
- },
- },
- mockParams: mockParams{
- chatMessageRelayerParams: chatMessageRelayerParams{
- chatRelayToAllExceptParams: chatRelayToAllExceptParams{
- {
- screenName: state.NewIdentScreenName("user_sending_chat_msg"),
- cookie: "the-chat-cookie",
- message: wire.SNACMessage{
- Frame: wire.SNACFrame{
- FoodGroup: wire.Chat,
- SubGroup: wire.ChatChannelMsgToClient,
- },
- Body: wire.SNAC_0x0E_0x06_ChatChannelMsgToClient{
- Cookie: 1234,
- Channel: 14,
- TLVRestBlock: wire.TLVRestBlock{
- TLVList: wire.TLVList{
- wire.NewTLVBE(wire.ChatTLVSenderInformation,
- newTestInstance("user_sending_chat_msg", sessOptCannedSignonTime).Session().TLVUserInfo()),
- wire.NewTLVBE(wire.ChatTLVPublicWhisperFlag, []byte{}),
- wire.NewTLVBE(wire.ChatTLVMessageInfo, wire.TLVRestBlock{
- TLVList: wire.TLVList{
- wire.NewTLVBE(wire.ChatTLVMessageInfoText,
- "<HTML><BODY BGCOLOR=\"#ffffff\"><FONT LANG=\"0\">Hello</FONT></BODY></HTML>"),
- },
- }),
- },
- },
- },
- },
- },
- },
- },
- },
- },
- {
- name: "fix malformed Kopete encoding ISO 8859 to ISO-8859-1",
- instance: newTestInstance("user_sending_chat_msg", sessOptCannedSignonTime,
- sessOptChatRoomCookie("the-chat-cookie")),
- inputSNAC: wire.SNACMessage{
- Frame: wire.SNACFrame{
- RequestID: 1234,
- },
- Body: wire.SNAC_0x0E_0x05_ChatChannelMsgToHost{
- Cookie: 1234,
- Channel: 14,
- TLVRestBlock: wire.TLVRestBlock{
- TLVList: wire.TLVList{
- {
- Tag: wire.ChatTLVPublicWhisperFlag,
- Value: []byte{},
- },
- wire.NewTLVBE(wire.ChatTLVMessageInfo, wire.TLVRestBlock{
- TLVList: wire.TLVList{
- wire.NewTLVBE(wire.ChatTLVMessageInfoText,
- "<HTML><BODY BGCOLOR=\"#ffffff\"><FONT LANG=\"0\">Hello</FONT></BODY></HTML>"),
- wire.NewTLVBE(wire.ChatTLVMessageInfoEncoding, "ISO 8859"),
- },
- }),
- },
- },
- },
- },
- mockParams: mockParams{
- chatMessageRelayerParams: chatMessageRelayerParams{
- chatRelayToAllExceptParams: chatRelayToAllExceptParams{
- {
- screenName: state.NewIdentScreenName("user_sending_chat_msg"),
- cookie: "the-chat-cookie",
- message: wire.SNACMessage{
- Frame: wire.SNACFrame{
- FoodGroup: wire.Chat,
- SubGroup: wire.ChatChannelMsgToClient,
- },
- Body: wire.SNAC_0x0E_0x06_ChatChannelMsgToClient{
- Cookie: 1234,
- Channel: 14,
- TLVRestBlock: wire.TLVRestBlock{
- TLVList: wire.TLVList{
- wire.NewTLVBE(wire.ChatTLVSenderInformation,
- newTestInstance("user_sending_chat_msg", sessOptCannedSignonTime).Session().TLVUserInfo()),
- wire.NewTLVBE(wire.ChatTLVPublicWhisperFlag, []byte{}),
- wire.NewTLVBE(wire.ChatTLVMessageInfo, wire.TLVRestBlock{
- TLVList: wire.TLVList{
- wire.NewTLVBE(wire.ChatTLVMessageInfoText,
- "<HTML><BODY BGCOLOR=\"#ffffff\"><FONT LANG=\"0\">Hello</FONT></BODY></HTML>"),
- wire.NewTLVBE(wire.ChatTLVMessageInfoEncoding, "ISO-8859-1"),
- },
- }),
- },
- },
- },
- },
- },
- },
- },
- },
- },
- }
- for _, tc := range cases {
- t.Run(tc.name, func(t *testing.T) {
- chatMessageRelayer := newMockChatMessageRelayer(t)
- for _, params := range tc.mockParams.chatRelayToAllExceptParams {
- chatMessageRelayer.EXPECT().
- RelayToAllExcept(mock.Anything, params.cookie, params.screenName, params.message)
- }
- for _, params := range tc.mockParams.chatRelayToScreenNameParams {
- chatMessageRelayer.EXPECT().
- RelayToScreenName(mock.Anything, params.cookie, params.screenName, params.message)
- }
- svc := NewChatService(chatMessageRelayer)
- svc.randRollDie = tc.randRollDie
- outputSNAC, err := svc.ChannelMsgToHost(context.Background(), tc.instance, tc.inputSNAC.Frame,
- tc.inputSNAC.Body.(wire.SNAC_0x0E_0x05_ChatChannelMsgToHost))
- assert.ErrorIs(t, err, tc.wantErr)
- assert.Equal(t, tc.expectOutput, outputSNAC)
- })
- }
- }
- func TestParseDiceCommand(t *testing.T) {
- tests := []struct {
- input []byte
- expectedValid bool
- expectedDice int
- expectedSides int
- }{
- {[]byte("//roll-sides999-dice15"), true, 15, 999},
- {[]byte("//roll-sides999-dice15 "), true, 15, 999},
- {[]byte("//roll-SIDES999-DICE15"), false, 0, 0},
- {[]byte("//roll-sides999-sides15"), false, 0, 0},
- {[]byte("//roll-sides999-dice15 as I was saying"), false, 0, 0},
- {[]byte("i'm gonna roll some dice //roll-sides999-dice15"), false, 0, 0},
- {[]byte("//roll-dice15-sides999"), true, 15, 999},
- {[]byte("//roll-dice15"), true, 15, 6},
- {[]byte("//roll-dice0"), false, 0, 0},
- {[]byte("//roll-sides0"), false, 0, 0},
- {[]byte("//roll-sides999"), true, 2, 999},
- {[]byte("//roll-dice16"), false, 0, 0},
- {[]byte("//roll-sides1000"), false, 0, 0},
- {[]byte("//roll-dice-5"), false, 0, 0},
- {[]byte("//roll-sides-9"), false, 0, 0},
- {[]byte("//roll"), true, 2, 6},
- {[]byte("invalid input"), false, 0, 0},
- }
- for _, test := range tests {
- t.Run(string(test.input), func(t *testing.T) {
- valid, dice, sides := parseDiceCommand(test.input)
- if valid != test.expectedValid {
- t.Errorf("For input '%s', expected valid = %v, got %v", test.input, test.expectedValid, valid)
- }
- if dice != test.expectedDice {
- t.Errorf("For input '%s', expected dice = %d, got %d", test.input, test.expectedDice, dice)
- }
- if sides != test.expectedSides {
- t.Errorf("For input '%s', expected sides = %d, got %d", test.input, test.expectedSides, sides)
- }
- })
- }
- }
|