From a3c05b41717bc652ef7736cc2c2b22c4eaed0f5d Mon Sep 17 00:00:00 2001 From: root Date: Sat, 25 Jul 2026 22:05:24 -0300 Subject: [PATCH 1/2] fix: skip NATS connection when URL is empty --- pkg/events/nats/nats_producer.go | 10 ++++++++++ pkg/events/nats/nats_producer_test.go | 16 ++++++++++++++++ 2 files changed, 26 insertions(+) create mode 100644 pkg/events/nats/nats_producer_test.go diff --git a/pkg/events/nats/nats_producer.go b/pkg/events/nats/nats_producer.go index f32561f5..f2c4f76d 100644 --- a/pkg/events/nats/nats_producer.go +++ b/pkg/events/nats/nats_producer.go @@ -5,6 +5,7 @@ import ( logger_wrapper "github.com/evolution-foundation/evolution-go/pkg/logger" "github.com/gomessguii/logger" "github.com/nats-io/nats.go" + "strings" ) type natsProducer struct { @@ -20,6 +21,15 @@ func NewNatsProducer( natsGlobalEvents []string, loggerWrapper *logger_wrapper.LoggerManager, ) producer_interfaces.Producer { + if strings.TrimSpace(url) == "" { + return &natsProducer{ + conn: nil, + natsGlobalEnabled: false, + natsGlobalEvents: nil, + loggerWrapper: loggerWrapper, + } + } + conn, err := nats.Connect(url) if err != nil { logger.LogError("Failed to connect to NATS: %v", err) diff --git a/pkg/events/nats/nats_producer_test.go b/pkg/events/nats/nats_producer_test.go new file mode 100644 index 00000000..d6013c95 --- /dev/null +++ b/pkg/events/nats/nats_producer_test.go @@ -0,0 +1,16 @@ +package nats_producer + +import "testing" + +func TestNewNatsProducerDoesNotConnectWithoutURL(t *testing.T) { + producer, ok := NewNatsProducer(" ", false, nil, nil).(*natsProducer) + if !ok { + t.Fatal("constructor returned an unexpected producer type") + } + if producer.conn != nil { + t.Fatal("NATS connection must stay nil when NATS_URL is empty") + } + if producer.natsGlobalEnabled { + t.Fatal("NATS global publishing must stay disabled without a URL") + } +} From 1860110296ea775734752be8a40b93726c34ff27 Mon Sep 17 00:00:00 2001 From: lojanica Date: Wed, 29 Jul 2026 17:55:22 -0300 Subject: [PATCH 2/2] test(nats): refactor tests and add global events state assertion --- pkg/events/nats/nats_producer_test.go | 31 ++++++++++++++++++++------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/pkg/events/nats/nats_producer_test.go b/pkg/events/nats/nats_producer_test.go index d6013c95..272fd682 100644 --- a/pkg/events/nats/nats_producer_test.go +++ b/pkg/events/nats/nats_producer_test.go @@ -3,14 +3,29 @@ package nats_producer import "testing" func TestNewNatsProducerDoesNotConnectWithoutURL(t *testing.T) { - producer, ok := NewNatsProducer(" ", false, nil, nil).(*natsProducer) - if !ok { - t.Fatal("constructor returned an unexpected producer type") + tests := []struct { + name string + url string + }{ + {"Empty URL", ""}, + {"Whitespace URL", " "}, } - if producer.conn != nil { - t.Fatal("NATS connection must stay nil when NATS_URL is empty") - } - if producer.natsGlobalEnabled { - t.Fatal("NATS global publishing must stay disabled without a URL") + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + producer, ok := NewNatsProducer(tt.url, true, []string{"event1"}, nil).(*natsProducer) + if !ok { + t.Fatal("constructor returned an unexpected producer type") + } + if producer.conn != nil { + t.Fatal("NATS connection must stay nil when NATS_URL is empty") + } + if producer.natsGlobalEnabled { + t.Fatal("NATS global publishing must stay disabled without a URL") + } + if producer.natsGlobalEvents != nil { + t.Fatal("natsGlobalEvents must be nil when NATS_URL is empty") + } + }) } }