actions_test.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. package server_test
  2. import (
  3. "encoding/json"
  4. "github.com/felamaslen/gmus-backend/pkg/server"
  5. . "github.com/onsi/ginkgo"
  6. . "github.com/onsi/gomega"
  7. "github.com/alicebob/miniredis/v2"
  8. "github.com/elliotchance/redismock"
  9. "github.com/go-redis/redis"
  10. )
  11. func newTestRedis() *redismock.ClientMock {
  12. mr, err := miniredis.Run()
  13. if err != nil {
  14. panic(err)
  15. }
  16. client := redis.NewClient(&redis.Options{
  17. Addr: mr.Addr(),
  18. })
  19. return redismock.NewNiceMock(client)
  20. }
  21. var _ = Describe("Server actions", func() {
  22. var rdb *redismock.ClientMock
  23. BeforeEach(func() {
  24. rdb = newTestRedis()
  25. rdb.On("Publish").Return(redis.NewIntResult(0, nil))
  26. })
  27. Describe("PublishActionFromClient", func() {
  28. Describe("state set actions", func() {
  29. Context("when the action is valid", func() {
  30. var action server.Action
  31. json.Unmarshal([]byte(actionStateSetValid), &action)
  32. myClient := "my-client"
  33. action.FromClient = &myClient
  34. songId := 123
  35. expectedAction := server.Action{
  36. Type: server.StateSet,
  37. FromClient: &myClient,
  38. Payload: server.MusicPlayer{
  39. SongId: &songId,
  40. Playing: true,
  41. CurrentTime: 94,
  42. SeekTime: -1,
  43. Master: "some-master-client",
  44. Queue: &[]int{},
  45. },
  46. }
  47. expectedActionString, jsonErr := json.Marshal(expectedAction)
  48. if jsonErr != nil {
  49. panic(jsonErr)
  50. }
  51. BeforeEach(func() {
  52. rdb.On("Publish", server.TOPIC_BROADCAST, expectedActionString).Return(redis.NewIntResult(0, nil))
  53. })
  54. It("should publish the action to the redis pubsub", func() {
  55. err := server.PublishActionFromClient(rdb, &action)
  56. Expect(rdb.Calls).NotTo(BeEmpty())
  57. Expect(err).To(BeNil())
  58. })
  59. })
  60. Context("when the song ID is non-positive", func() {
  61. var action server.Action
  62. json.Unmarshal([]byte(actionStateSetIdNonPositive), &action)
  63. It("should not publish a message", func() {
  64. err := server.PublishActionFromClient(rdb, &action)
  65. Expect(rdb.Calls).To(BeEmpty())
  66. Expect(err).NotTo(BeNil())
  67. })
  68. })
  69. Context("when the song ID is null", func() {
  70. var action server.Action
  71. json.Unmarshal([]byte(actionStateSetSongIdNull), &action)
  72. expectedAction := server.Action{
  73. Type: server.StateSet,
  74. Payload: server.MusicPlayer{
  75. SongId: nil,
  76. Playing: false,
  77. CurrentTime: 0,
  78. SeekTime: -1,
  79. Master: "some-master-client",
  80. Queue: &[]int{},
  81. },
  82. }
  83. expectedActionString, jsonErr := json.Marshal(expectedAction)
  84. if jsonErr != nil {
  85. panic(jsonErr)
  86. }
  87. BeforeEach(func() {
  88. rdb.On("Publish", server.TOPIC_BROADCAST, expectedActionString).Return(redis.NewIntResult(0, nil))
  89. })
  90. It("should publish a message", func() {
  91. err := server.PublishActionFromClient(rdb, &action)
  92. Expect(rdb.Calls).NotTo(BeEmpty())
  93. Expect(err).To(BeNil())
  94. })
  95. })
  96. Context("when the current time is negative", func() {
  97. var action server.Action
  98. json.Unmarshal([]byte(actionStateSetCurrentTimeNegative), &action)
  99. It("should not publish a message", func() {
  100. err := server.PublishActionFromClient(rdb, &action)
  101. Expect(rdb.Calls).To(BeEmpty())
  102. Expect(err).NotTo(BeNil())
  103. })
  104. })
  105. Context("when the seek time is less than -1", func() {
  106. var action server.Action
  107. json.Unmarshal([]byte(actionStateSetSeekTimeTooNegative), &action)
  108. It("should not publish a message", func() {
  109. err := server.PublishActionFromClient(rdb, &action)
  110. Expect(rdb.Calls).To(BeEmpty())
  111. Expect(err).NotTo(BeNil())
  112. })
  113. })
  114. Context("when the master is empty", func() {
  115. var action server.Action
  116. json.Unmarshal([]byte(actionStateSetMasterEmpty), &action)
  117. It("should not publish a message", func() {
  118. err := server.PublishActionFromClient(rdb, &action)
  119. Expect(rdb.Calls).To(BeEmpty())
  120. Expect(err).NotTo(BeNil())
  121. })
  122. })
  123. })
  124. Describe("when the action is unrecognised", func() {
  125. var action server.Action
  126. err := json.Unmarshal([]byte(actionUnrecognised), &action)
  127. if err != nil {
  128. panic(err)
  129. }
  130. BeforeEach(func() {
  131. rdb.On("Publish", server.TOPIC_BROADCAST, "").Return(redis.NewIntResult(0, nil))
  132. })
  133. It("should not publish a message", func() {
  134. err := server.PublishActionFromClient(rdb, &action)
  135. Expect(rdb.Calls).To(BeEmpty())
  136. Expect(err).NotTo(BeNil())
  137. })
  138. })
  139. })
  140. })