package server import ( "context" "testing" "time" "github.com/coder/websocket" "git.a-wan.net/alan/pipelines/internal/protocol" ) // One turn, one ping: a turn that already sent an agent notify does not also // announce its end, while a later turn without one does, and a notify sent // after a turn ended (background work) does not silence the next turn. func TestAgentNotifySuppressesThatTurnsReplyNotification(t *testing.T) { store := newOperatorStore(t) ctx := context.Background() if _, err := store.UpdateNotificationSettings(ctx, map[string]any{protocol.NotifyChatReplyFinished: true}); err != nil { t.Fatal(err) } h := chatAttentionHub(t, "ses_n") h.settings = store h.notes = newAppNotifier(store) start := func(turn string) { h.emit(protocol.ChatEventFrame{SessionID: "ses_n", Kind: protocol.ChatEventStep, Step: &protocol.ChatStep{Status: protocol.ChatStepStarted, TurnID: turn}}) } end := func(turn string) { h.emit(protocol.ChatEventFrame{SessionID: "ses_n", Kind: protocol.ChatEventStep, Step: &protocol.ChatStep{Status: protocol.ChatStepEnded, TurnID: turn}}) h.attentionWG.Wait() } kinds := func() []string { t.Helper() active, _, err := store.ActiveAppNotifications(ctx, "", time.Now()) if err != nil { t.Fatal(err) } var got []string for _, n := range active { got = append(got, n.Kind+":"+n.Title) } return got } start("turn_1") if got := h.publishToolNotification(ctx, "ses_n", chatToolNotice{ID: "n1", Title: "Halfway", Message: "Tests pass"}); !got.App { t.Fatalf("delivery = %+v", got) } end("turn_1") if got := kinds(); len(got) != 1 || got[0] != protocol.NotificationChatNotify+":Halfway" { t.Fatalf("turn with a notify recorded %+v", got) } // Background work notifies after the turn; the next turn still announces. h.publishToolNotification(ctx, "ses_n", chatToolNotice{ID: "n2", Title: "Deploy done", Message: "Live"}) start("turn_2") end("turn_2") got := kinds() if len(got) != 3 || got[2] != protocol.NotificationChatReply+":Chat replied" { t.Fatalf("later deliveries = %+v", got) } } // Clearing a chat app notification on one device pushes the clear to every // other connected client. func TestClearingAChatNotificationClearsEveryClient(t *testing.T) { ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second) defer cancel() f := newAppNotificationFixture(t, ctx) hub := f.handlerHub() web, desktop, phone := dialAckClient(t, ctx, f.url), dialAckClient(t, ctx, f.url), dialAckClient(t, ctx, f.url) subscribeNotifications(t, ctx, web, "") subscribeNotifications(t, ctx, desktop, "") subscribeNotifications(t, ctx, phone, "") if got := hub.publishToolNotification(ctx, "ses_c", chatToolNotice{ID: "shown_c", Title: "Look", Message: "Now"}); !got.App { t.Fatalf("delivery = %+v", got) } id := "chat:ses_c:notify:shown_c" for _, conn := range []*websocket.Conn{web, desktop, phone} { if push := readFrame(t, ctx, conn, protocol.NotificationPush).Notification; push == nil || push.ID != id { t.Fatalf("push = %+v", push) } } // Tapped on the phone: the phone gets the reply, every other client a push. writeChat(t, ctx, phone, protocol.Message{Type: protocol.ClearNotifications, ID: "tap", IDs: []string{id}}) if reply := readFrame(t, ctx, phone, protocol.NotificationsCleared); reply.ID != "tap" || len(reply.IDs) != 1 || reply.IDs[0] != id { t.Fatalf("phone reply = %+v", reply) } for name, conn := range map[string]*websocket.Conn{"web": web, "desktop": desktop} { if push := readUntil(t, ctx, conn, protocol.NotificationsCleared); len(push.IDs) != 1 || push.IDs[0] != id { t.Fatalf("%s cleared push = %+v", name, push) } } }