package streamqueue import ( "context" "os" "testing" "time" "github.com/google/uuid" "gitea.privcloud.tech/vlad_marlo/mtusi2026lab5/internal/pipeline" ) // queueFixture isolates stream and consumer state while using a real broker. func queueFixture(t *testing.T) *Queue { t.Helper() address := os.Getenv("BROKER_ADDRESS") if address == "" { t.Skip("isolated BROKER_ADDRESS required") } q, err := NewScoped(address, uuid.NewString(), "test-"+uuid.NewString()) if err != nil { t.Fatal(err) } t.Cleanup(func() { if err := q.client.Del(context.Background(), q.stream).Err(); err != nil { t.Error(err) } if err := q.Close(); err != nil { t.Error(err) } }) return q } // TestQueueDeliveryAndAck verifies durable envelope round-trip and acknowledgement. func TestQueueDeliveryAndAck(t *testing.T) { q := queueFixture(t) job := pipeline.Job{SchemaVersion: 1, ID: uuid.New(), AttachmentID: uuid.New(), Version: 1, Attempt: 1, Type: "attachment.process.v1", CreatedAt: time.Now().UTC()} if err := q.Publish(t.Context(), job); err != nil { t.Fatal(err) } received, err := q.Receive(t.Context()) if err != nil || received == nil || received.Job.ID != job.ID { t.Fatalf("receive: %v", err) } if err := q.Ack(t.Context(), received.Receipt); err != nil { t.Fatal(err) } if delivery, err := q.Receive(t.Context()); err != nil || delivery != nil { t.Fatal("acked job redelivered") } } // TestQueueMalformedNotification verifies malformed data cannot become a processing job. func TestQueueMalformedNotification(t *testing.T) { q := queueFixture(t) if err := q.client.Do(t.Context(), "XADD", q.stream, "*", "job", "invalid-json").Err(); err != nil { t.Fatal(err) } if delivery, err := q.Receive(t.Context()); err != nil || delivery != nil { t.Fatal("invalid job delivered") } } // TestQueueReclaimsAbandonedDelivery verifies a consumer crash does not lose a pending notification. func TestQueueReclaimsAbandonedDelivery(t *testing.T) { q := queueFixture(t) job := pipeline.Job{SchemaVersion: 1, ID: uuid.New(), AttachmentID: uuid.New(), Version: 1, Attempt: 1, Type: "attachment.process.v1", CreatedAt: time.Now().UTC()} if err := q.Publish(t.Context(), job); err != nil { t.Fatal(err) } received, err := q.Receive(t.Context()) if err != nil || received == nil { t.Fatal(err) } if err := q.client.Do(t.Context(), "XCLAIM", q.stream, group, "dead-consumer", 0, received.Receipt, "IDLE", 180000).Err(); err != nil { t.Fatal(err) } reclaimed, err := q.Receive(t.Context()) if err != nil || reclaimed == nil || reclaimed.Receipt != received.Receipt { t.Fatal("pending delivery lost") } if err := q.Ack(t.Context(), reclaimed.Receipt); err != nil { t.Fatal(err) } } // TestQueueCanceledOperations checks broker failures stay visible to the durable coordinator. func TestQueueCanceledOperations(t *testing.T) { q := queueFixture(t) ctx, cancel := context.WithCancel(t.Context()) cancel() t.Run("publish", func(t *testing.T) { if err := q.Publish(ctx, pipeline.Job{}); err == nil { t.Fatal("publish succeeded") } }) t.Run("receive", func(t *testing.T) { if _, err := q.Receive(ctx); err == nil { t.Fatal("receive succeeded") } }) t.Run("ack", func(t *testing.T) { if err := q.Ack(ctx, "0-0"); err == nil { t.Fatal("ack succeeded") } }) } // TestQueueConfiguration rejects absent dependencies without opening sockets. func TestQueueConfiguration(t *testing.T) { if _, err := New("", ""); err == nil { t.Fatal("empty configuration accepted") } q, err := New("127.0.0.1:1", "consumer") if err != nil { t.Fatal(err) } if err := q.Close(); err != nil { t.Fatal(err) } } // TestQueueRejectsUnserializableTimestamp prevents sending an envelope outside JSON time bounds. func TestQueueRejectsUnserializableTimestamp(t *testing.T) { q := queueFixture(t) if err := q.Publish(t.Context(), pipeline.Job{CreatedAt: time.Date(10000, 1, 1, 0, 0, 0, 0, time.UTC)}); err == nil { t.Fatal("invalid timestamp published") } }