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.

19.7 KB
Raw Blame
package rest

import (
	"context"
	"errors"
	"net/http"
	"net/http/httptest"
	"strings"
	"testing"

	"github.com/aldinokemal/go-whatsapp-web-multidevice/config"
	domainChatStorage "github.com/aldinokemal/go-whatsapp-web-multidevice/domains/chatstorage"
	domainSend "github.com/aldinokemal/go-whatsapp-web-multidevice/domains/send"
	"github.com/aldinokemal/go-whatsapp-web-multidevice/infrastructure/chatwoot"
	"github.com/aldinokemal/go-whatsapp-web-multidevice/infrastructure/whatsapp"
	"github.com/gofiber/fiber/v3"
)

// withSignConfig sets the Chatwoot signature globals for the duration of a
// single subtest and restores their prior values on cleanup, so sibling tests
// always observe the package defaults (sign disabled, "\n\n" delimiter).
func withSignConfig(t *testing.T, signMsg bool, delimiter string) {
	t.Helper()
	prevSign := config.ChatwootSignMsg
	prevDelim := config.ChatwootSignDelimiter
	config.ChatwootSignMsg = signMsg
	config.ChatwootSignDelimiter = delimiter
	t.Cleanup(func() {
		config.ChatwootSignMsg = prevSign
		config.ChatwootSignDelimiter = prevDelim
	})
}

func withChatwootWebhookSecret(t *testing.T, secret string) {
	t.Helper()
	prevSecret := config.ChatwootWebhookSecret
	config.ChatwootWebhookSecret = secret
	t.Cleanup(func() {
		config.ChatwootWebhookSecret = prevSecret
	})
}

type chatwootRouteTestRepo struct {
	domainChatStorage.IChatStorageRepository
	link  *domainChatStorage.ChatwootMessageLink
	count int
}

func (r *chatwootRouteTestRepo) CountChatwootDeviceConfigs() (int, error) {
	return r.count, nil
}

// GetLatestChatwootMessageLinkByConversation mirrors the real repo's matching
// rules (account scope, legacy-zero wildcard, config scope) so a handler that
// passed the wrong scope would get nil here instead of silently succeeding.
func (r *chatwootRouteTestRepo) GetLatestChatwootMessageLinkByConversation(conversationID, accountID int, allowLegacyZero bool, configID int64) (*domainChatStorage.ChatwootMessageLink, error) {
	if r.link == nil || r.link.ChatwootConversationID != conversationID {
		return nil, nil
	}
	if r.link.ChatwootAccountID != accountID && !(allowLegacyZero && r.link.ChatwootAccountID == 0) {
		return nil, nil
	}
	if configID != 0 && r.link.ChatwootConfigID != configID {
		return nil, nil
	}
	cloned := *r.link
	return &cloned, nil
}

