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.5 KB
Raw Blame
package chatstorage

import (
	"testing"
	"time"

	domainChatStorage "github.com/aldinokemal/go-whatsapp-web-multidevice/domains/chatstorage"
)

func TestSQLiteRepositoryStoresAndLooksUpChatwootMessageLinks(t *testing.T) {
	repo := newTestSQLiteRepository(t)
	now := time.Date(2026, time.June, 6, 10, 0, 0, 0, time.UTC)

	link := &domainChatStorage.ChatwootMessageLink{
		DeviceID:                     "[email protected]",
		WhatsAppMessageID:            "wa-1",
		WhatsAppChatJID:              "[email protected]",
		ChatwootMessageID:            101,
		ChatwootConversationID:       202,
		ChatwootInboxID:              303,
		ChatwootContactInboxSourceID: "[email protected]",
		SourceID:                     "WAID:wa-1",
		Direction:                    "incoming",
		IsRead:                       false,
		CreatedAt:                    now,
		UpdatedAt:                    now,
	}

	if err := repo.UpsertChatwootMessageLink(link); err != nil {
		t.Fatalf("upsert link: %v", err)
	}

	byWA, err := repo.GetChatwootMessageLinkByWhatsAppID(link.DeviceID, link.WhatsAppMessageID)
	if err != nil {
		t.Fatalf("lookup by whatsapp id: %v", err)
	}
	if byWA == nil {
		t.Fatal("expected link by whatsapp id")
	}
	if byWA.ChatwootMessageID != 101 || byWA.ChatwootConversationID != 202 || byWA.SourceID != "WAID:wa-1" {
		t.Fatalf("unexpected link by whatsapp id: %+v", byWA)
	}

	byCW, err := repo.GetChatwootMessageLinkByChatwootID(link.DeviceID, link.ChatwootMessageID)
	if err != nil {
		t.Fatalf("lookup by chatwoot id: %v", err)
	}
	if byCW == nil {
		t.Fatal("expected link by chatwoot id")
	}
	if byCW.WhatsAppMessageID != "wa-1" || byCW.WhatsAppChatJID != link.WhatsAppChatJID {
		t.Fatalf("unexpected link by chatwoot id: %+v", byCW)
	}

	link.ChatwootConversationID = 404
	link.IsRead = true
	if err := repo.UpsertChatwootMessageLink(link); err != nil {
		t.Fatalf("update link: %v", err)
	}

	updated, err := repo.GetChatwootMessageLinkByWhatsAppID(link.DeviceID, link.WhatsAppMessageID)
	if err != nil {
		t.Fatalf("lookup updated link: %v", err)
	}
	if updated.ChatwootConversationID != 404 || !updated.IsRead {
		t.Fatalf("expected updated link, got %+v", updated)
	}
}

func TestSQLiteRepositoryChatwootMessageLinksAreDeviceScoped(t *testing.T) {
	repo := newTestSQLiteRepository(t)

	if err := repo.UpsertChatwootMessageLink(&domainChatStorage.ChatwootMessageLink{
		DeviceID:          "[email protected]",
		WhatsAppMessageID: "same-wa-id",
		WhatsAppChatJID:   "[email protected]",
		ChatwootMessageID: 10,
		SourceID:          "WAID:same-wa-id",
		Direction:         "incoming",
	}); err != nil {
		t.Fatalf("upsert device A link: %v", err)
	}
	if err := repo.UpsertChatwootMessageLink(&domainChatStorage.ChatwootMessageLink{
		DeviceID:          "[email protected]",
		WhatsAppMessageID: "same-wa-id",
		WhatsAppChatJID:   "[email protected]",
		ChatwootMessageID: 20,
		SourceID:          "WAID:same-wa-id",
		Direction:         "incoming",
	}); err != nil {
		t.Fatalf("upsert device B link: %v", err)
	}

	linkA, err := repo.GetChatwootMessageLinkByWhatsAppID("[email protected]", "same-wa-id")
	if err != nil {
		t.Fatalf("lookup device A: %v", err)
	}
	linkB, err := repo.GetChatwootMessageLinkByWhatsAppID("[email protected]", "same-wa-id")
	if err != nil {
		t.Fatalf("lookup device B: %v", err)
	}

	if linkA.ChatwootMessageID != 10 {
		t.Fatalf("device A chatwoot id = %d, want 10", linkA.ChatwootMessageID)
	}
	if linkB.ChatwootMessageID != 20 {
		t.Fatalf("device B chatwoot id = %d, want 20", linkB.ChatwootMessageID)
	}

	if err := repo.DeleteDeviceData("[email protected]"); err != nil {
		t.Fatalf("delete device A data: %v", err)
	}
	linkA, err = repo.GetChatwootMessageLinkByWhatsAppID("[email protected]", "same-wa-id")
	if err != nil {
		t.Fatalf("lookup deleted device A: %v", err)
	}
	if linkA != nil {
		t.Fatalf("expected device A link to be deleted, got %+v", linkA)
	}

	linkB, err = repo.GetChatwootMessageLinkByWhatsAppID("[email protected]", "same-wa-id")
	if err != nil {
		t.Fatalf("lookup remaining device B: %v", err)
	}
	if linkB == nil || linkB.ChatwootMessageID != 20 {
		t.Fatalf("expected device B link to remain, got %+v", linkB)
	}
}

