1+ package signaling
2+
3+ import (
4+ "encoding/json"
5+ "strings"
6+ "testing"
7+ )
8+
9+ func TestWelcomePacketStructure (t * testing.T ) {
10+ // Test that WelcomePacket includes protocol version
11+ packet := WelcomePacket {
12+ Type : "welcome" ,
13+ ID : "test-id" ,
14+ Secret : "test-secret" ,
15+ ProtocolVersion : ProtocolVersion ,
16+ Warnings : []string {"test warning" },
17+ }
18+
19+ // Marshal to JSON to verify the structure
20+ data , err := json .Marshal (packet )
21+ if err != nil {
22+ t .Fatalf ("Failed to marshal WelcomePacket: %v" , err )
23+ }
24+
25+ // Unmarshal back to verify structure
26+ var unmarshaled WelcomePacket
27+ err = json .Unmarshal (data , & unmarshaled )
28+ if err != nil {
29+ t .Fatalf ("Failed to unmarshal WelcomePacket: %v" , err )
30+ }
31+
32+ // Verify all fields are preserved
33+ if unmarshaled .Type != "welcome" {
34+ t .Errorf ("Expected Type 'welcome', got %s" , unmarshaled .Type )
35+ }
36+ if unmarshaled .ID != "test-id" {
37+ t .Errorf ("Expected ID 'test-id', got %s" , unmarshaled .ID )
38+ }
39+ if unmarshaled .Secret != "test-secret" {
40+ t .Errorf ("Expected Secret 'test-secret', got %s" , unmarshaled .Secret )
41+ }
42+ if unmarshaled .ProtocolVersion != ProtocolVersion {
43+ t .Errorf ("Expected ProtocolVersion %s, got %s" , ProtocolVersion , unmarshaled .ProtocolVersion )
44+ }
45+ if len (unmarshaled .Warnings ) != 1 || unmarshaled .Warnings [0 ] != "test warning" {
46+ t .Errorf ("Expected Warnings ['test warning'], got %v" , unmarshaled .Warnings )
47+ }
48+ }
49+
50+ func TestWelcomePacketWithoutWarnings (t * testing.T ) {
51+ // Test that warnings field is omitted when empty
52+ packet := WelcomePacket {
53+ Type : "welcome" ,
54+ ID : "test-id" ,
55+ Secret : "test-secret" ,
56+ ProtocolVersion : ProtocolVersion ,
57+ }
58+
59+ // Marshal to JSON
60+ data , err := json .Marshal (packet )
61+ if err != nil {
62+ t .Fatalf ("Failed to marshal WelcomePacket: %v" , err )
63+ }
64+
65+ // Verify warnings field is not in JSON when empty
66+ var jsonMap map [string ]interface {}
67+ err = json .Unmarshal (data , & jsonMap )
68+ if err != nil {
69+ t .Fatalf ("Failed to unmarshal to map: %v" , err )
70+ }
71+
72+ if _ , hasWarnings := jsonMap ["warnings" ]; hasWarnings {
73+ t .Errorf ("Warnings field should be omitted when empty, but was present in JSON: %s" , string (data ))
74+ }
75+
76+ // Verify protocol version is always present
77+ if _ , hasProtocolVersion := jsonMap ["protocolVersion" ]; ! hasProtocolVersion {
78+ t .Errorf ("ProtocolVersion field should always be present in JSON: %s" , string (data ))
79+ }
80+ }
81+
82+ func TestProtocolVersionConstant (t * testing.T ) {
83+ // Verify protocol version is defined and not empty
84+ if ProtocolVersion == "" {
85+ t .Error ("ProtocolVersion constant should not be empty" )
86+ }
87+
88+ // Verify it follows semver-like pattern (basic check)
89+ if len (ProtocolVersion ) < 5 { // At minimum "1.0.0"
90+ t .Errorf ("ProtocolVersion should be a proper version string, got: %s" , ProtocolVersion )
91+ }
92+ }
93+
94+ func TestWelcomePacketWarningsExample (t * testing.T ) {
95+ // Test how warnings could be used in practice
96+ packet := WelcomePacket {
97+ Type : "welcome" ,
98+ ID : "test-id" ,
99+ Secret : "test-secret" ,
100+ ProtocolVersion : ProtocolVersion ,
101+ Warnings : []string {"Example warning: This feature will be deprecated in version 2.0.0" },
102+ }
103+
104+ // Marshal to JSON
105+ data , err := json .Marshal (packet )
106+ if err != nil {
107+ t .Fatalf ("Failed to marshal WelcomePacket with warnings: %v" , err )
108+ }
109+
110+ // Verify the JSON contains the warning
111+ jsonStr := string (data )
112+ if ! strings .Contains (jsonStr , "Example warning" ) {
113+ t .Errorf ("Expected JSON to contain warning message, got: %s" , jsonStr )
114+ }
115+
116+ if ! strings .Contains (jsonStr , "protocolVersion" ) {
117+ t .Errorf ("Expected JSON to contain protocolVersion, got: %s" , jsonStr )
118+ }
119+ }
0 commit comments