@@ -351,6 +351,23 @@ func TestNewServiceLogModeWithColorLogsToWriter(t *testing.T) {
351351 assert .Contains (t , output , "\033 [" , "expected ANSI escape sequences when color is enabled" )
352352}
353353
354+ func TestLogFlusherWritesNoneMarkerForEmptyPayload (t * testing.T ) {
355+ t .Run ("no color" , func (t * testing.T ) {
356+ var buf bytes.Buffer
357+ LogFlusher (& buf , false )(SendTelemetryPayload {})
358+ assert .Equal (t , "Telemetry payload: none\n " , buf .String ())
359+ })
360+
361+ t .Run ("with color" , func (t * testing.T ) {
362+ var buf bytes.Buffer
363+ LogFlusher (& buf , true )(SendTelemetryPayload {})
364+ output := buf .String ()
365+ assert .Contains (t , output , "Telemetry payload:" )
366+ assert .Contains (t , output , "none" )
367+ assert .Contains (t , output , "\x1b " ) // ANSI escape char for color codes
368+ })
369+ }
370+
354371func TestServiceDeviceIDFallback (t * testing.T ) {
355372 t .Cleanup (stubDeviceIDError (errors .New ("no device id" )))
356373
@@ -365,14 +382,19 @@ func TestServiceDeviceIDFallback(t *testing.T) {
365382}
366383
367384func TestServiceFlush (t * testing.T ) {
368- t .Run ("does nothing when no events recorded" , func (t * testing.T ) {
385+ t .Run ("calls flusher with empty payload when no events recorded" , func (t * testing.T ) {
369386 t .Cleanup (stubDeviceID ("test-device" ))
370387
388+ var captured SendTelemetryPayload
371389 called := false
372- svc := newService (func (SendTelemetryPayload ) { called = true }, nil )
390+ svc := newService (func (p SendTelemetryPayload ) {
391+ called = true
392+ captured = p
393+ }, nil )
373394 svc .Flush ()
374395
375- assert .False (t , called , "flusher should not be called with no events" )
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" )
376398 })
377399
378400 t .Run ("flushes events with merged dimensions" , func (t * testing.T ) {
@@ -599,45 +621,60 @@ func TestWithAdditionalCommonDimensions(t *testing.T) {
599621}
600622
601623func TestServiceDisable (t * testing.T ) {
602- t .Run ("prevents flush from sending events " , func (t * testing.T ) {
624+ t .Run ("drops recorded events from flushed payload " , func (t * testing.T ) {
603625 t .Cleanup (stubDeviceID ("test-device" ))
604626
627+ var captured SendTelemetryPayload
605628 called := false
606- svc := newService (func (SendTelemetryPayload ) { called = true }, nil )
629+ svc := newService (func (p SendTelemetryPayload ) {
630+ called = true
631+ captured = p
632+ }, nil )
607633
608634 svc .Record (ghtelemetry.Event {Type : "test" })
609635 svc .Disable ()
610636 svc .Flush ()
611637
612- assert .False (t , called , "flusher should not be called after Disable()" )
638+ assert .True (t , called , "flusher should still be called so log mode can surface the absence of events" )
639+ assert .Empty (t , captured .Events , "recorded events should be dropped after Disable()" )
613640 })
614641
615- t .Run ("prevents flush even with multiple recorded events" , func (t * testing.T ) {
642+ t .Run ("drops events even with multiple recorded events" , func (t * testing.T ) {
616643 t .Cleanup (stubDeviceID ("test-device" ))
617644
645+ var captured SendTelemetryPayload
618646 called := false
619- svc := newService (func (SendTelemetryPayload ) { called = true }, nil )
647+ svc := newService (func (p SendTelemetryPayload ) {
648+ called = true
649+ captured = p
650+ }, nil )
620651
621652 svc .Record (ghtelemetry.Event {Type : "event1" })
622653 svc .Record (ghtelemetry.Event {Type : "event2" })
623654 svc .Record (ghtelemetry.Event {Type : "event3" })
624655 svc .Disable ()
625656 svc .Flush ()
626657
627- assert .False (t , called , "flusher should not be called after Disable()" )
658+ assert .True (t , called , "flusher should still be called" )
659+ assert .Empty (t , captured .Events , "recorded events should be dropped after Disable()" )
628660 })
629661
630662 t .Run ("can be called before any events are recorded" , func (t * testing.T ) {
631663 t .Cleanup (stubDeviceID ("test-device" ))
632664
665+ var captured SendTelemetryPayload
633666 called := false
634- svc := newService (func (SendTelemetryPayload ) { called = true }, nil )
667+ svc := newService (func (p SendTelemetryPayload ) {
668+ called = true
669+ captured = p
670+ }, nil )
635671
636672 svc .Disable ()
637673 svc .Record (ghtelemetry.Event {Type : "test" })
638674 svc .Flush ()
639675
640- assert .False (t , called , "flusher should not be called when disabled before recording" )
676+ assert .True (t , called , "flusher should still be called" )
677+ assert .Empty (t , captured .Events , "events recorded after Disable() should be dropped" )
641678 })
642679}
643680
0 commit comments