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) } }