Files
gochat/backend/internal/channel/whatsapp/service_test.go
T
rogee aeddedf2a3 Reorganize repo: backend/, deploy/, docs/ layout + AGENTS.md
Restructure the monorepo into clear top-level directories:
- backend/: Go module root (cmd, internal, pkg, configs, migrations,
  docs/swagger, scripts, tests, go.mod, Makefile, .air.toml)
- deploy/: Docker (Dockerfile, docker-compose*), quickstart, fluentd
- docs/: project documentation + reports/ (moved from repo root)
- AGENTS.md: new AI coding-agent guide at repo root

Update all references to the new layout:
- Dockerfile: COPY backend/go.mod, COPY backend/ (context = repo root)
- docker-compose files: context ../.., dockerfile deploy/docker/Dockerfile,
  env_file ../../.env, volume mounts ../../backend:/app
- deploy/quickstart/compose.yaml: dockerfile deploy/docker/Dockerfile
- CI: working-directory: backend for go commands, file deploy/docker/Dockerfile,
  coverage path backend/coverage.out, health_check backend/scripts/
- backend/Makefile: docker target uses -f ../deploy/docker/Dockerfile ../
- README: architecture tree, quickstart, config paths updated

Move root stray scripts (rename_models.*, run_m11_tests.sh, verify_build.sh,
gorm_bool_main.go) to backend/scripts/legacy/. All moves via git mv to
preserve history. Build, vet, SQLite tests, and docker compose config verified.
2026-07-07 14:44:12 +08:00

84 lines
3.6 KiB
Go

package whatsapp
import (
"bytes"
"context"
"io"
"net/http"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
channelmodel "github.com/gochat/gochat/internal/model/channel"
)
func TestWhatsAppService_FetchHealthStatusFormatsChatwootPayload(t *testing.T) {
t.Setenv("FRONTEND_URL", "https://app.example.com")
svc := NewWhatsAppService(nil)
svc.graphAPIBase = "https://graph.example.test/v22.0"
svc.client.SetTransport(roundTripFunc(func(r *http.Request) (*http.Response, error) {
require.Equal(t, "/v22.0/phone-123", r.URL.Path)
assert.Contains(t, r.URL.Query().Get("fields"), "quality_rating")
assert.Equal(t, "token-abc", r.URL.Query().Get("access_token"))
body := `{"id":"phone-123","display_phone_number":"+1 555 0100","verified_name":"Support","name_status":"APPROVED","quality_rating":"GREEN","messaging_limit_tier":"TIER_1K","account_mode":"LIVE","code_verification_status":"VERIFIED","webhook_configuration":{"application":"app-1"},"throughput":{"level":"STANDARD"},"last_onboarded_time":"2026-06-05T00:00:00Z","platform_type":"CLOUD_API","certificate":"cert-data"}`
return &http.Response{
StatusCode: http.StatusOK,
Header: http.Header{"Content-Type": []string{"application/json"}},
Body: io.NopCloser(bytes.NewBufferString(body)),
}, nil
}))
channel := &channelmodel.ChannelWhatsApp{
PhoneNumber: "+1555010000",
PhoneNumberID: "phone-123",
BusinessAccountID: "waba-456",
AccessToken: "token-abc",
}
payload, err := svc.FetchHealthStatus(context.Background(), channel)
require.NoError(t, err)
assert.Equal(t, "phone-123", payload["id"])
assert.Equal(t, "GREEN", payload["quality_rating"])
assert.Equal(t, "https://app.example.com/webhooks/whatsapp/+1555010000", payload["expected_webhook_url"])
assert.Equal(t, "waba-456", payload["business_id"])
assert.NotContains(t, payload, "healthy")
}
func TestWhatsAppService_UpdateCallingStatusPostsSettings(t *testing.T) {
svc := NewWhatsAppService(nil)
svc.graphAPIBase = "https://graph.example.test/v22.0"
svc.client.SetTransport(roundTripFunc(func(r *http.Request) (*http.Response, error) {
require.Equal(t, "/v22.0/phone-123/settings", r.URL.Path)
assert.Equal(t, "Bearer token-abc", r.Header.Get("Authorization"))
body, err := io.ReadAll(r.Body)
require.NoError(t, err)
assert.JSONEq(t, `{"calling":{"status":"ENABLED"}}`, string(body))
return &http.Response{StatusCode: http.StatusOK, Body: io.NopCloser(bytes.NewBufferString(`{"success":true}`))}, nil
}))
channel := &channelmodel.ChannelWhatsApp{Provider: "whatsapp_cloud", PhoneNumberID: "phone-123", AccessToken: "token-abc"}
err := svc.UpdateCallingStatus(context.Background(), channel, "ENABLED")
require.NoError(t, err)
}
func TestWhatsAppService_UpdateCallingStatusUsesMetaErrorMessage(t *testing.T) {
svc := NewWhatsAppService(nil)
svc.graphAPIBase = "https://graph.example.test/v22.0"
svc.client.SetTransport(roundTripFunc(func(r *http.Request) (*http.Response, error) {
body := `{"error":{"error_user_msg":"Calling is not available for this number","message":"fallback"}}`
return &http.Response{StatusCode: http.StatusBadRequest, Body: io.NopCloser(bytes.NewBufferString(body))}, nil
}))
channel := &channelmodel.ChannelWhatsApp{Provider: "whatsapp_cloud", PhoneNumberID: "phone-123", AccessToken: "token-abc"}
err := svc.UpdateCallingStatus(context.Background(), channel, "ENABLED")
require.Error(t, err)
assert.Equal(t, "Calling is not available for this number", err.Error())
}
type roundTripFunc func(*http.Request) (*http.Response, error)
func (f roundTripFunc) RoundTrip(req *http.Request) (*http.Response, error) {
return f(req)
}