Turn on push from onboarding step 3 and add a test-push endpoint (Feature 19)
CI / ci (push) Successful in 6m59s
CI / ci (pull_request) Successful in 7m2s

Onboarding step 3's "Turn on notifications" now calls usePush().subscribePush
straight from that tap (the only place onboarding asks for permission) and
shows how it ended: on; blocked, with how to allow it in the browser's site
settings; not supported, with how to add MealQuest to the Home Screen on
iPhone; or a dismissed prompt that can be asked again. "Maybe later" still
finishes without asking. The interim notificationsUnavailable is gone.

POST /api/v1/settings/notifications/test-push queues "MealQuest test
notification" through the notification outbox as a plan_ready event with a
fresh plan_ready:test-<ULID> key, opening /plan, on the push channel only
(new notify.Service.EmitPush), never Google Chat. It answers 202
{event_key, subscriptions}, or 409 no_subscriptions / push_off rather than
reporting success when nothing would be sent.

Maestro task f89279b8-b92c-4d9f-a6c2-fd87fccda509.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-09 16:54:23 -04:00
co-authored by Claude Opus 5.5
parent 51e0213e92
commit 875a0faaae
12 changed files with 505 additions and 139 deletions
+58 -4
View File
@@ -14,6 +14,7 @@ import (
"strings"
"code.cubecraftlabs.com/Maniacs_Mansion/Meal-Quest/internal/db"
"code.cubecraftlabs.com/Maniacs_Mansion/Meal-Quest/internal/notify"
"code.cubecraftlabs.com/Maniacs_Mansion/Meal-Quest/internal/store"
"code.cubecraftlabs.com/Maniacs_Mansion/Meal-Quest/internal/vapid"
)
@@ -69,22 +70,75 @@ func (e *ValidationError) Error() string {
return "invalid push subscription: " + strings.Join(parts, "; ")
}
// Service stores push subscriptions.
// Service stores push subscriptions and sends a test notification to them.
type Service struct {
st *store.Store
publicKey string
notifier *notify.Service
}
// NewService returns a Service over st handing out publicKey, the VAPID
// public key in vapid's encoding.
func NewService(st *store.Store, publicKey string) (*Service, error) {
// public key in vapid's encoding, that sends its test notification through
// notifier.
func NewService(st *store.Store, publicKey string, notifier *notify.Service) (*Service, error) {
if st == nil {
return nil, errors.New("push: nil store")
}
if _, err := vapid.ParsePublicKey(publicKey); err != nil {
return nil, fmt.Errorf("push: VAPID public key: %w", err)
}
return &Service{st: st, publicKey: publicKey}, nil
if notifier == nil {
return nil, errors.New("push: nil notifier")
}
return &Service{st: st, publicKey: publicKey, notifier: notifier}, nil
}
// The test notification SendTest sends.
const (
TestTitle = "MealQuest test notification"
TestBody = "Push works on this device. Tap to open the plan."
TestURL = "/plan"
)
// ErrNoSubscriptions is returned by SendTest when no device has turned on
// push: there is nothing to send the test to.
var ErrNoSubscriptions = errors.New("push: no device has turned on notifications")
// TestSent is a test notification queued for delivery: its event key and
// how many subscriptions were stored when it was queued.
type TestSent struct {
EventKey string `json:"event_key"`
Subscriptions int `json:"subscriptions"`
}
// SendTest queues TestTitle for every stored subscription, through the
// notification outbox as a plan_ready event whose tap opens TestURL. Its
// key, "plan_ready:test-" and a new id, is never used twice, so it is never
// taken for an event already sent. It goes to push alone, never to Google
// Chat. With no subscription it is ErrNoSubscriptions, and with push
// switched off for plan_ready it is notify.ErrPushOff; neither queues
// anything. Delivery happens after SendTest returns, with the outbox's
// retries.
func (s *Service) SendTest(ctx context.Context) (TestSent, error) {
subs, err := db.New(s.st.Reader).ListPushSubscriptions(ctx)
if err != nil {
return TestSent{}, fmt.Errorf("push: list subscriptions: %w", err)
}
if len(subs) == 0 {
return TestSent{}, ErrNoSubscriptions
}
key := string(notify.PlanReady) + ":test-" + store.NewID()
err = s.notifier.EmitPush(ctx, notify.Event{
Type: notify.PlanReady,
Key: key,
Title: TestTitle,
Body: TestBody,
URL: TestURL,
})
if err != nil {
return TestSent{}, fmt.Errorf("push: send test notification: %w", err)
}
return TestSent{EventKey: key, Subscriptions: len(subs)}, nil
}
// PublicKey returns the VAPID public key, for applicationServerKey.
+87 -3
View File
@@ -9,6 +9,8 @@ import (
"strings"
"testing"
"code.cubecraftlabs.com/Maniacs_Mansion/Meal-Quest/internal/notify"
"code.cubecraftlabs.com/Maniacs_Mansion/Meal-Quest/internal/notify/notifytest"
"code.cubecraftlabs.com/Maniacs_Mansion/Meal-Quest/internal/store"
"code.cubecraftlabs.com/Maniacs_Mansion/Meal-Quest/internal/vapid"
"code.cubecraftlabs.com/Maniacs_Mansion/Meal-Quest/migrations"
@@ -35,7 +37,7 @@ func newTestService(t *testing.T) (*Service, *store.Store) {
if err != nil {
t.Fatal(err)
}
svc, err := NewService(st, pair.PublicKey)
svc, err := NewService(st, pair.PublicKey, notifytest.New(t, st))
if err != nil {
t.Fatal(err)
}
@@ -86,12 +88,20 @@ func rows(t *testing.T, st *store.Store) map[string]storedRow {
func TestNewServiceRefusesBadPublicKey(t *testing.T) {
t.Parallel()
_, st := newTestService(t)
if _, err := NewService(st, "not-a-key"); err == nil {
notifier := notifytest.New(t, st)
if _, err := NewService(st, "not-a-key", notifier); err == nil {
t.Error("NewService accepted a malformed public key")
}
if _, err := NewService(nil, ""); err == nil {
if _, err := NewService(nil, "", notifier); err == nil {
t.Error("NewService accepted a nil store")
}
pair, err := vapid.Generate()
if err != nil {
t.Fatal(err)
}
if _, err := NewService(st, pair.PublicKey, nil); err == nil {
t.Error("NewService accepted a nil notifier")
}
}
func TestSubscribeUpsertsByEndpoint(t *testing.T) {
@@ -222,3 +232,77 @@ func TestSubscribeValidates(t *testing.T) {
t.Errorf("invalid requests stored %d rows", len(got))
}
}
func TestSendTestQueuesOnePushToPlan(t *testing.T) {
t.Parallel()
svc, st := newTestService(t)
p256dh, auth := browserKeys(t)
for _, ep := range []string{endpoint, endpoint + "-2"} {
if _, err := svc.Subscribe(context.Background(), SubscribeRequest{Endpoint: ep, P256dh: p256dh, Auth: auth}); err != nil {
t.Fatal(err)
}
}
first, err := svc.SendTest(context.Background())
if err != nil {
t.Fatal(err)
}
if !strings.HasPrefix(first.EventKey, "plan_ready:test-") || first.Subscriptions != 2 {
t.Errorf("SendTest = %+v, want a plan_ready:test- key and 2 subscriptions", first)
}
// A second test is a new event, not taken for the first.
second, err := svc.SendTest(context.Background())
if err != nil {
t.Fatal(err)
}
if second.EventKey == first.EventKey {
t.Fatalf("two tests share the key %s", first.EventKey)
}
// Seeded prefs send plan_ready to Google Chat too; the test never does.
rows := notifytest.Rows(t, st, notify.PlanReady)
if len(rows) != 2 {
t.Fatalf("rows = %+v, want one per test", rows)
}
byKey := map[string]notifytest.Row{}
for _, r := range rows {
byKey[r.Key] = r
}
for _, sent := range []TestSent{first, second} {
want := notifytest.Row{Key: sent.EventKey, Channel: notify.ChannelPush, Payload: notify.Payload{
Event: notify.PlanReady, Title: "MealQuest test notification", Body: TestBody, URL: "/plan", Tag: sent.EventKey,
}}
if got := byKey[sent.EventKey]; got != want {
t.Errorf("row of %s = %+v, want %+v", sent.EventKey, got, want)
}
}
}
func TestSendTestWithoutSubscriptions(t *testing.T) {
t.Parallel()
svc, st := newTestService(t)
if _, err := svc.SendTest(context.Background()); !errors.Is(err, ErrNoSubscriptions) {
t.Fatalf("SendTest = %v, want ErrNoSubscriptions", err)
}
if rows := notifytest.Rows(t, st, notify.PlanReady); len(rows) != 0 {
t.Fatalf("rows = %+v, want none", rows)
}
}
func TestSendTestWithPushOff(t *testing.T) {
t.Parallel()
svc, st := newTestService(t)
p256dh, auth := browserKeys(t)
if _, err := svc.Subscribe(context.Background(), SubscribeRequest{Endpoint: endpoint, P256dh: p256dh, Auth: auth}); err != nil {
t.Fatal(err)
}
if _, err := st.Writer.Exec(`UPDATE notification_prefs SET push = 0 WHERE event = 'plan_ready'`); err != nil {
t.Fatal(err)
}
if _, err := svc.SendTest(context.Background()); !errors.Is(err, notify.ErrPushOff) {
t.Fatalf("SendTest = %v, want notify.ErrPushOff", err)
}
if rows := notifytest.Rows(t, st, notify.PlanReady); len(rows) != 0 {
t.Fatalf("rows = %+v, want none", rows)
}
}