Files
llmgw/internal/notify/notifytest/notifytest.go
T
Ilker ManapandClaude Opus 5.5 1b71c38f89 Notifications: webhooks for a parked model, a lost node, a spent budget and new weights; Alertmanager receiver templates
notify.webhooks sends signed JSON or Slack-compatible messages, queued,
retried, de-duplicated hourly; ctl notify test checks each webhook.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-04 16:58:11 +02:00

42 lines
1009 B
Go

// 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
}
}