|
| 1 | +package sqs |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/aws/aws-sdk-go-v2/aws" |
| 10 | + babelqueue "github.com/babelqueue/babelqueue-go" |
| 11 | +) |
| 12 | + |
| 13 | +// conformanceDir is the core's vendored copy of the canonical suite (synced from |
| 14 | +// the conformance/ repo). The SQS-binding cases live under manifest.json → "sqs". |
| 15 | +const conformanceDir = "../testdata/conformance" |
| 16 | + |
| 17 | +type sqsManifest struct { |
| 18 | + SQS struct { |
| 19 | + AttributeProjection struct { |
| 20 | + EnvelopeFile string `json:"envelope_file"` |
| 21 | + MessageAttributes map[string]goldenAttribute `json:"message_attributes"` |
| 22 | + } `json:"attribute_projection"` |
| 23 | + AttemptsReconciliation struct { |
| 24 | + Cases []reconcileCase `json:"cases"` |
| 25 | + } `json:"attempts_reconciliation"` |
| 26 | + } `json:"sqs"` |
| 27 | +} |
| 28 | + |
| 29 | +type goldenAttribute struct { |
| 30 | + DataType string `json:"DataType"` |
| 31 | + StringValue string `json:"StringValue"` |
| 32 | +} |
| 33 | + |
| 34 | +type reconcileCase struct { |
| 35 | + Name string `json:"name"` |
| 36 | + BodyAttempts int `json:"body_attempts"` |
| 37 | + ApproximateReceiveCount *string `json:"approximate_receive_count"` |
| 38 | + ExpectedAttempts int `json:"expected_attempts"` |
| 39 | +} |
| 40 | + |
| 41 | +func TestSqsConformance(t *testing.T) { |
| 42 | + raw, err := os.ReadFile(filepath.Join(conformanceDir, "manifest.json")) |
| 43 | + if err != nil { |
| 44 | + t.Skipf("vendored conformance manifest not found: %v", err) |
| 45 | + } |
| 46 | + var m sqsManifest |
| 47 | + if err := json.Unmarshal(raw, &m); err != nil { |
| 48 | + t.Fatalf("manifest: %v", err) |
| 49 | + } |
| 50 | + |
| 51 | + t.Run("attribute_projection", func(t *testing.T) { |
| 52 | + body, err := os.ReadFile(filepath.Join(conformanceDir, m.SQS.AttributeProjection.EnvelopeFile)) |
| 53 | + if err != nil { |
| 54 | + t.Fatalf("fixture: %v", err) |
| 55 | + } |
| 56 | + got := attributes(string(body)) |
| 57 | + want := m.SQS.AttributeProjection.MessageAttributes |
| 58 | + if len(got) != len(want) { |
| 59 | + t.Fatalf("projected %d attributes, want %d", len(got), len(want)) |
| 60 | + } |
| 61 | + for key, w := range want { |
| 62 | + g, ok := got[key] |
| 63 | + if !ok { |
| 64 | + t.Errorf("missing attribute %q", key) |
| 65 | + continue |
| 66 | + } |
| 67 | + if aws.ToString(g.DataType) != w.DataType || aws.ToString(g.StringValue) != w.StringValue { |
| 68 | + t.Errorf("%s = {%s,%q}, want {%s,%q}", |
| 69 | + key, aws.ToString(g.DataType), aws.ToString(g.StringValue), w.DataType, w.StringValue) |
| 70 | + } |
| 71 | + } |
| 72 | + }) |
| 73 | + |
| 74 | + for _, c := range m.SQS.AttemptsReconciliation.Cases { |
| 75 | + t.Run("attempts/"+c.Name, func(t *testing.T) { |
| 76 | + env, _ := babelqueue.Make("urn:babel:orders:created", map[string]any{"x": 1}) |
| 77 | + env.Attempts = c.BodyAttempts |
| 78 | + body, _ := env.Encode() |
| 79 | + |
| 80 | + out := string(body) |
| 81 | + if c.ApproximateReceiveCount != nil { // absent count → no reconciliation |
| 82 | + out = reconcileAttempts(out, *c.ApproximateReceiveCount) |
| 83 | + } |
| 84 | + decoded, _ := babelqueue.Decode([]byte(out)) |
| 85 | + if decoded.Attempts != c.ExpectedAttempts { |
| 86 | + t.Errorf("attempts = %d, want %d", decoded.Attempts, c.ExpectedAttempts) |
| 87 | + } |
| 88 | + }) |
| 89 | + } |
| 90 | +} |
0 commit comments