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.

10.6 KB
Raw Blame
package chatwoot

import (
	"sync"
	"testing"

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

// fakeConfigRepo implements just the methods ClientRegistry uses; the rest of
// IChatStorageRepository is satisfied by the embedded (nil) interface.
type fakeConfigRepo struct {
	domainChatStorage.IChatStorageRepository
	byIdentifier map[string]*domainChatStorage.ChatwootDeviceConfig
	byInbox      map[[2]int]*domainChatStorage.ChatwootDeviceConfig
	count        int
}

func (f *fakeConfigRepo) GetChatwootDeviceConfigByIdentifier(identifier string) (*domainChatStorage.ChatwootDeviceConfig, error) {
	return f.byIdentifier[identifier], nil
}

func (f *fakeConfigRepo) GetChatwootDeviceConfigByInbox(accountID, inboxID int) (*domainChatStorage.ChatwootDeviceConfig, error) {
	return f.byInbox[[2]int{accountID, inboxID}], nil
}

func (f *fakeConfigRepo) CountChatwootDeviceConfigs() (int, error) {
	return f.count, nil
}

func TestClientRegistryResolveByDeviceIDAndJID(t *testing.T) {
	cfg := &domainChatStorage.ChatwootDeviceConfig{
		ID: 7, DeviceID: "busine", DeviceJID: "[email protected]",
		ChatwootURL: "https://chat.example.com", AccountID: 3, InboxID: 9,
		APIToken: "tok", Enabled: true,
	}
	repo := &fakeConfigRepo{
		byIdentifier: map[string]*domainChatStorage.ChatwootDeviceConfig{
			"busine":             cfg,
			"[email protected]": cfg,
		},
		count: 1,
	}
	reg := NewClientRegistry(repo)

	for _, id := range []string{"busine", "[email protected]"} {
		rc, err := reg.Resolve(id)
		if err != nil || rc == nil || rc.Client == nil {
			t.Fatalf("resolve %q: rc=%v err=%v", id, rc, err)
		}
		if rc.ConfigID != 7 || rc.Client.AccountID != 3 || rc.Client.InboxID != 9 || rc.Client.BaseURL != "https://chat.example.com" {
			t.Fatalf("resolve %q wrong: %+v / %+v", id, rc, rc.Client)
		}
	}
}

func TestClientRegistryDisabledConfigReturnsNil(t *testing.T) {
	repo := &fakeConfigRepo{
		byIdentifier: map[string]*domainChatStorage.ChatwootDeviceConfig{
			"d": {ID: 1, DeviceID: "d", Enabled: false, AccountID: 1, InboxID: 1, ChatwootURL: "https://x.example.com", APIToken: "t"},
		},
		count: 1,
	}
	reg := NewClientRegistry(repo)
	rc, err := reg.Resolve("d")
	if err != nil {
		t.Fatalf("err: %v", err)
	}
	if rc != nil {
		t.Fatalf("disabled config must resolve to nil (skip), got %+v", rc)
	}
}

func TestClientRegistryEmptyTableFallsBackToEnv(t *testing.T) {
	origURL, origTok, origAcc, origInbox := config.ChatwootURL, config.ChatwootAPIToken, config.ChatwootAccountID, config.ChatwootInboxID
	t.Cleanup(func() {
		config.ChatwootURL, config.ChatwootAPIToken, config.ChatwootAccountID, config.ChatwootInboxID = origURL, origTok, origAcc, origInbox
	})
	config.ChatwootURL = "https://env.example.com"
	config.ChatwootAPIToken = "env-tok"
	config.ChatwootAccountID = 1
	config.ChatwootInboxID = 2

	repo := &fakeConfigRepo{count: 0} // empty table
	reg := NewClientRegistry(repo)
	rc, err := reg.Resolve("anything")
	if err != nil || rc == nil || rc.Client == nil {
		t.Fatalf("legacy resolve: rc=%v err=%v", rc, err)
	}
	if rc.ConfigID != 0 {
		t.Fatalf("legacy config must have ConfigID 0, got %d", rc.ConfigID)
	}
	if rc.Client.BaseURL != "https://env.example.com" || rc.Client.AccountID != 1 || rc.Client.InboxID != 2 {
		t.Fatalf("legacy client not built from env: %+v", rc.Client)
	}
}

func TestClientRegistryNonEmptyNoMatchFailsFast(t *testing.T) {
	repo := &fakeConfigRepo{count: 2} // table has configs, but none match "ghost"
	reg := NewClientRegistry(repo)
	rc, err := reg.Resolve("ghost")
	if err != nil {
		t.Fatalf("err: %v", err)
	}
	if rc != nil {
		t.Fatalf("unmapped device with non-empty table must fail-fast (nil), got %+v", rc)
	}
}

func TestClientRegistryInvalidateRefreshes(t *testing.T) {
	cfg := &domainChatStorage.ChatwootDeviceConfig{ID: 1, DeviceID: "d", ChatwootURL: "https://a.example.com", AccountID: 1, InboxID: 1, APIToken: "t", Enabled: true}
	repo := &fakeConfigRepo{
		byIdentifier: map[string]*domainChatStorage.ChatwootDeviceConfig{"d": cfg},
		count:        1,
	}
	reg := NewClientRegistry(repo)

	rc, _ := reg.Resolve("d")
	if rc.Client.InboxID != 1 {
		t.Fatalf("initial inbox = %d, want 1", rc.Client.InboxID)
	}
	// Change underlying config, then invalidate: next resolve must reflect it.
	cfg.InboxID = 99
	reg.Invalidate("d")
	rc, _ = reg.Resolve("d")
	if rc.Client.InboxID != 99 {
		t.Fatalf("after invalidate inbox = %d, want 99 (stale cache)", rc.Client.InboxID)
	}
}

// A device that resolved to the env fallback while the table was empty must
// not keep that client after the first per-device config is written. The
// forward path caches under the JID (with DeviceID = JID), so the write-path
// Invalidate(<user-facing id>) can only clear it by purging legacy entries
// wholesale — this is the regression test for that purge.
func TestClientRegistryInvalidatePurgesEnvFallbackEntries(t *testing.T) {
	origURL, origTok, origAcc, origInbox := config.ChatwootURL, config.ChatwootAPIToken, config.ChatwootAccountID, config.ChatwootInboxID
	t.Cleanup(func() {
		config.ChatwootURL, config.ChatwootAPIToken, config.ChatwootAccountID, config.ChatwootInboxID = origURL, origTok, origAcc, origInbox
	})
	config.ChatwootURL = "https://env.example.com"
	config.ChatwootAPIToken = "env-tok"
	config.ChatwootAccountID = 1
	config.ChatwootInboxID = 2

	repo := &fakeConfigRepo{byIdentifier: map[string]*domainChatStorage.ChatwootDeviceConfig{}}
	reg := NewClientRegistry(repo)

	// Table empty: the forward path (keyed by JID) resolves the env fallback.
	rc, err := reg.Resolve("[email protected]")
	if err != nil || rc == nil || rc.ConfigID != 0 {
		t.Fatalf("precondition: env fallback expected, got rc=%+v err=%v", rc, err)
	}

	// Operator creates this device's config via PUT (user-facing id "busine");
	// the handler invalidates by that id only.
	cfg := &domainChatStorage.ChatwootDeviceConfig{
		ID: 7, DeviceID: "busine", DeviceJID: "[email protected]",
		ChatwootURL: "https://device.example.com", AccountID: 3, InboxID: 9,
		APIToken: "tok", Enabled: true,
	}
	repo.byIdentifier["busine"] = cfg
	repo.byIdentifier["[email protected]"] = cfg
	repo.count = 1
	reg.Invalidate("busine")

	// The same device's next forward must use its own config, not the env inbox.
	rc, err = reg.Resolve("[email protected]")
	if err != nil || rc == nil {
		t.Fatalf("resolve after config create: rc=%v err=%v", rc, err)
	}
	if rc.ConfigID != 7 || rc.Client.BaseURL != "https://device.example.com" {
		t.Fatalf("stale env fallback survived Invalidate: %+v / %+v", rc, rc.Client)
	}

	// And an unrelated device that had also cached the env fallback fails fast.
	reg2 := NewClientRegistry(&fakeConfigRepo{byIdentifier: map[string]*domainChatStorage.ChatwootDeviceConfig{}})
	if rc, _ := reg2.Resolve("[email protected]"); rc == nil || rc.ConfigID != 0 {
		t.Fatalf("precondition: env fallback expected for unrelated device")
	}
	reg2.repo.(*fakeConfigRepo).count = 1
	reg2.Invalidate("busine")
	if rc, _ := reg2.Resolve("[email protected]"); rc != nil {
		t.Fatalf("unmapped device must fail-fast after first config write, got %+v", rc)
	}
}

type blockingConfigRepo struct {
	domainChatStorage.IChatStorageRepository
	mu      sync.Mutex
	current *domainChatStorage.ChatwootDeviceConfig
	calls   int
	entered chan struct{}
	release chan struct{}
	once    sync.Once
}

func (b *blockingConfigRepo) GetChatwootDeviceConfigByIdentifier(string) (*domainChatStorage.ChatwootDeviceConfig, error) {
	b.mu.Lock()
	b.calls++
	call := b.calls
	cfg := b.current
	b.mu.Unlock()
	if call == 1 {
		b.once.Do(func() { close(b.entered) })
		<-b.release
		return cfg, nil
	}
	b.mu.Lock()
	defer b.mu.Unlock()
	return b.current, nil
}

func (b *blockingConfigRepo) CountChatwootDeviceConfigs() (int, error) { return 1, nil }

func TestClientRegistryDoesNotCacheClientInvalidatedDuringLoad(t *testing.T) {
	oldCfg := &domainChatStorage.ChatwootDeviceConfig{
		ID: 1, DeviceID: "d", ChatwootURL: "https://old.example.com", AccountID: 1, InboxID: 1, APIToken: "t", Enabled: true,
	}
	newCfg := &domainChatStorage.ChatwootDeviceConfig{
		ID: 1, DeviceID: "d", ChatwootURL: "https://new.example.com", AccountID: 1, InboxID: 9, APIToken: "t", Enabled: true,
	}
	repo := &blockingConfigRepo{current: oldCfg, entered: make(chan struct{}), release: make(chan struct{})}
	reg := NewClientRegistry(repo)

	type result struct {
		rc  *ResolvedConfig
		err error
	}
	done := make(chan result, 1)
	go func() {
		rc, err := reg.Resolve("d")
		done <- result{rc, err}
	}()
	<-repo.entered
	repo.mu.Lock()
	repo.current = newCfg
	repo.mu.Unlock()
	reg.Invalidate("d")
	close(repo.release)

	got := <-done
	if got.err != nil {
		t.Fatalf("resolve: %v", got.err)
	}
	if got.rc == nil || got.rc.Client == nil || got.rc.Client.BaseURL != "https://new.example.com" || got.rc.Client.InboxID != 9 {
		t.Fatalf("in-flight resolve kept the pre-invalidation client: %+v", got.rc)
	}
	again, err := reg.Resolve("d")
	if err != nil || again == nil || again.Client == nil || again.Client.BaseURL != "https://new.example.com" {
		t.Fatalf("later resolve is stale: %+v err=%v", again, err)
	}
}

func TestClientRegistryDisabledDuringLoadIsNotCached(t *testing.T) {
	oldCfg := &domainChatStorage.ChatwootDeviceConfig{
		ID: 1, DeviceID: "d", ChatwootURL: "https://old.example.com", AccountID: 1, InboxID: 1, APIToken: "t", Enabled: true,
	}
	disabled := &domainChatStorage.ChatwootDeviceConfig{
		ID: 1, DeviceID: "d", ChatwootURL: "https://old.example.com", AccountID: 1, InboxID: 1, APIToken: "t", Enabled: false,
	}
	repo := &blockingConfigRepo{current: oldCfg, entered: make(chan struct{}), release: make(chan struct{})}
	reg := NewClientRegistry(repo)

	done := make(chan *ResolvedConfig, 1)
	go func() {
		rc, err := reg.Resolve("d")
		if err != nil {
			t.Errorf("resolve: %v", err)
		}
		done <- rc
	}()
	<-repo.entered
	repo.mu.Lock()
	repo.current = disabled
	repo.mu.Unlock()
	reg.Invalidate("d")
	close(repo.release)

	if rc := <-done; rc != nil {
		t.Fatalf("disabled config was returned: %+v", rc)
	}
	if rc, err := reg.Resolve("d"); err != nil || rc != nil {
		t.Fatalf("disabled config was cached: %+v err=%v", rc, err)
	}
}

func TestClientRegistryResolveByInbox(t *testing.T) {
	cfg := &domainChatStorage.ChatwootDeviceConfig{ID: 5, DeviceID: "d", ChatwootURL: "https://a.example.com", AccountID: 3, InboxID: 9, APIToken: "t", Enabled: true}
	repo := &fakeConfigRepo{byInbox: map[[2]int]*domainChatStorage.ChatwootDeviceConfig{{3, 9}: cfg}}
	reg := NewClientRegistry(repo)

	rc, err := reg.ResolveByInbox(3, 9)
	if err != nil || rc == nil || rc.ConfigID != 5 || rc.Client.InboxID != 9 {
		t.Fatalf("resolve by inbox: rc=%v err=%v", rc, err)
	}
	if rc, _ := reg.ResolveByInbox(1, 1); rc != nil {
		t.Fatalf("unknown inbox must resolve nil, got %+v", rc)
	}
}