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, NtfyEnabled: 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, NtfyEnabled: 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) } } // A run's end (succeeded or failed) enqueues its generic alert only for a run // marked "on", or for "auto" when run_finished (default off) opts into every // run end. Waiting notifies auto and on runs regardless; "off" never notifies. 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 == "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) != 0 { t.Fatalf("notifications = %+v, %v", got, err) } }