Files
pipelines/internal/pipeline/notification_settings_test.go
T
alan fc7dbb0ac0 Remove ntfy: notifications go only through the app notification stream
Server: drop ntfy publishing (stage hooks, run outbox, chat attention, chat
tool notify, auto-switch and schedule notices), the chat push outbox (its
table is dropped at startup), ntfy status/endpoints and PIPELINES_NTFY_*.
The app_notifications stream, subscribe/clear and kinds are unchanged.
Notification settings keep run_finished/chat_needs_you/chat_reply_finished;
ntfy_enabled is accepted and ignored and its stored row is never read.

Stage type=ntfy is rejected: "ntfy stages were removed; notifications go to
the Pipelines apps — use the stage notify hook". The notify hook remains.

Installer: no ntfy build, accounts, config or supervisor child; manifest
gets top-level "mode" (legacy ntfy.mode still read). Reinstall retires an
old ntfy (safe PID-file stop, deletes binary, config, data dir, token, log,
with a note in logs/install.log); uninstall cleans leftovers.

App: ntfy settings section replaced by "Notification events"; texts updated.
Docs: docs/ntfy.md folded into docs/notifications.md; install, docker, cli,
author skill references updated; security review M1 marked resolved.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-03 23:24:24 +00:00

181 lines
6.7 KiB
Go

