|
| 1 | +package module |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/GoCodeAlone/workflow/interfaces" |
| 9 | +) |
| 10 | + |
| 11 | +// fixedErrorStep is a test step that always returns a configured error. |
| 12 | +type fixedErrorStep struct { |
| 13 | + name string |
| 14 | + err error |
| 15 | +} |
| 16 | + |
| 17 | +func (s *fixedErrorStep) Name() string { return s.name } |
| 18 | +func (s *fixedErrorStep) Execute(_ context.Context, _ *PipelineContext) (*interfaces.StepResult, error) { |
| 19 | + return nil, s.err |
| 20 | +} |
| 21 | + |
| 22 | +// fixedSuccessStep is a test step that always succeeds. |
| 23 | +type fixedSuccessStep struct { |
| 24 | + name string |
| 25 | +} |
| 26 | + |
| 27 | +func (s *fixedSuccessStep) Name() string { return s.name } |
| 28 | +func (s *fixedSuccessStep) Execute(_ context.Context, _ *PipelineContext) (*interfaces.StepResult, error) { |
| 29 | + return &interfaces.StepResult{Output: map[string]any{"ok": true}}, nil |
| 30 | +} |
| 31 | + |
| 32 | +// TestErrorStatusStep_WrapsErrorWithValidationError verifies that a plain error |
| 33 | +// returned by the inner step is wrapped in a ValidationError with the configured |
| 34 | +// HTTP status code. |
| 35 | +func TestErrorStatusStep_WrapsErrorWithValidationError(t *testing.T) { |
| 36 | + inner := &fixedErrorStep{name: "validate", err: errors.New("card doesn't match")} |
| 37 | + step := NewErrorStatusStep(inner, 400) |
| 38 | + |
| 39 | + _, err := step.Execute(context.Background(), &PipelineContext{ |
| 40 | + Current: map[string]any{}, |
| 41 | + Metadata: map[string]any{}, |
| 42 | + }) |
| 43 | + if err == nil { |
| 44 | + t.Fatal("expected error, got nil") |
| 45 | + } |
| 46 | + if !interfaces.IsValidationError(err) { |
| 47 | + t.Fatalf("expected ValidationError, got %T: %v", err, err) |
| 48 | + } |
| 49 | + if got := interfaces.ValidationErrorStatus(err); got != 400 { |
| 50 | + t.Errorf("expected status 400, got %d", got) |
| 51 | + } |
| 52 | + if err.Error() != "card doesn't match" { |
| 53 | + t.Errorf("unexpected error message: %q", err.Error()) |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +// TestErrorStatusStep_PassesThroughExistingValidationError verifies that a |
| 58 | +// ValidationError already set by the inner step is not double-wrapped. |
| 59 | +func TestErrorStatusStep_PassesThroughExistingValidationError(t *testing.T) { |
| 60 | + inner := &fixedErrorStep{ |
| 61 | + name: "validate", |
| 62 | + err: interfaces.NewValidationError("already a validation error", 422), |
| 63 | + } |
| 64 | + step := NewErrorStatusStep(inner, 400) |
| 65 | + |
| 66 | + _, err := step.Execute(context.Background(), &PipelineContext{ |
| 67 | + Current: map[string]any{}, |
| 68 | + Metadata: map[string]any{}, |
| 69 | + }) |
| 70 | + if err == nil { |
| 71 | + t.Fatal("expected error, got nil") |
| 72 | + } |
| 73 | + // Should keep the original 422, not override with 400 |
| 74 | + if got := interfaces.ValidationErrorStatus(err); got != 422 { |
| 75 | + t.Errorf("expected original status 422 preserved, got %d", got) |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +// TestErrorStatusStep_SuccessPassesThrough verifies that a successful step |
| 80 | +// result is returned unchanged. |
| 81 | +func TestErrorStatusStep_SuccessPassesThrough(t *testing.T) { |
| 82 | + inner := &fixedSuccessStep{name: "ok-step"} |
| 83 | + step := NewErrorStatusStep(inner, 400) |
| 84 | + |
| 85 | + result, err := step.Execute(context.Background(), &PipelineContext{ |
| 86 | + Current: map[string]any{}, |
| 87 | + Metadata: map[string]any{}, |
| 88 | + }) |
| 89 | + if err != nil { |
| 90 | + t.Fatalf("unexpected error: %v", err) |
| 91 | + } |
| 92 | + if result == nil || result.Output["ok"] != true { |
| 93 | + t.Errorf("expected successful result with ok=true, got %v", result) |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +// TestStepConfig_ErrorStatus verifies that a pipeline with error_status on a |
| 98 | +// step surfaces the configured 4xx status when the step fails. |
| 99 | +func TestStepConfig_ErrorStatus(t *testing.T) { |
| 100 | + inner := &fixedErrorStep{name: "validate", err: errors.New("invalid input")} |
| 101 | + step := NewErrorStatusStep(inner, 400) |
| 102 | + |
| 103 | + p := &Pipeline{ |
| 104 | + Name: "test-pipeline", |
| 105 | + Steps: []PipelineStep{step}, |
| 106 | + OnError: ErrorStrategyStop, |
| 107 | + } |
| 108 | + |
| 109 | + _, err := p.Execute(context.Background(), map[string]any{}) |
| 110 | + if err == nil { |
| 111 | + t.Fatal("expected error from pipeline") |
| 112 | + } |
| 113 | + if !interfaces.IsValidationError(err) { |
| 114 | + t.Fatalf("expected ValidationError in pipeline error chain, got %T: %v", err, err) |
| 115 | + } |
| 116 | + if got := interfaces.ValidationErrorStatus(err); got != 400 { |
| 117 | + t.Errorf("expected status 400 from pipeline error, got %d", got) |
| 118 | + } |
| 119 | +} |
0 commit comments