func TestComposeOutgoingText(t *testing.T) {
	tests := []struct {
		name string
		// signMsg/delimiter drive the CHATWOOT_SIGN_* config globals for the case.
		signMsg   bool
		delimiter string
		content   string
		sender    string
		want      string
	}{
		{
			// Core reason this function exists: GFM/Chatwoot markdown is
			// rewritten to WhatsApp's syntax before delivery — bold (**bold**->
			// *bold*), italic (*it*->_it_), and strike (~~s~~->~s~) all translate.
			name:      "markdown translated chatwoot to whatsapp",
			signMsg:   false,
			delimiter: "\n\n",
			content:   "**bold** *it* ~~s~~",
			sender:    "",
			want:      "*bold* _it_ ~s~",
		},
		{
			// Empty content has no body, so there is nothing to send.
			name:      "empty content returns empty",
			signMsg:   false,
			delimiter: "\n\n",
			content:   "",
			sender:    "",
			want:      "",
		},
		{
			// Sign disabled: never attach a signature, even when the agent name
			// is present — the body must pass through (markdown-translated) only.
			name:      "sign disabled ignores sender name",
			signMsg:   false,
			delimiter: "\n\n",
			content:   "hello",
			sender:    "Agent Smith",
			want:      "hello",
		},
		{
			// Sign enabled with a name: prefix "*<name>*" + delimiter + body.
			// Default delimiter "\n\n" carries literal backslash-n escapes that
			// must be expanded to real newlines.
			name:      "sign enabled default delimiter expands escapes",
			signMsg:   true,
			delimiter: "\n\n",
			content:   "hello",
			sender:    "Agent Smith",
			want:      "*Agent Smith*\n\nhello",
		},
		{
			// Same path but the delimiter is the literal two-character escape
			// sequence backslash-n-backslash-n; composeOutgoingText must expand
			// each "\n" into a real newline rune in the output.
			name:      "sign enabled literal backslash-n delimiter expanded",
			signMsg:   true,
			delimiter: "\\n\\n",
			content:   "hello",
			sender:    "Agent Smith",
			want:      "*Agent Smith*\n\nhello",
		},
		{
			// Signing still translates the body markdown before prefixing the
			// signature, so both transforms compose in the signed path.
			name:      "sign enabled also translates body markdown",
			signMsg:   true,
			delimiter: " | ",
			content:   "**bold**",
			sender:    "Bob",
			want:      "*Bob* | *bold*",
		},
		{
			// Empty sender name => no signature; the body alone is returned.
			name:      "sign enabled empty sender name no signature",
			signMsg:   true,
			delimiter: "\n\n",
			content:   "hello",
			sender:    "",
			want:      "hello",
		},
		{
			// Whitespace-only sender name is trimmed to empty => no signature.
			name:      "sign enabled whitespace sender name no signature",
			signMsg:   true,
			delimiter: "\n\n",
			content:   "hello",
			sender:    "   \t  ",
			want:      "hello",
		},
		{
			// Sign enabled but no body: the guard text != "" prevents emitting a
			// lone signature with no message underneath.
			name:      "sign enabled empty content no lone signature",
			signMsg:   true,
			delimiter: "\n\n",
			content:   "",
			sender:    "Agent Smith",
			want:      "",
		},
		{
			// A surrounding sender name is trimmed before being wrapped, so the
			// signature uses the cleaned name, not the padded original.
			name:      "sign enabled sender name trimmed before wrapping",
			signMsg:   true,
			delimiter: "\n\n",
			content:   "hello",
			sender:    "  Agent Smith  ",
			want:      "*Agent Smith*\n\nhello",
		},
		{
			// Links are flattened in the agent's body; the signature is added
			// around the converted text.
			name:      "sign enabled flattens markdown link in body",
			signMsg:   true,
			delimiter: "\n\n",
			content:   "see [**https://x.example/**](https://x.example/) and [docs](https://x.example/a_b?c=1\\&d=2)",
			sender:    "Bob",
			want:      "*Bob*\n\nsee https://x.example/ and docs: https://x.example/a_b?c=1&d=2",
		},
		{
			// Chatwoot's own "send with signature" switch with an empty
			// signature leaves a dangling separator line; it is dropped.
			name:      "dangling chatwoot signature separator dropped",
			signMsg:   true,
			delimiter: "\n\n",
			content:   "Thanks\n\n--\n\n",
			sender:    "Bob",
			want:      "*Bob*\n\nThanks",
		},
		{
			// A message that is only the separator has no body: nothing is
			// sent as text (an attachment then goes out without a caption),
			// and no lone agent name either.
			name:      "only chatwoot signature separator returns empty",
			signMsg:   true,
			delimiter: "\n\n",
			content:   "\n\n--\n\n",
			sender:    "Bob",
			want:      "",
		},
		{
			// Plain text with no markdown delimiters is passed through unchanged
			// (the markdown translator skips text that holds no Markdown).
			name:      "plain text no markdown unchanged",
			signMsg:   false,
			delimiter: "\n\n",
			content:   "just plain text",
			sender:    "",
			want:      "just plain text",
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			withSignConfig(t, tt.signMsg, tt.delimiter)

			payload := chatwoot.WebhookPayload{
				Content: tt.content,
				Sender:  chatwoot.Contact{Name: tt.sender},
			}

			got := composeOutgoingText(payload)
			if got != tt.want {
				t.Errorf("composeOutgoingText() = %q, want %q", got, tt.want)
			}
		})
	}
}

// TestComposeOutgoingTextDefaultsRestored asserts the config globals are left at
// their package defaults after the table-driven cases run, guarding against a
// save/restore leak that would make unrelated package tests flaky.
func TestComposeOutgoingTextDefaultsRestored(t *testing.T) {
	if config.ChatwootSignMsg != false {
		t.Errorf("ChatwootSignMsg leaked: got %v, want false", config.ChatwootSignMsg)
	}
	if config.ChatwootSignDelimiter != "\n\n" {
		t.Errorf("ChatwootSignDelimiter leaked: got %q, want %q", config.ChatwootSignDelimiter, "\n\n")
	}
}