package pipeline
import (
"context"
"path/filepath"
"strconv"
"strings"
"testing"
"time"
"git.a-wan.net/alan/pipelines/internal/protocol"
)
func TestNotificationSettingsDefaultPersistAndPartialUpdate(t *testing.T) {
ctx := context.Background()
path := filepath.Join(t.TempDir(), "notify-settings.db")
s, err := Open(path)
if err != nil {
t.Fatal(err)
}
got, err := s.NotificationSettings(ctx)
if err != nil || got != protocol.DefaultNotificationSettings() {
t.Fatalf("defaults = %+v, %v", got, err)
}
if got.RunFinished || !got.ChatNeedsYou || got.ChatReplyFinished {
t.Fatalf("default values changed: %+v", got)
}
got, err = s.UpdateNotificationSettings(ctx, map[string]any{protocol.NotifyChatReplyFinished: true})
want := protocol.NotificationSettings{RunFinished: false, ChatNeedsYou: true, ChatReplyFinished: true}
if err != nil || got != want {
t.Fatalf("partial update = %+v, %v", got, err)
}
got, err = s.UpdateNotificationSettings(ctx, map[string]any{protocol.NotifyRunFinished: true, protocol.NotifyChatNeedsYou: false})
want = protocol.NotificationSettings{RunFinished: true, ChatNeedsYou: false, ChatReplyFinished: true}
if err != nil || got != want {
t.Fatalf("second update = %+v, %v", got, err)
}
if err := s.Close(); err != nil {
t.Fatal(err)
}
s, err = Open(path)
if err != nil {
t.Fatal(err)
}
defer s.Close()
if got, err := s.NotificationSettings(ctx); err != nil || got != want {
t.Fatalf("after reopen = %+v, %v", got, err)
}
// The OpenCode endpoint row shares the table and is unaffected.
if endpoint, err := s.OpenCodeEndpoint(ctx); err != nil || endpoint != "" {
t.Fatalf("endpoint = %q, %v", endpoint, err)
}
}
func TestNotificationSettingsRejectUnknownOrInvalid(t *testing.T) {
ctx := context.Background()
s, err := Open(filepath.Join(t.TempDir(), "reject.db"))
if err != nil {
t.Fatal(err)
}
defer s.Close()
for name, update := range map[string]map[string]any{
"unknown": {protocol.NotifyRunFinished: false, "run_started": true},
"non-bool": {protocol.NotifyChatNeedsYou: "false"},
"empty": {},
"nil value": {protocol.NotifyChatReplyFinished: nil},
} {
if _, err := s.UpdateNotificationSettings(ctx, update); err == nil {
t.Fatalf("%s update accepted", name)
}
}
// A rejected update writes nothing, not even its valid keys.
if got, err := s.NotificationSettings(ctx); err != nil || got != protocol.DefaultNotificationSettings() {
t.Fatalf("rejected update changed settings: %+v, %v", got, err)
}
if _, err := s.UpdateNotificationSettings(ctx, map[string]any{"bogus": true}); err == nil || !strings.Contains(err.Error(), `"bogus"`) {
t.Fatalf("unknown key error = %v", err)
}
}
// Failed runs always enqueue their generic alert (unless off). Successful runs
// require "on" or run_finished. Waiting notifies auto and on runs regardless.
func TestRunEndEnqueueFollowsNotifyModeAndRunFinished(t *testing.T) {
for _, status := range []string{"succeeded", "failed", "waiting"} {
for _, tc := range []struct {
mode string
runFinished bool
want int
}{
{"auto", false, 0}, {"auto", true, 1}, {"on", false, 1}, {"on", true, 1}, {"off", false, 0}, {"off", true, 0},
} {
want := tc.want
if status == "failed" && tc.mode == "auto" {
want = 1
}
if status == "waiting" && tc.mode == "auto" {
want = 1
}
t.Run(status+"-"+tc.mode+map[bool]string{true: "-any", false: "-marked"}[tc.runFinished], func(t *testing.T) {
ctx := context.Background()
s, id := notificationTransitionRun(t, filepath.Join(t.TempDir(), "end.db"), tc.mode)
defer s.Close()
if tc.runFinished {
if _, err := s.UpdateNotificationSettings(ctx, map[string]any{protocol.NotifyRunFinished: true}); err != nil {
t.Fatal(err)
}
}
if err := notificationTransition(ctx, s, id, status); err != nil {
t.Fatal(err)
}
got, err := s.RunNotifications(ctx, id)
if err != nil || len(got) != want {
t.Fatalf("notifications = %+v, %v; want %d", got, err, want)
}
if want == 1 {
title := map[string]string{"succeeded": "Run succeeded", "failed": "Run failed", "waiting": "Run needs your decision"}[status]
n, err := s.ClaimRunNotification(ctx, time.Now().Unix())
if err != nil || n == nil || n.Event != status || n.Title != title || n.Click != "pipelines://run/"+strconv.FormatInt(id, 10) {
t.Fatalf("claimed = %+v, %v", n, err)
}
}
})
}
}
}
// run_finished was stored as "notify.run_finished" when it meant "auto runs
// announce success" (default on). That stale value must not opt the operator
// into every run end under the new meaning.
func TestLegacyRunFinishedValueIsIgnored(t *testing.T) {
ctx := context.Background()
s, id := notificationTransitionRun(t, filepath.Join(t.TempDir(), "legacy.db"), "auto")
defer s.Close()
if _, err := s.db.ExecContext(ctx, `INSERT INTO server_settings(key,value) VALUES('notify.run_finished','true')`); err != nil {
t.Fatal(err)
}
if got, err := s.NotificationSettings(ctx); err != nil || got.RunFinished {
t.Fatalf("legacy value honoured: %+v, %v", got, err)
}
if err := notificationTransition(ctx, s, id, "failed"); err != nil {
t.Fatal(err)
}
if got, err := s.RunNotifications(ctx, id); err != nil || len(got) != 1 {
t.Fatalf("notifications = %+v, %v", got, err)
}
}
// ntfy was removed. A stored notify.ntfy_enabled row from an earlier server is
// never read, the other stored choices keep their meaning, and an older client
// that still sends the ntfy_enabled switch is accepted without changing
// anything.
func TestLegacyNtfySettingIsIgnored(t *testing.T) {
ctx := context.Background()
s, err := Open(filepath.Join(t.TempDir(), "legacy-ntfy.db"))
if err != nil {
t.Fatal(err)
}
defer s.Close()
for key, value := range map[string]string{"notify.ntfy_enabled": "false", "notify.chat_reply_finished": "true", "notify.any_run_ended": "true"} {
if _, err := s.db.ExecContext(ctx, `INSERT INTO server_settings(key,value) VALUES(?,?)`, key, value); err != nil {
t.Fatal(err)
}
}
want := protocol.NotificationSettings{RunFinished: true, ChatNeedsYou: true, ChatReplyFinished: true}
if got, err := s.NotificationSettings(ctx); err != nil || got != want {
t.Fatalf("settings with a stored ntfy row = %+v, %v", got, err)
}
if _, present := want.Map()[protocol.LegacyNotifyNtfyEnabled]; present {
t.Fatal("the wire form still carries ntfy_enabled")
}
got, err := s.UpdateNotificationSettings(ctx, map[string]any{protocol.LegacyNotifyNtfyEnabled: true})
if err != nil || got != want {
t.Fatalf("ntfy-only update = %+v, %v", got, err)
}
got, err = s.UpdateNotificationSettings(ctx, map[string]any{protocol.LegacyNotifyNtfyEnabled: false, protocol.NotifyChatNeedsYou: false})
want.ChatNeedsYou = false
if err != nil || got != want {
t.Fatalf("mixed update = %+v, %v", got, err)
}
}