Files
mtusi2026lab6/internal/broker/redisstream/envelope_test.go
T
2026-11-01 14:46:14 +03:00

75 lines
2.2 KiB
Go

package redisstream
import (
"testing"
"time"
"github.com/google/uuid"
"github.com/stretchr/testify/require"
)
func TestEnvelopeRoundTrip(t *testing.T) {
envelope := validEnvelope()
payload, err := EncodeEnvelope(envelope)
require.NoError(t, err)
decoded, err := DecodeEnvelope(payload)
require.NoError(t, err)
require.Equal(t, envelope, decoded)
}
func TestDecodeEnvelopeRejectsUnknownField(t *testing.T) {
payload := []byte(`{
"schema_version":1,
"job_id":"11111111-1111-4111-8111-111111111111",
"type":"attachment.process.v1",
"attachment_id":"22222222-2222-4222-8222-222222222222",
"pipeline_version":1,
"attempt":1,
"created_at":"2026-09-01T12:00:00Z",
"storage_key":"must-not-enter-the-broker"
}`)
_, err := DecodeEnvelope(payload)
require.Error(t, err)
}
func TestEnvelopeValidation(t *testing.T) {
tests := []struct {
name string
mutate func(*Envelope)
}{
{name: "schema", mutate: func(e *Envelope) { e.SchemaVersion = 2 }},
{name: "type", mutate: func(e *Envelope) { e.Type = "attachment.delete.v1" }},
{name: "job id", mutate: func(e *Envelope) { e.JobID = uuid.Nil }},
{name: "attachment id", mutate: func(e *Envelope) { e.AttachmentID = uuid.Nil }},
{name: "pipeline", mutate: func(e *Envelope) { e.PipelineVersion = 0 }},
{name: "attempt", mutate: func(e *Envelope) { e.Attempt = 0 }},
{name: "created at", mutate: func(e *Envelope) { e.CreatedAt = time.Time{} }},
{name: "traceparent", mutate: func(e *Envelope) { e.Traceparent = "invalid" }},
{name: "tracestate length", mutate: func(e *Envelope) { e.Tracestate = string(make([]byte, 513)) }},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
envelope := validEnvelope()
tt.mutate(&envelope)
require.Error(t, envelope.Validate())
})
}
}
func validEnvelope() Envelope {
return Envelope{
SchemaVersion: SchemaVersion,
JobID: uuid.MustParse("11111111-1111-4111-8111-111111111111"),
Type: JobType,
AttachmentID: uuid.MustParse("22222222-2222-4222-8222-222222222222"),
PipelineVersion: 1,
Attempt: 1,
CreatedAt: time.Date(2026, 9, 1, 12, 0, 0, 0, time.UTC),
Traceparent: "00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01",
Tracestate: "vendor=value",
}
}