// Package notifytest catches notifications in tests. package notifytest import ( "encoding/json" "io" "net/http" "net/http/httptest" "sync" "testing" "gitea.mynodes.xyz/ilker/llmgw/internal/notify" ) // Catch returns a notifier whose webhook records what it is sent, and a func // that waits for the sending and returns the events. The notifier takes no // more events once that func is called. func Catch(t *testing.T) (*notify.Notifier, func() []notify.Event) { t.Helper() var mu sync.Mutex var got []notify.Event srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { b, _ := io.ReadAll(req.Body) var e notify.Event _ = json.Unmarshal(b, &e) mu.Lock() got = append(got, e) mu.Unlock() })) t.Cleanup(srv.Close) n, err := notify.New(t.Context(), notify.Config{Webhooks: []notify.Webhook{{URL: srv.URL}}}, nil) if err != nil { t.Fatal(err) } return n, func() []notify.Event { n.Wait() mu.Lock() defer mu.Unlock() return got } }