|
| 1 | +package external |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/GoCodeAlone/workflow/module" |
| 8 | + pb "github.com/GoCodeAlone/workflow/plugin/external/proto" |
| 9 | + "google.golang.org/grpc" |
| 10 | + "google.golang.org/protobuf/types/known/emptypb" |
| 11 | + "google.golang.org/protobuf/types/known/structpb" |
| 12 | +) |
| 13 | + |
| 14 | +// stubPluginServiceClient is a minimal PluginServiceClient that captures |
| 15 | +// ExecuteStep requests for assertion in tests. |
| 16 | +type stubPluginServiceClient struct { |
| 17 | + pb.UnimplementedPluginServiceServer // provides no-op implementations |
| 18 | + |
| 19 | + lastRequest *pb.ExecuteStepRequest |
| 20 | + response *pb.ExecuteStepResponse |
| 21 | +} |
| 22 | + |
| 23 | +// ExecuteStep records the request and returns the configured response. |
| 24 | +func (c *stubPluginServiceClient) ExecuteStep(_ context.Context, req *pb.ExecuteStepRequest, _ ...grpc.CallOption) (*pb.ExecuteStepResponse, error) { |
| 25 | + c.lastRequest = req |
| 26 | + if c.response != nil { |
| 27 | + return c.response, nil |
| 28 | + } |
| 29 | + return &pb.ExecuteStepResponse{Output: &structpb.Struct{}}, nil |
| 30 | +} |
| 31 | + |
| 32 | +// Implement the remaining PluginServiceClient interface methods as no-ops. |
| 33 | +func (c *stubPluginServiceClient) GetManifest(_ context.Context, _ *emptypb.Empty, _ ...grpc.CallOption) (*pb.Manifest, error) { |
| 34 | + return &pb.Manifest{}, nil |
| 35 | +} |
| 36 | +func (c *stubPluginServiceClient) GetModuleTypes(_ context.Context, _ *emptypb.Empty, _ ...grpc.CallOption) (*pb.TypeList, error) { |
| 37 | + return &pb.TypeList{}, nil |
| 38 | +} |
| 39 | +func (c *stubPluginServiceClient) GetStepTypes(_ context.Context, _ *emptypb.Empty, _ ...grpc.CallOption) (*pb.TypeList, error) { |
| 40 | + return &pb.TypeList{}, nil |
| 41 | +} |
| 42 | +func (c *stubPluginServiceClient) GetTriggerTypes(_ context.Context, _ *emptypb.Empty, _ ...grpc.CallOption) (*pb.TypeList, error) { |
| 43 | + return &pb.TypeList{}, nil |
| 44 | +} |
| 45 | +func (c *stubPluginServiceClient) GetModuleSchemas(_ context.Context, _ *emptypb.Empty, _ ...grpc.CallOption) (*pb.ModuleSchemaList, error) { |
| 46 | + return &pb.ModuleSchemaList{}, nil |
| 47 | +} |
| 48 | +func (c *stubPluginServiceClient) CreateModule(_ context.Context, _ *pb.CreateModuleRequest, _ ...grpc.CallOption) (*pb.HandleResponse, error) { |
| 49 | + return &pb.HandleResponse{}, nil |
| 50 | +} |
| 51 | +func (c *stubPluginServiceClient) InitModule(_ context.Context, _ *pb.HandleRequest, _ ...grpc.CallOption) (*pb.ErrorResponse, error) { |
| 52 | + return &pb.ErrorResponse{}, nil |
| 53 | +} |
| 54 | +func (c *stubPluginServiceClient) StartModule(_ context.Context, _ *pb.HandleRequest, _ ...grpc.CallOption) (*pb.ErrorResponse, error) { |
| 55 | + return &pb.ErrorResponse{}, nil |
| 56 | +} |
| 57 | +func (c *stubPluginServiceClient) StopModule(_ context.Context, _ *pb.HandleRequest, _ ...grpc.CallOption) (*pb.ErrorResponse, error) { |
| 58 | + return &pb.ErrorResponse{}, nil |
| 59 | +} |
| 60 | +func (c *stubPluginServiceClient) DestroyModule(_ context.Context, _ *pb.HandleRequest, _ ...grpc.CallOption) (*pb.ErrorResponse, error) { |
| 61 | + return &pb.ErrorResponse{}, nil |
| 62 | +} |
| 63 | +func (c *stubPluginServiceClient) CreateStep(_ context.Context, _ *pb.CreateStepRequest, _ ...grpc.CallOption) (*pb.HandleResponse, error) { |
| 64 | + return &pb.HandleResponse{}, nil |
| 65 | +} |
| 66 | +func (c *stubPluginServiceClient) DestroyStep(_ context.Context, _ *pb.HandleRequest, _ ...grpc.CallOption) (*pb.ErrorResponse, error) { |
| 67 | + return &pb.ErrorResponse{}, nil |
| 68 | +} |
| 69 | +func (c *stubPluginServiceClient) InvokeService(_ context.Context, _ *pb.InvokeServiceRequest, _ ...grpc.CallOption) (*pb.InvokeServiceResponse, error) { |
| 70 | + return &pb.InvokeServiceResponse{}, nil |
| 71 | +} |
| 72 | +func (c *stubPluginServiceClient) DeliverMessage(_ context.Context, _ *pb.DeliverMessageRequest, _ ...grpc.CallOption) (*pb.DeliverMessageResponse, error) { |
| 73 | + return &pb.DeliverMessageResponse{}, nil |
| 74 | +} |
| 75 | +func (c *stubPluginServiceClient) GetConfigFragment(_ context.Context, _ *emptypb.Empty, _ ...grpc.CallOption) (*pb.ConfigFragmentResponse, error) { |
| 76 | + return &pb.ConfigFragmentResponse{}, nil |
| 77 | +} |
| 78 | +func (c *stubPluginServiceClient) GetAsset(_ context.Context, _ *pb.GetAssetRequest, _ ...grpc.CallOption) (*pb.GetAssetResponse, error) { |
| 79 | + return &pb.GetAssetResponse{}, nil |
| 80 | +} |
| 81 | + |
| 82 | +// TestRemoteStep_Execute_ResolvesTemplatesInConfig verifies that template |
| 83 | +// expressions in step config are resolved against the PipelineContext before |
| 84 | +// the gRPC ExecuteStep call is made. |
| 85 | +func TestRemoteStep_Execute_ResolvesTemplatesInConfig(t *testing.T) { |
| 86 | + stub := &stubPluginServiceClient{} |
| 87 | + cfg := map[string]any{ |
| 88 | + "user_id": `{{ index .steps "fetch-user" "row" "id" }}`, |
| 89 | + "static": "plain-value", |
| 90 | + } |
| 91 | + step := NewRemoteStep("test-step", "handle-1", stub, cfg) |
| 92 | + |
| 93 | + pc := module.NewPipelineContext(nil, nil) |
| 94 | + pc.MergeStepOutput("fetch-user", map[string]any{ |
| 95 | + "row": map[string]any{"id": "user-42"}, |
| 96 | + }) |
| 97 | + |
| 98 | + _, err := step.Execute(context.Background(), pc) |
| 99 | + if err != nil { |
| 100 | + t.Fatalf("unexpected error: %v", err) |
| 101 | + } |
| 102 | + |
| 103 | + if stub.lastRequest == nil { |
| 104 | + t.Fatal("expected ExecuteStep to be called") |
| 105 | + } |
| 106 | + |
| 107 | + sent := stub.lastRequest.Config.AsMap() |
| 108 | + |
| 109 | + if sent["user_id"] != "user-42" { |
| 110 | + t.Errorf("expected config user_id='user-42', got %q", sent["user_id"]) |
| 111 | + } |
| 112 | + if sent["static"] != "plain-value" { |
| 113 | + t.Errorf("expected config static='plain-value', got %q", sent["static"]) |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +// TestRemoteStep_Execute_NilConfig verifies that a step with no config does |
| 118 | +// not panic and sends a nil Config in the request (so the plugin receives nil, |
| 119 | +// not an empty map). |
| 120 | +func TestRemoteStep_Execute_NilConfig(t *testing.T) { |
| 121 | + stub := &stubPluginServiceClient{} |
| 122 | + step := NewRemoteStep("test-step", "handle-2", stub, nil) |
| 123 | + |
| 124 | + pc := module.NewPipelineContext(nil, nil) |
| 125 | + |
| 126 | + _, err := step.Execute(context.Background(), pc) |
| 127 | + if err != nil { |
| 128 | + t.Fatalf("unexpected error: %v", err) |
| 129 | + } |
| 130 | + if stub.lastRequest == nil { |
| 131 | + t.Fatal("expected ExecuteStep to be called") |
| 132 | + } |
| 133 | + if stub.lastRequest.Config != nil { |
| 134 | + t.Errorf("expected nil Config for step with no config, got %v", stub.lastRequest.Config) |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +// TestRemoteStep_Execute_StaticConfigPassthrough verifies that a config with |
| 139 | +// no template expressions is passed through unmodified. |
| 140 | +func TestRemoteStep_Execute_StaticConfigPassthrough(t *testing.T) { |
| 141 | + stub := &stubPluginServiceClient{} |
| 142 | + cfg := map[string]any{ |
| 143 | + "endpoint": "https://api.example.com", |
| 144 | + "timeout": float64(30), |
| 145 | + } |
| 146 | + step := NewRemoteStep("test-step", "handle-3", stub, cfg) |
| 147 | + |
| 148 | + _, err := step.Execute(context.Background(), module.NewPipelineContext(nil, nil)) |
| 149 | + if err != nil { |
| 150 | + t.Fatalf("unexpected error: %v", err) |
| 151 | + } |
| 152 | + |
| 153 | + sent := stub.lastRequest.Config.AsMap() |
| 154 | + if sent["endpoint"] != "https://api.example.com" { |
| 155 | + t.Errorf("expected endpoint='https://api.example.com', got %q", sent["endpoint"]) |
| 156 | + } |
| 157 | + if sent["timeout"] != float64(30) { |
| 158 | + t.Errorf("expected timeout=30, got %v", sent["timeout"]) |
| 159 | + } |
| 160 | +} |
0 commit comments