MCPcopy
hub / github.com/cli/cli / TestServiceFlush

Function TestServiceFlush

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

Source from the content-addressed store, hash-verified

382}
383
384func TestServiceFlush(t *testing.T) {
385 t.Run("calls flusher with empty payload when no events recorded", func(t *testing.T) {
386 t.Cleanup(stubDeviceID("test-device"))
387
388 var captured SendTelemetryPayload
389 called := false
390 svc := newService(func(p SendTelemetryPayload) {
391 called = true
392 captured = p
393 }, nil)
394 svc.Flush()
395
396 assert.True(t, called, "flusher should be called even with no events so log mode can surface the absence")
397 assert.Empty(t, captured.Events, "payload should have no events")
398 })
399
400 t.Run("flushes events with merged dimensions", func(t *testing.T) {
401 t.Cleanup(stubDeviceID("test-device"))
402
403 var captured SendTelemetryPayload
404 svc := newService(func(p SendTelemetryPayload) { captured = p }, ghtelemetry.Dimensions{"version": "2.45.0"})
405
406 svc.Record(ghtelemetry.Event{
407 Type: "command_invocation",
408 Dimensions: map[string]string{"command": "gh pr list"},
409 Measures: map[string]int64{"duration_ms": 150},
410 })
411 svc.Flush()
412
413 require.Len(t, captured.Events, 1)
414 event := captured.Events[0]
415 assert.Equal(t, "command_invocation", event.Type)
416 assert.Equal(t, "gh pr list", event.Dimensions["command"])
417 assert.Equal(t, "2.45.0", event.Dimensions["version"])
418 assert.Equal(t, "test-device", event.Dimensions["device_id"])
419 assert.NotEmpty(t, event.Dimensions["timestamp"])
420 assert.NotEmpty(t, event.Dimensions["invocation_id"])
421 assert.Equal(t, int64(150), event.Measures["duration_ms"])
422 })
423
424 t.Run("flushes multiple events", func(t *testing.T) {
425 t.Cleanup(stubDeviceID("test-device"))
426
427 var captured SendTelemetryPayload
428 svc := newService(func(p SendTelemetryPayload) { captured = p }, nil)
429
430 svc.Record(ghtelemetry.Event{Type: "event1"})
431 svc.Record(ghtelemetry.Event{Type: "event2"})
432 svc.Flush()
433
434 require.Len(t, captured.Events, 2)
435 assert.Equal(t, "event1", captured.Events[0].Type)
436 assert.Equal(t, "event2", captured.Events[1].Type)
437 })
438
439 t.Run("is idempotent", func(t *testing.T) {
440 t.Cleanup(stubDeviceID("test-device"))
441

Callers

nothing calls this directly

Calls 7

stubDeviceIDFunction · 0.85
newServiceFunction · 0.85
EqualMethod · 0.80
RunMethod · 0.65
FlushMethod · 0.65
RecordMethod · 0.65
LenMethod · 0.65

Tested by

no test coverage detected