func TestSQLiteRepositoryGetsLatestUnreadIncomingChatwootMessageLinkByChat(t *testing.T) {
	repo := newTestSQLiteRepository(t)
	deviceID := "[email protected]"
	chatJID := "[email protected]"

	links := []*domainChatStorage.ChatwootMessageLink{
		{
			DeviceID:          deviceID,
			WhatsAppMessageID: "old-unread",
			WhatsAppChatJID:   chatJID,
			ChatwootMessageID: 11,
			Direction:         "incoming",
			IsRead:            false,
			UpdatedAt:         time.Date(2026, time.June, 6, 9, 0, 0, 0, time.UTC),
		},
		{
			DeviceID:          deviceID,
			WhatsAppMessageID: "latest-unread",
			WhatsAppChatJID:   chatJID,
			ChatwootMessageID: 12,
			Direction:         "incoming",
			IsRead:            false,
			UpdatedAt:         time.Date(2026, time.June, 6, 10, 0, 0, 0, time.UTC),
		},
		{
			DeviceID:          deviceID,
			WhatsAppMessageID: "outgoing",
			WhatsAppChatJID:   chatJID,
			ChatwootMessageID: 13,
			Direction:         "outgoing",
			IsRead:            false,
			UpdatedAt:         time.Date(2026, time.June, 6, 11, 0, 0, 0, time.UTC),
		},
		{
			DeviceID:          deviceID,
			WhatsAppMessageID: "read",
			WhatsAppChatJID:   chatJID,
			ChatwootMessageID: 14,
			Direction:         "incoming",
			IsRead:            true,
			UpdatedAt:         time.Date(2026, time.June, 6, 12, 0, 0, 0, time.UTC),
		},
	}
	for _, link := range links {
		if err := repo.UpsertChatwootMessageLink(link); err != nil {
			t.Fatalf("upsert %s: %v", link.WhatsAppMessageID, err)
		}
	}

	link, err := repo.GetLatestUnreadChatwootMessageLinkByChat(deviceID, chatJID)
	if err != nil {
		t.Fatalf("lookup latest unread: %v", err)
	}
	if link == nil || link.WhatsAppMessageID != "latest-unread" {
		t.Fatalf("latest unread = %+v, want latest-unread", link)
	}

	link.IsRead = true
	if err := repo.UpsertChatwootMessageLink(link); err != nil {
		t.Fatalf("mark latest read: %v", err)
	}
	next, err := repo.GetLatestUnreadChatwootMessageLinkByChat(deviceID, chatJID)
	if err != nil {
		t.Fatalf("lookup next unread: %v", err)
	}
	if next == nil || next.WhatsAppMessageID != "old-unread" {
		t.Fatalf("next unread = %+v, want old-unread", next)
	}
}

