package server import ( "context" "encoding/json" "fmt" "net/http/httptest" "testing" "time" "github.com/coder/websocket" "github.com/coder/websocket/wsjson" "git.a-wan.net/alan/pipelines/internal/pipeline" "git.a-wan.net/alan/pipelines/internal/protocol" "git.a-wan.net/alan/pipelines/internal/stage" "git.a-wan.net/alan/pipelines/internal/summarise" ) // appNotificationFixture is a handler over a temp store with one pipeline, // plus helpers to make a run end with a durable notification intent. type appNotificationFixture struct { t *testing.T ctx context.Context store *pipeline.Store runner *runner url string def *protocol.PipelineDetail } func newAppNotificationFixture(t *testing.T, ctx context.Context) *appNotificationFixture { t.Helper() store := newOperatorStore(t) if _, err := store.Submit(ctx, "notes", pipeline.Template([]string{"one"}, true)); err != nil { t.Fatal(err) } items, err := store.List(ctx) if err != nil { t.Fatal(err) } def, err := store.Get(ctx, items[0].ID, 1) if err != nil { t.Fatal(err) } handlerCtx, stop := context.WithCancel(ctx) handler := Handler(handlerCtx, store) t.Cleanup(func() { stop(); WaitRuns(handler) }) srv := httptest.NewServer(handler) t.Cleanup(srv.Close) return &appNotificationFixture{t: t, ctx: ctx, store: store, runner: handler.(*runHandler).runner, url: srv.URL, def: def} } // failRun creates a "Notify me" run and fails it, which commits a durable run // notification intent in the same transaction, then drains the outbox. func (f *appNotificationFixture) failRun(name string) int64 { f.t.Helper() id, _, err := f.store.CreateRunTitled(f.ctx, f.def, name, f.t.TempDir(), false, nil, nil, pipeline.RunOverrides{NotifyMode: "on"}, summarise.Title{}) if err != nil { f.t.Fatal(err) } if err := f.store.FinishRun(f.ctx, id, "failed", "error", "boom", nil); err != nil { f.t.Fatal(err) } f.runner.deliverRunNotifications(time.Now()) return id } func readFrame(t *testing.T, ctx context.Context, conn *websocket.Conn, want string) protocol.Message { t.Helper() var msg protocol.Message if err := wsjson.Read(ctx, conn, &msg); err != nil { t.Fatal(err) } if msg.Type != want { t.Fatalf("frame type = %q, want %q: %+v", msg.Type, want, msg) } return msg } // readFrames reads exactly one frame of each wanted type, in any order. func readFrames(t *testing.T, ctx context.Context, conn *websocket.Conn, want ...string) map[string]protocol.Message { t.Helper() got := map[string]protocol.Message{} for len(got) < len(want) { var msg protocol.Message if err := wsjson.Read(ctx, conn, &msg); err != nil { t.Fatal(err) } expected := false for _, kind := range want { if msg.Type == kind { expected = true } } if _, dup := got[msg.Type]; !expected || dup { t.Fatalf("unexpected frame %q, want one each of %v: %+v", msg.Type, want, msg) } got[msg.Type] = msg } return got } func subscribeNotifications(t *testing.T, ctx context.Context, conn *websocket.Conn, after string) protocol.Message { t.Helper() if err := wsjson.Write(ctx, conn, protocol.Message{Type: protocol.SubscribeNotifications, ID: "sub", After: after}); err != nil { t.Fatal(err) } reply := readFrame(t, ctx, conn, protocol.NotificationsSubscribed) if reply.ID != "sub" { t.Fatalf("subscribe reply id = %q", reply.ID) } return reply } func TestAppNotificationStreamMatchesNtfy(t *testing.T) { t.Setenv("PIPELINES_WEB_PUBLIC_URL", "") ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second) defer cancel() captured := newNtfyCapture(t) f := newAppNotificationFixture(t, ctx) phone := dialAckClient(t, ctx, f.url) initial := subscribeNotifications(t, ctx, phone, "") if initial.Notifications == nil || len(initial.Notifications) != 0 || initial.IDs == nil { t.Fatalf("initial reply = %+v", initial) } run := f.failRun("stream") push := readFrame(t, ctx, phone, protocol.NotificationPush) got := captured() if len(got) != 1 { t.Fatalf("ntfy deliveries = %+v", got) } n := push.Notification if n == nil || n.Title != got[0].Title || n.Message != got[0].Message || n.Click != got[0].Click { t.Fatalf("stream %+v differs from ntfy %+v", n, got[0]) } if n.ID != fmt.Sprintf("run:%d:0:failed", run) || n.Kind != protocol.NotificationRunFailed || n.Group != fmt.Sprintf("run:%d", run) || n.Priority != 3 || n.Tags == nil || len(n.Tags) != 0 || n.Click != fmt.Sprintf("pipelines://run/%d", run) { t.Fatalf("notification fields = %+v", n) } if _, err := time.Parse(time.RFC3339, n.CreatedAt); err != nil { t.Fatalf("created_at = %q", n.CreatedAt) } // The wire shape the app parses. raw, _ := json.Marshal(push) var wire map[string]any _ = json.Unmarshal(raw, &wire) body, _ := wire["notification"].(map[string]any) for _, key := range []string{"id", "kind", "title", "message", "click", "priority", "tags", "created_at", "group"} { if _, ok := body[key]; !ok { t.Fatalf("notification wire lacks %q: %s", key, raw) } } // A retried publication of the same event is not streamed twice. f.runner.deliverRunNotifications(time.Now().Add(time.Hour)) reply := subscribeNotifications(t, ctx, phone, "") if len(reply.Notifications) != 1 || reply.Notifications[0].ID != n.ID || reply.Notifications[0].CreatedAt != n.CreatedAt { t.Fatalf("replay = %+v", reply.Notifications) } } func TestAppNotificationReplayAfterCursorAndCrossDeviceClear(t *testing.T) { t.Setenv("PIPELINES_NTFY_URL", "") ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second) defer cancel() f := newAppNotificationFixture(t, ctx) // Offline: two runs fail with no app connected (ntfy unconfigured too). first := f.failRun("first") second := f.failRun("second") firstID, secondID := fmt.Sprintf("run:%d:0:failed", first), fmt.Sprintf("run:%d:0:failed", second) phone := dialAckClient(t, ctx, f.url) all := subscribeNotifications(t, ctx, phone, "") if len(all.Notifications) != 2 || all.Notifications[0].ID != firstID || all.Notifications[1].ID != secondID { t.Fatalf("full replay = %+v", all.Notifications) } // Reconnect with the cursor of the first: only the missed second replays. phone.CloseNow() phone = dialAckClient(t, ctx, f.url) missed := subscribeNotifications(t, ctx, phone, firstID) if len(missed.Notifications) != 1 || missed.Notifications[0].ID != secondID { t.Fatalf("replay after %s = %+v", firstID, missed.Notifications) } // An unknown cursor replays the bounded active set. web := dialAckClient(t, ctx, f.url) if unknown := subscribeNotifications(t, ctx, web, "run:999:0:failed"); len(unknown.Notifications) != 2 { t.Fatalf("unknown cursor replay = %+v", unknown.Notifications) } // Clearing on the web broadcasts to the phone and acks the run. if err := wsjson.Write(ctx, web, protocol.Message{Type: protocol.ClearNotifications, ID: "clr", IDs: []string{firstID, "unknown"}}); err != nil { t.Fatal(err) } reply := readFrame(t, ctx, web, protocol.NotificationsCleared) if reply.ID != "clr" || len(reply.IDs) != 1 || reply.IDs[0] != firstID { t.Fatalf("clear reply = %+v", reply) } // The clear and the ack change are separate broadcasts with no ordering // guarantee between them, so the phone's two frames are read in any order. phoneFrames := readFrames(t, ctx, phone, protocol.NotificationsCleared, protocol.AttentionAcksChanged) pushed := phoneFrames[protocol.NotificationsCleared] if pushed.ID != "" || len(pushed.IDs) != 1 || pushed.IDs[0] != firstID { t.Fatalf("phone cleared push = %+v", pushed) } // Same effect as the ack: the phone's Needs-you list hears it too. changed := phoneFrames[protocol.AttentionAcksChanged] if changed.AttentionAcks == nil || len(changed.AttentionAcks.Dismissed) != 1 || changed.AttentionAcks.Dismissed[0] != fmt.Sprintf("%d:Failed", first) { t.Fatalf("ack push = %+v", changed) } // An ack_attention from the phone clears the second on the web. ack := ackExchange(t, ctx, phone, protocol.Message{Type: protocol.AckAttention, ID: "ack", AttentionAcks: &protocol.AttentionAcks{Dismissed: []string{fmt.Sprintf("%d:Failed", second)}}}) if ack.Type != protocol.AttentionAcksReply { t.Fatalf("ack reply = %+v", ack) } // The web receives the ack push (from the ack hub) and the clear push in // either order. seen := map[string]protocol.Message{} for len(seen) < 2 { var msg protocol.Message if err := wsjson.Read(ctx, web, &msg); err != nil { t.Fatal(err) } seen[msg.Type] = msg } if c := seen[protocol.NotificationsCleared]; len(c.IDs) != 1 || c.IDs[0] != secondID { t.Fatalf("web cleared push = %+v", seen) } phoneClear := readFrame(t, ctx, phone, protocol.NotificationsCleared) if len(phoneClear.IDs) != 1 || phoneClear.IDs[0] != secondID { t.Fatalf("phone cleared push = %+v", phoneClear) } // Nothing active is left; both ids are reported cleared on resubscribe. final := subscribeNotifications(t, ctx, phone, "") if len(final.Notifications) != 0 || len(final.IDs) != 2 { t.Fatalf("final replay = %+v", final) } // A malformed clear is refused. if err := wsjson.Write(ctx, phone, protocol.Message{Type: protocol.ClearNotifications, ID: "bad"}); err != nil { t.Fatal(err) } if bad := readFrame(t, ctx, phone, protocol.Error); bad.ID != "bad" { t.Fatalf("bad clear = %+v", bad) } } func TestNtfyDisabledStillStreams(t *testing.T) { ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second) defer cancel() captured := newNtfyCapture(t) f := newAppNotificationFixture(t, ctx) phone := dialAckClient(t, ctx, f.url) set := ackExchange(t, ctx, phone, protocol.Message{Type: protocol.SetNotificationSettings, ID: "off", NotificationSettings: map[string]any{protocol.NotifyNtfyEnabled: false}}) if set.Type != protocol.NotificationSettingsReply || set.NotificationSettings[protocol.NotifyNtfyEnabled] != false { t.Fatalf("settings reply = %+v", set) } subscribeNotifications(t, ctx, phone, "") run := f.failRun("quiet") push := readFrame(t, ctx, phone, protocol.NotificationPush) if push.Notification == nil || push.Notification.ID != fmt.Sprintf("run:%d:0:failed", run) || push.Notification.Title != "Run failed" { t.Fatalf("push = %+v", push) } if got := captured(); len(got) != 0 { t.Fatalf("ntfy published while disabled: %+v", got) } // The outbox is settled, not retried against ntfy. rows, err := f.store.RunNotifications(ctx, run) if err != nil || len(rows) != 1 || rows[0].Status != "sent" { t.Fatalf("outbox = %+v %v", rows, err) } } func TestChatAppNotificationsRespectNtfyEnabledAndClearOnAnswer(t *testing.T) { t.Setenv("PIPELINES_NTFY_URL", "http://ntfy.invalid/pipelines") t.Setenv("PIPELINES_WEB_PUBLIC_URL", "") var published []ntfyCapture original := chatAttentionPublish chatAttentionPublish = func(_ context.Context, _, title, message, _ string, _ []byte, click string, _ ...stage.NtfyView) (stage.Values, error) { published = append(published, ntfyCapture{Title: title, Message: message, Click: click}) return stage.Values{}, nil } defer func() { chatAttentionPublish = original }() ctx := context.Background() store := newOperatorStore(t) h := chatAttentionHub(t, "ses_a") h.settings = store h.notes = newAppNotifier(store) form := protocol.ChatForm{ID: "que_1", SessionID: "ses_a", Kind: "question", Title: "Ship it?"} h.emit(protocol.ChatEventFrame{SessionID: "ses_a", Kind: protocol.ChatEventForm, Form: &form}) h.attentionWG.Wait() active, _, err := store.ActiveAppNotifications(ctx, "", time.Now()) if err != nil || len(published) != 1 || len(active) != 1 { t.Fatalf("published=%+v active=%+v err=%v", published, active, err) } n := active[0] if n.ID != "chat:ses_a:form:que_1" || n.Kind != protocol.NotificationChatQuestion || n.Group != "chat:ses_a" || n.Title != published[0].Title || n.Message != published[0].Message || n.Click != published[0].Click { t.Fatalf("chat notification %+v vs ntfy %+v", n, published[0]) } // Answering the question clears it. form.Resolved = true h.emit(protocol.ChatEventFrame{SessionID: "ses_a", Kind: protocol.ChatEventForm, Form: &form}) h.attentionWG.Wait() if active, _, _ := store.ActiveAppNotifications(ctx, "", time.Now()); len(active) != 0 { t.Fatalf("answered question still active: %+v", active) } // With ntfy off, the approval is streamed but not published. if _, err := store.UpdateNotificationSettings(ctx, map[string]any{protocol.NotifyNtfyEnabled: false}); err != nil { t.Fatal(err) } h.pushPermission("ses_a", protocol.ChatPermissionFrame{SessionID: "ses_a", RequestID: "per_1", Message: "Allow rm?"}) h.attentionWG.Wait() active, _, _ = store.ActiveAppNotifications(ctx, "", time.Now()) if len(published) != 1 || len(active) != 1 || active[0].Kind != protocol.NotificationChatApproval || active[0].ID != "chat:ses_a:permission:per_1" { t.Fatalf("published=%+v active=%+v", published, active) } h.confirmPermission("ses_a", "per_1", "phone", "once") h.attentionWG.Wait() if active, _, _ := store.ActiveAppNotifications(ctx, "", time.Now()); len(active) != 0 { t.Fatalf("answered approval still active: %+v", active) } } func TestRunLeavingWaitingClearsWaitingNotification(t *testing.T) { ctx := context.Background() store := newOperatorStore(t) notes := newAppNotifier(store) r := &runner{ctx: ctx, store: store, notes: notes} notes.record(ctx, runNotificationRecord(5, 2, protocol.NotifyWaiting, protocol.NotificationRunWaiting, "Run needs your decision", "Run #5")) notes.record(ctx, runNotificationRecord(5, 1, "success", protocol.NotificationStageSucceeded, "Pipeline stage success", "digest")) r.emitRun(protocol.RunEventFrame{RunID: 5, Status: protocol.RunStatusWaiting}, false) if active, _, _ := store.ActiveAppNotifications(ctx, "", time.Now()); len(active) != 2 { t.Fatalf("waiting frame cleared: %+v", active) } r.emitRun(protocol.RunEventFrame{RunID: 5, Status: "running"}, false) active, cleared, _ := store.ActiveAppNotifications(ctx, "", time.Now()) if len(active) != 1 || active[0].Kind != protocol.NotificationStageSucceeded || len(cleared) != 1 || cleared[0] != "run:5:2:waiting" { t.Fatalf("active=%+v cleared=%+v", active, cleared) } } // A stage notify hook streams one notification per step event (artifacts stay // ntfy attachments) with the hook's title and summary, and with ntfy disabled // it still streams while publishing nothing. func TestStageHookAppNotification(t *testing.T) { t.Setenv("PIPELINES_WEB_PUBLIC_URL", "") captured := newNtfyCapture(t) ctx, cancel := context.WithCancel(context.Background()) defer cancel() store := newOperatorStore(t) if _, err := store.Submit(ctx, "hooks", pipeline.Template([]string{"one"}, true)); err != nil { t.Fatal(err) } items, _ := store.List(ctx) d, _ := store.Get(ctx, items[0].ID, 0) workspace := t.TempDir() r := &runner{ctx: ctx, store: store, notes: newAppNotifier(store)} policy := &protocol.NotificationPolicy{On: []string{"success"}, Destination: "system", Artifacts: []string{"markdown"}} for i, ntfyOn := range []bool{true, false} { if _, err := store.UpdateNotificationSettings(ctx, map[string]any{protocol.NotifyNtfyEnabled: ntfyOn}); err != nil { t.Fatal(err) } id, _, err := store.CreateRun(ctx, d, fmt.Sprintf("hook%d", i), workspace, false, nil, map[string]any{"prompt": "task"}) if err != nil { t.Fatal(err) } if err := store.BeginStep(ctx, id, 1, "one", nil); err != nil { t.Fatal(err) } step := protocol.RunStep{Sequence: 1, Stage: "one", Status: "completed", Outcome: "success", Target: "two", Output: map[string]any{"markdown": "# Digest"}} if err := store.FinishStep(ctx, id, step); err != nil { t.Fatal(err) } before := len(captured()) r.notifyStep(id, d.Name, workspace, policy, step) active, _, err := store.ActiveAppNotifications(ctx, "", time.Now()) if err != nil || len(active) != i+1 { t.Fatalf("active = %+v %v", active, err) } n := active[i] if n.ID != fmt.Sprintf("run:%d:1:success", id) || n.Kind != protocol.NotificationStageSucceeded || n.Click != fmt.Sprintf("pipelines://run/%d", id) { t.Fatalf("hook notification = %+v", n) } got := captured() if ntfyOn && (len(got) != before+1 || got[before].Title != n.Title || got[before].Message != n.Message || got[before].Click != n.Click) { t.Fatalf("ntfy %+v vs stream %+v", got, n) } if !ntfyOn && len(got) != before { t.Fatalf("ntfy published while disabled: %+v", got) } } }