func TestIsEchoOfForwardedMessage(t *testing.T) {
	tests := []struct {
		name     string
		sourceID string
		want     bool
	}{
		{"forwarded message", "WAID:3EB0ABC123", true},
		{"agent reply, no source_id", "", false},
		{"unrelated source_id", "12345", false},
		{"prefix only as substring elsewhere", "X-WAID:3EB0", false},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			payload := chatwoot.WebhookPayload{SourceID: tt.sourceID}
			if got := isEchoOfForwardedMessage(payload); got != tt.want {
				t.Errorf("isEchoOfForwardedMessage(source_id=%q) = %v, want %v", tt.sourceID, got, tt.want)
			}
		})
	}
}

func TestChatwootSendFailureContent(t *testing.T) {
	tests := []struct {
		name string
		err  error
		want string
	}{
		{
			name: "with error",
			err:  errors.New("device offline"),
			want: "Message was not sent to WhatsApp.\n\nError: device offline",
		},
		{
			name: "nil error",
			err:  nil,
			want: "Message was not sent to WhatsApp.",
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			if got := chatwootSendFailureContent(tt.err); got != tt.want {
				t.Fatalf("chatwootSendFailureContent() = %q, want %q", got, tt.want)
			}
		})
	}
}

func TestChatwootPayloadDeleted(t *testing.T) {
	tests := []struct {
		name string
		in   map[string]any
		want bool
	}{
		{name: "bool true", in: map[string]any{"deleted": true}, want: true},
		{name: "bool false", in: map[string]any{"deleted": false}, want: false},
		{name: "string true", in: map[string]any{"deleted": "true"}, want: true},
		{name: "missing", in: nil, want: false},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			payload := chatwoot.WebhookPayload{ContentAttributes: tt.in}
			if got := chatwootPayloadDeleted(payload); got != tt.want {
				t.Fatalf("chatwootPayloadDeleted() = %v, want %v", got, tt.want)
			}
		})
	}
}

func TestChatwootWebhookRejectsInvalidSecret(t *testing.T) {
	withChatwootWebhookSecret(t, "expected-secret")

	app := fiber.New()
	handler := &ChatwootHandler{}
	app.Post("/chatwoot/webhook", handler.HandleWebhook)

	req := httptest.NewRequest(http.MethodPost, "/chatwoot/webhook", strings.NewReader(`{"event":"message_created"}`))
	req.Header.Set("Content-Type", "application/json")
	req.Header.Set("X-Chatwoot-Webhook-Secret", "wrong-secret")

	resp, err := app.Test(req)
	if err != nil {
		t.Fatalf("app.Test: %v", err)
	}
	if resp.StatusCode != fiber.StatusUnauthorized {
		t.Fatalf("status = %d, want %d", resp.StatusCode, fiber.StatusUnauthorized)
	}
}

func TestResolveChatwootWebhookRoutePrefersConversationLink(t *testing.T) {
	handler := &ChatwootHandler{
		ChatStorageRepo: &chatwootRouteTestRepo{
			link: &domainChatStorage.ChatwootMessageLink{
				DeviceID:               "[email protected]",
				WhatsAppMessageID:      "wa-1",
				WhatsAppChatJID:        "[email protected]",
				ChatwootConversationID: 202,
			},
		},
	}

	route := handler.resolveChatwootWebhookRoute(chatwoot.WebhookPayload{
		Conversation: chatwoot.ConversationWebhook{
			ID: 202,
			Meta: chatwoot.ConversationMeta{
				Sender: chatwoot.Contact{
					PhoneNumber: "+628999999999",
					CustomAttributes: map[string]any{
						"gowa_whatsapp_jid": "[email protected]",
					},
				},
			},
		},
	}, nil)

	if route.DeviceID != "[email protected]" {
		t.Fatalf("DeviceID = %q, want [email protected]", route.DeviceID)
	}
	if route.Destination != "[email protected]" {
		t.Fatalf("Destination = %q, want linked WhatsApp chat", route.Destination)
	}
}