func TestSQLiteRepositoryGetsLatestChatwootMessageLinkByConversation(t *testing.T) {
	repo := newTestSQLiteRepository(t)
	conversationID := 202

	links := []*domainChatStorage.ChatwootMessageLink{
		{
			DeviceID:               "[email protected]",
			WhatsAppMessageID:      "old",
			WhatsAppChatJID:        "[email protected]",
			ChatwootMessageID:      11,
			ChatwootConversationID: conversationID,
			UpdatedAt:              time.Date(2026, time.June, 6, 9, 0, 0, 0, time.UTC),
		},
		{
			DeviceID:               "[email protected]",
			WhatsAppMessageID:      "latest",
			WhatsAppChatJID:        "[email protected]",
			ChatwootMessageID:      12,
			ChatwootConversationID: conversationID,
			UpdatedAt:              time.Date(2026, time.June, 6, 10, 0, 0, 0, time.UTC),
		},
		{
			DeviceID:               "[email protected]",
			WhatsAppMessageID:      "other-conversation",
			WhatsAppChatJID:        "[email protected]",
			ChatwootMessageID:      13,
			ChatwootConversationID: 303,
			UpdatedAt:              time.Date(2026, time.June, 6, 11, 0, 0, 0, time.UTC),
		},
	}
	for _, link := range links {
		if err := repo.UpsertChatwootMessageLink(link); err != nil {
			t.Fatalf("upsert %s: %v", link.WhatsAppMessageID, err)
		}
	}

	link, err := repo.GetLatestChatwootMessageLinkByConversation(conversationID, 0, true, 0)
	if err != nil {
		t.Fatalf("lookup latest conversation link: %v", err)
	}
	if link == nil || link.WhatsAppMessageID != "latest" || link.DeviceID != "[email protected]" {
		t.Fatalf("latest conversation link = %+v, want device-b/latest", link)
	}
}

func TestSQLiteRepositoryConversationLookupExcludesLegacyZeroInPerDeviceMode(t *testing.T) {
	repo := newTestSQLiteRepository(t)

	// A pre-migration legacy link (account id 0) for conversation 9. Chatwoot
	// numbers conversations per account, so account 7 can also have a conversation
	// 9 — the legacy row must not be allowed to satisfy that account's reply.
	legacy := &domainChatStorage.ChatwootMessageLink{
		DeviceID:               "legacy-dev",
		WhatsAppMessageID:      "legacy-1",
		WhatsAppChatJID:        "[email protected]",
		ChatwootConversationID: 9,
		ChatwootAccountID:      0,
		Direction:              "incoming",
	}
	if err := repo.UpsertChatwootMessageLink(legacy); err != nil {
		t.Fatalf("upsert legacy link: %v", err)
	}

	// Per-device mode: account 7 webhook must NOT match the account-0 legacy row.
	got, err := repo.GetLatestChatwootMessageLinkByConversation(9, 7, false, 0)
	if err != nil {
		t.Fatalf("scoped lookup: %v", err)
	}
	if got != nil {
		t.Fatalf("per-device lookup must not match legacy account-0 link, got %+v", got)
	}

	// Legacy mode: the account-0 wildcard still resolves the historical link.
	got, err = repo.GetLatestChatwootMessageLinkByConversation(9, 7, true, 0)
	if err != nil {
		t.Fatalf("legacy lookup: %v", err)
	}
	if got == nil || got.DeviceID != "legacy-dev" {
		t.Fatalf("legacy-mode lookup should match account-0 link, got %+v", got)
	}
}

