package automation import ( "context" "net/http" "net/http/httptest" "testing" "time" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) // --- webhookEventName tests --- func TestWebhookEventName_CustomEvent_Cov(t *testing.T) { req := AutomationWebhookRequest{WebhookEvent: "custom.event", EventName: "message_created"} assert.Equal(t, "custom.event", webhookEventName(req)) } func TestWebhookEventName_DefaultEvent_Cov(t *testing.T) { req := AutomationWebhookRequest{EventName: "message_created"} assert.Equal(t, "automation_event.message_created", webhookEventName(req)) } func TestWebhookEventName_EmptyEvent_Cov(t *testing.T) { req := AutomationWebhookRequest{WebhookEvent: " ", EventName: "conversation_opened"} assert.Equal(t, "automation_event.conversation_opened", webhookEventName(req)) } // --- HTTPAutomationWebhookDeliverer tests --- func TestHTTPAutomationWebhookDeliverer_Deliver_Cov(t *testing.T) { server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) })) defer server.Close() d := NewHTTPAutomationWebhookDeliverer(nil, 1, 0) result, err := d.DeliverWebhook(context.Background(), AutomationWebhookRequest{ URL: server.URL, EventName: "test_event", }) require.NoError(t, err) assert.Equal(t, 200, result.ResponseCode) } func TestHTTPAutomationWebhookDeliverer_DeliverFailure_Cov(t *testing.T) { server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusInternalServerError) })) defer server.Close() d := NewHTTPAutomationWebhookDeliverer(nil, 1, 0) _, err := d.DeliverWebhook(context.Background(), AutomationWebhookRequest{ URL: server.URL, EventName: "test_event", }) require.Error(t, err) } func TestHTTPAutomationWebhookDeliverer_DeliverInvalidURL_Cov(t *testing.T) { d := NewHTTPAutomationWebhookDeliverer(nil, 1, 0) _, err := d.DeliverWebhook(context.Background(), AutomationWebhookRequest{ URL: "http://invalid.invalid.invalid", }) assert.Error(t, err) } func TestNewHTTPAutomationWebhookDeliverer_NilClient_Cov(t *testing.T) { d := NewHTTPAutomationWebhookDeliverer(nil, 3, 5*time.Second) assert.NotNil(t, d) } func TestNewHTTPAutomationWebhookDeliverer_WithClient_Cov(t *testing.T) { client := &http.Client{Timeout: 10 * time.Second} d := NewHTTPAutomationWebhookDeliverer(client, 3, 5*time.Second) assert.NotNil(t, d) } // --- ResolveTemplateVars tests --- func TestResolveTemplateVars_NoVars_Cov(t *testing.T) { result := ResolveTemplateVars("hello world", TemplateContext{}) assert.Equal(t, "hello world", result) } func TestResolveActionParams_NoParams_Cov(t *testing.T) { result := ResolveActionParams(nil, TemplateContext{}) assert.Nil(t, result) } func TestResolveActionParams_StaticParams_Cov(t *testing.T) { params := map[string]interface{}{"key": "static_value"} result := ResolveActionParams(params, TemplateContext{}) assert.Equal(t, "static_value", result["key"]) }