|
4 | 4 | package cmd |
5 | 5 |
|
6 | 6 | import ( |
| 7 | + "net/http" |
7 | 8 | "os" |
8 | 9 | "path/filepath" |
9 | 10 | "testing" |
@@ -178,3 +179,131 @@ func TestToServiceKey(t *testing.T) { |
178 | 179 | }) |
179 | 180 | } |
180 | 181 | } |
| 182 | + |
| 183 | +func TestCaptureResponseSession_NilClient(t *testing.T) { |
| 184 | + t.Parallel() |
| 185 | + |
| 186 | + tests := []struct { |
| 187 | + name string |
| 188 | + sid string |
| 189 | + headerVal string |
| 190 | + }{ |
| 191 | + {name: "no header", sid: "", headerVal: ""}, |
| 192 | + {name: "header present but nil client", sid: "", headerVal: "server-session-abc"}, |
| 193 | + {name: "client sid set with header", sid: "existing-session", headerVal: "server-session-abc"}, |
| 194 | + } |
| 195 | + |
| 196 | + for _, tt := range tests { |
| 197 | + t.Run(tt.name, func(t *testing.T) { |
| 198 | + t.Parallel() |
| 199 | + |
| 200 | + resp := &http.Response{Header: http.Header{}} |
| 201 | + if tt.headerVal != "" { |
| 202 | + resp.Header.Set("x-agent-session-id", tt.headerVal) |
| 203 | + } |
| 204 | + |
| 205 | + // Must not panic with nil azdClient. |
| 206 | + captureResponseSession(t.Context(), nil, "test-agent", tt.sid, resp, "Session: ") |
| 207 | + }) |
| 208 | + } |
| 209 | +} |
| 210 | + |
| 211 | +func TestLoadSaveLocalContext(t *testing.T) { |
| 212 | + t.Parallel() |
| 213 | + |
| 214 | + t.Run("round trip", func(t *testing.T) { |
| 215 | + t.Parallel() |
| 216 | + |
| 217 | + dir := t.TempDir() |
| 218 | + configPath := filepath.Join(dir, ConfigFile) |
| 219 | + |
| 220 | + agentCtx := &AgentLocalContext{ |
| 221 | + AgentName: "my-agent", |
| 222 | + Sessions: map[string]string{"agent1": "sess-123"}, |
| 223 | + } |
| 224 | + |
| 225 | + if err := saveLocalContext(agentCtx, configPath); err != nil { |
| 226 | + t.Fatalf("saveLocalContext failed: %v", err) |
| 227 | + } |
| 228 | + |
| 229 | + loaded := loadLocalContext(configPath) |
| 230 | + if loaded.AgentName != "my-agent" { |
| 231 | + t.Errorf("AgentName = %q, want %q", loaded.AgentName, "my-agent") |
| 232 | + } |
| 233 | + if loaded.Sessions["agent1"] != "sess-123" { |
| 234 | + t.Errorf("Sessions[agent1] = %q, want %q", loaded.Sessions["agent1"], "sess-123") |
| 235 | + } |
| 236 | + }) |
| 237 | + |
| 238 | + t.Run("missing file returns empty context", func(t *testing.T) { |
| 239 | + t.Parallel() |
| 240 | + |
| 241 | + loaded := loadLocalContext(filepath.Join(t.TempDir(), "nonexistent.json")) |
| 242 | + if loaded.Sessions != nil { |
| 243 | + t.Errorf("expected nil Sessions for missing file, got %v", loaded.Sessions) |
| 244 | + } |
| 245 | + }) |
| 246 | + |
| 247 | + t.Run("corrupt file returns empty context", func(t *testing.T) { |
| 248 | + t.Parallel() |
| 249 | + |
| 250 | + dir := t.TempDir() |
| 251 | + configPath := filepath.Join(dir, ConfigFile) |
| 252 | + if err := os.WriteFile(configPath, []byte("{bad json"), 0600); err != nil { |
| 253 | + t.Fatalf("failed to write corrupt file: %v", err) |
| 254 | + } |
| 255 | + |
| 256 | + loaded := loadLocalContext(configPath) |
| 257 | + if loaded.Sessions != nil { |
| 258 | + t.Errorf("expected nil Sessions for corrupt file, got %v", loaded.Sessions) |
| 259 | + } |
| 260 | + }) |
| 261 | +} |
| 262 | + |
| 263 | +func TestContextMap(t *testing.T) { |
| 264 | + t.Parallel() |
| 265 | + |
| 266 | + tests := []struct { |
| 267 | + name string |
| 268 | + field string |
| 269 | + }{ |
| 270 | + {name: "sessions", field: "sessions"}, |
| 271 | + {name: "conversations", field: "conversations"}, |
| 272 | + {name: "invocations", field: "invocations"}, |
| 273 | + {name: "unknown", field: "unknown"}, |
| 274 | + } |
| 275 | + |
| 276 | + for _, tt := range tests { |
| 277 | + t.Run(tt.name, func(t *testing.T) { |
| 278 | + t.Parallel() |
| 279 | + |
| 280 | + agentCtx := &AgentLocalContext{} |
| 281 | + m := contextMap(agentCtx, tt.field) |
| 282 | + if m == nil { |
| 283 | + t.Fatal("expected non-nil map") |
| 284 | + } |
| 285 | + m["key"] = "value" |
| 286 | + |
| 287 | + // For known fields, the map should be stored on the struct. |
| 288 | + switch tt.field { |
| 289 | + case "sessions": |
| 290 | + if agentCtx.Sessions["key"] != "value" { |
| 291 | + t.Error("sessions map not stored on struct") |
| 292 | + } |
| 293 | + case "conversations": |
| 294 | + if agentCtx.Conversations["key"] != "value" { |
| 295 | + t.Error("conversations map not stored on struct") |
| 296 | + } |
| 297 | + case "invocations": |
| 298 | + if agentCtx.Invocations["key"] != "value" { |
| 299 | + t.Error("invocations map not stored on struct") |
| 300 | + } |
| 301 | + case "unknown": |
| 302 | + // Detached map — verify it doesn't affect any struct field. |
| 303 | + if agentCtx.Sessions != nil || agentCtx.Conversations != nil || agentCtx.Invocations != nil { |
| 304 | + t.Error("unknown field should not initialize struct maps") |
| 305 | + } |
| 306 | + } |
| 307 | + }) |
| 308 | + } |
| 309 | +} |
0 commit comments