func TestSQLiteRepositoryBackfillChatwootMessageLinkAccount(t *testing.T) {
	repo := newTestSQLiteRepository(t)

	legacyLink := &domainChatStorage.ChatwootMessageLink{
		DeviceID:               "legacy-dev",
		WhatsAppMessageID:      "legacy-1",
		WhatsAppChatJID:        "[email protected]",
		ChatwootConversationID: 9,
		ChatwootAccountID:      0,
		Direction:              "incoming",
	}
	scopedLink := &domainChatStorage.ChatwootMessageLink{
		DeviceID:               "scoped-dev",
		WhatsAppMessageID:      "scoped-1",
		WhatsAppChatJID:        "[email protected]",
		ChatwootConversationID: 10,
		ChatwootAccountID:      7,
		Direction:              "incoming",
	}
	for _, l := range []*domainChatStorage.ChatwootMessageLink{legacyLink, scopedLink} {
		if err := repo.UpsertChatwootMessageLink(l); err != nil {
			t.Fatalf("upsert %s: %v", l.WhatsAppMessageID, err)
		}
	}

	n, err := repo.BackfillChatwootMessageLinkAccount(5)
	if err != nil {
		t.Fatalf("backfill: %v", err)
	}
	if n != 1 {
		t.Fatalf("backfill rows affected = %d, want 1 (only the account-0 row)", n)
	}

	got, err := repo.GetChatwootMessageLinkByWhatsAppID("legacy-dev", "legacy-1")
	if err != nil || got == nil {
		t.Fatalf("get legacy link: %v (%v)", err, got)
	}
	if got.ChatwootAccountID != 5 {
		t.Fatalf("legacy link account id = %d, want 5 after backfill", got.ChatwootAccountID)
	}

	got, err = repo.GetChatwootMessageLinkByWhatsAppID("scoped-dev", "scoped-1")
	if err != nil || got == nil {
		t.Fatalf("get scoped link: %v (%v)", err, got)
	}
	if got.ChatwootAccountID != 7 {
		t.Fatalf("non-zero link account id = %d, want untouched 7", got.ChatwootAccountID)
	}

	// Idempotent: a second run finds nothing left at 0.
	n, err = repo.BackfillChatwootMessageLinkAccount(5)
	if err != nil {
		t.Fatalf("second backfill: %v", err)
	}
	if n != 0 {
		t.Fatalf("second backfill rows affected = %d, want 0", n)
	}
}

func TestSQLiteRepositoryChatwootForwardQueueLifecycle(t *testing.T) {
	repo := newTestSQLiteRepository(t)
	now := time.Date(2026, time.June, 6, 10, 0, 0, 0, time.UTC)

	err := repo.EnqueueChatwootForwardEvent(&domainChatStorage.ChatwootForwardEvent{
		DeviceID:          "[email protected]",
		EventName:         "message",
		WhatsAppMessageID: "wa-queue-1",
		PayloadJSON:       `{"payload":{"id":"wa-queue-1"}}`,
		LastError:         "chatwoot down",
		NextAttemptAt:     now,
	})
	if err != nil {
		t.Fatalf("enqueue retry: %v", err)
	}

	due, err := repo.ListDueChatwootForwardEvents(now.Add(-time.Second), 10)
	if err != nil {
		t.Fatalf("list early due events: %v", err)
	}
	if len(due) != 0 {
		t.Fatalf("early due len = %d, want 0", len(due))
	}

	due, err = repo.ListDueChatwootForwardEvents(now, 10)
	if err != nil {
		t.Fatalf("list due events: %v", err)
	}
	if len(due) != 1 {
		t.Fatalf("due len = %d, want 1", len(due))
	}
	if due[0].DeviceID != "[email protected]" || due[0].WhatsAppMessageID != "wa-queue-1" || due[0].Attempts != 0 {
		t.Fatalf("unexpected due event: %+v", due[0])
	}

	nextAttempt := now.Add(2 * time.Minute)
	if err := repo.MarkChatwootForwardEventFailed(due[0].ID, "still down", nextAttempt); err != nil {
		t.Fatalf("mark retry failed: %v", err)
	}

	due, err = repo.ListDueChatwootForwardEvents(nextAttempt.Add(-time.Second), 10)
	if err != nil {
		t.Fatalf("list before next attempt: %v", err)
	}
	if len(due) != 0 {
		t.Fatalf("due before next len = %d, want 0", len(due))
	}

	due, err = repo.ListDueChatwootForwardEvents(nextAttempt, 10)
	if err != nil {
		t.Fatalf("list next due events: %v", err)
	}
	if len(due) != 1 || due[0].Attempts != 1 || due[0].LastError != "still down" {
		t.Fatalf("unexpected retried event: %+v", due)
	}

	if err := repo.MarkChatwootForwardEventDone(due[0].ID); err != nil {
		t.Fatalf("mark retry done: %v", err)
	}
	due, err = repo.ListDueChatwootForwardEvents(nextAttempt.Add(time.Hour), 10)
	if err != nil {
		t.Fatalf("list after done: %v", err)
	}
	if len(due) != 0 {
		t.Fatalf("due after done len = %d, want 0", len(due))
	}
}

