Artifact 40720fafd3cd1c069b8a914dc5c188f533b02d06a78c60007fb7474d098c0fd2:
- File
internal/notify/notify_test.go
— part of check-in
[37f62c9a82]
at
2026-08-16 05:04:31
on branch trunk
— notify: fire mention and activity triggers to the notify companion (0001 §5)
internal/notify: a Novu-compatible POST /v1/events/trigger client (quarry triggers, notify owns delivery). chat.post parses @mentions and fires quarry-mention per handle; wiki/ticket write ops fire quarry-activity to a repo:<name> topic. All best-effort — a notify outage never fails the user action — with idempotency keys so replays dedup. --notify enables it (disabled by default). Verified live: mention and activity triggers POSTed with the right Novu shape. (user: dmestas, size: 1889) [annotate] [blame] [check-ins using]
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") } }