Skip to content

GOWA - WhatsApp REST API with support for UI, Multi Account, Webhooks, and MCP, and Chatwoot. Built with Golang for efficient memory use.

test: isolate echo dedup and media download globals (#911)

gobeyondptydc03211505c4
diff --git a/src/infrastructure/chatwoot/client_methods_test.go b/src/infrastructure/chatwoot/client_methods_test.go
index a8df64f0..46bccff5 100644
--- a/src/infrastructure/chatwoot/client_methods_test.go
+++ b/src/infrastructure/chatwoot/client_methods_test.go
@@ -1257,7 +1257,17 @@ func TestCreateMessage_AttachmentMissingIDReturnsZero(t *testing.T) {
// --- Echo dedup map (MarkMessageAsSent / IsMessageSentByUs) ----------------
+// resetEchoDedup empties the package-global echo-dedup map for the duration of
+// a test and again when it ends, so a test neither sees ids left by an earlier
+// test (or an earlier -count iteration) nor leaves its own behind.
+func resetEchoDedup(t *testing.T) {
+ t.Helper()
+ sentMessageIDs.Clear()
+ t.Cleanup(sentMessageIDs.Clear)
+}
+
func TestEchoDedup_MapBehavior(t *testing.T) {
+ resetEchoDedup(t)
const acc = 1
// MarkMessageAsSent(_, 0) is a no-op and IsMessageSentByUs(_, 0) is always
// false: id 0 is the "no id" sentinel returned by CreateMessage and must
@@ -1267,9 +1277,7 @@ func TestEchoDedup_MapBehavior(t *testing.T) {
t.Error("IsMessageSentByUs(acc, 0) = true, want false")
}
- // A real id, once marked, is recognized as ours. We use a large, fixed
- // id unlikely to collide with anything (the map is package-global and
- // shared, but this exact value is only touched here). IsMessageSentByUs
+ // A real id, once marked, is recognized as ours. IsMessageSentByUs
// does not delete on read, so it stays true on a second check — Chatwoot
// fires multiple webhook events for one message.
const id = 987654321
@@ -1295,6 +1303,7 @@ func TestEchoDedup_MapBehavior(t *testing.T) {
// message we sent in account A would suppress a genuine agent reply with the
// same id in account B (dropped reply).
func TestEchoDedup_AccountPartitioned(t *testing.T) {
+ resetEchoDedup(t)
const id = 555111222
const accA, accB = 11, 22
diff --git a/src/infrastructure/whatsapp/event_message_test.go b/src/infrastructure/whatsapp/event_message_test.go
index 08eec010..81dc40c0 100644
--- a/src/infrastructure/whatsapp/event_message_test.go
+++ b/src/infrastructure/whatsapp/event_message_test.go
@@ -167,8 +167,17 @@ func protoProtocolMessageType(value waE2E.ProtocolMessage_Type) *waE2E.ProtocolM
return &value
}
-func TestBuildEventPayloadImageWithCaption(t *testing.T) {
+// disableAutoDownloadMedia turns media auto-download off for one test and
+// restores the previous setting when it ends.
+func disableAutoDownloadMedia(t *testing.T) {
+ t.Helper()
+ previous := config.WhatsappAutoDownloadMedia
config.WhatsappAutoDownloadMedia = false
+ t.Cleanup(func() { config.WhatsappAutoDownloadMedia = previous })
+}
+
+func TestBuildEventPayloadImageWithCaption(t *testing.T) {
+ disableAutoDownloadMedia(t)
caption := "Check this out!"
evt := &events.Message{
Info: types.MessageInfo{
@@ -204,7 +213,7 @@ func TestBuildEventPayloadImageWithCaption(t *testing.T) {
}
func TestBuildEventPayloadVideoWithCaption(t *testing.T) {
- config.WhatsappAutoDownloadMedia = false
+ disableAutoDownloadMedia(t)
caption := "Watch this video"
evt := &events.Message{
Info: types.MessageInfo{
@@ -240,7 +249,7 @@ func TestBuildEventPayloadVideoWithCaption(t *testing.T) {
}
func TestBuildEventPayloadImageWithoutCaption(t *testing.T) {
- config.WhatsappAutoDownloadMedia = false
+ disableAutoDownloadMedia(t)
evt := &events.Message{
Info: types.MessageInfo{
MessageSource: types.MessageSource{
@@ -266,7 +275,7 @@ func TestBuildEventPayloadImageWithoutCaption(t *testing.T) {
}
func TestBuildEventPayloadDocumentWithCaption(t *testing.T) {
- config.WhatsappAutoDownloadMedia = false
+ disableAutoDownloadMedia(t)
caption := "Important document"
evt := &events.Message{
Info: types.MessageInfo{