// replyLink is a link in the synthetic reply scope: Chatwoot message 41 of
// conversation 7 (account 3, inbox 5, config 11), sent by device-a to peer-a.
func replyLink(waMessageID string, createdAt time.Time) *domainChatStorage.ChatwootMessageLink {
	return &domainChatStorage.ChatwootMessageLink{
		DeviceID: "[email protected]", WhatsAppMessageID: waMessageID, WhatsAppChatJID: "[email protected]",
		ChatwootMessageID: 41, ChatwootConversationID: 7, ChatwootAccountID: 3, ChatwootInboxID: 5, ChatwootConfigID: 11,
		Direction: "outgoing", CreatedAt: createdAt,
	}
}

func replyLinkFilter() domainChatStorage.ChatwootReplyLinkFilter {
	return domainChatStorage.ChatwootReplyLinkFilter{
		DeviceID: "[email protected]", ChatwootMessageID: 41, ChatwootConversationID: 7,
		ChatwootAccountID: 3, ChatwootInboxID: 5, ChatwootConfigID: 11,
	}
}

// storeReplyLinks returns a fresh repository holding links.
func storeReplyLinks(t *testing.T, links ...*domainChatStorage.ChatwootMessageLink) *SQLiteRepository {
	t.Helper()
	repo := newTestSQLiteRepository(t)
	for _, link := range links {
		if err := repo.UpsertChatwootMessageLink(link); err != nil {
			t.Fatalf("upsert %s: %v", link.WhatsAppMessageID, err)
		}
	}
	return repo
}

// findReplyLink returns the WhatsApp id of the link found for filter, or "".
func findReplyLink(t *testing.T, repo *SQLiteRepository, filter domainChatStorage.ChatwootReplyLinkFilter) string {
	t.Helper()
	link, err := repo.GetChatwootMessageLinkForReply(filter)
	if err != nil {
		t.Fatalf("GetChatwootMessageLinkForReply: %v", err)
	}
	if link == nil {
		return ""
	}
	return link.WhatsAppMessageID
}

// The scope is applied before LIMIT 1. A link that differs in one field is
// older and sorts first, so it would be returned if that field were left out
// of the query; on its own it is never returned.
func TestSQLiteRepositoryReplyLinkLookupScopesBeforeLimit(t *testing.T) {
	base := time.Date(2026, time.October, 1, 9, 0, 0, 0, time.UTC)
	cases := map[string]func(*domainChatStorage.ChatwootMessageLink){
		"device":       func(l *domainChatStorage.ChatwootMessageLink) { l.DeviceID = "[email protected]" },
		"message":      func(l *domainChatStorage.ChatwootMessageLink) { l.ChatwootMessageID = 40 },
		"conversation": func(l *domainChatStorage.ChatwootMessageLink) { l.ChatwootConversationID = 8 },
		"account":      func(l *domainChatStorage.ChatwootMessageLink) { l.ChatwootAccountID = 4 },
		"inbox":        func(l *domainChatStorage.ChatwootMessageLink) { l.ChatwootInboxID = 6 },
		"config":       func(l *domainChatStorage.ChatwootMessageLink) { l.ChatwootConfigID = 0 },
	}
	for field, differ := range cases {
		t.Run(field, func(t *testing.T) {
			other := replyLink("WA-A-OTHER", base)
			differ(other)
			repo := storeReplyLinks(t, other)
			if got := findReplyLink(t, repo, replyLinkFilter()); got != "" {
				t.Fatalf("only a link of another %s: got %q, want none", field, got)
			}
			if err := repo.UpsertChatwootMessageLink(replyLink("WA-B-MATCH", base.Add(time.Minute))); err != nil {
				t.Fatalf("upsert WA-B-MATCH: %v", err)
			}
			if got := findReplyLink(t, repo, replyLinkFilter()); got != "WA-B-MATCH" {
				t.Fatalf("next to a link of another %s: got %q, want WA-B-MATCH", field, got)
			}
		})
	}
}

