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: true, 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: false, protocol.NotifyChatNeedsYou: false}) want = protocol.NotificationSettings{RunFinished: false, 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) } } // A run reaching succeeded enqueues its generic alert according to its notify // mode and, for auto, the run_finished setting read in the same transaction. func TestSucceededRunEnqueueFollowsRunFinishedSetting(t *testing.T) { for _, tc := range []struct { mode string runFinished bool want int }{ {"auto", true, 1}, {"auto", false, 0}, {"on", true, 1}, {"on", false, 1}, {"off", true, 0}, {"off", false, 0}, } { t.Run(tc.mode+map[bool]string{true: "-enabled", false: "-disabled"}[tc.runFinished], func(t *testing.T) { ctx := context.Background() s, id := notificationTransitionRun(t, filepath.Join(t.TempDir(), "succeeded.db"), tc.mode) defer s.Close() if _, err := s.UpdateNotificationSettings(ctx, map[string]any{protocol.NotifyRunFinished: tc.runFinished}); err != nil { t.Fatal(err) } if err := notificationTransition(ctx, s, id, "succeeded"); err != nil { t.Fatal(err) } got, err := s.RunNotifications(ctx, id) if err != nil || len(got) != tc.want { t.Fatalf("notifications = %+v, %v; want %d", got, err, tc.want) } if tc.want == 1 { n, err := s.ClaimRunNotification(ctx, time.Now().Unix()) if err != nil || n == nil || n.Event != "succeeded" || n.Title != "Run succeeded" || n.Click != "pipelines://run/"+strconv.FormatInt(id, 10) { t.Fatalf("claimed = %+v, %v", n, err) } } }) } } // Waiting and failed alerts do not depend on run_finished. func TestRunFinishedSettingLeavesAttentionAlone(t *testing.T) { for _, status := range []string{"waiting", "failed"} { t.Run(status, func(t *testing.T) { ctx := context.Background() s, id := notificationTransitionRun(t, filepath.Join(t.TempDir(), "attention.db"), "auto") defer s.Close() if _, err := s.UpdateNotificationSettings(ctx, map[string]any{protocol.NotifyRunFinished: false}); err != nil { t.Fatal(err) } if err := notificationTransition(ctx, s, id, status); err != nil { t.Fatal(err) } if got, err := s.RunNotifications(ctx, id); err != nil || len(got) != 1 || got[0].Event != status { t.Fatalf("notifications = %+v, %v", got, err) } }) } }