Skip to content
quarry Artifact [40720fafd3]
Login

Artifact [40720fafd3]

Artifact 40720fafd3cd1c069b8a914dc5c188f533b02d06a78c60007fb7474d098c0fd2:


package notify

import (
	"context"
	"encoding/json"
	"io"
	"net/http"
	"net/http/httptest"
	"testing"
)

// The client POSTs the Novu-shaped trigger to /v1/events/trigger.
func TestTriggerShape(t *testing.T) {
	var got map[string]any
	srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		if r.URL.Path != "/v1/events/trigger" || r.Method != "POST" {
			t.Errorf("unexpected %s %s", r.Method, r.URL.Path)
		}
		b, _ := io.ReadAll(r.Body)
		json.Unmarshal(b, &got)
		w.WriteHeader(201)
	}))
	defer srv.Close()

	c := New(srv.URL)
	err := c.Trigger(context.Background(), "quarry-mention",
		Recipient{SubscriberID: "dan"},
		map[string]any{"repo": "r1", "text": "hi @dan"}, "txn-1")
	if err != nil {
		t.Fatal(err)
	}
	if got["name"] != "quarry-mention" || got["transactionId"] != "txn-1" {
		t.Fatalf("trigger body = %+v", got)
	}
	to := got["to"].(map[string]any)
	if to["subscriberId"] != "dan" {
		t.Fatalf("recipient = %+v", to)
	}
	if p := got["payload"].(map[string]any); p["repo"] != "r1" {
		t.Fatalf("payload = %+v", p)
	}
}

// A disabled client (empty base) no-ops without a call.
func TestDisabledClientNoOps(t *testing.T) {
	if err := New("").Trigger(context.Background(), "x", Recipient{}, nil, ""); err != nil {
		t.Fatalf("disabled client should no-op: %v", err)
	}
	var nilClient *Client
	if err := nilClient.Trigger(context.Background(), "x", Recipient{}, nil, ""); err != nil {
		t.Fatalf("nil client should no-op: %v", err)
	}
}

// A non-2xx response surfaces as an error (best-effort callers log it).
func TestTriggerErrorStatus(t *testing.T) {
	srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
		w.WriteHeader(500)
	}))
	defer srv.Close()
	if err := New(srv.URL).Trigger(context.Background(), "x", Recipient{SubscriberID: "u"}, nil, ""); err == nil {
		t.Fatal("want error on 500")
	}
}