// Account 0 (legacy) rows match only when the caller allows them and the
// config is 0. Config 0 is an equality, never a wildcard, and the exact
// account comes before an older legacy row.
func TestSQLiteRepositoryReplyLinkLookupLegacyAccountZero(t *testing.T) {
	base := time.Date(2026, time.October, 1, 9, 0, 0, 0, time.UTC)
	link := func(waMessageID string, accountID int, configID int64, minute int) *domainChatStorage.ChatwootMessageLink {
		l := replyLink(waMessageID, base.Add(time.Duration(minute)*time.Minute))
		l.ChatwootAccountID, l.ChatwootConfigID = accountID, configID
		return l
	}
	filter := func(accountID int, configID int64, allowLegacy bool) domainChatStorage.ChatwootReplyLinkFilter {
		f := replyLinkFilter()
		f.ChatwootAccountID, f.ChatwootConfigID, f.AllowLegacyAccountZero = accountID, configID, allowLegacy
		return f
	}
	cases := []struct {
		name   string
		filter domainChatStorage.ChatwootReplyLinkFilter
		links  []*domainChatStorage.ChatwootMessageLink
		want   string
	}{
		{"legacy row allowed", filter(3, 0, true), []*domainChatStorage.ChatwootMessageLink{link("WA-LEGACY", 0, 0, 0)}, "WA-LEGACY"},
		{"legacy row not allowed", filter(3, 0, false), []*domainChatStorage.ChatwootMessageLink{link("WA-LEGACY", 0, 0, 0)}, ""},
		{"account 0 under a config", filter(3, 11, true), []*domainChatStorage.ChatwootMessageLink{link("WA-LEGACY", 0, 11, 0)}, ""},
		{"legacy row of another account", filter(3, 0, true), []*domainChatStorage.ChatwootMessageLink{link("WA-OTHER", 4, 0, 0)}, ""},
		{"config 0 is not a wildcard", filter(3, 0, true), []*domainChatStorage.ChatwootMessageLink{link("WA-CURRENT", 3, 11, 0)}, ""},
		{"no account", filter(0, 0, false), []*domainChatStorage.ChatwootMessageLink{link("WA-LEGACY", 0, 0, 0)}, ""},
		{"exact account before an older legacy row", filter(3, 0, true),
			[]*domainChatStorage.ChatwootMessageLink{link("WA-A-LEGACY", 0, 0, 0), link("WA-B-EXACT", 3, 0, 1)}, "WA-B-EXACT"},
	}
	for _, tc := range cases {
		t.Run(tc.name, func(t *testing.T) {
			if got := findReplyLink(t, storeReplyLinks(t, tc.links...), tc.filter); got != tc.want {
				t.Fatalf("got %q, want %q", got, tc.want)
			}
		})
	}
}

// A message sent with several attachments has one link per file; the lookup
// returns the first one delivered, whatever the insertion order.
func TestSQLiteRepositoryReplyLinkLookupPicksTheFirstFile(t *testing.T) {
	base := time.Date(2026, time.October, 1, 9, 0, 0, 0, time.UTC)
	first, second := replyLink("WA-Z-FIRST", base), replyLink("WA-A-SECOND", base.Add(time.Second))
	if got := findReplyLink(t, storeReplyLinks(t, first, second), replyLinkFilter()); got != "WA-Z-FIRST" {
		t.Fatalf("first stored first: got %q, want WA-Z-FIRST", got)
	}
	if got := findReplyLink(t, storeReplyLinks(t, second, first), replyLinkFilter()); got != "WA-Z-FIRST" {
		t.Fatalf("first stored last: got %q, want WA-Z-FIRST", got)
	}
	if got := findReplyLink(t, storeReplyLinks(t, replyLink("WA-B", base), replyLink("WA-A", base)), replyLinkFilter()); got != "WA-A" {
		t.Fatalf("same created_at: got %q, want the lowest WhatsApp id WA-A", got)
	}
}