func TestResolveSendDestination(t *testing.T) {
	tests := []struct {
		name        string
		in          string
		wantDest    string
		wantIsGroup bool
	}{
		// Plain @s.whatsapp.net JIDs are reduced to the bare phone number.
		{name: "private jid stripped to phone", in: "[email protected]", wantDest: "628123456789", wantIsGroup: false},
		// A "+"-prefixed phone is cleaned and passes through with no @ suffix.
		{name: "plus phone cleaned", in: "+628123456789", wantDest: "628123456789", wantIsGroup: false},
		// A bare phone number is unchanged.
		{name: "bare phone unchanged", in: "628123456789", wantDest: "628123456789", wantIsGroup: false},
		// Groups keep the full @g.us JID and report isGroup=true.
		{name: "group jid preserved", in: "[email protected]", wantDest: "[email protected]", wantIsGroup: true},
		// Regression: privacy-masked @lid JIDs must be preserved verbatim so the
		// send layer's LID resolution can run. Stripping the suffix here misroutes
		// the reply into @s.whatsapp.net and breaks delivery to @lid contacts.
		{name: "lid jid preserved verbatim", in: "1234567890123@lid", wantDest: "1234567890123@lid", wantIsGroup: false},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			gotDest, gotIsGroup := resolveSendDestination(tt.in)
			if gotDest != tt.wantDest || gotIsGroup != tt.wantIsGroup {
				t.Fatalf("resolveSendDestination(%q) = (%q, %v), want (%q, %v)", tt.in, gotDest, gotIsGroup, tt.wantDest, tt.wantIsGroup)
			}
		})
	}
}

func TestChatwootLinkChatJID(t *testing.T) {
	tests := []struct {
		name string
		in   string
		want string
	}{
		{name: "phone gets whatsapp suffix", in: "+628123456789", want: "[email protected]"},
		{name: "private jid stays jid", in: "[email protected]", want: "[email protected]"},
		{name: "group jid stays jid", in: "[email protected]", want: "[email protected]"},
		{name: "lid jid stays jid", in: "abc@lid", want: "abc@lid"},
		{name: "empty", in: "", want: ""},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			if got := chatwootLinkChatJID(tt.in); got != tt.want {
				t.Fatalf("chatwootLinkChatJID(%q) = %q, want %q", tt.in, got, tt.want)
			}
		})
	}
}

// chatwootReplySendStub records what an agent reply makes the webhook send.
// The embedded interface is nil, so any send the tests do not expect panics.
type chatwootReplySendStub struct {
	domainSend.ISendUsecase
	texts    []string
	captions []string // one entry per attachment sent, in order
}

func (s *chatwootReplySendStub) SendText(_ context.Context, request domainSend.MessageRequest) (domainSend.GenericResponse, error) {
	s.texts = append(s.texts, request.Message)
	return domainSend.GenericResponse{Status: "ok"}, nil
}

func (s *chatwootReplySendStub) SendImage(_ context.Context, request domainSend.ImageRequest) (domainSend.GenericResponse, error) {
	s.captions = append(s.captions, "image:"+request.Caption)
	return domainSend.GenericResponse{Status: "ok"}, nil
}

func (s *chatwootReplySendStub) SendFile(_ context.Context, request domainSend.FileRequest) (domainSend.GenericResponse, error) {
	s.captions = append(s.captions, "file:"+request.Caption)
	return domainSend.GenericResponse{Status: "ok"}, nil
}

