@@ -12,9 +12,14 @@ import (
1212
1313 "github.com/ActiveState/termtest"
1414 "github.com/thoas/go-funk"
15+ "golang.org/x/net/context"
1516
17+ "github.com/ActiveState/cli/internal/errs"
18+ "github.com/ActiveState/cli/internal/ipc"
1619 "github.com/ActiveState/cli/internal/runbits/runtime/trigger"
20+ "github.com/ActiveState/cli/internal/svcctl"
1721 "github.com/ActiveState/cli/internal/testhelpers/suite"
22+ "github.com/ActiveState/cli/pkg/platform/model"
1823
1924 "github.com/ActiveState/cli/internal/analytics/client/sync/reporters"
2025 anaConst "github.com/ActiveState/cli/internal/analytics/constants"
@@ -644,6 +649,79 @@ func (suite *AnalyticsIntegrationTestSuite) TestCIAndInteractiveDimensions() {
644649 }
645650}
646651
652+ func (suite * AnalyticsIntegrationTestSuite ) TestAnalyticsPixelOverride () {
653+ suite .OnlyRunForTags (tagsuite .Analytics )
654+
655+ ts := e2e .New (suite .T (), false )
656+ defer ts .Close ()
657+
658+ testURL := "https://example.com"
659+ cp := ts .Spawn ("config" , "set" , constants .AnalyticsPixelOverrideConfig , testURL )
660+ cp .Expect ("Successfully set config key" )
661+ cp .ExpectExitCode (0 )
662+
663+ // Create IPC client using the test's socket directory to connect to the same state-svc instance
664+ // that the spawned state tool commands are using.
665+ // We make a request to the service directly to ensure we're hitting an endpoint that will send an event.
666+ sockPath := & ipc.SockPath {
667+ RootDir : ts .Dirs .SockRoot ,
668+ AppName : constants .CommandName ,
669+ AppChannel : constants .ChannelName ,
670+ }
671+ ipcClient := ipc .NewClient (sockPath )
672+
673+ svcPort , err := svcctl .LocateHTTP (ipcClient )
674+ suite .Require ().NoError (err , errs .JoinMessage (err ))
675+
676+ svcmodel := model .NewSvcModel (svcPort )
677+ _ , err = svcmodel .LocalProjects (context .Background ())
678+ suite .Require ().NoError (err , errs .JoinMessage (err ))
679+
680+ time .Sleep (time .Second ) // Ensure state-svc has time to report events
681+
682+ suite .eventsfile = filepath .Join (ts .Dirs .Config , reporters .TestReportFilename )
683+ events := parseAnalyticsEvents (suite , ts )
684+ suite .Require ().NotEmpty (events )
685+
686+ // Some events will fire before the config is updated, so we expect to
687+ // find at least one event with the new configuration values after the service is restarted.
688+ found := false
689+ for _ , e := range events {
690+ // Specifically check an event sent via the state-svc and ensure that the URL is the one we set in the config
691+ if e .Category == anaConst .CatStateSvc && e .Action == "endpoint" && e .Label == "Projects" {
692+ suite .Assert ().Equal (testURL , e .URL )
693+ found = true
694+ }
695+ }
696+ suite .Assert ().True (found , "Should find at least one state-svc endpoint Projects event" )
697+
698+ // Check that all events after the config set event have the correct URL.
699+ configSetEventIndex := - 1
700+ for i , e := range events {
701+ if e .Category == anaConst .CatConfig && e .Action == anaConst .ActConfigSet && e .Label == constants .AnalyticsPixelOverrideConfig {
702+ configSetEventIndex = i
703+ break
704+ }
705+ }
706+ suite .Require ().NotEqual (- 1 , configSetEventIndex , "Should find the config set event" )
707+
708+ // Check all events after config set, skipping the immediate update event
709+ for i , e := range events {
710+ if i > configSetEventIndex {
711+ // Skip the immediate update event right after config set.
712+ // The update event is sent before the config is updated, so it will have an empty URL.
713+ if i == configSetEventIndex + 1 && e .Category == "updates" {
714+ continue
715+ }
716+ // All other events with URLs should have the correct one.
717+ // This includes events from the state tool and the state-svc.
718+ if e .URL != testURL {
719+ suite .Equal (testURL , e .URL , "Event after config set (%s:%s) should have the updated URL" , e .Category , e .Action )
720+ }
721+ }
722+ }
723+ }
724+
647725func TestAnalyticsIntegrationTestSuite (t * testing.T ) {
648726 suite .Run (t , new (AnalyticsIntegrationTestSuite ))
649727}
0 commit comments