11package agent
22
33import (
4+ "context"
5+ "encoding/base64"
46 "testing"
7+ "time"
58
9+ "github.com/gorilla/websocket"
610 "github.com/rs/zerolog"
711)
812
13+ // testAgentToken is a minimal JWT-format string with a known subject.
14+ // The signature is intentionally fake; DefaultConfig only calls jwt.ParseUnverified,
15+ // which does not check the signature.
16+ var testAgentToken = func () string {
17+ header := base64 .RawURLEncoding .EncodeToString ([]byte (`{"alg":"EdDSA","typ":"JWT"}` ))
18+ payload := base64 .RawURLEncoding .EncodeToString ([]byte (`{"sub":"test-agent-id"}` ))
19+ return header + "." + payload + ".fakesig"
20+ }()
21+
922func TestDefaultConfig_Valid (t * testing.T ) {
1023 t .Setenv ("HUB_URL" , "https://hub.example.com" )
11- t .Setenv ("AUTH_TOKEN" , "test-token" )
24+ t .Setenv ("AUTH_TOKEN" , testAgentToken )
1225 t .Setenv ("LOG_LEVEL" , "debug" )
1326 t .Setenv ("LOG_JSON" , "true" )
1427
@@ -26,14 +39,17 @@ func TestDefaultConfig_Valid(t *testing.T) {
2639 if cfg .HubUrl != "wss://hub.example.com/api/v1/ws" {
2740 t .Errorf ("HubUrl = %q, want %q" , cfg .HubUrl , "wss://hub.example.com/api/v1/ws" )
2841 }
29- if cfg .AuthToken != "test-token" {
30- t .Errorf ("AuthToken = %q, want %q" , cfg .AuthToken , "test-token" )
42+ if cfg .AuthToken != testAgentToken {
43+ t .Errorf ("AuthToken = %q, want %q" , cfg .AuthToken , testAgentToken )
44+ }
45+ if cfg .AgentID != "test-agent-id" {
46+ t .Errorf ("AgentID = %q, want %q" , cfg .AgentID , "test-agent-id" )
3147 }
3248}
3349
3450func TestDefaultConfig_Defaults (t * testing.T ) {
3551 t .Setenv ("HUB_URL" , "https://hub.example.com" )
36- t .Setenv ("AUTH_TOKEN" , "test-token" )
52+ t .Setenv ("AUTH_TOKEN" , testAgentToken )
3753 t .Setenv ("LOG_LEVEL" , "" )
3854 t .Setenv ("LOG_JSON" , "" )
3955
@@ -66,7 +82,7 @@ func TestDefaultConfig_LogLevels(t *testing.T) {
6682 for _ , tt := range tests {
6783 t .Run (tt .input , func (t * testing.T ) {
6884 t .Setenv ("HUB_URL" , "https://hub.example.com" )
69- t .Setenv ("AUTH_TOKEN" , "test-token" )
85+ t .Setenv ("AUTH_TOKEN" , testAgentToken )
7086 t .Setenv ("LOG_LEVEL" , tt .input )
7187
7288 cfg , err := DefaultConfig ()
@@ -95,7 +111,7 @@ func TestDefaultConfig_LogJSON(t *testing.T) {
95111 for _ , tt := range tests {
96112 t .Run (tt .input , func (t * testing.T ) {
97113 t .Setenv ("HUB_URL" , "https://hub.example.com" )
98- t .Setenv ("AUTH_TOKEN" , "test-token" )
114+ t .Setenv ("AUTH_TOKEN" , testAgentToken )
99115 t .Setenv ("LOG_JSON" , tt .input )
100116
101117 cfg , err := DefaultConfig ()
@@ -109,14 +125,50 @@ func TestDefaultConfig_LogJSON(t *testing.T) {
109125 }
110126}
111127
128+ func TestParseAgentID_MissingSubject (t * testing.T ) {
129+ // JWT with empty subject field.
130+ header := base64 .RawURLEncoding .EncodeToString ([]byte (`{"alg":"EdDSA","typ":"JWT"}` ))
131+ payload := base64 .RawURLEncoding .EncodeToString ([]byte (`{"sub":""}` ))
132+ token := header + "." + payload + ".fakesig"
133+
134+ _ , err := parseAgentID (token )
135+ if err == nil {
136+ t .Fatal ("expected error for token with empty subject" )
137+ }
138+ }
139+
140+ func TestParseAgentID_InvalidToken (t * testing.T ) {
141+ _ , err := parseAgentID ("not.a.jwt" )
142+ if err == nil {
143+ t .Fatal ("expected error for malformed JWT" )
144+ }
145+ }
146+
147+ func TestConnTracker_SetAndCancelled_CancelledCtx (t * testing.T ) {
148+ srv := newTestServer (t , func (serverConn * websocket.Conn ) {
149+ serverConn .SetReadDeadline (time .Now ().Add (500 * time .Millisecond )) //nolint:errcheck,gosec
150+ _ , _ , _ = serverConn .ReadMessage ()
151+ })
152+
153+ clientConn := dialServer (t , srv )
154+
155+ ctx , cancel := context .WithCancel (context .Background ())
156+ cancel () // pre-cancel so ctx.Err() != nil
157+
158+ var tracker connTracker
159+ if cancelled := tracker .setAndCancelled (ctx , clientConn ); ! cancelled {
160+ t .Error ("expected setAndCancelled to return true for a pre-cancelled context" )
161+ }
162+ }
163+
112164func TestDefaultConfig_Errors (t * testing.T ) {
113165 tests := []struct {
114166 name string
115167 hubURL string
116168 authToken string
117169 }{
118170 {"missing auth token" , "https://hub.example.com" , "" },
119- {"invalid hub url" , "not-a-url" , "test-token" },
171+ {"invalid hub url" , "not-a-url" , testAgentToken },
120172 }
121173
122174 for _ , tt := range tests {
0 commit comments