You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

104 lines
2.7 KiB

8 years ago
8 years ago
  1. package clients
  2. import (
  3. "fmt"
  4. "net/http"
  5. "reflect"
  6. "testing"
  7. "github.com/matrix-org/go-neb/database"
  8. "github.com/matrix-org/go-neb/types"
  9. "maunium.net/go/mautrix"
  10. mevt "maunium.net/go/mautrix/event"
  11. "maunium.net/go/mautrix/id"
  12. )
  13. var commandParseTests = []struct {
  14. body string
  15. expectArgs []string
  16. }{
  17. {"!test word", []string{"word"}},
  18. {"!test two words", []string{"two", "words"}},
  19. {`!test "words with double quotes"`, []string{"words with double quotes"}},
  20. {"!test 'words with single quotes'", []string{"words with single quotes"}},
  21. {`!test 'single quotes' "double quotes"`, []string{"single quotes", "double quotes"}},
  22. {`!test ‘smart single quotes’ “smart double quotes”`, []string{"smart single quotes", "smart double quotes"}},
  23. }
  24. type MockService struct {
  25. types.DefaultService
  26. commands []types.Command
  27. }
  28. func (s *MockService) Commands(cli types.MatrixClient) []types.Command {
  29. return s.commands
  30. }
  31. type MockStore struct {
  32. database.NopStorage
  33. service types.Service
  34. }
  35. func (d *MockStore) LoadServicesForUser(userID id.UserID) ([]types.Service, error) {
  36. return []types.Service{d.service}, nil
  37. }
  38. type MockTransport struct {
  39. roundTrip func(*http.Request) (*http.Response, error)
  40. }
  41. func (t MockTransport) RoundTrip(req *http.Request) (*http.Response, error) {
  42. return t.roundTrip(req)
  43. }
  44. func TestCommandParsing(t *testing.T) {
  45. var executedCmdArgs []string
  46. cmds := []types.Command{
  47. types.Command{
  48. Path: []string{"test"},
  49. Command: func(roomID id.RoomID, userID id.UserID, args []string) (interface{}, error) {
  50. executedCmdArgs = args
  51. return nil, nil
  52. },
  53. },
  54. }
  55. s := MockService{commands: cmds}
  56. store := MockStore{service: &s}
  57. database.SetServiceDB(&store)
  58. trans := struct{ MockTransport }{}
  59. trans.roundTrip = func(*http.Request) (*http.Response, error) {
  60. return nil, fmt.Errorf("unhandled test path")
  61. }
  62. cli := &http.Client{
  63. Transport: trans,
  64. }
  65. clients := New(&store, cli)
  66. mxCli, _ := mautrix.NewClient("https://someplace.somewhere", "@service:user", "token")
  67. mxCli.Client = cli
  68. botClient := BotClient{Client: mxCli}
  69. for _, input := range commandParseTests {
  70. executedCmdArgs = []string{}
  71. content := mevt.Content{Raw: map[string]interface{}{
  72. "body": input.body,
  73. "msgtype": "m.text",
  74. }}
  75. if veryRaw, err := content.MarshalJSON(); err != nil {
  76. t.Errorf("Error marshalling JSON: %s", err)
  77. } else {
  78. content.VeryRaw = veryRaw
  79. }
  80. event := mevt.Event{
  81. Type: mevt.EventMessage,
  82. Sender: "@someone:somewhere",
  83. RoomID: "!foo:bar",
  84. Content: content,
  85. }
  86. clients.onMessageEvent(&botClient, &event)
  87. if !reflect.DeepEqual(executedCmdArgs, input.expectArgs) {
  88. t.Errorf("TestCommandParsing want %s, got %s", input.expectArgs, executedCmdArgs)
  89. }
  90. }
  91. }