// TestChatwootWebhookEmptyReplyText drives the webhook end to end with a stub
// send usecase: a reply whose text comes out empty (nothing, or only Chatwoot's
// "--" signature separator) must not be sent as a text message, and must not
// become the caption of its attachments, which are still delivered.
func TestChatwootWebhookEmptyReplyText(t *testing.T) {
	const attachments = `[{"id":1,"file_type":"image","data_url":"https://chat.example/a.png"},{"id":2,"file_type":"file","data_url":"https://chat.example/b.pdf"}]`
	tests := []struct {
		name         string
		content      string
		attachments  string
		wantTexts    []string
		wantCaptions []string
	}{
		{
			name:    "separator only sends nothing",
			content: `\n\n--\n\n`,
		},
		{
			name:    "bare separator sends nothing",
			content: `--`,
		},
		{
			name:    "empty content sends nothing",
			content: ``,
		},
		{
			// Control: the same route does send a reply that has a body, with
			// the dangling separator dropped and the agent's name added.
			name:      "body with dangling separator is sent as text",
			content:   `Thanks\n\n--\n\n`,
			wantTexts: []string{"*Bob*\n\nThanks"},
		},
		{
			name:         "separator only with attachments sends them without caption",
			content:      `\n\n--\n\n`,
			attachments:  attachments,
			wantCaptions: []string{"image:", "file:"},
		},
		{
			name:         "empty content with attachments sends them without caption",
			content:      ``,
			attachments:  attachments,
			wantCaptions: []string{"image:", "file:"},
		},
		{
			// Control: a body rides on the first attachment only.
			name:         "body with attachments captions the first one",
			content:      `Thanks\n\n--\n\n`,
			attachments:  attachments,
			wantCaptions: []string{"image:*Bob*\n\nThanks", "file:"},
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			// Signing is on: an empty body must not go out as a lone agent name.
			withSignConfig(t, true, "\n\n")
			prevDevice := config.ChatwootDeviceID
			config.ChatwootDeviceID = "reply-device"
			t.Cleanup(func() { config.ChatwootDeviceID = prevDevice })
			chatwoot.InitClientRegistry(nil)
			t.Cleanup(func() { chatwoot.InitClientRegistry(nil) })

			manager := whatsapp.NewDeviceManager(nil, nil, nil)
			manager.AddDevice(whatsapp.NewDeviceInstance("reply-device", nil, nil))
			stub := &chatwootReplySendStub{}
			handler := &ChatwootHandler{SendUsecase: stub, DeviceManager: manager}
			app := fiber.New()
			app.Post("/chatwoot/webhook", handler.HandleWebhook)

			body := `{"event":"message_created","message_type":"outgoing","content":"` + tt.content + `",` +
				`"sender":{"name":"Bob"},"account":{"id":1},` +
				`"conversation":{"id":5,"inbox_id":9,"meta":{"sender":{"phone_number":"+628123456789"}}}`
			if tt.attachments != "" {
				body += `,"attachments":` + tt.attachments
			}
			body += `}`
			withChatwootWebhookSecret(t, "reply-secret")
			req := httptest.NewRequest(http.MethodPost, "/chatwoot/webhook", strings.NewReader(body))
			req.Header.Set("Content-Type", "application/json")
			req.Header.Set("X-Chatwoot-Webhook-Secret", "reply-secret")
			resp, err := app.Test(req)
			if err != nil {
				t.Fatalf("app.Test: %v", err)
			}
			if resp.StatusCode != fiber.StatusOK {
				t.Fatalf("status = %d, want 200", resp.StatusCode)
			}
			if strings.Join(stub.texts, "|") != strings.Join(tt.wantTexts, "|") || len(stub.texts) != len(tt.wantTexts) {
				t.Errorf("SendText messages = %q, want %q", stub.texts, tt.wantTexts)
			}
			if strings.Join(stub.captions, "|") != strings.Join(tt.wantCaptions, "|") || len(stub.captions) != len(tt.wantCaptions) {
				t.Errorf("attachments sent = %q, want %q", stub.captions, tt.wantCaptions)
			}
		})
	}
}

// TestChatwootWebhookLegacyWithoutDeviceIDSendsFromOnlyDevice: the documented
// single-device setup leaves CHATWOOT_DEVICE_ID empty. A reply to a new
// conversation (no link yet) is sent from the only device.
func TestChatwootWebhookLegacyWithoutDeviceIDSendsFromOnlyDevice(t *testing.T) {
	withChatwootWebhookSecret(t, "legacy-secret")
	prevDevice := config.ChatwootDeviceID
	config.ChatwootDeviceID = ""
	t.Cleanup(func() { config.ChatwootDeviceID = prevDevice })
	chatwoot.InitClientRegistry(nil)
	t.Cleanup(func() { chatwoot.InitClientRegistry(nil) })

	manager := whatsapp.NewDeviceManager(nil, nil, nil)
	manager.AddDevice(whatsapp.NewDeviceInstance("only-device", nil, nil))
	stub := &chatwootReplySendStub{}
	handler := &ChatwootHandler{SendUsecase: stub, DeviceManager: manager, ChatStorageRepo: &deviceWebhookTestRepo{}}
	app := fiber.New()
	app.Post("/chatwoot/webhook", handler.HandleWebhook)

	body := `{"event":"message_created","message_type":"outgoing","content":"hello","account":{"id":1},` +
		`"conversation":{"id":5,"inbox_id":9,"meta":{"sender":{"phone_number":"+628123456789"}}}}`
	req := webhookAuthRequest{body: body, headers: map[string]string{"X-Chatwoot-Webhook-Secret": "legacy-secret"}}
	if got := postChatwootWebhook(t, app, req); got != fiber.StatusOK {
		t.Fatalf("status = %d, want 200", got)
	}
	if len(stub.texts) != 1 || stub.texts[0] != "hello" {
		t.Fatalf("SendText messages = %q, want [\"hello\"]", stub.texts)
	}
}