Files
pipelines/internal/pipeline/chat_notifications_test.go
T
alan 9f13c93be6 Chat reliability: durable ntfy outbox, queue recovery retries, unattended cap
- Chat phone pushes (attention items, auto-switch notices) go through a
  durable chat_notifications outbox with leases, bounded attempts and
  backoff, retried by a worker; pushes cleared in the app first are never
  published, and a publication racing a clear is cleared again.
- Queued/scheduled chat prompts are deleted on recovery only when they can
  never be rebuilt; transient failures keep the rows and retry with backoff.
- Unattended chats with live work always keep notifying; the idle cap rises
  from 16 to 64.

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

77 lines
2.9 KiB
Go

package pipeline
import (
"context"
"errors"
"path/filepath"
"testing"
"time"
"git.a-wan.net/alan/pipelines/internal/protocol"
)
func TestChatNotificationOutboxRetriesAndSkipsCleared(t *testing.T) {
ctx := context.Background()
s, err := Open(filepath.Join(t.TempDir(), "chat-outbox.db"))
if err != nil {
t.Fatal(err)
}
defer s.Close()
now := time.Unix(1000, 0)
n := ChatNotification{ID: "chat:ses_a:form:q1", SessionID: "ses_a", Title: "T", Body: "B", Click: "pipelines://chat/ses_a", SequenceID: "pl-1"}
for range 2 { // idempotent by id
if err := s.EnqueueChatNotification(ctx, n, now); err != nil {
t.Fatal(err)
}
}
first, err := s.ClaimChatNotification(ctx, 1000)
if err != nil || first == nil || first.Attempts != 1 || first.SequenceID != "pl-1" {
t.Fatalf("claim = %+v %v", first, err)
}
if again, err := s.ClaimChatNotification(ctx, 1000); err != nil || again != nil {
t.Fatalf("leased row claimed twice: %+v %v", again, err)
}
if err := s.FinishChatNotification(ctx, *first, 1000, "", errors.New("ntfy down")); err != nil {
t.Fatal(err)
}
if early, _ := s.ClaimChatNotification(ctx, 1004); early != nil {
t.Fatalf("retried before backoff: %+v", early)
}
second, err := s.ClaimChatNotification(ctx, 1005)
if err != nil || second == nil || second.Attempts != 2 {
t.Fatalf("retry claim = %+v %v", second, err)
}
if err := s.FinishChatNotification(ctx, *second, 1005, "m1", nil); err != nil {
t.Fatal(err)
}
if status, attempts, _ := s.ChatNotificationStatus(ctx, n.ID); status != "sent" || attempts != 2 {
t.Fatalf("status = %s/%d", status, attempts)
}
// A lease lost to a crash is reclaimed after it expires.
crashed := ChatNotification{ID: "chat:ses_a:form:q2", SessionID: "ses_a", Title: "T", Body: "B"}
_ = s.EnqueueChatNotification(ctx, crashed, now)
if c, _ := s.ClaimChatNotification(ctx, 2000); c == nil {
t.Fatal("no claim")
}
if c, _ := s.ClaimChatNotification(ctx, 2061); c == nil || c.Attempts != 2 {
t.Fatalf("expired lease not reclaimed: %+v", c)
} else if err := s.FinishChatNotification(ctx, *c, 2061, "m2", nil); err != nil {
t.Fatal(err)
}
// A push whose app notification was cleared first is never published.
cleared := ChatNotification{ID: "chat:ses_b:form:q3", SessionID: "ses_b", Title: "T", Body: "B"}
if _, _, err := s.RecordAppNotification(ctx, AppNotificationRecord{AppNotification: protocol.AppNotification{ID: cleared.ID, Kind: protocol.NotificationChatQuestion, Title: "T"}, SessionID: "ses_b"}, now); err != nil {
t.Fatal(err)
}
if _, err := s.ClearAppNotifications(ctx, []string{cleared.ID}, now); err != nil {
t.Fatal(err)
}
_ = s.EnqueueChatNotification(ctx, cleared, now)
if c, _ := s.ClaimChatNotification(ctx, 3000); c != nil {
t.Fatalf("cleared push claimed: %+v", c)
}
if status, _, _ := s.ChatNotificationStatus(ctx, cleared.ID); status != "cleared" {
t.Fatalf("cleared push status = %q", status)
}
}