|
| 1 | +package bounce |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "errors" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/nats-io/nats.go" |
| 10 | + |
| 11 | + "dispatch/internal/domain" |
| 12 | +) |
| 13 | + |
| 14 | +const ( |
| 15 | + testCrawlerMailbox = "bounce@example.com" |
| 16 | + errUnexpected = "unexpected error: %v" |
| 17 | +) |
| 18 | + |
| 19 | +// --- stubs --- |
| 20 | + |
| 21 | +type stubGraph struct { |
| 22 | + msgs []NDRMessage |
| 23 | + err error |
| 24 | +} |
| 25 | + |
| 26 | +func (s *stubGraph) GetUnreadMessages(_ context.Context, _ string) ([]NDRMessage, error) { |
| 27 | + return s.msgs, s.err |
| 28 | +} |
| 29 | +func (s *stubGraph) MarkAsRead(_ context.Context, _, _ string) error { return s.err } |
| 30 | + |
| 31 | +type captureJS struct { |
| 32 | + published [][]byte |
| 33 | + err error |
| 34 | +} |
| 35 | + |
| 36 | +func (c *captureJS) Publish(_ string, data []byte, _ ...nats.PubOpt) (*nats.PubAck, error) { |
| 37 | + if c.err != nil { |
| 38 | + return nil, c.err |
| 39 | + } |
| 40 | + c.published = append(c.published, data) |
| 41 | + return &nats.PubAck{}, nil |
| 42 | +} |
| 43 | + |
| 44 | +// --- extractTraceID --- |
| 45 | + |
| 46 | +func TestExtractTraceID_Found(t *testing.T) { |
| 47 | + body := "Some NDR text\nX-Dispatch-TraceId: 550e8400-e29b-41d4-a716-446655440000\nMore text" |
| 48 | + got := extractTraceID(body) |
| 49 | + if got != "550e8400-e29b-41d4-a716-446655440000" { |
| 50 | + t.Errorf("want trace ID, got %q", got) |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +func TestExtractTraceID_NotFound(t *testing.T) { |
| 55 | + got := extractTraceID("no trace id here") |
| 56 | + if got != "" { |
| 57 | + t.Errorf("want empty, got %q", got) |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +func TestExtractTraceID_Empty(t *testing.T) { |
| 62 | + got := extractTraceID("") |
| 63 | + if got != "" { |
| 64 | + t.Errorf("want empty string, got %q", got) |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +// --- Crawler.Run --- |
| 69 | + |
| 70 | +func TestRun_NoMessages(t *testing.T) { |
| 71 | + crawler := NewCrawler(&stubGraph{msgs: []NDRMessage{}}, &captureJS{}, testCrawlerMailbox) |
| 72 | + if err := crawler.Run(context.Background()); err != nil { |
| 73 | + t.Fatalf(errUnexpected, err) |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +func TestRun_GraphError(t *testing.T) { |
| 78 | + crawler := NewCrawler(&stubGraph{err: errors.New("graph down")}, &captureJS{}, testCrawlerMailbox) |
| 79 | + if err := crawler.Run(context.Background()); err == nil { |
| 80 | + t.Fatal("expected error when graph fails") |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +func TestRun_PublishesBouncRecord(t *testing.T) { |
| 85 | + msgs := []NDRMessage{ |
| 86 | + {ID: "m1", Subject: "Undeliverable: Hi", Body: "X-Dispatch-TraceId: aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"}, |
| 87 | + } |
| 88 | + js := &captureJS{} |
| 89 | + crawler := NewCrawler(&stubGraph{msgs: msgs}, js, testCrawlerMailbox) |
| 90 | + |
| 91 | + if err := crawler.Run(context.Background()); err != nil { |
| 92 | + t.Fatalf(errUnexpected, err) |
| 93 | + } |
| 94 | + if len(js.published) != 1 { |
| 95 | + t.Fatalf("want 1 published message, got %d", len(js.published)) |
| 96 | + } |
| 97 | + |
| 98 | + var rec domain.BounceRecord |
| 99 | + if err := json.Unmarshal(js.published[0], &rec); err != nil { |
| 100 | + t.Fatalf("unmarshal bounce record: %v", err) |
| 101 | + } |
| 102 | + if rec.OriginalTraceID != "aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee" { |
| 103 | + t.Errorf("OriginalTraceID: want trace id, got %q", rec.OriginalTraceID) |
| 104 | + } |
| 105 | + if rec.BounceReason != "Undeliverable: Hi" { |
| 106 | + t.Errorf("BounceReason: want subject, got %q", rec.BounceReason) |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +func TestRun_NoTraceID_StillPublishes(t *testing.T) { |
| 111 | + msgs := []NDRMessage{ |
| 112 | + {ID: "m2", Subject: "NDR", Body: "no trace id in body"}, |
| 113 | + } |
| 114 | + js := &captureJS{} |
| 115 | + crawler := NewCrawler(&stubGraph{msgs: msgs}, js, testCrawlerMailbox) |
| 116 | + |
| 117 | + if err := crawler.Run(context.Background()); err != nil { |
| 118 | + t.Fatalf(errUnexpected, err) |
| 119 | + } |
| 120 | + if len(js.published) != 1 { |
| 121 | + t.Fatalf("want 1 published message, got %d", len(js.published)) |
| 122 | + } |
| 123 | + |
| 124 | + var rec domain.BounceRecord |
| 125 | + _ = json.Unmarshal(js.published[0], &rec) |
| 126 | + if rec.OriginalTraceID != "" { |
| 127 | + t.Errorf("OriginalTraceID: want empty, got %q", rec.OriginalTraceID) |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +func TestRun_PublishError_ContinuesToNextMessage(t *testing.T) { |
| 132 | + msgs := []NDRMessage{ |
| 133 | + {ID: "m1", Subject: "NDR1", Body: ""}, |
| 134 | + {ID: "m2", Subject: "NDR2", Body: ""}, |
| 135 | + } |
| 136 | + js := &captureJS{err: errors.New("NATS down")} |
| 137 | + crawler := NewCrawler(&stubGraph{msgs: msgs}, js, testCrawlerMailbox) |
| 138 | + |
| 139 | + // Run must not return an error — publish errors are logged but do not abort the loop |
| 140 | + if err := crawler.Run(context.Background()); err != nil { |
| 141 | + t.Fatalf(errUnexpected, err) |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | +func TestRun_MultipleMessages(t *testing.T) { |
| 146 | + msgs := []NDRMessage{ |
| 147 | + {ID: "m1", Subject: "NDR1", Body: "X-Dispatch-TraceId: 11111111-1111-1111-1111-111111111111"}, |
| 148 | + {ID: "m2", Subject: "NDR2", Body: "X-Dispatch-TraceId: 22222222-2222-2222-2222-222222222222"}, |
| 149 | + } |
| 150 | + js := &captureJS{} |
| 151 | + crawler := NewCrawler(&stubGraph{msgs: msgs}, js, testCrawlerMailbox) |
| 152 | + |
| 153 | + if err := crawler.Run(context.Background()); err != nil { |
| 154 | + t.Fatalf(errUnexpected, err) |
| 155 | + } |
| 156 | + if len(js.published) != 2 { |
| 157 | + t.Errorf("want 2 published messages, got %d", len(js.published)) |
| 158 | + } |
| 159 | +} |
0 commit comments