package server import ( "context" "sync" "testing" "time" "github.com/coder/websocket" "git.a-wan.net/alan/pipelines/internal/protocol" "git.a-wan.net/alan/pipelines/internal/stage" ) // 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) { t.Setenv("PIPELINES_WEB_PUBLIC_URL", "") captured := newNtfyCapture(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() } start("turn_1") if got := h.publishToolNotification(ctx, "ses_n", chatToolNotice{ID: "n1", Title: "Halfway", Message: "Tests pass"}); !got.App || !got.Phone { t.Fatalf("delivery = %+v", got) } end("turn_1") if got := captured(); len(got) != 1 || got[0].Title != "Halfway" { t.Fatalf("turn with a notify published %+v", got) } active, _, err := store.ActiveAppNotifications(ctx, "", time.Now()) if err != nil || len(active) != 1 || active[0].Kind != protocol.NotificationChatNotify { t.Fatalf("app notifications = %+v, %v", active, err) } // 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 := captured() if len(got) != 3 || got[2].Title != "Chat replied" { t.Fatalf("later deliveries = %+v", got) } } // Every chat ntfy message carries a sequence id derived from its app id, and // clearing the app notification (here: on another device) clears it in ntfy // and pushes the clear to every other connected client. func TestClearingAChatNotificationClearsEveryClientAndNtfy(t *testing.T) { t.Setenv("PIPELINES_WEB_PUBLIC_URL", "") t.Setenv("PIPELINES_NTFY_URL", "http://ntfy.invalid/pipelines") ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second) defer cancel() var mu sync.Mutex var published []string originalPublish, originalClear := chatAttentionPublish, chatAttentionClear chatAttentionPublish = func(ctx context.Context, _, _, _, _ string, _ []byte, _ string, _ ...stage.NtfyView) (stage.Values, error) { mu.Lock() published = append(published, stage.NtfySequence(ctx)) mu.Unlock() return stage.Values{"message_id": "m"}, nil } cleared := make(chan string, 8) chatAttentionClear = func(_ context.Context, destination, sequence string) error { if destination == "system" { cleared <- sequence } return nil } defer func() { chatAttentionPublish, chatAttentionClear = originalPublish, originalClear }() 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 || !got.Phone { 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) } } mu.Lock() if len(published) != 1 || published[0] != chatNtfySequence(id) { t.Fatalf("published sequences = %v", published) } mu.Unlock() // 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) } } select { case sequence := <-cleared: if sequence != chatNtfySequence(id) { t.Fatalf("ntfy cleared %q", sequence) } case <-ctx.Done(): t.Fatal("ntfy was not cleared") } } func TestChatNtfySequenceIsAValidStableNtfyID(t *testing.T) { a, b := chatNtfySequence("chat:ses_x:notify:abc"), chatNtfySequence("chat:ses_x:notify:abd") if a != chatNtfySequence("chat:ses_x:notify:abc") || a == b || len(a) != 27 { t.Fatalf("sequences %q %q", a, b) } }