package main
import (
"os"
"regexp"
"strings"
"testing"
)
// Every "no such endpoint / delivery" refusal in the webhook seam is a
// not-found, by sentinel — never a bare error, which the router maps
// to 500.
//
// This exists because the same fix was made twice and was incomplete
// both times. Ping and Redeliver were given ErrNotFound; Deliveries and
// RotateWebhookSecret, two methods further down the same types, were
// not, so GET .../hook/<gone>/deliveries answered 500 "internal error"
// for a hook that had simply been removed — measured on production the
// hour after the first fix shipped. Fixing the methods one remembers is
// how the third one is missed; this reads every such sentence in the
// file instead.
func TestEveryMissingHookRefusalIsNotFound(t *testing.T) {
src, err := os.ReadFile("main.go")
if err != nil {
t.Fatal(err)
}
re := regexp.MustCompile(`fmt\.Errorf\("(no endpoint|no delivery|the endpoint delivery)[^\n]*`)
var bare []string
n := 0
for _, m := range re.FindAllString(string(src), -1) {
n++
if !strings.Contains(m, "ops.ErrNotFound") {
bare = append(bare, m)
}
}
if n == 0 {
t.Fatal("FAIL: found no hook refusals at all; the pattern no longer matches the code")
}
if len(bare) > 0 {
t.Fatalf("FAIL: %d of %d hook refusals are not ErrNotFound and will answer 500:\n %s",
len(bare), n, strings.Join(bare, "\n "))
}
t.Logf("ok: all %d hook refusals wrap ErrNotFound", n)
}