github.com/machinefi/w3bstream@v1.6.5-rc9.0.20240426031326-b8c7c4876e72/cmd/srv-applet-mgr/tests/integrations/strategy_test.go (about)

     1  package integrations
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"os"
     7  	"path"
     8  	"testing"
     9  
    10  	. "github.com/onsi/gomega"
    11  
    12  	"github.com/machinefi/w3bstream/cmd/srv-applet-mgr/tests/clients/applet_mgr"
    13  	"github.com/machinefi/w3bstream/cmd/srv-applet-mgr/tests/requires"
    14  	"github.com/machinefi/w3bstream/pkg/depends/kit/httptransport/transformer"
    15  	"github.com/machinefi/w3bstream/pkg/modules/applet"
    16  	"github.com/machinefi/w3bstream/pkg/types"
    17  )
    18  
    19  func TestStrategyAPIs(t *testing.T) {
    20  	var (
    21  		client      = requires.AuthClient()
    22  		projectName = "test_strategy_project"
    23  		eventType   = "TYPE0"
    24  		handler     = "start"
    25  
    26  		appletID   types.SFID
    27  		strategyID types.SFID
    28  	)
    29  
    30  	t.Logf("random a project name: %s, use this name create a project and an applet.", projectName)
    31  
    32  	{
    33  		req := &applet_mgr.CreateProject{}
    34  		req.CreateReq.Name = projectName
    35  
    36  		_, _, err := client.CreateProject(req)
    37  		if err != nil {
    38  			panic(err)
    39  		}
    40  	}
    41  
    42  	{
    43  		cwd, err := os.Getwd()
    44  		filename := path.Join(cwd, "../testdata/log.wasm")
    45  		appletName := "testApplet"
    46  		wasmName := "test.log"
    47  
    48  		req := &applet_mgr.CreateApplet{ProjectName: projectName}
    49  		req.CreateReq.File = transformer.MustNewFileHeader("file", filename, bytes.NewBuffer(code))
    50  		req.CreateReq.Info = applet.Info{
    51  			AppletName: appletName,
    52  			WasmName:   wasmName,
    53  		}
    54  
    55  		rsp, _, err := client.CreateApplet(req)
    56  		if err != nil {
    57  			panic(err)
    58  		}
    59  		appletID = rsp.AppletID
    60  	}
    61  
    62  	defer func() {
    63  		req := &applet_mgr.RemoveProject{ProjectName: projectName}
    64  		_, err := client.RemoveProject(req)
    65  		if err != nil {
    66  			panic(err)
    67  		}
    68  	}()
    69  
    70  	t.Logf("random a strategy with EventType and Handler: %s - %s, then create it .", eventType, handler)
    71  
    72  	t.Run("Strategy", func(t *testing.T) {
    73  		t.Run("#CreateStrategy", func(t *testing.T) {
    74  			t.Run("#Success", func(t *testing.T) {
    75  
    76  				// create strategy
    77  				{
    78  					req := &applet_mgr.CreateStrategy{ProjectName: projectName}
    79  					req.CreateReq.AppletID = appletID
    80  					req.CreateReq.EventType = eventType
    81  					req.CreateReq.Handler = handler
    82  
    83  					rsp, _, err := client.CreateStrategy(req)
    84  					NewWithT(t).Expect(err).To(BeNil())
    85  					NewWithT(t).Expect(rsp.EventType).To(Equal(eventType))
    86  					strategyID = rsp.StrategyID
    87  				}
    88  
    89  				// get strategy
    90  				{
    91  					req := &applet_mgr.GetStrategy{StrategyID: strategyID}
    92  					rsp, _, err := client.GetStrategy(req)
    93  					NewWithT(t).Expect(err).To(BeNil())
    94  					NewWithT(t).Expect(rsp.EventType).To(Equal(eventType))
    95  					NewWithT(t).Expect(rsp.Handler).To(Equal(handler))
    96  				}
    97  
    98  				// update strategy
    99  				{
   100  					updateType := "updatetype"
   101  					updateHandle := "updatehandle"
   102  					req := &applet_mgr.UpdateStrategy{StrategyID: strategyID}
   103  					req.UpdateReq.AppletID = appletID
   104  					req.UpdateReq.EventType = updateType
   105  					req.UpdateReq.Handler = updateHandle
   106  					_, err := client.UpdateStrategy(req)
   107  					NewWithT(t).Expect(err).To(BeNil())
   108  				}
   109  
   110  				// remove strategy
   111  				{
   112  					req := &applet_mgr.RemoveStrategy{StrategyID: strategyID}
   113  					_, err := client.RemoveStrategy(req)
   114  					NewWithT(t).Expect(err).To(BeNil())
   115  				}
   116  			})
   117  		})
   118  	})
   119  
   120  	t.Run("BatchStrategies", func(t *testing.T) {
   121  		t.Run("#CreateStrategies", func(t *testing.T) {
   122  			t.Run("#Success", func(t *testing.T) {
   123  
   124  				// prepare data
   125  				num := 5
   126  				{
   127  					for i := 0; i < num; i++ {
   128  						updateType := fmt.Sprintf("updatetype%d", i)
   129  						req := &applet_mgr.CreateStrategy{ProjectName: projectName}
   130  						req.CreateReq.AppletID = appletID
   131  						req.CreateReq.EventType = updateType
   132  						req.CreateReq.Handler = handler
   133  
   134  						rsp, _, err := client.CreateStrategy(req)
   135  						NewWithT(t).Expect(err).To(BeNil())
   136  						NewWithT(t).Expect(rsp.EventType).To(Equal(updateType))
   137  					}
   138  				}
   139  
   140  				// get list strategies
   141  				{
   142  					req := &applet_mgr.ListStrategy{ProjectName: projectName}
   143  					_, _, err := client.ListStrategy(req)
   144  					NewWithT(t).Expect(err).To(BeNil())
   145  				}
   146  
   147  				// remove batch strategies
   148  				{
   149  					req := &applet_mgr.BatchRemoveStrategy{ProjectName: projectName}
   150  					_, err := client.BatchRemoveStrategy(req)
   151  					NewWithT(t).Expect(err).To(BeNil())
   152  				}
   153  
   154  			})
   155  		})
   156  	})
   157  }