feat(channels): align whatsapp calling toggles

This commit is contained in:
2026-06-06 20:10:06 +08:00
parent 0277898979
commit e2fdb8a171
12 changed files with 436 additions and 10 deletions
+78
View File
@@ -64,7 +64,10 @@ type fakeInboxWhatsAppService struct {
templateErr error
fetchCalls int
webhookURL string
webhookFields []string
webhookErr error
callingStatus string
callingErr error
}
func (f *fakeInboxWhatsAppService) FetchMessageTemplates(context.Context, *channelmodel.ChannelWhatsApp) ([]interface{}, error) {
@@ -84,6 +87,17 @@ func (f *fakeInboxWhatsAppService) SetupWebhook(_ context.Context, _ *channelmod
return f.webhookErr
}
func (f *fakeInboxWhatsAppService) SetupWebhookFields(_ context.Context, _ *channelmodel.ChannelWhatsApp, webhookURL string, fields []string) error {
f.webhookURL = webhookURL
f.webhookFields = fields
return f.webhookErr
}
func (f *fakeInboxWhatsAppService) UpdateCallingStatus(_ context.Context, _ *channelmodel.ChannelWhatsApp, status string) error {
f.callingStatus = status
return f.callingErr
}
// createInboxTestPrereqs creates prerequisite Account and Inbox for service tests.
func createInboxTestPrereqs(t *testing.T, db *gorm.DB, channelType string) (*model.Account, *model.Inbox) {
t.Helper()
@@ -447,3 +461,67 @@ func TestInboxService_RegisterWebhook_NonCloudWhatsAppRejected(t *testing.T) {
err := svc.RegisterWebhook(context.Background(), account.ID, inbox.ID, RegisterWebhookRequest{})
require.ErrorIs(t, err, ErrInboxHealthWhatsAppCloudOnly)
}
// ========================================
// WhatsApp calling service tests
// ========================================
func TestInboxService_EnableWhatsAppCalling_SetsProviderConfigAndWebhook(t *testing.T) {
svc, db := setupInboxServiceTest(t)
account, inbox, channel := createWhatsAppInboxTestPrereqs(t, db, "whatsapp_cloud")
account.FeatureFlags = `{"channel_voice":true}`
require.NoError(t, db.Save(account).Error)
fake := &fakeInboxWhatsAppService{}
svc.whatsappService = fake
t.Setenv("FRONTEND_URL", "https://app.example.test")
err := svc.EnableWhatsAppCalling(context.Background(), account.ID, inbox.ID)
require.NoError(t, err)
assert.Equal(t, "ENABLED", fake.callingStatus)
assert.Equal(t, "https://app.example.test/webhooks/whatsapp/+1555010000", fake.webhookURL)
assert.Empty(t, fake.webhookFields)
var updated channelmodel.ChannelWhatsApp
require.NoError(t, db.First(&updated, channel.ID).Error)
providerConfig := parseJSONMap(updated.ProviderConfig)
assert.Equal(t, true, providerConfig["calling_enabled"])
var updatedInbox model.Inbox
require.NoError(t, db.First(&updatedInbox, inbox.ID).Error)
channelConfig := parseJSONMap(updatedInbox.ChannelConfig)
assert.Equal(t, true, channelConfig["voice_enabled"])
}
func TestInboxService_EnableWhatsAppCalling_RequiresCloudAndFeature(t *testing.T) {
svc, db := setupInboxServiceTest(t)
account, inbox, _ := createWhatsAppInboxTestPrereqs(t, db, "360dialog")
account.FeatureFlags = `{"channel_voice":true}`
require.NoError(t, db.Save(account).Error)
svc.whatsappService = &fakeInboxWhatsAppService{}
err := svc.EnableWhatsAppCalling(context.Background(), account.ID, inbox.ID)
require.ErrorIs(t, err, ErrInboxWhatsAppCallingUnsupported)
account, inbox, _ = createWhatsAppInboxTestPrereqs(t, db, "whatsapp_cloud")
err = svc.EnableWhatsAppCalling(context.Background(), account.ID, inbox.ID)
require.ErrorIs(t, err, ErrInboxWhatsAppCallingFeatureRequired)
}
func TestInboxService_DisableWhatsAppCalling_PersistsFalseAndIgnoresWebhookFailure(t *testing.T) {
svc, db := setupInboxServiceTest(t)
account, inbox, channel := createWhatsAppInboxTestPrereqs(t, db, "whatsapp_cloud")
channel.ProviderConfig = `{"calling_enabled":true,"phone_number_id":"phone-1","business_account_id":"waba-1"}`
require.NoError(t, db.Save(channel).Error)
fake := &fakeInboxWhatsAppService{webhookErr: fmt.Errorf("meta unavailable")}
svc.whatsappService = fake
t.Setenv("FRONTEND_URL", "https://app.example.test")
err := svc.DisableWhatsAppCalling(context.Background(), account.ID, inbox.ID)
require.NoError(t, err)
assert.Equal(t, []string{"messages", "smb_message_echoes"}, fake.webhookFields)
var updated channelmodel.ChannelWhatsApp
require.NoError(t, db.First(&updated, channel.ID).Error)
providerConfig := parseJSONMap(updated.ProviderConfig)
assert.Equal(t, false, providerConfig["calling_enabled"])
}