actions_test.go 4.4 KB

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