| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248 |
- package server_test
- import (
- "encoding/json"
- "github.com/felamaslen/gmus-backend/pkg/server"
- . "github.com/onsi/ginkgo"
- . "github.com/onsi/gomega"
- "github.com/alicebob/miniredis/v2"
- "github.com/elliotchance/redismock"
- "github.com/go-redis/redis"
- )
- func newTestRedis() *redismock.ClientMock {
- mr, err := miniredis.Run()
- if err != nil {
- panic(err)
- }
- client := redis.NewClient(&redis.Options{
- Addr: mr.Addr(),
- })
- return redismock.NewNiceMock(client)
- }
- var _ = Describe("Server actions", func() {
- var rdb *redismock.ClientMock
- BeforeEach(func() {
- rdb = newTestRedis()
- rdb.On("Publish").Return(redis.NewIntResult(0, nil))
- })
- Describe("PublishActionFromClient", func() {
- Describe("state set actions", func() {
- Context("when the action is valid", func() {
- var action server.Action
- json.Unmarshal([]byte(`
- {
- "type": "STATE_SET",
- "payload": {
- "songId": 123,
- "playing": true,
- "currentTime": 94,
- "seekTime": -1,
- "queue": [],
- "master": "some-master-client"
- }
- }
- `), &action)
- myClient := "my-client"
- action.FromClient = &myClient
- songId := 123
- expectedAction := server.Action{
- Type: server.StateSet,
- FromClient: &myClient,
- Payload: server.MusicPlayer{
- SongId: &songId,
- Playing: true,
- CurrentTime: 94,
- SeekTime: -1,
- Master: "some-master-client",
- Queue: &[]int{},
- },
- }
- expectedActionString, jsonErr := json.Marshal(expectedAction)
- if jsonErr != nil {
- panic(jsonErr)
- }
- BeforeEach(func() {
- rdb.On("Publish", server.TOPIC_BROADCAST, expectedActionString).Return(redis.NewIntResult(0, nil))
- })
- It("should publish the action to the redis pubsub", func() {
- err := server.PublishActionFromClient(rdb, &action)
- Expect(rdb.Calls).NotTo(BeEmpty())
- Expect(err).To(BeNil())
- })
- })
- Context("when the song ID is non-positive", func() {
- var action server.Action
- json.Unmarshal([]byte(`
- {
- "type": "STATE_SET",
- "payload": {
- "songId": 0,
- "playing": true,
- "currentTime": 94,
- "seekTime": -1,
- "queue": [],
- "master": "some-master-client"
- }
- }
- `), &action)
- It("should not publish a message", func() {
- err := server.PublishActionFromClient(rdb, &action)
- Expect(rdb.Calls).To(BeEmpty())
- Expect(err).NotTo(BeNil())
- })
- })
- Context("when the song ID is null", func() {
- var action server.Action
- json.Unmarshal([]byte(`
- {
- "type": "STATE_SET",
- "payload": {
- "songId": null,
- "playing": false,
- "currentTime": 0,
- "seekTime": -1,
- "queue": [],
- "master": "some-master-client"
- }
- }
- `), &action)
- expectedAction := server.Action{
- Type: server.StateSet,
- Payload: server.MusicPlayer{
- SongId: nil,
- Playing: false,
- CurrentTime: 0,
- SeekTime: -1,
- Master: "some-master-client",
- Queue: &[]int{},
- },
- }
- expectedActionString, jsonErr := json.Marshal(expectedAction)
- if jsonErr != nil {
- panic(jsonErr)
- }
- BeforeEach(func() {
- rdb.On("Publish", server.TOPIC_BROADCAST, expectedActionString).Return(redis.NewIntResult(0, nil))
- })
- It("should publish a message", func() {
- err := server.PublishActionFromClient(rdb, &action)
- Expect(rdb.Calls).NotTo(BeEmpty())
- Expect(err).To(BeNil())
- })
- })
- Context("when the current time is negative", func() {
- var action server.Action
- json.Unmarshal([]byte(`
- {
- "type": "STATE_SET",
- "payload": {
- "songId": 123,
- "playing": false,
- "currentTime": -32,
- "seekTime": -1,
- "queue": [],
- "master": "some-master-client"
- }
- }
- `), &action)
- It("should not publish a message", func() {
- err := server.PublishActionFromClient(rdb, &action)
- Expect(rdb.Calls).To(BeEmpty())
- Expect(err).NotTo(BeNil())
- })
- })
- Context("when the seek time is less than -1", func() {
- var action server.Action
- json.Unmarshal([]byte(`
- {
- "type": "STATE_SET",
- "payload": {
- "songId": 123,
- "playing": false,
- "currentTime": 13,
- "seekTime": -3,
- "queue": [],
- "master": "some-master-client"
- }
- }
- `), &action)
- It("should not publish a message", func() {
- err := server.PublishActionFromClient(rdb, &action)
- Expect(rdb.Calls).To(BeEmpty())
- Expect(err).NotTo(BeNil())
- })
- })
- Context("when the master is empty", func() {
- var action server.Action
- json.Unmarshal([]byte(`
- {
- "type": "STATE_SET",
- "payload": {
- "songId": 123,
- "playing": false,
- "currentTime": 13,
- "seekTime": -3,
- "queue": [],
- "master": ""
- }
- }
- `), &action)
- It("should not publish a message", func() {
- err := server.PublishActionFromClient(rdb, &action)
- Expect(rdb.Calls).To(BeEmpty())
- Expect(err).NotTo(BeNil())
- })
- })
- })
- Describe("when the action is unrecognised", func() {
- var action server.Action
- // CLIENT_LIST_UPDATED should only ever come from the server
- err := json.Unmarshal([]byte(`
- {
- "type": "CLIENT_LIST_UPDATED",
- "payload": [
- { "name": "client-a", "lastPing": 123 },
- { "name": "client-b", "lastPing": 456 }
- ]
- }
- `), &action)
- if err != nil {
- panic(err)
- }
- BeforeEach(func() {
- rdb.On("Publish", server.TOPIC_BROADCAST, "").Return(redis.NewIntResult(0, nil))
- })
- It("should not publish a message", func() {
- err := server.PublishActionFromClient(rdb, &action)
- Expect(rdb.Calls).To(BeEmpty())
- Expect(err).NotTo(BeNil())
- })
- })
- })
- })
|