|
| 1 | +package correlation_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "log/slog" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/studiolambda/cosmos/contract" |
| 10 | + "github.com/studiolambda/cosmos/framework/correlation" |
| 11 | + |
| 12 | + "github.com/stretchr/testify/require" |
| 13 | +) |
| 14 | + |
| 15 | +func TestHandlerAddsIDFromContext(t *testing.T) { |
| 16 | + t.Parallel() |
| 17 | + |
| 18 | + var buf bytes.Buffer |
| 19 | + handler := correlation.Handler(slog.NewTextHandler(&buf, nil)) |
| 20 | + logger := slog.New(handler) |
| 21 | + |
| 22 | + ctx := context.WithValue(context.Background(), contract.CorrelationIDKey, "trace-abc-123") |
| 23 | + logger.InfoContext(ctx, "test message") |
| 24 | + |
| 25 | + output := buf.String() |
| 26 | + require.Contains(t, output, "correlation_id") |
| 27 | + require.Contains(t, output, "trace-abc-123") |
| 28 | +} |
| 29 | + |
| 30 | +func TestHandlerOmitsWhenMissing(t *testing.T) { |
| 31 | + t.Parallel() |
| 32 | + |
| 33 | + var buf bytes.Buffer |
| 34 | + handler := correlation.Handler(slog.NewTextHandler(&buf, nil)) |
| 35 | + logger := slog.New(handler) |
| 36 | + |
| 37 | + logger.InfoContext(context.Background(), "test message") |
| 38 | + |
| 39 | + output := buf.String() |
| 40 | + require.NotContains(t, output, "correlation_id") |
| 41 | +} |
| 42 | + |
| 43 | +func TestHandlerOmitsWhenEmpty(t *testing.T) { |
| 44 | + t.Parallel() |
| 45 | + |
| 46 | + var buf bytes.Buffer |
| 47 | + handler := correlation.Handler(slog.NewTextHandler(&buf, nil)) |
| 48 | + logger := slog.New(handler) |
| 49 | + |
| 50 | + ctx := context.WithValue(context.Background(), contract.CorrelationIDKey, "") |
| 51 | + logger.InfoContext(ctx, "test message") |
| 52 | + |
| 53 | + output := buf.String() |
| 54 | + require.NotContains(t, output, "correlation_id") |
| 55 | +} |
| 56 | + |
| 57 | +func TestHandlerPreservesExistingAttrs(t *testing.T) { |
| 58 | + t.Parallel() |
| 59 | + |
| 60 | + var buf bytes.Buffer |
| 61 | + handler := correlation.Handler(slog.NewTextHandler(&buf, nil)) |
| 62 | + logger := slog.New(handler) |
| 63 | + |
| 64 | + ctx := context.WithValue(context.Background(), contract.CorrelationIDKey, "id-456") |
| 65 | + logger.InfoContext(ctx, "test", "extra", "value") |
| 66 | + |
| 67 | + output := buf.String() |
| 68 | + require.Contains(t, output, "correlation_id") |
| 69 | + require.Contains(t, output, "id-456") |
| 70 | + require.Contains(t, output, "extra") |
| 71 | + require.Contains(t, output, "value") |
| 72 | +} |
| 73 | + |
| 74 | +func TestHandlerWithAttrsPreservesWrapping(t *testing.T) { |
| 75 | + t.Parallel() |
| 76 | + |
| 77 | + var buf bytes.Buffer |
| 78 | + handler := correlation.Handler(slog.NewTextHandler(&buf, nil)) |
| 79 | + logger := slog.New(handler).With("service", "api") |
| 80 | + |
| 81 | + ctx := context.WithValue(context.Background(), contract.CorrelationIDKey, "id-789") |
| 82 | + logger.InfoContext(ctx, "test") |
| 83 | + |
| 84 | + output := buf.String() |
| 85 | + require.Contains(t, output, "service") |
| 86 | + require.Contains(t, output, "api") |
| 87 | + require.Contains(t, output, "correlation_id") |
| 88 | + require.Contains(t, output, "id-789") |
| 89 | +} |
| 90 | + |
| 91 | +func TestHandlerWithGroupPreservesWrapping(t *testing.T) { |
| 92 | + t.Parallel() |
| 93 | + |
| 94 | + var buf bytes.Buffer |
| 95 | + handler := correlation.Handler(slog.NewTextHandler(&buf, nil)) |
| 96 | + logger := slog.New(handler).WithGroup("request") |
| 97 | + |
| 98 | + ctx := context.WithValue(context.Background(), contract.CorrelationIDKey, "id-group") |
| 99 | + logger.InfoContext(ctx, "test") |
| 100 | + |
| 101 | + output := buf.String() |
| 102 | + require.Contains(t, output, "id-group") |
| 103 | +} |
| 104 | + |
| 105 | +func TestHandlerRespectsLevel(t *testing.T) { |
| 106 | + t.Parallel() |
| 107 | + |
| 108 | + var buf bytes.Buffer |
| 109 | + handler := correlation.Handler( |
| 110 | + slog.NewTextHandler(&buf, &slog.HandlerOptions{Level: slog.LevelError}), |
| 111 | + ) |
| 112 | + logger := slog.New(handler) |
| 113 | + |
| 114 | + ctx := context.WithValue(context.Background(), contract.CorrelationIDKey, "id-level") |
| 115 | + logger.InfoContext(ctx, "should not appear") |
| 116 | + |
| 117 | + require.Empty(t, buf.String()) |
| 118 | +} |
0 commit comments