|
9 | 9 | "errors" |
10 | 10 | "fmt" |
11 | 11 | "io" |
| 12 | + "maps" |
12 | 13 | "net" |
13 | 14 | "net/http" |
14 | 15 | "net/http/httptest" |
@@ -238,6 +239,73 @@ func TestWaitForLocalPort(t *testing.T) { |
238 | 239 | }) |
239 | 240 | } |
240 | 241 |
|
| 242 | +func TestReportLocalClientRouteSelected(t *testing.T) { |
| 243 | + t.Parallel() |
| 244 | + |
| 245 | + tests := []struct { |
| 246 | + name string |
| 247 | + activityProfile activityRunProfile |
| 248 | + suppressClient bool |
| 249 | + reportErr error |
| 250 | + wantRoute string |
| 251 | + }{ |
| 252 | + { |
| 253 | + name: "selects Inspector for non-activity agent", |
| 254 | + wantRoute: localClientRouteInspector, |
| 255 | + }, |
| 256 | + { |
| 257 | + name: "selects Playground for activity agent", |
| 258 | + activityProfile: activityRunProfile{IsActivity: true}, |
| 259 | + wantRoute: localClientRoutePlayground, |
| 260 | + }, |
| 261 | + { |
| 262 | + name: "selects suppressed for non-activity agent", |
| 263 | + suppressClient: true, |
| 264 | + wantRoute: localClientRouteSuppressed, |
| 265 | + }, |
| 266 | + { |
| 267 | + name: "suppression overrides activity route", |
| 268 | + activityProfile: activityRunProfile{IsActivity: true}, |
| 269 | + suppressClient: true, |
| 270 | + wantRoute: localClientRouteSuppressed, |
| 271 | + }, |
| 272 | + { |
| 273 | + name: "reporting failure is best effort", |
| 274 | + reportErr: errors.New("telemetry unavailable"), |
| 275 | + wantRoute: localClientRouteInspector, |
| 276 | + }, |
| 277 | + } |
| 278 | + |
| 279 | + for _, tt := range tests { |
| 280 | + t.Run(tt.name, func(t *testing.T) { |
| 281 | + t.Parallel() |
| 282 | + |
| 283 | + telemetry := &recordingTelemetryClient{err: tt.reportErr} |
| 284 | + reportLocalClientRouteSelected( |
| 285 | + t.Context(), |
| 286 | + telemetry, |
| 287 | + tt.activityProfile, |
| 288 | + tt.suppressClient, |
| 289 | + ) |
| 290 | + |
| 291 | + if telemetry.request == nil { |
| 292 | + t.Fatal("expected telemetry request") |
| 293 | + } |
| 294 | + if telemetry.request.EventName != localClientRouteSelectedEvent { |
| 295 | + t.Fatalf( |
| 296 | + "event name = %q, want %q", |
| 297 | + telemetry.request.EventName, |
| 298 | + localClientRouteSelectedEvent, |
| 299 | + ) |
| 300 | + } |
| 301 | + wantAttributes := map[string]string{localClientRouteAttribute: tt.wantRoute} |
| 302 | + if !maps.Equal(telemetry.request.Attributes, wantAttributes) { |
| 303 | + t.Fatalf("attributes = %v, want %v", telemetry.request.Attributes, wantAttributes) |
| 304 | + } |
| 305 | + }) |
| 306 | + } |
| 307 | +} |
| 308 | + |
241 | 309 | func TestLaunchInspectorUsesWorkflowCommand(t *testing.T) { |
242 | 310 | t.Parallel() |
243 | 311 |
|
@@ -743,6 +811,11 @@ type recordingWorkflowClient struct { |
743 | 811 | called chan struct{} |
744 | 812 | } |
745 | 813 |
|
| 814 | +type recordingTelemetryClient struct { |
| 815 | + request *azdext.ReportUsageRequest |
| 816 | + err error |
| 817 | +} |
| 818 | + |
746 | 819 | type lockedBuffer struct { |
747 | 820 | mu sync.Mutex |
748 | 821 | bytes.Buffer |
@@ -772,6 +845,18 @@ func (c *recordingWorkflowClient) Run( |
772 | 845 | return &azdext.EmptyResponse{}, c.err |
773 | 846 | } |
774 | 847 |
|
| 848 | +func (c *recordingTelemetryClient) ReportUsage( |
| 849 | + _ context.Context, |
| 850 | + request *azdext.ReportUsageRequest, |
| 851 | + _ ...grpc.CallOption, |
| 852 | +) (*azdext.ReportUsageResponse, error) { |
| 853 | + c.request = request |
| 854 | + if c.err != nil { |
| 855 | + return nil, c.err |
| 856 | + } |
| 857 | + return &azdext.ReportUsageResponse{Accepted: true}, nil |
| 858 | +} |
| 859 | + |
775 | 860 | // createVenv sets up a minimal .venv directory structure for testing. |
776 | 861 | // Returns the path to the .venv directory. |
777 | 862 | func createVenv(t *testing.T, projectDir string) string { |
|
0 commit comments