actions_test.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. playing := true
  36. currentTime := float32(94)
  37. seekTime := float32(-1)
  38. expectedAction := server.Action{
  39. Type: server.StateSet,
  40. FromClient: &myClient,
  41. Payload: server.MusicPlayer{
  42. SongId: &songId,
  43. Playing: &playing,
  44. CurrentTime: &currentTime,
  45. SeekTime: &seekTime,
  46. Master: "some-master-client",
  47. ActiveClients: &[]string{},
  48. Queue: &[]int{},
  49. },
  50. }
  51. expectedActionString, jsonErr := json.Marshal(expectedAction)
  52. if jsonErr != nil {
  53. panic(jsonErr)
  54. }
  55. BeforeEach(func() {
  56. rdb.On("Publish", server.TOPIC_BROADCAST, expectedActionString).Return(redis.NewIntResult(0, nil))
  57. })
  58. It("should publish the action to the redis pubsub", func() {
  59. err := server.PublishActionFromClient(rdb, &action)
  60. Expect(rdb.Calls).NotTo(BeEmpty())
  61. Expect(err).To(BeNil())
  62. })
  63. })
  64. Context("when the song ID is non-positive", func() {
  65. var action server.Action
  66. json.Unmarshal([]byte(actionStateSetIdNonPositive), &action)
  67. It("should not publish a message", func() {
  68. err := server.PublishActionFromClient(rdb, &action)
  69. Expect(rdb.Calls).To(BeEmpty())
  70. Expect(err).NotTo(BeNil())
  71. })
  72. })
  73. Context("when the song ID is null", func() {
  74. var action server.Action
  75. json.Unmarshal([]byte(actionStateSetSongIdNull), &action)
  76. playing := false
  77. currentTime := float32(0)
  78. seekTime := float32(-1)
  79. expectedAction := server.Action{
  80. Type: server.StateSet,
  81. Payload: server.MusicPlayer{
  82. SongId: nil,
  83. Playing: &playing,
  84. CurrentTime: &currentTime,
  85. SeekTime: &seekTime,
  86. Master: "some-master-client",
  87. ActiveClients: &[]string{},
  88. Queue: &[]int{},
  89. },
  90. }
  91. expectedActionString, jsonErr := json.Marshal(expectedAction)
  92. if jsonErr != nil {
  93. panic(jsonErr)
  94. }
  95. BeforeEach(func() {
  96. rdb.On("Publish", server.TOPIC_BROADCAST, expectedActionString).Return(redis.NewIntResult(0, nil))
  97. })
  98. It("should publish a message", func() {
  99. err := server.PublishActionFromClient(rdb, &action)
  100. Expect(rdb.Calls).NotTo(BeEmpty())
  101. Expect(err).To(BeNil())
  102. })
  103. })
  104. Context("when the current time is negative", func() {
  105. var action server.Action
  106. json.Unmarshal([]byte(actionStateSetCurrentTimeNegative), &action)
  107. It("should not publish a message", func() {
  108. err := server.PublishActionFromClient(rdb, &action)
  109. Expect(rdb.Calls).To(BeEmpty())
  110. Expect(err).NotTo(BeNil())
  111. })
  112. })
  113. Context("when the seek time is less than -1", func() {
  114. var action server.Action
  115. json.Unmarshal([]byte(actionStateSetSeekTimeTooNegative), &action)
  116. It("should not publish a message", func() {
  117. err := server.PublishActionFromClient(rdb, &action)
  118. Expect(rdb.Calls).To(BeEmpty())
  119. Expect(err).NotTo(BeNil())
  120. })
  121. })
  122. Context("when the master is empty", func() {
  123. var action server.Action
  124. json.Unmarshal([]byte(actionStateSetMasterEmpty), &action)
  125. It("should not publish a message", func() {
  126. err := server.PublishActionFromClient(rdb, &action)
  127. Expect(rdb.Calls).To(BeEmpty())
  128. Expect(err).NotTo(BeNil())
  129. })
  130. })
  131. })
  132. Describe("when the action is unrecognised", func() {
  133. var action server.Action
  134. err := json.Unmarshal([]byte(actionUnrecognised), &action)
  135. if err != nil {
  136. panic(err)
  137. }
  138. BeforeEach(func() {
  139. rdb.On("Publish", server.TOPIC_BROADCAST, "").Return(redis.NewIntResult(0, nil))
  140. })
  141. It("should not publish a message", func() {
  142. err := server.PublishActionFromClient(rdb, &action)
  143. Expect(rdb.Calls).To(BeEmpty())
  144. Expect(err).NotTo(BeNil())
  145. })
  146. })
  147. })
  148. })