Files
pipelines/internal/server/chat_ntfy_outbox_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

62 lines
2.2 KiB
Go

package server
import (
"context"
"errors"
"sync"
"testing"
"time"
"git.a-wan.net/alan/pipelines/internal/protocol"
"git.a-wan.net/alan/pipelines/internal/stage"
)
// A chat attention push that ntfy refuses is kept in the durable outbox and
// retried by the worker with its sequence id, not lost after one attempt.
func TestChatAttentionPushIsRetriedFromTheOutbox(t *testing.T) {
t.Setenv("PIPELINES_NTFY_URL", "http://ntfy.invalid/pipelines")
t.Setenv("PIPELINES_WEB_PUBLIC_URL", "")
interval := chatOutboxInterval
chatOutboxInterval = 20 * time.Millisecond
defer func() { chatOutboxInterval = interval }()
var mu sync.Mutex
var sequences []string
original := chatAttentionPublish
chatAttentionPublish = func(ctx context.Context, _, _, _, _ string, _ []byte, _ string, _ ...stage.NtfyView) (stage.Values, error) {
mu.Lock()
defer mu.Unlock()
sequences = append(sequences, stage.NtfySequence(ctx))
if len(sequences) == 1 {
return nil, errors.New("ntfy unavailable")
}
return stage.Values{"message_id": "m1"}, nil
}
defer func() { chatAttentionPublish = original }()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
store := newOperatorStore(t)
h := chatAttentionHub(t, "ses_a")
h.settings = store
h.notes = newAppNotifier(store)
h.startChatNotificationOutbox(ctx, store)
form := protocol.ChatForm{ID: "que_1", SessionID: "ses_a", Kind: "question", Title: "Ship it?"}
h.emit(protocol.ChatEventFrame{SessionID: "ses_a", Kind: protocol.ChatEventForm, Form: &form})
h.attentionWG.Wait()
id := "chat:ses_a:form:que_1"
if status, attempts, err := store.ChatNotificationStatus(ctx, id); err != nil || status != "pending" || attempts != 1 {
t.Fatalf("after a refused attempt: %s/%d %v", status, attempts, err)
}
// The first backoff is 5 s; move the outbox clock past it.
chatOutboxSkew.Store(int64(10 * time.Second))
defer chatOutboxSkew.Store(0)
waitFor(t, "retried push sent", func() bool {
status, _, _ := store.ChatNotificationStatus(ctx, id)
return status == "sent"
})
mu.Lock()
defer mu.Unlock()
if len(sequences) != 2 || sequences[0] != chatNtfySequence(id) || sequences[1] != sequences[0] {
t.Fatalf("sequence ids = %v", sequences)
}
}