Extracted scenario
This commit is contained in:
parent
c02435ccaa
commit
cc2058090c
13 changed files with 235 additions and 173 deletions
|
|
@ -1,23 +1,18 @@
|
|||
package vk
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"log"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"gitlab.com/flygrounder/go-mtg-vk/internal/scenario"
|
||||
)
|
||||
|
||||
type Handler struct {
|
||||
Sender sender
|
||||
Logger *log.Logger
|
||||
Scenario *scenario.Scenario
|
||||
SecretKey string
|
||||
GroupId int64
|
||||
ConfirmationString string
|
||||
DictPath string
|
||||
Cache cardCache
|
||||
InfoFetcher cardInfoFetcher
|
||||
}
|
||||
|
||||
type cardInfoFetcher interface {
|
||||
|
|
@ -59,62 +54,16 @@ func (h *Handler) HandleMessage(c *gin.Context) {
|
|||
case "confirmation":
|
||||
h.handleConfirmation(c, &req)
|
||||
case "message_new":
|
||||
go h.handleSearch(&req)
|
||||
go h.Scenario.HandleSearch(&scenario.UserMessage{
|
||||
Body: req.Object.Body,
|
||||
UserId: req.Object.UserId,
|
||||
})
|
||||
c.String(http.StatusOK, "ok")
|
||||
}
|
||||
}
|
||||
|
||||
func (h *Handler) handleSearch(req *messageRequest) {
|
||||
cardName, err := h.getCardNameByCommand(req.Object.Body)
|
||||
if err != nil {
|
||||
h.Sender.send(req.Object.UserId, incorrectMessage)
|
||||
h.Logger.Printf("[info] Not correct command. Message: %s user input: %s", err.Error(), req.Object.Body)
|
||||
} else if cardName == "" {
|
||||
h.Sender.send(req.Object.UserId, cardNotFoundMessage)
|
||||
h.Logger.Printf("[info] Could not find card. User input: %s", req.Object.Body)
|
||||
} else {
|
||||
message, err := h.getMessage(cardName)
|
||||
if err != nil {
|
||||
h.Sender.send(req.Object.UserId, pricesUnavailableMessage)
|
||||
h.Logger.Printf("[error] Could not find SCG prices. Message: %s card name: %s", err.Error(), cardName)
|
||||
return
|
||||
}
|
||||
h.Sender.send(req.Object.UserId, message)
|
||||
}
|
||||
}
|
||||
|
||||
func (h *Handler) handleConfirmation(c *gin.Context, req *messageRequest) {
|
||||
if (req.Type == "confirmation") && (req.GroupId == h.GroupId) {
|
||||
c.String(http.StatusOK, h.ConfirmationString)
|
||||
}
|
||||
}
|
||||
|
||||
func (h *Handler) getMessage(cardName string) (string, error) {
|
||||
val, err := h.Cache.Get(cardName)
|
||||
if err != nil {
|
||||
message, err := h.InfoFetcher.GetFormattedCardPrices(cardName)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
h.Cache.Set(cardName, message)
|
||||
return message, nil
|
||||
}
|
||||
return val, nil
|
||||
}
|
||||
|
||||
func (h *Handler) getCardNameByCommand(command string) (string, error) {
|
||||
var name string
|
||||
switch {
|
||||
case strings.HasPrefix(command, "!s"):
|
||||
split := strings.Split(command, " ")
|
||||
if len(split) < 3 {
|
||||
return "", errors.New("wrong command")
|
||||
}
|
||||
set := split[1]
|
||||
number := split[2]
|
||||
name = h.InfoFetcher.GetNameByCardId(set, number)
|
||||
default:
|
||||
name = h.InfoFetcher.GetOriginalName(command)
|
||||
}
|
||||
return name, nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,9 +1,9 @@
|
|||
package vk
|
||||
|
||||
import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestHandler_HandleMessage_Confirm(t *testing.T) {
|
||||
|
|
@ -36,86 +36,3 @@ func TestHandler_HandleMessage_NoSecretKey(t *testing.T) {
|
|||
assert.Equal(t, "", testCtx.recorder.Body.String())
|
||||
}
|
||||
|
||||
func TestHandler_handleSearch_BadCommand(t *testing.T) {
|
||||
testCtx := getTestHandlerCtx()
|
||||
testCtx.handler.handleSearch(&messageRequest{
|
||||
Object: userMessage{
|
||||
Body: "!s",
|
||||
UserId: 1,
|
||||
},
|
||||
})
|
||||
assert.Equal(t, []testMessage{
|
||||
{
|
||||
userId: 1,
|
||||
message: incorrectMessage,
|
||||
},
|
||||
}, testCtx.sender.sent)
|
||||
assert.True(t, strings.Contains(testCtx.logBuf.String(), "[info]"))
|
||||
}
|
||||
|
||||
func TestHandler_handleSearch_GoodCommand(t *testing.T) {
|
||||
testCtx := getTestHandlerCtx()
|
||||
testCtx.handler.handleSearch(&messageRequest{
|
||||
Object: userMessage{
|
||||
Body: "!s grn 228",
|
||||
UserId: 1,
|
||||
},
|
||||
})
|
||||
assert.Equal(t, []testMessage{
|
||||
{
|
||||
userId: 1,
|
||||
message: "good",
|
||||
},
|
||||
}, testCtx.sender.sent)
|
||||
}
|
||||
|
||||
func TestHandler_handleSearch_NotFoundCard(t *testing.T) {
|
||||
testCtx := getTestHandlerCtx()
|
||||
testCtx.handler.handleSearch(&messageRequest{
|
||||
Object: userMessage{
|
||||
Body: "absolutely_random_card",
|
||||
UserId: 1,
|
||||
},
|
||||
})
|
||||
assert.Equal(t, []testMessage{
|
||||
{
|
||||
userId: 1,
|
||||
message: cardNotFoundMessage,
|
||||
},
|
||||
}, testCtx.sender.sent)
|
||||
assert.True(t, strings.Contains(testCtx.logBuf.String(), "[info]"))
|
||||
}
|
||||
|
||||
func TestHandler_handleSearch_BadCard(t *testing.T) {
|
||||
testCtx := getTestHandlerCtx()
|
||||
testCtx.handler.handleSearch(&messageRequest{
|
||||
Object: userMessage{
|
||||
Body: "bad",
|
||||
UserId: 1,
|
||||
},
|
||||
})
|
||||
assert.Equal(t, []testMessage{
|
||||
{
|
||||
userId: 1,
|
||||
message: pricesUnavailableMessage,
|
||||
},
|
||||
}, testCtx.sender.sent)
|
||||
assert.True(t, strings.Contains(testCtx.logBuf.String(), "[error]"))
|
||||
}
|
||||
func TestHandler_handleSearch_Uncached(t *testing.T) {
|
||||
testCtx := getTestHandlerCtx()
|
||||
testCtx.handler.handleSearch(&messageRequest{
|
||||
Object: userMessage{
|
||||
Body: "uncached",
|
||||
UserId: 1,
|
||||
},
|
||||
})
|
||||
assert.Equal(t, []testMessage{
|
||||
{
|
||||
userId: 1,
|
||||
message: "uncached",
|
||||
},
|
||||
}, testCtx.sender.sent)
|
||||
msg, _ := testCtx.handler.Cache.Get("uncached")
|
||||
assert.Equal(t, "uncached", msg)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -31,7 +31,7 @@ type errorResponse struct {
|
|||
ErrorMsg string `json:"error_msg"`
|
||||
}
|
||||
|
||||
func (s *ApiSender) send(userId int64, message string) {
|
||||
func (s *ApiSender) Send(userId int64, message string) {
|
||||
randomId := rand.Int63()
|
||||
params := []string{
|
||||
"access_token=" + s.Token,
|
||||
|
|
|
|||
|
|
@ -25,7 +25,7 @@ func TestApiSender_Send_OK(t *testing.T) {
|
|||
).ParamPresent("random_id").Reply(http.StatusOK)
|
||||
|
||||
sender := ApiSender{Token: "token"}
|
||||
sender.send(1, "msg")
|
||||
sender.Send(1, "msg")
|
||||
assert.False(t, gock.HasUnmatchedRequest())
|
||||
}
|
||||
|
||||
|
|
@ -39,7 +39,7 @@ func TestApiSender_Send_NotOK(t *testing.T) {
|
|||
Token: "token",
|
||||
Logger: log.New(b, "", 0),
|
||||
}
|
||||
sender.send(1, "msg")
|
||||
sender.Send(1, "msg")
|
||||
assert.True(t, strings.Contains(b.String(), "[error]"))
|
||||
}
|
||||
|
||||
|
|
@ -60,6 +60,6 @@ func TestApiSender_Send_ErrorCode(t *testing.T) {
|
|||
Token: "token",
|
||||
Logger: log.New(b, "", 0),
|
||||
}
|
||||
sender.send(1, "msg")
|
||||
sender.Send(1, "msg")
|
||||
assert.True(t, strings.Contains(b.String(), "[error]"))
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,19 +0,0 @@
|
|||
package vk
|
||||
|
||||
import "errors"
|
||||
|
||||
type testCache struct {
|
||||
table map[string]string
|
||||
}
|
||||
|
||||
func (t *testCache) Get(cardName string) (string, error) {
|
||||
msg, ok := t.table[cardName]
|
||||
if !ok {
|
||||
return "", errors.New("test")
|
||||
}
|
||||
return msg, nil
|
||||
}
|
||||
|
||||
func (t *testCache) Set(cardName string, message string) {
|
||||
t.table[cardName] = message
|
||||
}
|
||||
|
|
@ -3,37 +3,26 @@ package vk
|
|||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"github.com/gin-gonic/gin"
|
||||
"log"
|
||||
"net/http/httptest"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"gitlab.com/flygrounder/go-mtg-vk/internal/scenario"
|
||||
)
|
||||
|
||||
type testCtx struct {
|
||||
handler *Handler
|
||||
recorder *httptest.ResponseRecorder
|
||||
sender *testSender
|
||||
logBuf *bytes.Buffer
|
||||
}
|
||||
|
||||
func getTestHandlerCtx() testCtx {
|
||||
sender := &testSender{}
|
||||
buf := &bytes.Buffer{}
|
||||
s := scenario.GetTestScenarioCtx()
|
||||
return testCtx{
|
||||
logBuf: buf,
|
||||
handler: &Handler{
|
||||
SecretKey: "sec",
|
||||
GroupId: 10,
|
||||
ConfirmationString: "con",
|
||||
Sender: sender,
|
||||
Logger: log.New(buf, "", 0),
|
||||
InfoFetcher: &testInfoFetcher{},
|
||||
Cache: &testCache{
|
||||
table: map[string]string{
|
||||
"good": "good",
|
||||
},
|
||||
},
|
||||
Scenario: s.Scenario,
|
||||
},
|
||||
sender: sender,
|
||||
recorder: httptest.NewRecorder(),
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,25 +0,0 @@
|
|||
package vk
|
||||
|
||||
import (
|
||||
"errors"
|
||||
)
|
||||
|
||||
type testInfoFetcher struct{}
|
||||
|
||||
func (t *testInfoFetcher) GetFormattedCardPrices(name string) (string, error) {
|
||||
if name == "good" || name == "uncached" {
|
||||
return name, nil
|
||||
}
|
||||
return "", errors.New("test")
|
||||
}
|
||||
|
||||
func (t *testInfoFetcher) GetNameByCardId(_ string, _ string) string {
|
||||
return "good"
|
||||
}
|
||||
|
||||
func (t *testInfoFetcher) GetOriginalName(name string) string {
|
||||
if name == "good" || name == "bad" || name == "uncached" {
|
||||
return name
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
|
@ -1,17 +0,0 @@
|
|||
package vk
|
||||
|
||||
type testSender struct {
|
||||
sent []testMessage
|
||||
}
|
||||
|
||||
type testMessage struct {
|
||||
userId int64
|
||||
message string
|
||||
}
|
||||
|
||||
func (s *testSender) send(userId int64, message string) {
|
||||
s.sent = append(s.sent, testMessage{
|
||||
userId: userId,
|
||||
message: message,
|
||||
})
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue