github.com/anycable/anycable-go@v1.5.1/common/common_test.go (about)

     1  package common
     2  
     3  import (
     4  	"encoding/json"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  )
     9  
    10  func TestEnvMergeConnectionState(t *testing.T) {
    11  	env := NewSessionEnv("", nil)
    12  	(*env.ConnectionState)["a"] = "old"
    13  
    14  	t.Run("when adding and rewriting", func(t *testing.T) {
    15  		env.MergeConnectionState(&map[string]string{"a": "new", "b": "super new"})
    16  
    17  		assert.Len(t, *env.ConnectionState, 2)
    18  		assert.Equal(t, "new", (*env.ConnectionState)["a"])
    19  		assert.Equal(t, "super new", (*env.ConnectionState)["b"])
    20  	})
    21  }
    22  
    23  func TestEnvMergeChannelState(t *testing.T) {
    24  	env := NewSessionEnv("", nil)
    25  	(*env.ChannelStates)["id"] = map[string]string{"a": "old"}
    26  
    27  	t.Run("when adding and rewriting", func(t *testing.T) {
    28  		env.MergeChannelState("id", &map[string]string{"a": "new", "b": "super new"})
    29  
    30  		assert.Len(t, (*env.ChannelStates)["id"], 2)
    31  		assert.Equal(t, "new", (*env.ChannelStates)["id"]["a"])
    32  		assert.Equal(t, "super new", (*env.ChannelStates)["id"]["b"])
    33  	})
    34  
    35  	t.Run("when new channel", func(t *testing.T) {
    36  		env.MergeChannelState("id2", &map[string]string{"a": "new"})
    37  
    38  		assert.Len(t, (*env.ChannelStates)["id2"], 1)
    39  		assert.Equal(t, "new", (*env.ChannelStates)["id2"]["a"])
    40  	})
    41  }
    42  
    43  func TestEnvGetChannelStateField(t *testing.T) {
    44  	env := NewSessionEnv("", nil)
    45  	(*env.ChannelStates)["id"] = map[string]string{"a": "old"}
    46  
    47  	assert.Equal(t, "old", env.GetChannelStateField("id", "a"))
    48  	assert.Equal(t, "", env.GetChannelStateField("no_id", "a"))
    49  	assert.Equal(t, "", env.GetChannelStateField("id", "no_field"))
    50  }
    51  
    52  func TestEnvGetConnectionStateField(t *testing.T) {
    53  	env := NewSessionEnv("", nil)
    54  	(*env.ConnectionState)["a"] = "old"
    55  
    56  	assert.Equal(t, "old", env.GetConnectionStateField("a"))
    57  	assert.Equal(t, "", env.GetConnectionStateField("no_a"))
    58  }
    59  
    60  func TestPubSubMessageFromJSON(t *testing.T) {
    61  	t.Run("Remote disconnect message", func(t *testing.T) {
    62  		msg := []byte("{\"command\":\"disconnect\",\"payload\":{\"identifier\":\"14\",\"reconnect\":false}}")
    63  
    64  		result, err := PubSubMessageFromJSON(msg)
    65  		assert.Nil(t, err)
    66  
    67  		casted := result.(RemoteCommandMessage)
    68  
    69  		assert.Equal(t, "disconnect", casted.Command)
    70  
    71  		dmsg, _ := casted.ToRemoteDisconnectMessage()
    72  
    73  		assert.Equal(t, "14", dmsg.Identifier)
    74  		assert.Equal(t, false, dmsg.Reconnect)
    75  	})
    76  
    77  	t.Run("Broadcast message", func(t *testing.T) {
    78  		msg := []byte("{\"stream\":\"bread-test\",\"data\":\"test\"}")
    79  
    80  		result, err := PubSubMessageFromJSON(msg)
    81  		assert.Nil(t, err)
    82  
    83  		casted := result.(StreamMessage)
    84  		assert.Equal(t, "bread-test", casted.Stream)
    85  		assert.Equal(t, "test", casted.Data)
    86  	})
    87  }
    88  
    89  func TestConfirmationMessage(t *testing.T) {
    90  	assert.Equal(t, "{\"type\":\"confirm_subscription\",\"identifier\":\"test_channel\"}", ConfirmationMessage("test_channel"))
    91  }
    92  
    93  func TestRejectionMessage(t *testing.T) {
    94  	assert.Equal(t, "{\"type\":\"reject_subscription\",\"identifier\":\"test_channel\"}", RejectionMessage("test_channel"))
    95  }
    96  
    97  func TestMessageJSONSerialization(t *testing.T) {
    98  	command := `{
    99  		"command": "subscribe",
   100  		"identifier": "test_channel",
   101  		"data": {"foo": "bar"},
   102  		"history": {
   103  			"since": 20202020,
   104  			"streams": {
   105  				"1": {
   106  					"epoch": "test",
   107  					"offset": 14
   108  				},
   109  				"2": {
   110  					"epoch": "test",
   111  					"offset": 42
   112  				}
   113  			}
   114  		}
   115  	}`
   116  
   117  	var msg Message
   118  
   119  	err := json.Unmarshal([]byte(command), &msg)
   120  	assert.NoError(t, err)
   121  
   122  	assert.Equal(t, msg.Command, "subscribe")
   123  	assert.Equal(t, msg.Identifier, "test_channel")
   124  	assert.Equal(t, msg.Data.(map[string]interface{}), map[string]interface{}{"foo": "bar"})
   125  	assert.EqualValues(t, msg.History.Since, 20202020)
   126  	assert.Equal(t, msg.History.Streams["1"], HistoryPosition{Epoch: "test", Offset: 14})
   127  	assert.Equal(t, msg.History.Streams["2"], HistoryPosition{Epoch: "test", Offset: 42})
   128  }