MCPcopy Create free account
hub / github.com/cli/cli / TestServiceFinish

Function TestServiceFinish

internal/telemetry/telemetry_test.go:365–430  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

363}
364
365func TestServiceFinish(t *testing.T) {
366 t.Run("logs none when no events recorded", func(t *testing.T) {
367 // Given an invocation without events and log delivery
368 t.Cleanup(stubDeviceID("test-device"))
369 var buf bytes.Buffer
370 service := NewService(LogFlusher(&buf, false))
371
372 // When the invocation finishes
373 service.Finish()
374
375 // Then log mode explains the absence of telemetry
376 assert.Equal(t, "Telemetry payload: none\n", buf.String())
377 })
378
379 t.Run("delivers events with merged dimensions", func(t *testing.T) {
380 // Given an invocation with common dimensions
381 t.Cleanup(stubDeviceID("test-device"))
382 var captured SendTelemetryPayload
383 service := NewService(func(p SendTelemetryPayload) { captured = p },
384 WithAdditionalCommonDimensions(ghtelemetry.Dimensions{
385 "version": "2.45.0",
386 "agent": "none",
387 }),
388 )
389
390 // When an event with its own dimensions and measures is completed and delivered
391 service.Record(ghtelemetry.Event{
392 Type: "command_invocation",
393 Dimensions: map[string]string{"command": "gh pr list"},
394 Measures: map[string]int64{"duration_ms": 150},
395 })
396 service.Finish()
397
398 // Then the payload includes both common and event-specific facts
399 require.Len(t, captured.Events, 1)
400 event := captured.Events[0]
401 assert.Equal(t, "command_invocation", event.Type)
402 assert.Equal(t, "gh pr list", event.Dimensions["command"])
403 assert.Equal(t, "2.45.0", event.Dimensions["version"])
404 assert.Equal(t, "none", event.Dimensions["agent"])
405 assert.Equal(t, "test-device", event.Dimensions["device_id"])
406 assert.NotEmpty(t, event.Dimensions["timestamp"])
407 assert.NotEmpty(t, event.Dimensions["invocation_id"])
408 assert.NotEmpty(t, event.Dimensions["os"])
409 assert.NotEmpty(t, event.Dimensions["architecture"])
410 assert.Equal(t, int64(150), event.Measures["duration_ms"])
411 })
412
413 t.Run("event dimensions override common dimensions", func(t *testing.T) {
414 // Given common and event dimensions share a key
415 t.Cleanup(stubDeviceID("test-device"))
416 var captured SendTelemetryPayload
417 service := NewService(func(p SendTelemetryPayload) { captured = p }, WithAdditionalCommonDimensions(ghtelemetry.Dimensions{"shared": "common"}))
418 service.Record(ghtelemetry.Event{
419 Type: "test",
420 Dimensions: map[string]string{"shared": "event-level"},
421 })
422

Callers

nothing calls this directly

Calls 9

FinishMethod · 0.95
stubDeviceIDFunction · 0.85
NewServiceFunction · 0.85
EqualMethod · 0.80
RunMethod · 0.65
StringMethod · 0.65
RecordMethod · 0.65
LenMethod · 0.65

Tested by

no test coverage detected