From 65236923945e65b52643debaedc01c6229bf1dc2 Mon Sep 17 00:00:00 2001 From: Quint Daenen Date: Mon, 6 May 2024 13:05:22 +0200 Subject: [PATCH] Remove mock replica package. --- gen/generator.go | 195 ------ gen/templates/agent_test.gotmpl | 57 -- ic/assetstorage/agent_test.go | 1069 ------------------------------- ic/cmc/agent_test.go | 260 -------- ic/ic/agent_test.go | 860 ------------------------- ic/icparchive/agent_test.go | 101 --- ic/icpledger/agent_test.go | 741 --------------------- ic/icrc1/agent_test.go | 280 -------- ic/testdata/gen.go | 12 - ic/types_test.go | 237 ------- ic/wallet/agent_test.go | 1041 ------------------------------ mock/replica.go | 265 -------- mock/replica_test.go | 92 --- 13 files changed, 5210 deletions(-) delete mode 100644 gen/templates/agent_test.gotmpl delete mode 100755 ic/assetstorage/agent_test.go delete mode 100755 ic/cmc/agent_test.go delete mode 100755 ic/ic/agent_test.go delete mode 100644 ic/icparchive/agent_test.go delete mode 100644 ic/icpledger/agent_test.go delete mode 100755 ic/icrc1/agent_test.go delete mode 100644 ic/types_test.go delete mode 100755 ic/wallet/agent_test.go delete mode 100644 mock/replica.go delete mode 100644 mock/replica_test.go diff --git a/gen/generator.go b/gen/generator.go index 1f41680..616b3a3 100644 --- a/gen/generator.go +++ b/gen/generator.go @@ -255,198 +255,12 @@ func (g *Generator) GenerateActorTypes() ([]byte, error) { return io.ReadAll(&tmpl) } -func (g *Generator) GenerateMock() ([]byte, error) { - definitions := make(map[string]did.Data) - for _, definition := range g.ServiceDescription.Definitions { - switch definition := definition.(type) { - case did.Type: - definitions[definition.Id] = definition.Data - } - } - var methods []agentArgsMethod - for _, service := range g.ServiceDescription.Services { - for _, method := range service.Methods { - name := rawName(method.Name) - f := method.Func - - var argumentTypes []agentArgsMethodArgument - var filledArgumentTypes []agentArgsMethodArgument - for i, t := range f.ArgTypes { - var n string - if (t.Name != nil) && (*t.Name != "") { - n = *t.Name - } else { - n = fmt.Sprintf("arg%d", i) - } - argumentTypes = append(argumentTypes, agentArgsMethodArgument{ - Name: n, - Type: g.dataToString(g.PackageName, t.Data), - }) - filledArgumentTypes = append(filledArgumentTypes, agentArgsMethodArgument{ - Name: n, - Type: g.dataToGoReturnValue(definitions, g.PackageName, t.Data), - }) - } - - var returnTypes []string - for _, t := range f.ResTypes { - returnTypes = append(returnTypes, g.dataToGoReturnValue(definitions, g.PackageName, t.Data)) - } - - typ := "Call" - if f.Annotation != nil && *f.Annotation == did.AnnQuery { - typ = "Query" - } - - methods = append(methods, agentArgsMethod{ - RawName: name, - Name: funcName("", name), - Type: typ, - ArgumentTypes: argumentTypes, - FilledArgumentTypes: filledArgumentTypes, - ReturnTypes: returnTypes, - }) - } - } - t, ok := templates["agent_test"] - if !ok { - return nil, fmt.Errorf("template not found") - } - var tmpl bytes.Buffer - if err := t.Execute(&tmpl, agentMockArgs{ - AgentName: g.AgentName, - CanisterName: g.CanisterName, - PackageName: g.PackageName, - ModulePath: g.ModulePath, - UsedIDL: g.usedIDL, - Methods: methods, - }); err != nil { - return nil, err - } - return io.ReadAll(&tmpl) -} - // Indirect sets the generator to generate indirect calls. func (g *Generator) Indirect() *Generator { g.indirect = true return g } -func (g *Generator) dataToGoReturnValue(definitions map[string]did.Data, prefix string, data did.Data) string { - switch t := data.(type) { - case did.Primitive: - switch t { - case "nat": - g.usedIDL = true - return "idl.NewNat(uint(0))" - case "int": - g.usedIDL = true - return "idl.NewInt(0)" - default: - return fmt.Sprintf("*new(%s)", g.dataToString(prefix, data)) - } - case did.DataId: - switch data := definitions[t.String()].(type) { - case did.Record: - var fields []string - for _, f := range data { - var data did.Data - if f.Data != nil { - data = *f.Data - } else { - data = did.DataId(*f.NameData) - } - fields = append(fields, g.dataToGoReturnValue(definitions, prefix, data)) - } - if len(fields) == 0 { - return fmt.Sprintf("%s{}", g.dataToString(prefix, t)) - } - return fmt.Sprintf("%s{\n%s,\n}", g.dataToString(prefix, t), strings.Join(fields, ",\n")) - case did.Variant: - f := data[0] - var d did.Data - if f.Data != nil { - d = *f.Data - } else { - d = did.DataId(*f.NameData) - } - field := g.dataToGoReturnValue(definitions, prefix, d) - if !strings.HasPrefix(field, "*") { - g.usedIDL = true - field = fmt.Sprintf("idl.Ptr(%s)", field) - } else { - field = strings.TrimPrefix(field, "*") - } - var name string - if f.Name != nil { - name = *f.Name - } else { - name = *f.NameData - } - return fmt.Sprintf("%s{\n%s: %s,\n}", g.dataToString(prefix, t), funcName("", name), field) - default: - switch data := data.(type) { - case did.Primitive: - switch data { - case "nat": - g.usedIDL = true - return "idl.NewNat(uint(0))" - case "int": - g.usedIDL = true - return "idl.NewInt(0)" - } - } - if data != nil { - return fmt.Sprintf("*new(%s)", g.dataToString(prefix, data)) - } - return "*new(idl.Null)" - } - case did.Record: - var fields []string - for _, f := range t { - var data did.Data - if f.Data != nil { - data = *f.Data - } else { - data = did.DataId(*f.NameData) - } - fields = append(fields, g.dataToGoReturnValue(definitions, prefix, data)) - } - if len(fields) == 0 { - return fmt.Sprintf("%s{}", g.dataToString(prefix, data)) - } - return fmt.Sprintf("%s{\n%s,\n}", g.dataToString(prefix, data), strings.Join(fields, ",\n")) - case did.Variant: - f := t[0] - var name string - var d did.Data - if f.Data != nil { - name = *f.Name - d = *f.Data - } else { - name = *f.NameData - d = did.DataId(*f.NameData) - } - field := g.dataToGoReturnValue(definitions, prefix, d) - if !strings.HasPrefix(field, "*") { - g.usedIDL = true - field = fmt.Sprintf("idl.Ptr(%s)", field) - } else { - field = strings.TrimPrefix(field, "*") - } - return fmt.Sprintf("%s{\n%s: %s,\n}", g.dataToString(prefix, data), funcName("", name), field) - case did.Vector: - switch t.Data.(type) { - case did.DataId: - return fmt.Sprintf("[]%s{%s}", funcName(prefix, t.Data.String()), g.dataToGoReturnValue(definitions, prefix, t.Data)) - default: - return fmt.Sprintf("[]%s{%s}", g.dataToString(prefix, t.Data), g.dataToGoReturnValue(definitions, prefix, t.Data)) - } - default: - return fmt.Sprintf("*new(%s)", g.dataToString(prefix, data)) - } -} - func (g *Generator) dataToMotokoReturnValue(s rand.Source, definitions map[string]did.Data, data did.Data) string { r := rand.New(s) switch t := data.(type) { @@ -809,12 +623,3 @@ type agentArgsMethodArgument struct { Name string Type string } - -type agentMockArgs struct { - AgentName string - CanisterName string - PackageName string - ModulePath string - UsedIDL bool - Methods []agentArgsMethod -} diff --git a/gen/templates/agent_test.gotmpl b/gen/templates/agent_test.gotmpl deleted file mode 100644 index 6051bd8..0000000 --- a/gen/templates/agent_test.gotmpl +++ /dev/null @@ -1,57 +0,0 @@ -// Do NOT edit this file. It was automatically generated by https://github.com/aviate-labs/agent-go. -package {{ .PackageName }}_test - -import ( - "github.com/aviate-labs/agent-go" - {{ if .UsedIDL }}"github.com/aviate-labs/agent-go/candid/idl"{{ end }} - "github.com/aviate-labs/agent-go/mock" - "github.com/aviate-labs/agent-go/principal" - "net/http/httptest" - "net/url" - "testing" - - "{{ .ModulePath }}/{{ .PackageName }}" -) - -// newAgent creates a new agent with the given (mock) methods. -// Runs a mock replica in the background. -func newAgent(methods []mock.Method) (*{{ .PackageName }}.Agent, error) { - replica := mock.NewReplica() - canisterId := principal.Principal{Raw: []byte("{{ .PackageName }}")} - replica.AddCanister(canisterId, methods) - s := httptest.NewServer(replica) - u, _ := url.Parse(s.URL) - a, err := {{ .PackageName }}.NewAgent(canisterId, agent.Config{ - ClientConfig: &agent.ClientConfig{Host: u}, - FetchRootKey: true, - }) - if err != nil { - return nil, err - } - return a, nil -} -{{- range .Methods }} - -// Test_{{ .Name }} tests the "{{ .RawName }}" method on the "{{ $.CanisterName }}" canister. -func Test_{{ .Name }}(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "{{ .RawName }}", - Arguments: []any{{ "{" }}{{ range $i, $e := .ArgumentTypes }}{{ if $i }}, {{ end }}new({{ $e.Type }}){{ end }}{{ "}" }}, - Handler: func (request mock.Request) ([]any, error) { - return []any{{ "{" }}{{ range $i, $e := .ReturnTypes }}{{ if $i }}, {{ end }}{{ $e }}{{ end }}{{ "}" }}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - {{ range $i, $e := .FilledArgumentTypes }} - var a{{ $i }} = {{ $e.Type }} - {{- end }} - if {{ range .ReturnTypes }}_, {{ end }}err := a.{{ .Name }}({{ range $i, $e := .ArgumentTypes }}{{ if $i }}, {{ end }}a{{ $i }}{{ end }}); err != nil { - t.Fatal(err) - } - -} -{{- end }} diff --git a/ic/assetstorage/agent_test.go b/ic/assetstorage/agent_test.go deleted file mode 100755 index 01192c9..0000000 --- a/ic/assetstorage/agent_test.go +++ /dev/null @@ -1,1069 +0,0 @@ -// Do NOT edit this file. It was automatically generated by https://github.com/aviate-labs/agent-go. -package assetstorage_test - -import ( - "github.com/aviate-labs/agent-go" - "github.com/aviate-labs/agent-go/candid/idl" - "github.com/aviate-labs/agent-go/mock" - "github.com/aviate-labs/agent-go/principal" - "net/http/httptest" - "net/url" - "testing" - - "github.com/aviate-labs/agent-go/ic/assetstorage" -) - -// Test_ApiVersion tests the "api_version" method on the "assetstorage" canister. -func Test_ApiVersion(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "api_version", - Arguments: []any{}, - Handler: func(request mock.Request) ([]any, error) { - return []any{*new(uint16)}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - if _, err := a.ApiVersion(); err != nil { - t.Fatal(err) - } - -} - -// Test_Authorize tests the "authorize" method on the "assetstorage" canister. -func Test_Authorize(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "authorize", - Arguments: []any{new(principal.Principal)}, - Handler: func(request mock.Request) ([]any, error) { - return []any{}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - var a0 = *new(principal.Principal) - if err := a.Authorize(a0); err != nil { - t.Fatal(err) - } - -} - -// Test_CertifiedTree tests the "certified_tree" method on the "assetstorage" canister. -func Test_CertifiedTree(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "certified_tree", - Arguments: []any{new(struct { - })}, - Handler: func(request mock.Request) ([]any, error) { - return []any{struct { - Certificate []byte `ic:"certificate" json:"certificate"` - Tree []byte `ic:"tree" json:"tree"` - }{ - *new([]byte), - *new([]byte), - }}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - var a0 = struct { - }{} - if _, err := a.CertifiedTree(a0); err != nil { - t.Fatal(err) - } - -} - -// Test_Clear tests the "clear" method on the "assetstorage" canister. -func Test_Clear(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "clear", - Arguments: []any{new(assetstorage.ClearArguments)}, - Handler: func(request mock.Request) ([]any, error) { - return []any{}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - var a0 = assetstorage.ClearArguments{} - if err := a.Clear(a0); err != nil { - t.Fatal(err) - } - -} - -// Test_CommitBatch tests the "commit_batch" method on the "assetstorage" canister. -func Test_CommitBatch(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "commit_batch", - Arguments: []any{new(assetstorage.CommitBatchArguments)}, - Handler: func(request mock.Request) ([]any, error) { - return []any{}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - var a0 = assetstorage.CommitBatchArguments{ - idl.NewNat(uint(0)), - []assetstorage.BatchOperationKind{{ - CreateAsset: idl.Ptr(assetstorage.CreateAssetArguments{ - *new(string), - *new(string), - *new(*uint64), - *new(*[]assetstorage.HeaderField), - *new(*bool), - *new(*bool), - }), - }}, - } - if err := a.CommitBatch(a0); err != nil { - t.Fatal(err) - } - -} - -// Test_CommitProposedBatch tests the "commit_proposed_batch" method on the "assetstorage" canister. -func Test_CommitProposedBatch(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "commit_proposed_batch", - Arguments: []any{new(assetstorage.CommitProposedBatchArguments)}, - Handler: func(request mock.Request) ([]any, error) { - return []any{}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - var a0 = assetstorage.CommitProposedBatchArguments{ - idl.NewNat(uint(0)), - *new([]byte), - } - if err := a.CommitProposedBatch(a0); err != nil { - t.Fatal(err) - } - -} - -// Test_ComputeEvidence tests the "compute_evidence" method on the "assetstorage" canister. -func Test_ComputeEvidence(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "compute_evidence", - Arguments: []any{new(assetstorage.ComputeEvidenceArguments)}, - Handler: func(request mock.Request) ([]any, error) { - return []any{*new(*[]byte)}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - var a0 = assetstorage.ComputeEvidenceArguments{ - idl.NewNat(uint(0)), - *new(*uint16), - } - if _, err := a.ComputeEvidence(a0); err != nil { - t.Fatal(err) - } - -} - -// Test_Configure tests the "configure" method on the "assetstorage" canister. -func Test_Configure(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "configure", - Arguments: []any{new(assetstorage.ConfigureArguments)}, - Handler: func(request mock.Request) ([]any, error) { - return []any{}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - var a0 = assetstorage.ConfigureArguments{ - *new(**uint64), - *new(**uint64), - *new(**uint64), - } - if err := a.Configure(a0); err != nil { - t.Fatal(err) - } - -} - -// Test_CreateAsset tests the "create_asset" method on the "assetstorage" canister. -func Test_CreateAsset(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "create_asset", - Arguments: []any{new(assetstorage.CreateAssetArguments)}, - Handler: func(request mock.Request) ([]any, error) { - return []any{}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - var a0 = assetstorage.CreateAssetArguments{ - *new(string), - *new(string), - *new(*uint64), - *new(*[]assetstorage.HeaderField), - *new(*bool), - *new(*bool), - } - if err := a.CreateAsset(a0); err != nil { - t.Fatal(err) - } - -} - -// Test_CreateBatch tests the "create_batch" method on the "assetstorage" canister. -func Test_CreateBatch(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "create_batch", - Arguments: []any{new(struct { - })}, - Handler: func(request mock.Request) ([]any, error) { - return []any{struct { - BatchId assetstorage.BatchId `ic:"batch_id" json:"batch_id"` - }{ - idl.NewNat(uint(0)), - }}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - var a0 = struct { - }{} - if _, err := a.CreateBatch(a0); err != nil { - t.Fatal(err) - } - -} - -// Test_CreateChunk tests the "create_chunk" method on the "assetstorage" canister. -func Test_CreateChunk(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "create_chunk", - Arguments: []any{new(struct { - BatchId assetstorage.BatchId `ic:"batch_id" json:"batch_id"` - Content []byte `ic:"content" json:"content"` - })}, - Handler: func(request mock.Request) ([]any, error) { - return []any{struct { - ChunkId assetstorage.ChunkId `ic:"chunk_id" json:"chunk_id"` - }{ - idl.NewNat(uint(0)), - }}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - var a0 = struct { - BatchId assetstorage.BatchId `ic:"batch_id" json:"batch_id"` - Content []byte `ic:"content" json:"content"` - }{ - idl.NewNat(uint(0)), - *new([]byte), - } - if _, err := a.CreateChunk(a0); err != nil { - t.Fatal(err) - } - -} - -// Test_Deauthorize tests the "deauthorize" method on the "assetstorage" canister. -func Test_Deauthorize(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "deauthorize", - Arguments: []any{new(principal.Principal)}, - Handler: func(request mock.Request) ([]any, error) { - return []any{}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - var a0 = *new(principal.Principal) - if err := a.Deauthorize(a0); err != nil { - t.Fatal(err) - } - -} - -// Test_DeleteAsset tests the "delete_asset" method on the "assetstorage" canister. -func Test_DeleteAsset(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "delete_asset", - Arguments: []any{new(assetstorage.DeleteAssetArguments)}, - Handler: func(request mock.Request) ([]any, error) { - return []any{}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - var a0 = assetstorage.DeleteAssetArguments{ - *new(string), - } - if err := a.DeleteAsset(a0); err != nil { - t.Fatal(err) - } - -} - -// Test_DeleteBatch tests the "delete_batch" method on the "assetstorage" canister. -func Test_DeleteBatch(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "delete_batch", - Arguments: []any{new(assetstorage.DeleteBatchArguments)}, - Handler: func(request mock.Request) ([]any, error) { - return []any{}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - var a0 = assetstorage.DeleteBatchArguments{ - idl.NewNat(uint(0)), - } - if err := a.DeleteBatch(a0); err != nil { - t.Fatal(err) - } - -} - -// Test_Get tests the "get" method on the "assetstorage" canister. -func Test_Get(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "get", - Arguments: []any{new(struct { - Key assetstorage.Key `ic:"key" json:"key"` - AcceptEncodings []string `ic:"accept_encodings" json:"accept_encodings"` - })}, - Handler: func(request mock.Request) ([]any, error) { - return []any{struct { - Content []byte `ic:"content" json:"content"` - ContentType string `ic:"content_type" json:"content_type"` - ContentEncoding string `ic:"content_encoding" json:"content_encoding"` - Sha256 *[]byte `ic:"sha256,omitempty" json:"sha256,omitempty"` - TotalLength idl.Nat `ic:"total_length" json:"total_length"` - }{ - *new([]byte), - *new(string), - *new(string), - *new(*[]byte), - idl.NewNat(uint(0)), - }}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - var a0 = struct { - Key assetstorage.Key `ic:"key" json:"key"` - AcceptEncodings []string `ic:"accept_encodings" json:"accept_encodings"` - }{ - *new(string), - []string{*new(string)}, - } - if _, err := a.Get(a0); err != nil { - t.Fatal(err) - } - -} - -// Test_GetAssetProperties tests the "get_asset_properties" method on the "assetstorage" canister. -func Test_GetAssetProperties(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "get_asset_properties", - Arguments: []any{new(assetstorage.Key)}, - Handler: func(request mock.Request) ([]any, error) { - return []any{struct { - MaxAge *uint64 `ic:"max_age,omitempty" json:"max_age,omitempty"` - Headers *[]assetstorage.HeaderField `ic:"headers,omitempty" json:"headers,omitempty"` - AllowRawAccess *bool `ic:"allow_raw_access,omitempty" json:"allow_raw_access,omitempty"` - IsAliased *bool `ic:"is_aliased,omitempty" json:"is_aliased,omitempty"` - }{ - *new(*uint64), - *new(*[]assetstorage.HeaderField), - *new(*bool), - *new(*bool), - }}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - var a0 = *new(string) - if _, err := a.GetAssetProperties(a0); err != nil { - t.Fatal(err) - } - -} - -// Test_GetChunk tests the "get_chunk" method on the "assetstorage" canister. -func Test_GetChunk(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "get_chunk", - Arguments: []any{new(struct { - Key assetstorage.Key `ic:"key" json:"key"` - ContentEncoding string `ic:"content_encoding" json:"content_encoding"` - Index idl.Nat `ic:"index" json:"index"` - Sha256 *[]byte `ic:"sha256,omitempty" json:"sha256,omitempty"` - })}, - Handler: func(request mock.Request) ([]any, error) { - return []any{struct { - Content []byte `ic:"content" json:"content"` - }{ - *new([]byte), - }}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - var a0 = struct { - Key assetstorage.Key `ic:"key" json:"key"` - ContentEncoding string `ic:"content_encoding" json:"content_encoding"` - Index idl.Nat `ic:"index" json:"index"` - Sha256 *[]byte `ic:"sha256,omitempty" json:"sha256,omitempty"` - }{ - *new(string), - *new(string), - idl.NewNat(uint(0)), - *new(*[]byte), - } - if _, err := a.GetChunk(a0); err != nil { - t.Fatal(err) - } - -} - -// Test_GetConfiguration tests the "get_configuration" method on the "assetstorage" canister. -func Test_GetConfiguration(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "get_configuration", - Arguments: []any{}, - Handler: func(request mock.Request) ([]any, error) { - return []any{assetstorage.ConfigurationResponse{ - *new(*uint64), - *new(*uint64), - *new(*uint64), - }}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - if _, err := a.GetConfiguration(); err != nil { - t.Fatal(err) - } - -} - -// Test_GrantPermission tests the "grant_permission" method on the "assetstorage" canister. -func Test_GrantPermission(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "grant_permission", - Arguments: []any{new(assetstorage.GrantPermission)}, - Handler: func(request mock.Request) ([]any, error) { - return []any{}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - var a0 = assetstorage.GrantPermission{ - *new(principal.Principal), - assetstorage.Permission{ - Commit: new(idl.Null), - }, - } - if err := a.GrantPermission(a0); err != nil { - t.Fatal(err) - } - -} - -// Test_HttpRequest tests the "http_request" method on the "assetstorage" canister. -func Test_HttpRequest(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "http_request", - Arguments: []any{new(assetstorage.HttpRequest)}, - Handler: func(request mock.Request) ([]any, error) { - return []any{assetstorage.HttpResponse{ - *new(uint16), - []assetstorage.HeaderField{{ - *new(string), - *new(string), - }}, - *new([]byte), - *new(*assetstorage.StreamingStrategy), - }}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - var a0 = assetstorage.HttpRequest{ - *new(string), - *new(string), - []assetstorage.HeaderField{{ - *new(string), - *new(string), - }}, - *new([]byte), - *new(*uint16), - } - if _, err := a.HttpRequest(a0); err != nil { - t.Fatal(err) - } - -} - -// Test_HttpRequestStreamingCallback tests the "http_request_streaming_callback" method on the "assetstorage" canister. -func Test_HttpRequestStreamingCallback(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "http_request_streaming_callback", - Arguments: []any{new(assetstorage.StreamingCallbackToken)}, - Handler: func(request mock.Request) ([]any, error) { - return []any{*new(*assetstorage.StreamingCallbackHttpResponse)}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - var a0 = assetstorage.StreamingCallbackToken{ - *new(string), - *new(string), - idl.NewNat(uint(0)), - *new(*[]byte), - } - if _, err := a.HttpRequestStreamingCallback(a0); err != nil { - t.Fatal(err) - } - -} - -// Test_List tests the "list" method on the "assetstorage" canister. -func Test_List(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "list", - Arguments: []any{new(struct { - })}, - Handler: func(request mock.Request) ([]any, error) { - return []any{[]struct { - Key assetstorage.Key `ic:"key" json:"key"` - ContentType string `ic:"content_type" json:"content_type"` - Encodings []struct { - ContentEncoding string `ic:"content_encoding" json:"content_encoding"` - Sha256 *[]byte `ic:"sha256,omitempty" json:"sha256,omitempty"` - Length idl.Nat `ic:"length" json:"length"` - Modified assetstorage.Time `ic:"modified" json:"modified"` - } `ic:"encodings" json:"encodings"` - }{ - - { - *new(string), - *new(string), - []struct { - ContentEncoding string `ic:"content_encoding" json:"content_encoding"` - Sha256 *[]byte `ic:"sha256,omitempty" json:"sha256,omitempty"` - Length idl.Nat `ic:"length" json:"length"` - Modified assetstorage.Time `ic:"modified" json:"modified"` - }{ - - { - *new(string), - *new(*[]byte), - idl.NewNat(uint(0)), - idl.NewInt(0), - }}, - }}}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - var a0 = struct { - }{} - if _, err := a.List(a0); err != nil { - t.Fatal(err) - } - -} - -// Test_ListAuthorized tests the "list_authorized" method on the "assetstorage" canister. -func Test_ListAuthorized(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "list_authorized", - Arguments: []any{}, - Handler: func(request mock.Request) ([]any, error) { - return []any{[]principal.Principal{*new(principal.Principal)}}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - if _, err := a.ListAuthorized(); err != nil { - t.Fatal(err) - } - -} - -// Test_ListPermitted tests the "list_permitted" method on the "assetstorage" canister. -func Test_ListPermitted(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "list_permitted", - Arguments: []any{new(assetstorage.ListPermitted)}, - Handler: func(request mock.Request) ([]any, error) { - return []any{[]principal.Principal{*new(principal.Principal)}}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - var a0 = assetstorage.ListPermitted{ - assetstorage.Permission{ - Commit: new(idl.Null), - }, - } - if _, err := a.ListPermitted(a0); err != nil { - t.Fatal(err) - } - -} - -// Test_ProposeCommitBatch tests the "propose_commit_batch" method on the "assetstorage" canister. -func Test_ProposeCommitBatch(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "propose_commit_batch", - Arguments: []any{new(assetstorage.CommitBatchArguments)}, - Handler: func(request mock.Request) ([]any, error) { - return []any{}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - var a0 = assetstorage.CommitBatchArguments{ - idl.NewNat(uint(0)), - []assetstorage.BatchOperationKind{{ - CreateAsset: idl.Ptr(assetstorage.CreateAssetArguments{ - *new(string), - *new(string), - *new(*uint64), - *new(*[]assetstorage.HeaderField), - *new(*bool), - *new(*bool), - }), - }}, - } - if err := a.ProposeCommitBatch(a0); err != nil { - t.Fatal(err) - } - -} - -// Test_RevokePermission tests the "revoke_permission" method on the "assetstorage" canister. -func Test_RevokePermission(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "revoke_permission", - Arguments: []any{new(assetstorage.RevokePermission)}, - Handler: func(request mock.Request) ([]any, error) { - return []any{}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - var a0 = assetstorage.RevokePermission{ - *new(principal.Principal), - assetstorage.Permission{ - Commit: new(idl.Null), - }, - } - if err := a.RevokePermission(a0); err != nil { - t.Fatal(err) - } - -} - -// Test_SetAssetContent tests the "set_asset_content" method on the "assetstorage" canister. -func Test_SetAssetContent(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "set_asset_content", - Arguments: []any{new(assetstorage.SetAssetContentArguments)}, - Handler: func(request mock.Request) ([]any, error) { - return []any{}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - var a0 = assetstorage.SetAssetContentArguments{ - *new(string), - *new(string), - []assetstorage.ChunkId{idl.NewNat(uint(0))}, - *new(*[]byte), - } - if err := a.SetAssetContent(a0); err != nil { - t.Fatal(err) - } - -} - -// Test_SetAssetProperties tests the "set_asset_properties" method on the "assetstorage" canister. -func Test_SetAssetProperties(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "set_asset_properties", - Arguments: []any{new(assetstorage.SetAssetPropertiesArguments)}, - Handler: func(request mock.Request) ([]any, error) { - return []any{}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - var a0 = assetstorage.SetAssetPropertiesArguments{ - *new(string), - *new(**uint64), - *new(**[]assetstorage.HeaderField), - *new(**bool), - *new(**bool), - } - if err := a.SetAssetProperties(a0); err != nil { - t.Fatal(err) - } - -} - -// Test_Store tests the "store" method on the "assetstorage" canister. -func Test_Store(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "store", - Arguments: []any{new(struct { - Key assetstorage.Key `ic:"key" json:"key"` - ContentType string `ic:"content_type" json:"content_type"` - ContentEncoding string `ic:"content_encoding" json:"content_encoding"` - Content []byte `ic:"content" json:"content"` - Sha256 *[]byte `ic:"sha256,omitempty" json:"sha256,omitempty"` - })}, - Handler: func(request mock.Request) ([]any, error) { - return []any{}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - var a0 = struct { - Key assetstorage.Key `ic:"key" json:"key"` - ContentType string `ic:"content_type" json:"content_type"` - ContentEncoding string `ic:"content_encoding" json:"content_encoding"` - Content []byte `ic:"content" json:"content"` - Sha256 *[]byte `ic:"sha256,omitempty" json:"sha256,omitempty"` - }{ - *new(string), - *new(string), - *new(string), - *new([]byte), - *new(*[]byte), - } - if err := a.Store(a0); err != nil { - t.Fatal(err) - } - -} - -// Test_TakeOwnership tests the "take_ownership" method on the "assetstorage" canister. -func Test_TakeOwnership(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "take_ownership", - Arguments: []any{}, - Handler: func(request mock.Request) ([]any, error) { - return []any{}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - if err := a.TakeOwnership(); err != nil { - t.Fatal(err) - } - -} - -// Test_UnsetAssetContent tests the "unset_asset_content" method on the "assetstorage" canister. -func Test_UnsetAssetContent(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "unset_asset_content", - Arguments: []any{new(assetstorage.UnsetAssetContentArguments)}, - Handler: func(request mock.Request) ([]any, error) { - return []any{}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - var a0 = assetstorage.UnsetAssetContentArguments{ - *new(string), - *new(string), - } - if err := a.UnsetAssetContent(a0); err != nil { - t.Fatal(err) - } - -} - -// Test_ValidateCommitProposedBatch tests the "validate_commit_proposed_batch" method on the "assetstorage" canister. -func Test_ValidateCommitProposedBatch(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "validate_commit_proposed_batch", - Arguments: []any{new(assetstorage.CommitProposedBatchArguments)}, - Handler: func(request mock.Request) ([]any, error) { - return []any{assetstorage.ValidationResult{ - Ok: new(string), - }}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - var a0 = assetstorage.CommitProposedBatchArguments{ - idl.NewNat(uint(0)), - *new([]byte), - } - if _, err := a.ValidateCommitProposedBatch(a0); err != nil { - t.Fatal(err) - } - -} - -// Test_ValidateConfigure tests the "validate_configure" method on the "assetstorage" canister. -func Test_ValidateConfigure(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "validate_configure", - Arguments: []any{new(assetstorage.ConfigureArguments)}, - Handler: func(request mock.Request) ([]any, error) { - return []any{assetstorage.ValidationResult{ - Ok: new(string), - }}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - var a0 = assetstorage.ConfigureArguments{ - *new(**uint64), - *new(**uint64), - *new(**uint64), - } - if _, err := a.ValidateConfigure(a0); err != nil { - t.Fatal(err) - } - -} - -// Test_ValidateGrantPermission tests the "validate_grant_permission" method on the "assetstorage" canister. -func Test_ValidateGrantPermission(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "validate_grant_permission", - Arguments: []any{new(assetstorage.GrantPermission)}, - Handler: func(request mock.Request) ([]any, error) { - return []any{assetstorage.ValidationResult{ - Ok: new(string), - }}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - var a0 = assetstorage.GrantPermission{ - *new(principal.Principal), - assetstorage.Permission{ - Commit: new(idl.Null), - }, - } - if _, err := a.ValidateGrantPermission(a0); err != nil { - t.Fatal(err) - } - -} - -// Test_ValidateRevokePermission tests the "validate_revoke_permission" method on the "assetstorage" canister. -func Test_ValidateRevokePermission(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "validate_revoke_permission", - Arguments: []any{new(assetstorage.RevokePermission)}, - Handler: func(request mock.Request) ([]any, error) { - return []any{assetstorage.ValidationResult{ - Ok: new(string), - }}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - var a0 = assetstorage.RevokePermission{ - *new(principal.Principal), - assetstorage.Permission{ - Commit: new(idl.Null), - }, - } - if _, err := a.ValidateRevokePermission(a0); err != nil { - t.Fatal(err) - } - -} - -// Test_ValidateTakeOwnership tests the "validate_take_ownership" method on the "assetstorage" canister. -func Test_ValidateTakeOwnership(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "validate_take_ownership", - Arguments: []any{}, - Handler: func(request mock.Request) ([]any, error) { - return []any{assetstorage.ValidationResult{ - Ok: new(string), - }}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - if _, err := a.ValidateTakeOwnership(); err != nil { - t.Fatal(err) - } - -} - -// newAgent creates a new agent with the given (mock) methods. -// Runs a mock replica in the background. -func newAgent(methods []mock.Method) (*assetstorage.Agent, error) { - replica := mock.NewReplica() - canisterId := principal.Principal{Raw: []byte("assetstorage")} - replica.AddCanister(canisterId, methods) - s := httptest.NewServer(replica) - u, _ := url.Parse(s.URL) - a, err := assetstorage.NewAgent(canisterId, agent.Config{ - ClientConfig: &agent.ClientConfig{Host: u}, - FetchRootKey: true, - }) - if err != nil { - return nil, err - } - return a, nil -} diff --git a/ic/cmc/agent_test.go b/ic/cmc/agent_test.go deleted file mode 100755 index 7c207d1..0000000 --- a/ic/cmc/agent_test.go +++ /dev/null @@ -1,260 +0,0 @@ -// Do NOT edit this file. It was automatically generated by https://github.com/aviate-labs/agent-go. -package cmc_test - -import ( - "github.com/aviate-labs/agent-go" - "github.com/aviate-labs/agent-go/candid/idl" - "github.com/aviate-labs/agent-go/mock" - "github.com/aviate-labs/agent-go/principal" - "net/http/httptest" - "net/url" - "testing" - - "github.com/aviate-labs/agent-go/ic/cmc" -) - -// Test_CreateCanister tests the "create_canister" method on the "cmc" canister. -func Test_CreateCanister(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "create_canister", - Arguments: []any{new(cmc.CreateCanisterArg)}, - Handler: func(request mock.Request) ([]any, error) { - return []any{cmc.CreateCanisterResult{ - Ok: new(principal.Principal), - }}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - var a0 = cmc.CreateCanisterArg{ - *new(*cmc.CanisterSettings), - *new(*string), - *new(*cmc.SubnetSelection), - } - if _, err := a.CreateCanister(a0); err != nil { - t.Fatal(err) - } - -} - -// Test_GetBuildMetadata tests the "get_build_metadata" method on the "cmc" canister. -func Test_GetBuildMetadata(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "get_build_metadata", - Arguments: []any{}, - Handler: func(request mock.Request) ([]any, error) { - return []any{*new(string)}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - if _, err := a.GetBuildMetadata(); err != nil { - t.Fatal(err) - } - -} - -// Test_GetIcpXdrConversionRate tests the "get_icp_xdr_conversion_rate" method on the "cmc" canister. -func Test_GetIcpXdrConversionRate(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "get_icp_xdr_conversion_rate", - Arguments: []any{}, - Handler: func(request mock.Request) ([]any, error) { - return []any{cmc.IcpXdrConversionRateResponse{ - cmc.IcpXdrConversionRate{ - *new(uint64), - *new(uint64), - }, - *new([]byte), - *new([]byte), - }}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - if _, err := a.GetIcpXdrConversionRate(); err != nil { - t.Fatal(err) - } - -} - -// Test_GetPrincipalsAuthorizedToCreateCanistersToSubnets tests the "get_principals_authorized_to_create_canisters_to_subnets" method on the "cmc" canister. -func Test_GetPrincipalsAuthorizedToCreateCanistersToSubnets(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "get_principals_authorized_to_create_canisters_to_subnets", - Arguments: []any{}, - Handler: func(request mock.Request) ([]any, error) { - return []any{cmc.PrincipalsAuthorizedToCreateCanistersToSubnetsResponse{ - []struct { - Field0 principal.Principal `ic:"0" json:"0"` - Field1 []principal.Principal `ic:"1" json:"1"` - }{ - - { - *new(principal.Principal), - []principal.Principal{*new(principal.Principal)}, - }}, - }}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - if _, err := a.GetPrincipalsAuthorizedToCreateCanistersToSubnets(); err != nil { - t.Fatal(err) - } - -} - -// Test_GetSubnetTypesToSubnets tests the "get_subnet_types_to_subnets" method on the "cmc" canister. -func Test_GetSubnetTypesToSubnets(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "get_subnet_types_to_subnets", - Arguments: []any{}, - Handler: func(request mock.Request) ([]any, error) { - return []any{cmc.SubnetTypesToSubnetsResponse{ - []struct { - Field0 string `ic:"0" json:"0"` - Field1 []principal.Principal `ic:"1" json:"1"` - }{ - - { - *new(string), - []principal.Principal{*new(principal.Principal)}, - }}, - }}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - if _, err := a.GetSubnetTypesToSubnets(); err != nil { - t.Fatal(err) - } - -} - -// Test_NotifyCreateCanister tests the "notify_create_canister" method on the "cmc" canister. -func Test_NotifyCreateCanister(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "notify_create_canister", - Arguments: []any{new(cmc.NotifyCreateCanisterArg)}, - Handler: func(request mock.Request) ([]any, error) { - return []any{cmc.NotifyCreateCanisterResult{ - Ok: new(principal.Principal), - }}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - var a0 = cmc.NotifyCreateCanisterArg{ - *new(uint64), - *new(principal.Principal), - *new(*string), - *new(*cmc.SubnetSelection), - *new(*cmc.CanisterSettings), - } - if _, err := a.NotifyCreateCanister(a0); err != nil { - t.Fatal(err) - } - -} - -// Test_NotifyMintCycles tests the "notify_mint_cycles" method on the "cmc" canister. -func Test_NotifyMintCycles(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "notify_mint_cycles", - Arguments: []any{new(cmc.NotifyMintCyclesArg)}, - Handler: func(request mock.Request) ([]any, error) { - return []any{cmc.NotifyMintCyclesResult{ - Ok: idl.Ptr(cmc.NotifyMintCyclesSuccess{ - idl.NewNat(uint(0)), - idl.NewNat(uint(0)), - idl.NewNat(uint(0)), - }), - }}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - var a0 = cmc.NotifyMintCyclesArg{ - *new(uint64), - *new(*[]byte), - *new(*[]byte), - } - if _, err := a.NotifyMintCycles(a0); err != nil { - t.Fatal(err) - } - -} - -// Test_NotifyTopUp tests the "notify_top_up" method on the "cmc" canister. -func Test_NotifyTopUp(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "notify_top_up", - Arguments: []any{new(cmc.NotifyTopUpArg)}, - Handler: func(request mock.Request) ([]any, error) { - return []any{cmc.NotifyTopUpResult{ - Ok: idl.Ptr(idl.NewNat(uint(0))), - }}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - var a0 = cmc.NotifyTopUpArg{ - *new(uint64), - *new(principal.Principal), - } - if _, err := a.NotifyTopUp(a0); err != nil { - t.Fatal(err) - } - -} - -// newAgent creates a new agent with the given (mock) methods. -// Runs a mock replica in the background. -func newAgent(methods []mock.Method) (*cmc.Agent, error) { - replica := mock.NewReplica() - canisterId := principal.Principal{Raw: []byte("cmc")} - replica.AddCanister(canisterId, methods) - s := httptest.NewServer(replica) - u, _ := url.Parse(s.URL) - a, err := cmc.NewAgent(canisterId, agent.Config{ - ClientConfig: &agent.ClientConfig{Host: u}, - FetchRootKey: true, - }) - if err != nil { - return nil, err - } - return a, nil -} diff --git a/ic/ic/agent_test.go b/ic/ic/agent_test.go deleted file mode 100755 index c7dbf66..0000000 --- a/ic/ic/agent_test.go +++ /dev/null @@ -1,860 +0,0 @@ -// Do NOT edit this file. It was automatically generated by https://github.com/aviate-labs/agent-go. -package ic_test - -import ( - "github.com/aviate-labs/agent-go" - "github.com/aviate-labs/agent-go/candid/idl" - "github.com/aviate-labs/agent-go/mock" - "github.com/aviate-labs/agent-go/principal" - "net/http/httptest" - "net/url" - "testing" - - "github.com/aviate-labs/agent-go/ic/ic" -) - -// Test_BitcoinGetBalance tests the "bitcoin_get_balance" method on the "ic" canister. -func Test_BitcoinGetBalance(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "bitcoin_get_balance", - Arguments: []any{new(ic.BitcoinGetBalanceArgs)}, - Handler: func(request mock.Request) ([]any, error) { - return []any{*new(ic.Satoshi)}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - var a0 = ic.BitcoinGetBalanceArgs{ - *new(string), - ic.BitcoinNetwork{ - Mainnet: new(idl.Null), - }, - *new(*uint32), - } - if _, err := a.BitcoinGetBalance(a0); err != nil { - t.Fatal(err) - } - -} - -// Test_BitcoinGetBalanceQuery tests the "bitcoin_get_balance_query" method on the "ic" canister. -func Test_BitcoinGetBalanceQuery(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "bitcoin_get_balance_query", - Arguments: []any{new(ic.BitcoinGetBalanceQueryArgs)}, - Handler: func(request mock.Request) ([]any, error) { - return []any{*new(ic.Satoshi)}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - var a0 = ic.BitcoinGetBalanceQueryArgs{ - *new(string), - ic.BitcoinNetwork{ - Mainnet: new(idl.Null), - }, - *new(*uint32), - } - if _, err := a.BitcoinGetBalanceQuery(a0); err != nil { - t.Fatal(err) - } - -} - -// Test_BitcoinGetCurrentFeePercentiles tests the "bitcoin_get_current_fee_percentiles" method on the "ic" canister. -func Test_BitcoinGetCurrentFeePercentiles(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "bitcoin_get_current_fee_percentiles", - Arguments: []any{new(ic.BitcoinGetCurrentFeePercentilesArgs)}, - Handler: func(request mock.Request) ([]any, error) { - return []any{*new([]ic.MillisatoshiPerByte)}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - var a0 = ic.BitcoinGetCurrentFeePercentilesArgs{ - ic.BitcoinNetwork{ - Mainnet: new(idl.Null), - }, - } - if _, err := a.BitcoinGetCurrentFeePercentiles(a0); err != nil { - t.Fatal(err) - } - -} - -// Test_BitcoinGetUtxos tests the "bitcoin_get_utxos" method on the "ic" canister. -func Test_BitcoinGetUtxos(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "bitcoin_get_utxos", - Arguments: []any{new(ic.BitcoinGetUtxosArgs)}, - Handler: func(request mock.Request) ([]any, error) { - return []any{ic.BitcoinGetUtxosResult{ - []ic.Utxo{{ - ic.Outpoint{ - *new([]byte), - *new(uint32), - }, - *new(uint64), - *new(uint32), - }}, - *new([]byte), - *new(uint32), - *new(*[]byte), - }}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - var a0 = ic.BitcoinGetUtxosArgs{ - *new(string), - ic.BitcoinNetwork{ - Mainnet: new(idl.Null), - }, - *new(*struct { - MinConfirmations *uint32 `ic:"min_confirmations,variant"` - Page *[]byte `ic:"page,variant"` - }), - } - if _, err := a.BitcoinGetUtxos(a0); err != nil { - t.Fatal(err) - } - -} - -// Test_BitcoinGetUtxosQuery tests the "bitcoin_get_utxos_query" method on the "ic" canister. -func Test_BitcoinGetUtxosQuery(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "bitcoin_get_utxos_query", - Arguments: []any{new(ic.BitcoinGetUtxosQueryArgs)}, - Handler: func(request mock.Request) ([]any, error) { - return []any{ic.BitcoinGetUtxosQueryResult{ - []ic.Utxo{{ - ic.Outpoint{ - *new([]byte), - *new(uint32), - }, - *new(uint64), - *new(uint32), - }}, - *new([]byte), - *new(uint32), - *new(*[]byte), - }}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - var a0 = ic.BitcoinGetUtxosQueryArgs{ - *new(string), - ic.BitcoinNetwork{ - Mainnet: new(idl.Null), - }, - *new(*struct { - MinConfirmations *uint32 `ic:"min_confirmations,variant"` - Page *[]byte `ic:"page,variant"` - }), - } - if _, err := a.BitcoinGetUtxosQuery(a0); err != nil { - t.Fatal(err) - } - -} - -// Test_BitcoinSendTransaction tests the "bitcoin_send_transaction" method on the "ic" canister. -func Test_BitcoinSendTransaction(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "bitcoin_send_transaction", - Arguments: []any{new(ic.BitcoinSendTransactionArgs)}, - Handler: func(request mock.Request) ([]any, error) { - return []any{}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - var a0 = ic.BitcoinSendTransactionArgs{ - *new([]byte), - ic.BitcoinNetwork{ - Mainnet: new(idl.Null), - }, - } - if err := a.BitcoinSendTransaction(a0); err != nil { - t.Fatal(err) - } - -} - -// Test_CanisterInfo tests the "canister_info" method on the "ic" canister. -func Test_CanisterInfo(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "canister_info", - Arguments: []any{new(ic.CanisterInfoArgs)}, - Handler: func(request mock.Request) ([]any, error) { - return []any{ic.CanisterInfoResult{ - *new(uint64), - []ic.Change{{ - *new(uint64), - *new(uint64), - ic.ChangeOrigin{ - FromUser: idl.Ptr(struct { - UserId principal.Principal `ic:"user_id" json:"user_id"` - }{ - *new(principal.Principal), - }), - }, - ic.ChangeDetails{ - Creation: idl.Ptr(struct { - Controllers []principal.Principal `ic:"controllers" json:"controllers"` - }{ - []principal.Principal{*new(principal.Principal)}, - }), - }, - }}, - *new(*[]byte), - []principal.Principal{*new(principal.Principal)}, - }}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - var a0 = ic.CanisterInfoArgs{ - *new(principal.Principal), - *new(*uint64), - } - if _, err := a.CanisterInfo(a0); err != nil { - t.Fatal(err) - } - -} - -// Test_CanisterStatus tests the "canister_status" method on the "ic" canister. -func Test_CanisterStatus(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "canister_status", - Arguments: []any{new(ic.CanisterStatusArgs)}, - Handler: func(request mock.Request) ([]any, error) { - return []any{ic.CanisterStatusResult{ - struct { - Running *idl.Null `ic:"running,variant"` - Stopping *idl.Null `ic:"stopping,variant"` - Stopped *idl.Null `ic:"stopped,variant"` - }{ - Running: new(idl.Null), - }, - ic.DefiniteCanisterSettings{ - []principal.Principal{*new(principal.Principal)}, - idl.NewNat(uint(0)), - idl.NewNat(uint(0)), - idl.NewNat(uint(0)), - idl.NewNat(uint(0)), - }, - *new(*[]byte), - idl.NewNat(uint(0)), - idl.NewNat(uint(0)), - idl.NewNat(uint(0)), - idl.NewNat(uint(0)), - }}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - var a0 = ic.CanisterStatusArgs{ - *new(principal.Principal), - } - if _, err := a.CanisterStatus(a0); err != nil { - t.Fatal(err) - } - -} - -// Test_ClearChunkStore tests the "clear_chunk_store" method on the "ic" canister. -func Test_ClearChunkStore(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "clear_chunk_store", - Arguments: []any{new(ic.ClearChunkStoreArgs)}, - Handler: func(request mock.Request) ([]any, error) { - return []any{}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - var a0 = ic.ClearChunkStoreArgs{ - *new(principal.Principal), - } - if err := a.ClearChunkStore(a0); err != nil { - t.Fatal(err) - } - -} - -// Test_CreateCanister tests the "create_canister" method on the "ic" canister. -func Test_CreateCanister(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "create_canister", - Arguments: []any{new(ic.CreateCanisterArgs)}, - Handler: func(request mock.Request) ([]any, error) { - return []any{ic.CreateCanisterResult{ - *new(principal.Principal), - }}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - var a0 = ic.CreateCanisterArgs{ - *new(*ic.CanisterSettings), - *new(*uint64), - } - if _, err := a.CreateCanister(a0); err != nil { - t.Fatal(err) - } - -} - -// Test_DeleteCanister tests the "delete_canister" method on the "ic" canister. -func Test_DeleteCanister(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "delete_canister", - Arguments: []any{new(ic.DeleteCanisterArgs)}, - Handler: func(request mock.Request) ([]any, error) { - return []any{}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - var a0 = ic.DeleteCanisterArgs{ - *new(principal.Principal), - } - if err := a.DeleteCanister(a0); err != nil { - t.Fatal(err) - } - -} - -// Test_DepositCycles tests the "deposit_cycles" method on the "ic" canister. -func Test_DepositCycles(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "deposit_cycles", - Arguments: []any{new(ic.DepositCyclesArgs)}, - Handler: func(request mock.Request) ([]any, error) { - return []any{}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - var a0 = ic.DepositCyclesArgs{ - *new(principal.Principal), - } - if err := a.DepositCycles(a0); err != nil { - t.Fatal(err) - } - -} - -// Test_EcdsaPublicKey tests the "ecdsa_public_key" method on the "ic" canister. -func Test_EcdsaPublicKey(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "ecdsa_public_key", - Arguments: []any{new(ic.EcdsaPublicKeyArgs)}, - Handler: func(request mock.Request) ([]any, error) { - return []any{ic.EcdsaPublicKeyResult{ - *new([]byte), - *new([]byte), - }}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - var a0 = ic.EcdsaPublicKeyArgs{ - *new(*ic.CanisterId), - [][]byte{*new([]byte)}, - struct { - Curve ic.EcdsaCurve `ic:"curve" json:"curve"` - Name string `ic:"name" json:"name"` - }{ - ic.EcdsaCurve{ - Secp256k1: new(idl.Null), - }, - *new(string), - }, - } - if _, err := a.EcdsaPublicKey(a0); err != nil { - t.Fatal(err) - } - -} - -// Test_HttpRequest tests the "http_request" method on the "ic" canister. -func Test_HttpRequest(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "http_request", - Arguments: []any{new(ic.HttpRequestArgs)}, - Handler: func(request mock.Request) ([]any, error) { - return []any{ic.HttpRequestResult{ - idl.NewNat(uint(0)), - []ic.HttpHeader{{ - *new(string), - *new(string), - }}, - *new([]byte), - }}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - var a0 = ic.HttpRequestArgs{ - *new(string), - *new(*uint64), - struct { - Get *idl.Null `ic:"get,variant"` - Head *idl.Null `ic:"head,variant"` - Post *idl.Null `ic:"post,variant"` - }{ - Get: new(idl.Null), - }, - []ic.HttpHeader{{ - *new(string), - *new(string), - }}, - *new(*[]byte), - *new(*struct { - Function struct { /* NOT SUPPORTED */ - } `ic:"function" json:"function"` - Context []byte `ic:"context" json:"context"` - }), - } - if _, err := a.HttpRequest(a0); err != nil { - t.Fatal(err) - } - -} - -// Test_InstallChunkedCode tests the "install_chunked_code" method on the "ic" canister. -func Test_InstallChunkedCode(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "install_chunked_code", - Arguments: []any{new(ic.InstallChunkedCodeArgs)}, - Handler: func(request mock.Request) ([]any, error) { - return []any{}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - var a0 = ic.InstallChunkedCodeArgs{ - ic.CanisterInstallMode{ - Install: new(idl.Null), - }, - *new(principal.Principal), - *new(*ic.CanisterId), - []ic.ChunkHash{{ - *new([]byte), - }}, - *new([]byte), - *new([]byte), - *new(*uint64), - } - if err := a.InstallChunkedCode(a0); err != nil { - t.Fatal(err) - } - -} - -// Test_InstallCode tests the "install_code" method on the "ic" canister. -func Test_InstallCode(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "install_code", - Arguments: []any{new(ic.InstallCodeArgs)}, - Handler: func(request mock.Request) ([]any, error) { - return []any{}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - var a0 = ic.InstallCodeArgs{ - ic.CanisterInstallMode{ - Install: new(idl.Null), - }, - *new(principal.Principal), - *new([]byte), - *new([]byte), - *new(*uint64), - } - if err := a.InstallCode(a0); err != nil { - t.Fatal(err) - } - -} - -// Test_NodeMetricsHistory tests the "node_metrics_history" method on the "ic" canister. -func Test_NodeMetricsHistory(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "node_metrics_history", - Arguments: []any{new(ic.NodeMetricsHistoryArgs)}, - Handler: func(request mock.Request) ([]any, error) { - return []any{*new([]struct { - TimestampNanos uint64 `ic:"timestamp_nanos" json:"timestamp_nanos"` - NodeMetrics []ic.NodeMetrics `ic:"node_metrics" json:"node_metrics"` - })}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - var a0 = ic.NodeMetricsHistoryArgs{ - *new(principal.Principal), - *new(uint64), - } - if _, err := a.NodeMetricsHistory(a0); err != nil { - t.Fatal(err) - } - -} - -// Test_ProvisionalCreateCanisterWithCycles tests the "provisional_create_canister_with_cycles" method on the "ic" canister. -func Test_ProvisionalCreateCanisterWithCycles(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "provisional_create_canister_with_cycles", - Arguments: []any{new(ic.ProvisionalCreateCanisterWithCyclesArgs)}, - Handler: func(request mock.Request) ([]any, error) { - return []any{ic.ProvisionalCreateCanisterWithCyclesResult{ - *new(principal.Principal), - }}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - var a0 = ic.ProvisionalCreateCanisterWithCyclesArgs{ - *new(*idl.Nat), - *new(*ic.CanisterSettings), - *new(*ic.CanisterId), - *new(*uint64), - } - if _, err := a.ProvisionalCreateCanisterWithCycles(a0); err != nil { - t.Fatal(err) - } - -} - -// Test_ProvisionalTopUpCanister tests the "provisional_top_up_canister" method on the "ic" canister. -func Test_ProvisionalTopUpCanister(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "provisional_top_up_canister", - Arguments: []any{new(ic.ProvisionalTopUpCanisterArgs)}, - Handler: func(request mock.Request) ([]any, error) { - return []any{}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - var a0 = ic.ProvisionalTopUpCanisterArgs{ - *new(principal.Principal), - idl.NewNat(uint(0)), - } - if err := a.ProvisionalTopUpCanister(a0); err != nil { - t.Fatal(err) - } - -} - -// Test_RawRand tests the "raw_rand" method on the "ic" canister. -func Test_RawRand(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "raw_rand", - Arguments: []any{}, - Handler: func(request mock.Request) ([]any, error) { - return []any{*new([]byte)}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - if _, err := a.RawRand(); err != nil { - t.Fatal(err) - } - -} - -// Test_SignWithEcdsa tests the "sign_with_ecdsa" method on the "ic" canister. -func Test_SignWithEcdsa(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "sign_with_ecdsa", - Arguments: []any{new(ic.SignWithEcdsaArgs)}, - Handler: func(request mock.Request) ([]any, error) { - return []any{ic.SignWithEcdsaResult{ - *new([]byte), - }}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - var a0 = ic.SignWithEcdsaArgs{ - *new([]byte), - [][]byte{*new([]byte)}, - struct { - Curve ic.EcdsaCurve `ic:"curve" json:"curve"` - Name string `ic:"name" json:"name"` - }{ - ic.EcdsaCurve{ - Secp256k1: new(idl.Null), - }, - *new(string), - }, - } - if _, err := a.SignWithEcdsa(a0); err != nil { - t.Fatal(err) - } - -} - -// Test_StartCanister tests the "start_canister" method on the "ic" canister. -func Test_StartCanister(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "start_canister", - Arguments: []any{new(ic.StartCanisterArgs)}, - Handler: func(request mock.Request) ([]any, error) { - return []any{}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - var a0 = ic.StartCanisterArgs{ - *new(principal.Principal), - } - if err := a.StartCanister(a0); err != nil { - t.Fatal(err) - } - -} - -// Test_StopCanister tests the "stop_canister" method on the "ic" canister. -func Test_StopCanister(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "stop_canister", - Arguments: []any{new(ic.StopCanisterArgs)}, - Handler: func(request mock.Request) ([]any, error) { - return []any{}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - var a0 = ic.StopCanisterArgs{ - *new(principal.Principal), - } - if err := a.StopCanister(a0); err != nil { - t.Fatal(err) - } - -} - -// Test_StoredChunks tests the "stored_chunks" method on the "ic" canister. -func Test_StoredChunks(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "stored_chunks", - Arguments: []any{new(ic.StoredChunksArgs)}, - Handler: func(request mock.Request) ([]any, error) { - return []any{*new([]ic.ChunkHash)}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - var a0 = ic.StoredChunksArgs{ - *new(principal.Principal), - } - if _, err := a.StoredChunks(a0); err != nil { - t.Fatal(err) - } - -} - -// Test_UninstallCode tests the "uninstall_code" method on the "ic" canister. -func Test_UninstallCode(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "uninstall_code", - Arguments: []any{new(ic.UninstallCodeArgs)}, - Handler: func(request mock.Request) ([]any, error) { - return []any{}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - var a0 = ic.UninstallCodeArgs{ - *new(principal.Principal), - *new(*uint64), - } - if err := a.UninstallCode(a0); err != nil { - t.Fatal(err) - } - -} - -// Test_UpdateSettings tests the "update_settings" method on the "ic" canister. -func Test_UpdateSettings(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "update_settings", - Arguments: []any{new(ic.UpdateSettingsArgs)}, - Handler: func(request mock.Request) ([]any, error) { - return []any{}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - var a0 = ic.UpdateSettingsArgs{ - *new(principal.Principal), - ic.CanisterSettings{ - *new(*[]principal.Principal), - *new(*idl.Nat), - *new(*idl.Nat), - *new(*idl.Nat), - *new(*idl.Nat), - }, - *new(*uint64), - } - if err := a.UpdateSettings(a0); err != nil { - t.Fatal(err) - } - -} - -// Test_UploadChunk tests the "upload_chunk" method on the "ic" canister. -func Test_UploadChunk(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "upload_chunk", - Arguments: []any{new(ic.UploadChunkArgs)}, - Handler: func(request mock.Request) ([]any, error) { - return []any{*new(ic.ChunkHash)}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - var a0 = ic.UploadChunkArgs{ - *new(principal.Principal), - *new([]byte), - } - if _, err := a.UploadChunk(a0); err != nil { - t.Fatal(err) - } - -} - -// newAgent creates a new agent with the given (mock) methods. -// Runs a mock replica in the background. -func newAgent(methods []mock.Method) (*ic.Agent, error) { - replica := mock.NewReplica() - canisterId := principal.Principal{Raw: []byte("ic")} - replica.AddCanister(canisterId, methods) - s := httptest.NewServer(replica) - u, _ := url.Parse(s.URL) - a, err := ic.NewAgent(canisterId, agent.Config{ - ClientConfig: &agent.ClientConfig{Host: u}, - FetchRootKey: true, - }) - if err != nil { - return nil, err - } - return a, nil -} diff --git a/ic/icparchive/agent_test.go b/ic/icparchive/agent_test.go deleted file mode 100644 index d53011a..0000000 --- a/ic/icparchive/agent_test.go +++ /dev/null @@ -1,101 +0,0 @@ -// Do NOT edit this file. It was automatically generated by https://github.com/aviate-labs/agent-go. -package icparchive_test - -import ( - "github.com/aviate-labs/agent-go" - "github.com/aviate-labs/agent-go/candid/idl" - "github.com/aviate-labs/agent-go/mock" - "github.com/aviate-labs/agent-go/principal" - "net/http/httptest" - "net/url" - "testing" - - "github.com/aviate-labs/agent-go/ic/icparchive" -) - -// Test_GetBlocks tests the "get_blocks" method on the "icparchive" canister. -func Test_GetBlocks(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "get_blocks", - Arguments: []any{new(icparchive.GetBlocksArgs)}, - Handler: func(request mock.Request) ([]any, error) { - return []any{icparchive.GetBlocksResult{ - Ok: idl.Ptr(icparchive.BlockRange{ - []icparchive.Block{{ - *new(*[]byte), - icparchive.Transaction{ - *new(uint64), - *new(*[]byte), - *new(*icparchive.Operation), - icparchive.Timestamp{ - *new(uint64), - }, - }, - icparchive.Timestamp{ - *new(uint64), - }, - }}, - }), - }}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - var a0 = icparchive.GetBlocksArgs{ - *new(uint64), - *new(uint64), - } - if _, err := a.GetBlocks(a0); err != nil { - t.Fatal(err) - } - -} - -// Test_GetEncodedBlocks tests the "get_encoded_blocks" method on the "icparchive" canister. -func Test_GetEncodedBlocks(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "get_encoded_blocks", - Arguments: []any{new(icparchive.GetBlocksArgs)}, - Handler: func(request mock.Request) ([]any, error) { - return []any{icparchive.GetEncodedBlocksResult{ - Ok: idl.Ptr([][]byte{*new([]byte)}), - }}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - var a0 = icparchive.GetBlocksArgs{ - *new(uint64), - *new(uint64), - } - if _, err := a.GetEncodedBlocks(a0); err != nil { - t.Fatal(err) - } - -} - -// newAgent creates a new agent with the given (mock) methods. -// Runs a mock replica in the background. -func newAgent(methods []mock.Method) (*icparchive.Agent, error) { - replica := mock.NewReplica() - canisterId := principal.Principal{Raw: []byte("icparchive")} - replica.AddCanister(canisterId, methods) - s := httptest.NewServer(replica) - u, _ := url.Parse(s.URL) - a, err := icparchive.NewAgent(canisterId, agent.Config{ - ClientConfig: &agent.ClientConfig{Host: u}, - FetchRootKey: true, - }) - if err != nil { - return nil, err - } - return a, nil -} diff --git a/ic/icpledger/agent_test.go b/ic/icpledger/agent_test.go deleted file mode 100644 index b5e16d8..0000000 --- a/ic/icpledger/agent_test.go +++ /dev/null @@ -1,741 +0,0 @@ -// Do NOT edit this file. It was automatically generated by https://github.com/aviate-labs/agent-go. -package icpledger_test - -import ( - "github.com/aviate-labs/agent-go" - "github.com/aviate-labs/agent-go/candid/idl" - "github.com/aviate-labs/agent-go/mock" - "github.com/aviate-labs/agent-go/principal" - "net/http/httptest" - "net/url" - "testing" - - "github.com/aviate-labs/agent-go/ic/icpledger" -) - -// Test_AccountBalance tests the "account_balance" method on the "icpledger" canister. -func Test_AccountBalance(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "account_balance", - Arguments: []any{new(icpledger.AccountBalanceArgs)}, - Handler: func(request mock.Request) ([]any, error) { - return []any{icpledger.Tokens{ - *new(uint64), - }}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - var a0 = icpledger.AccountBalanceArgs{ - *new([]byte), - } - if _, err := a.AccountBalance(a0); err != nil { - t.Fatal(err) - } - -} - -// Test_AccountBalanceDfx tests the "account_balance_dfx" method on the "icpledger" canister. -func Test_AccountBalanceDfx(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "account_balance_dfx", - Arguments: []any{new(icpledger.AccountBalanceArgsDfx)}, - Handler: func(request mock.Request) ([]any, error) { - return []any{icpledger.Tokens{ - *new(uint64), - }}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - var a0 = icpledger.AccountBalanceArgsDfx{ - *new(string), - } - if _, err := a.AccountBalanceDfx(a0); err != nil { - t.Fatal(err) - } - -} - -// Test_AccountIdentifier tests the "account_identifier" method on the "icpledger" canister. -func Test_AccountIdentifier(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "account_identifier", - Arguments: []any{new(icpledger.Account)}, - Handler: func(request mock.Request) ([]any, error) { - return []any{*new([]byte)}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - var a0 = icpledger.Account{ - *new(principal.Principal), - *new(*icpledger.SubAccount), - } - if _, err := a.AccountIdentifier(a0); err != nil { - t.Fatal(err) - } - -} - -// Test_Archives tests the "archives" method on the "icpledger" canister. -func Test_Archives(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "archives", - Arguments: []any{}, - Handler: func(request mock.Request) ([]any, error) { - return []any{icpledger.Archives{ - []icpledger.Archive{{ - *new(principal.Principal), - }}, - }}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - if _, err := a.Archives(); err != nil { - t.Fatal(err) - } - -} - -// Test_Decimals tests the "decimals" method on the "icpledger" canister. -func Test_Decimals(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "decimals", - Arguments: []any{}, - Handler: func(request mock.Request) ([]any, error) { - return []any{struct { - Decimals uint32 `ic:"decimals" json:"decimals"` - }{ - *new(uint32), - }}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - if _, err := a.Decimals(); err != nil { - t.Fatal(err) - } - -} - -// Test_Icrc1BalanceOf tests the "icrc1_balance_of" method on the "icpledger" canister. -func Test_Icrc1BalanceOf(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "icrc1_balance_of", - Arguments: []any{new(icpledger.Account)}, - Handler: func(request mock.Request) ([]any, error) { - return []any{idl.NewNat(uint(0))}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - var a0 = icpledger.Account{ - *new(principal.Principal), - *new(*icpledger.SubAccount), - } - if _, err := a.Icrc1BalanceOf(a0); err != nil { - t.Fatal(err) - } - -} - -// Test_Icrc1Decimals tests the "icrc1_decimals" method on the "icpledger" canister. -func Test_Icrc1Decimals(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "icrc1_decimals", - Arguments: []any{}, - Handler: func(request mock.Request) ([]any, error) { - return []any{*new(uint8)}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - if _, err := a.Icrc1Decimals(); err != nil { - t.Fatal(err) - } - -} - -// Test_Icrc1Fee tests the "icrc1_fee" method on the "icpledger" canister. -func Test_Icrc1Fee(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "icrc1_fee", - Arguments: []any{}, - Handler: func(request mock.Request) ([]any, error) { - return []any{idl.NewNat(uint(0))}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - if _, err := a.Icrc1Fee(); err != nil { - t.Fatal(err) - } - -} - -// Test_Icrc1Metadata tests the "icrc1_metadata" method on the "icpledger" canister. -func Test_Icrc1Metadata(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "icrc1_metadata", - Arguments: []any{}, - Handler: func(request mock.Request) ([]any, error) { - return []any{[]struct { - Field0 string `ic:"0" json:"0"` - Field1 icpledger.Value `ic:"1" json:"1"` - }{ - - { - *new(string), - icpledger.Value{ - Nat: idl.Ptr(idl.NewNat(uint(0))), - }, - }}}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - if _, err := a.Icrc1Metadata(); err != nil { - t.Fatal(err) - } - -} - -// Test_Icrc1MintingAccount tests the "icrc1_minting_account" method on the "icpledger" canister. -func Test_Icrc1MintingAccount(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "icrc1_minting_account", - Arguments: []any{}, - Handler: func(request mock.Request) ([]any, error) { - return []any{*new(*icpledger.Account)}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - if _, err := a.Icrc1MintingAccount(); err != nil { - t.Fatal(err) - } - -} - -// Test_Icrc1Name tests the "icrc1_name" method on the "icpledger" canister. -func Test_Icrc1Name(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "icrc1_name", - Arguments: []any{}, - Handler: func(request mock.Request) ([]any, error) { - return []any{*new(string)}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - if _, err := a.Icrc1Name(); err != nil { - t.Fatal(err) - } - -} - -// Test_Icrc1SupportedStandards tests the "icrc1_supported_standards" method on the "icpledger" canister. -func Test_Icrc1SupportedStandards(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "icrc1_supported_standards", - Arguments: []any{}, - Handler: func(request mock.Request) ([]any, error) { - return []any{[]struct { - Name string `ic:"name" json:"name"` - Url string `ic:"url" json:"url"` - }{ - - { - *new(string), - *new(string), - }}}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - if _, err := a.Icrc1SupportedStandards(); err != nil { - t.Fatal(err) - } - -} - -// Test_Icrc1Symbol tests the "icrc1_symbol" method on the "icpledger" canister. -func Test_Icrc1Symbol(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "icrc1_symbol", - Arguments: []any{}, - Handler: func(request mock.Request) ([]any, error) { - return []any{*new(string)}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - if _, err := a.Icrc1Symbol(); err != nil { - t.Fatal(err) - } - -} - -// Test_Icrc1TotalSupply tests the "icrc1_total_supply" method on the "icpledger" canister. -func Test_Icrc1TotalSupply(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "icrc1_total_supply", - Arguments: []any{}, - Handler: func(request mock.Request) ([]any, error) { - return []any{idl.NewNat(uint(0))}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - if _, err := a.Icrc1TotalSupply(); err != nil { - t.Fatal(err) - } - -} - -// Test_Icrc1Transfer tests the "icrc1_transfer" method on the "icpledger" canister. -func Test_Icrc1Transfer(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "icrc1_transfer", - Arguments: []any{new(icpledger.TransferArg)}, - Handler: func(request mock.Request) ([]any, error) { - return []any{icpledger.Icrc1TransferResult{ - Ok: idl.Ptr(idl.NewNat(uint(0))), - }}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - var a0 = icpledger.TransferArg{ - *new(*icpledger.SubAccount), - icpledger.Account{ - *new(principal.Principal), - *new(*icpledger.SubAccount), - }, - idl.NewNat(uint(0)), - *new(*icpledger.Icrc1Tokens), - *new(*[]byte), - *new(*icpledger.Icrc1Timestamp), - } - if _, err := a.Icrc1Transfer(a0); err != nil { - t.Fatal(err) - } - -} - -// Test_Icrc2Allowance tests the "icrc2_allowance" method on the "icpledger" canister. -func Test_Icrc2Allowance(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "icrc2_allowance", - Arguments: []any{new(icpledger.AllowanceArgs)}, - Handler: func(request mock.Request) ([]any, error) { - return []any{icpledger.Allowance{ - idl.NewNat(uint(0)), - *new(*icpledger.Icrc1Timestamp), - }}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - var a0 = icpledger.AllowanceArgs{ - icpledger.Account{ - *new(principal.Principal), - *new(*icpledger.SubAccount), - }, - icpledger.Account{ - *new(principal.Principal), - *new(*icpledger.SubAccount), - }, - } - if _, err := a.Icrc2Allowance(a0); err != nil { - t.Fatal(err) - } - -} - -// Test_Icrc2Approve tests the "icrc2_approve" method on the "icpledger" canister. -func Test_Icrc2Approve(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "icrc2_approve", - Arguments: []any{new(icpledger.ApproveArgs)}, - Handler: func(request mock.Request) ([]any, error) { - return []any{icpledger.ApproveResult{ - Ok: idl.Ptr(idl.NewNat(uint(0))), - }}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - var a0 = icpledger.ApproveArgs{ - *new(*icpledger.SubAccount), - icpledger.Account{ - *new(principal.Principal), - *new(*icpledger.SubAccount), - }, - idl.NewNat(uint(0)), - *new(*icpledger.Icrc1Tokens), - *new(*icpledger.Icrc1Timestamp), - *new(*icpledger.Icrc1Tokens), - *new(*[]byte), - *new(*icpledger.Icrc1Timestamp), - } - if _, err := a.Icrc2Approve(a0); err != nil { - t.Fatal(err) - } - -} - -// Test_Icrc2TransferFrom tests the "icrc2_transfer_from" method on the "icpledger" canister. -func Test_Icrc2TransferFrom(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "icrc2_transfer_from", - Arguments: []any{new(icpledger.TransferFromArgs)}, - Handler: func(request mock.Request) ([]any, error) { - return []any{icpledger.TransferFromResult{ - Ok: idl.Ptr(idl.NewNat(uint(0))), - }}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - var a0 = icpledger.TransferFromArgs{ - *new(*icpledger.SubAccount), - icpledger.Account{ - *new(principal.Principal), - *new(*icpledger.SubAccount), - }, - icpledger.Account{ - *new(principal.Principal), - *new(*icpledger.SubAccount), - }, - idl.NewNat(uint(0)), - *new(*icpledger.Icrc1Tokens), - *new(*[]byte), - *new(*icpledger.Icrc1Timestamp), - } - if _, err := a.Icrc2TransferFrom(a0); err != nil { - t.Fatal(err) - } - -} - -// Test_Name tests the "name" method on the "icpledger" canister. -func Test_Name(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "name", - Arguments: []any{}, - Handler: func(request mock.Request) ([]any, error) { - return []any{struct { - Name string `ic:"name" json:"name"` - }{ - *new(string), - }}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - if _, err := a.Name(); err != nil { - t.Fatal(err) - } - -} - -// Test_QueryBlocks tests the "query_blocks" method on the "icpledger" canister. -func Test_QueryBlocks(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "query_blocks", - Arguments: []any{new(icpledger.GetBlocksArgs)}, - Handler: func(request mock.Request) ([]any, error) { - return []any{icpledger.QueryBlocksResponse{ - *new(uint64), - *new(*[]byte), - []icpledger.Block{{ - *new(*[]byte), - icpledger.Transaction{ - *new(uint64), - *new(*[]byte), - *new(*icpledger.Operation), - icpledger.TimeStamp{ - *new(uint64), - }, - }, - icpledger.TimeStamp{ - *new(uint64), - }, - }}, - *new(uint64), - []icpledger.ArchivedBlocksRange{{ - *new(uint64), - *new(uint64), - *new(struct { /* NOT SUPPORTED */ - }), - }}, - }}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - var a0 = icpledger.GetBlocksArgs{ - *new(uint64), - *new(uint64), - } - if _, err := a.QueryBlocks(a0); err != nil { - t.Fatal(err) - } - -} - -// Test_QueryEncodedBlocks tests the "query_encoded_blocks" method on the "icpledger" canister. -func Test_QueryEncodedBlocks(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "query_encoded_blocks", - Arguments: []any{new(icpledger.GetBlocksArgs)}, - Handler: func(request mock.Request) ([]any, error) { - return []any{icpledger.QueryEncodedBlocksResponse{ - *new(*[]byte), - [][]byte{*new([]byte)}, - *new(uint64), - *new(uint64), - []icpledger.ArchivedEncodedBlocksRange{{ - *new(struct { /* NOT SUPPORTED */ - }), - *new(uint64), - *new(uint64), - }}, - }}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - var a0 = icpledger.GetBlocksArgs{ - *new(uint64), - *new(uint64), - } - if _, err := a.QueryEncodedBlocks(a0); err != nil { - t.Fatal(err) - } - -} - -// Test_SendDfx tests the "send_dfx" method on the "icpledger" canister. -func Test_SendDfx(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "send_dfx", - Arguments: []any{new(icpledger.SendArgs)}, - Handler: func(request mock.Request) ([]any, error) { - return []any{*new(uint64)}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - var a0 = icpledger.SendArgs{ - *new(uint64), - icpledger.Tokens{ - *new(uint64), - }, - icpledger.Tokens{ - *new(uint64), - }, - *new(*icpledger.SubAccount), - *new(string), - *new(*icpledger.TimeStamp), - } - if _, err := a.SendDfx(a0); err != nil { - t.Fatal(err) - } - -} - -// Test_Symbol tests the "symbol" method on the "icpledger" canister. -func Test_Symbol(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "symbol", - Arguments: []any{}, - Handler: func(request mock.Request) ([]any, error) { - return []any{struct { - Symbol string `ic:"symbol" json:"symbol"` - }{ - *new(string), - }}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - if _, err := a.Symbol(); err != nil { - t.Fatal(err) - } - -} - -// Test_Transfer tests the "transfer" method on the "icpledger" canister. -func Test_Transfer(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "transfer", - Arguments: []any{new(icpledger.TransferArgs)}, - Handler: func(request mock.Request) ([]any, error) { - return []any{icpledger.TransferResult{ - Ok: new(uint64), - }}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - var a0 = icpledger.TransferArgs{ - *new(uint64), - icpledger.Tokens{ - *new(uint64), - }, - icpledger.Tokens{ - *new(uint64), - }, - *new(*icpledger.SubAccount), - *new([]byte), - *new(*icpledger.TimeStamp), - } - if _, err := a.Transfer(a0); err != nil { - t.Fatal(err) - } - -} - -// Test_TransferFee tests the "transfer_fee" method on the "icpledger" canister. -func Test_TransferFee(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "transfer_fee", - Arguments: []any{new(icpledger.TransferFeeArg)}, - Handler: func(request mock.Request) ([]any, error) { - return []any{icpledger.TransferFee{ - icpledger.Tokens{ - *new(uint64), - }, - }}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - var a0 = icpledger.TransferFeeArg{} - if _, err := a.TransferFee(a0); err != nil { - t.Fatal(err) - } - -} - -// newAgent creates a new agent with the given (mock) methods. -// Runs a mock replica in the background. -func newAgent(methods []mock.Method) (*icpledger.Agent, error) { - replica := mock.NewReplica() - canisterId := principal.Principal{Raw: []byte("icpledger")} - replica.AddCanister(canisterId, methods) - s := httptest.NewServer(replica) - u, _ := url.Parse(s.URL) - a, err := icpledger.NewAgent(canisterId, agent.Config{ - ClientConfig: &agent.ClientConfig{Host: u}, - FetchRootKey: true, - }) - if err != nil { - return nil, err - } - return a, nil -} diff --git a/ic/icrc1/agent_test.go b/ic/icrc1/agent_test.go deleted file mode 100755 index 04026ca..0000000 --- a/ic/icrc1/agent_test.go +++ /dev/null @@ -1,280 +0,0 @@ -// Do NOT edit this file. It was automatically generated by https://github.com/aviate-labs/agent-go. -package icrc1_test - -import ( - "github.com/aviate-labs/agent-go" - "github.com/aviate-labs/agent-go/candid/idl" - "github.com/aviate-labs/agent-go/mock" - "github.com/aviate-labs/agent-go/principal" - "net/http/httptest" - "net/url" - "testing" - - "github.com/aviate-labs/agent-go/ic/icrc1" -) - -// Test_Icrc1BalanceOf tests the "icrc1_balance_of" method on the "icrc1" canister. -func Test_Icrc1BalanceOf(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "icrc1_balance_of", - Arguments: []any{new(icrc1.Account)}, - Handler: func(request mock.Request) ([]any, error) { - return []any{idl.NewNat(uint(0))}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - var a0 = icrc1.Account{ - *new(principal.Principal), - *new(*icrc1.Subaccount), - } - if _, err := a.Icrc1BalanceOf(a0); err != nil { - t.Fatal(err) - } - -} - -// Test_Icrc1Decimals tests the "icrc1_decimals" method on the "icrc1" canister. -func Test_Icrc1Decimals(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "icrc1_decimals", - Arguments: []any{}, - Handler: func(request mock.Request) ([]any, error) { - return []any{*new(uint8)}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - if _, err := a.Icrc1Decimals(); err != nil { - t.Fatal(err) - } - -} - -// Test_Icrc1Fee tests the "icrc1_fee" method on the "icrc1" canister. -func Test_Icrc1Fee(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "icrc1_fee", - Arguments: []any{}, - Handler: func(request mock.Request) ([]any, error) { - return []any{idl.NewNat(uint(0))}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - if _, err := a.Icrc1Fee(); err != nil { - t.Fatal(err) - } - -} - -// Test_Icrc1Metadata tests the "icrc1_metadata" method on the "icrc1" canister. -func Test_Icrc1Metadata(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "icrc1_metadata", - Arguments: []any{}, - Handler: func(request mock.Request) ([]any, error) { - return []any{[]struct { - Field0 string `ic:"0" json:"0"` - Field1 icrc1.Value `ic:"1" json:"1"` - }{ - - { - *new(string), - icrc1.Value{ - Nat: idl.Ptr(idl.NewNat(uint(0))), - }, - }}}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - if _, err := a.Icrc1Metadata(); err != nil { - t.Fatal(err) - } - -} - -// Test_Icrc1MintingAccount tests the "icrc1_minting_account" method on the "icrc1" canister. -func Test_Icrc1MintingAccount(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "icrc1_minting_account", - Arguments: []any{}, - Handler: func(request mock.Request) ([]any, error) { - return []any{*new(*icrc1.Account)}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - if _, err := a.Icrc1MintingAccount(); err != nil { - t.Fatal(err) - } - -} - -// Test_Icrc1Name tests the "icrc1_name" method on the "icrc1" canister. -func Test_Icrc1Name(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "icrc1_name", - Arguments: []any{}, - Handler: func(request mock.Request) ([]any, error) { - return []any{*new(string)}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - if _, err := a.Icrc1Name(); err != nil { - t.Fatal(err) - } - -} - -// Test_Icrc1SupportedStandards tests the "icrc1_supported_standards" method on the "icrc1" canister. -func Test_Icrc1SupportedStandards(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "icrc1_supported_standards", - Arguments: []any{}, - Handler: func(request mock.Request) ([]any, error) { - return []any{[]struct { - Name string `ic:"name" json:"name"` - Url string `ic:"url" json:"url"` - }{ - - { - *new(string), - *new(string), - }}}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - if _, err := a.Icrc1SupportedStandards(); err != nil { - t.Fatal(err) - } - -} - -// Test_Icrc1Symbol tests the "icrc1_symbol" method on the "icrc1" canister. -func Test_Icrc1Symbol(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "icrc1_symbol", - Arguments: []any{}, - Handler: func(request mock.Request) ([]any, error) { - return []any{*new(string)}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - if _, err := a.Icrc1Symbol(); err != nil { - t.Fatal(err) - } - -} - -// Test_Icrc1TotalSupply tests the "icrc1_total_supply" method on the "icrc1" canister. -func Test_Icrc1TotalSupply(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "icrc1_total_supply", - Arguments: []any{}, - Handler: func(request mock.Request) ([]any, error) { - return []any{idl.NewNat(uint(0))}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - if _, err := a.Icrc1TotalSupply(); err != nil { - t.Fatal(err) - } - -} - -// Test_Icrc1Transfer tests the "icrc1_transfer" method on the "icrc1" canister. -func Test_Icrc1Transfer(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "icrc1_transfer", - Arguments: []any{new(icrc1.TransferArgs)}, - Handler: func(request mock.Request) ([]any, error) { - return []any{struct { - Ok *idl.Nat `ic:"Ok,variant"` - Err *icrc1.TransferError `ic:"Err,variant"` - }{ - Ok: idl.Ptr(idl.NewNat(uint(0))), - }}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - var a0 = icrc1.TransferArgs{ - *new(*icrc1.Subaccount), - icrc1.Account{ - *new(principal.Principal), - *new(*icrc1.Subaccount), - }, - idl.NewNat(uint(0)), - *new(*idl.Nat), - *new(*[]byte), - *new(*icrc1.Timestamp), - } - if _, err := a.Icrc1Transfer(a0); err != nil { - t.Fatal(err) - } - -} - -// newAgent creates a new agent with the given (mock) methods. -// Runs a mock replica in the background. -func newAgent(methods []mock.Method) (*icrc1.Agent, error) { - replica := mock.NewReplica() - canisterId := principal.Principal{Raw: []byte("icrc1")} - replica.AddCanister(canisterId, methods) - s := httptest.NewServer(replica) - u, _ := url.Parse(s.URL) - a, err := icrc1.NewAgent(canisterId, agent.Config{ - ClientConfig: &agent.ClientConfig{Host: u}, - FetchRootKey: true, - }) - if err != nil { - return nil, err - } - return a, nil -} diff --git a/ic/testdata/gen.go b/ic/testdata/gen.go index dc0d596..17858ab 100644 --- a/ic/testdata/gen.go +++ b/ic/testdata/gen.go @@ -115,18 +115,6 @@ func main() { } _ = os.WriteFile(fmt.Sprintf("%s/agent.go", dir), raw, os.ModePerm) } - { - g, err := gen.NewGenerator("", name, name, did) - g.ModulePath = "github.com/aviate-labs/agent-go/ic" - if err != nil { - log.Panic(err) - } - raw, err := g.GenerateMock() - if err != nil { - log.Panic(err) - } - _ = os.WriteFile(fmt.Sprintf("%s/agent_test.go", dir), raw, os.ModePerm) - } { g, err := gen.NewGenerator("", name, name, did) g.ModulePath = "github.com/aviate-labs/agent-go/ic" diff --git a/ic/types_test.go b/ic/types_test.go deleted file mode 100644 index 3a7ce92..0000000 --- a/ic/types_test.go +++ /dev/null @@ -1,237 +0,0 @@ -package ic_test - -import ( - "github.com/aviate-labs/agent-go" - "github.com/aviate-labs/agent-go/candid/idl" - "github.com/aviate-labs/agent-go/mock" - "github.com/aviate-labs/agent-go/principal" - "net/http/httptest" - "net/url" - "reflect" - "testing" -) - -func TestTypesAgent_prim(t *testing.T) { - replica := mock.NewReplica() - var canisterId principal.Principal - replica.AddCanister( - canisterId, - []mock.Method{ - { - Name: "nat", - Arguments: []any{new(idl.Nat), new(uint8), new(uint16), new(uint32), new(uint64)}, - Handler: passArguments, - }, - { - Name: "vec_nat", - Arguments: []any{new([]idl.Nat), new([]uint8), new([]uint16), new([]uint32), new([]uint64)}, - Handler: passArguments, - }, - { - Name: "int", - Arguments: []any{new(idl.Int), new(int8), new(int16), new(int32), new(int64)}, - Handler: passArguments, - }, - { - Name: "vec_int", - Arguments: []any{new([]idl.Int), new([]int8), new([]int16), new([]int32), new([]int64)}, - Handler: passArguments, - }, - { - Name: "float", - Arguments: []any{new(float32), new([]float32), new(float64), new([]float64)}, - Handler: passArguments, - }, - { - Name: "text", - Arguments: []any{new(string), new([]string)}, - Handler: passArguments, - }, - { - Name: "bool", - Arguments: []any{new(bool), new([]bool)}, - Handler: passArguments, - }, - { - Name: "principal", - Arguments: []any{new(principal.Principal), new([]principal.Principal)}, - Handler: passArguments, - }, - }, - ) - - s := httptest.NewServer(replica) - u, _ := url.Parse(s.URL) - a, _ := NewTypesAgent(canisterId, agent.Config{ - ClientConfig: &agent.ClientConfig{Host: u}, - FetchRootKey: true, - }) - - t.Run("nat", func(t *testing.T) { - var a0 = idl.NewNat(uint(0xFF)) - var a1 = uint8(8) - var a2 = uint16(16) - var a3 = uint32(32) - var a4 = uint64(64) - r0, r1, r2, r3, r4, err := a.Nat(a0, a1, a2, a3, a4) - if err != nil { - t.Fatal(err) - } - if !reflect.DeepEqual(a0, *r0) { - t.Errorf("expected %v, got %v", a0, *r0) - } - if a1 != *r1 { - t.Errorf("expected %v, got %v", a1, *r1) - } - if a2 != *r2 { - t.Errorf("expected %v, got %v", a2, *r2) - } - if a3 != *r3 { - t.Errorf("expected %v, got %v", a3, *r3) - } - if a4 != *r4 { - t.Errorf("expected %v, got %v", a4, *r4) - } - }) - t.Run("vec nat", func(t *testing.T) { - var a0 = []idl.Nat{idl.NewNat(uint(0xFF))} - var a1 = []uint8{8} - var a2 = []uint16{16} - var a3 = []uint32{32} - var a4 = []uint64{64} - r0, r1, r2, r3, r4, err := a.VecNat(a0, a1, a2, a3, a4) - if err != nil { - t.Fatal(err) - } - if !reflect.DeepEqual(a0, *r0) { - t.Errorf("expected %v, got %v", a0, *r0) - } - if !reflect.DeepEqual(a1, *r1) { - t.Errorf("expected %v, got %v", a1, *r1) - } - if !reflect.DeepEqual(a2, *r2) { - t.Errorf("expected %v, got %v", a2, *r2) - } - if !reflect.DeepEqual(a3, *r3) { - t.Errorf("expected %v, got %v", a3, *r3) - } - if !reflect.DeepEqual(a4, *r4) { - t.Errorf("expected %v, got %v", a4, *r4) - } - }) - t.Run("int", func(t *testing.T) { - var a0 = idl.NewInt(0xFF) - var a1 = int8(8) - var a2 = int16(16) - var a3 = int32(32) - var a4 = int64(64) - r0, r1, r2, r3, r4, err := a.Int(a0, a1, a2, a3, a4) - if err != nil { - t.Fatal(err) - } - if !reflect.DeepEqual(a0, *r0) { - t.Errorf("expected %v, got %v", a0, *r0) - } - if a1 != *r1 { - t.Errorf("expected %v, got %v", a1, *r1) - } - if a2 != *r2 { - t.Errorf("expected %v, got %v", a2, *r2) - } - if a3 != *r3 { - t.Errorf("expected %v, got %v", a3, *r3) - } - if a4 != *r4 { - t.Errorf("expected %v, got %v", a4, *r4) - } - }) - t.Run("vec int", func(t *testing.T) { - var a0 = []idl.Int{idl.NewInt(0xFF)} - var a1 = []int8{8} - var a2 = []int16{16} - var a3 = []int32{32} - var a4 = []int64{64} - r0, r1, r2, r3, r4, err := a.VecInt(a0, a1, a2, a3, a4) - if err != nil { - t.Fatal(err) - } - if !reflect.DeepEqual(a0, *r0) { - t.Errorf("expected %v, got %v", a0, *r0) - } - if !reflect.DeepEqual(a1, *r1) { - t.Errorf("expected %v, got %v", a1, *r1) - } - if !reflect.DeepEqual(a2, *r2) { - t.Errorf("expected %v, got %v", a2, *r2) - } - if !reflect.DeepEqual(a3, *r3) { - t.Errorf("expected %v, got %v", a3, *r3) - } - if !reflect.DeepEqual(a4, *r4) { - t.Errorf("expected %v, got %v", a4, *r4) - } - }) - t.Run("float", func(t *testing.T) { - var a0 = float32(3.2) - var a1 = []float32{3.2} - var a2 = float64(6.4) - var a3 = []float64{6.4} - r0, r1, r2, r3, err := a.Float(a0, a1, a2, a3) - if err != nil { - t.Fatal(err) - } - if a0 != *r0 { - t.Errorf("expected %v, got %v", a0, *r0) - } - if !reflect.DeepEqual(a1, *r1) { - t.Errorf("expected %v, got %v", a1, *r1) - } - if a2 != *r2 { - t.Errorf("expected %v, got %v", a2, *r2) - } - if !reflect.DeepEqual(a3, *r3) { - t.Errorf("expected %v, got %v", a3, *r3) - } - }) - t.Run("text", func(t *testing.T) { - var a0 = "a0" - var a1 = []string{"a1"} - r0, r1, err := a.Text(a0, a1) - if err != nil { - t.Fatal(err) - } - if a0 != *r0 { - t.Errorf("expected %v, got %v", a0, *r0) - } - if !reflect.DeepEqual(a1, *r1) { - t.Errorf("expected %v, got %v", a1, *r1) - } - }) - t.Run("bool", func(t *testing.T) { - var a0 = true - var a1 = []bool{true} - r0, r1, err := a.Bool(a0, a1) - if err != nil { - t.Fatal(err) - } - if a0 != *r0 { - t.Errorf("expected %v, got %v", a0, *r0) - } - if !reflect.DeepEqual(a1, *r1) { - t.Errorf("expected %v, got %v", a1, *r1) - } - }) -} - -func passArguments(request mock.Request) ([]any, error) { - return removePtr(request.Arguments), nil -} - -func removePtr(args []any) []any { - var result []any - for _, arg := range args { - v := reflect.ValueOf(arg) - result = append(result, v.Elem().Interface()) - } - return result -} diff --git a/ic/wallet/agent_test.go b/ic/wallet/agent_test.go deleted file mode 100755 index 01bc5d5..0000000 --- a/ic/wallet/agent_test.go +++ /dev/null @@ -1,1041 +0,0 @@ -// Do NOT edit this file. It was automatically generated by https://github.com/aviate-labs/agent-go. -package wallet_test - -import ( - "github.com/aviate-labs/agent-go" - "github.com/aviate-labs/agent-go/candid/idl" - "github.com/aviate-labs/agent-go/mock" - "github.com/aviate-labs/agent-go/principal" - "net/http/httptest" - "net/url" - "testing" - - "github.com/aviate-labs/agent-go/ic/wallet" -) - -// Test_AddAddress tests the "add_address" method on the "wallet" canister. -func Test_AddAddress(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "add_address", - Arguments: []any{new(wallet.AddressEntry)}, - Handler: func(request mock.Request) ([]any, error) { - return []any{}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - var a0 = wallet.AddressEntry{ - *new(principal.Principal), - *new(*string), - wallet.Kind{ - Unknown: new(idl.Null), - }, - wallet.Role{ - Contact: new(idl.Null), - }, - } - if err := a.AddAddress(a0); err != nil { - t.Fatal(err) - } - -} - -// Test_AddController tests the "add_controller" method on the "wallet" canister. -func Test_AddController(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "add_controller", - Arguments: []any{new(principal.Principal)}, - Handler: func(request mock.Request) ([]any, error) { - return []any{}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - var a0 = *new(principal.Principal) - if err := a.AddController(a0); err != nil { - t.Fatal(err) - } - -} - -// Test_Authorize tests the "authorize" method on the "wallet" canister. -func Test_Authorize(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "authorize", - Arguments: []any{new(principal.Principal)}, - Handler: func(request mock.Request) ([]any, error) { - return []any{}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - var a0 = *new(principal.Principal) - if err := a.Authorize(a0); err != nil { - t.Fatal(err) - } - -} - -// Test_Deauthorize tests the "deauthorize" method on the "wallet" canister. -func Test_Deauthorize(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "deauthorize", - Arguments: []any{new(principal.Principal)}, - Handler: func(request mock.Request) ([]any, error) { - return []any{wallet.WalletResult{ - Ok: new(idl.Null), - }}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - var a0 = *new(principal.Principal) - if _, err := a.Deauthorize(a0); err != nil { - t.Fatal(err) - } - -} - -// Test_GetChart tests the "get_chart" method on the "wallet" canister. -func Test_GetChart(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "get_chart", - Arguments: []any{new(*struct { - Count *uint32 `ic:"count,omitempty" json:"count,omitempty"` - Precision *uint64 `ic:"precision,omitempty" json:"precision,omitempty"` - })}, - Handler: func(request mock.Request) ([]any, error) { - return []any{[]struct { - Field0 uint64 `ic:"0" json:"0"` - Field1 uint64 `ic:"1" json:"1"` - }{ - - { - *new(uint64), - *new(uint64), - }}}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - var a0 = *new(*struct { - Count *uint32 `ic:"count,omitempty" json:"count,omitempty"` - Precision *uint64 `ic:"precision,omitempty" json:"precision,omitempty"` - }) - if _, err := a.GetChart(a0); err != nil { - t.Fatal(err) - } - -} - -// Test_GetControllers tests the "get_controllers" method on the "wallet" canister. -func Test_GetControllers(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "get_controllers", - Arguments: []any{}, - Handler: func(request mock.Request) ([]any, error) { - return []any{[]principal.Principal{*new(principal.Principal)}}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - if _, err := a.GetControllers(); err != nil { - t.Fatal(err) - } - -} - -// Test_GetCustodians tests the "get_custodians" method on the "wallet" canister. -func Test_GetCustodians(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "get_custodians", - Arguments: []any{}, - Handler: func(request mock.Request) ([]any, error) { - return []any{[]principal.Principal{*new(principal.Principal)}}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - if _, err := a.GetCustodians(); err != nil { - t.Fatal(err) - } - -} - -// Test_GetEvents tests the "get_events" method on the "wallet" canister. -func Test_GetEvents(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "get_events", - Arguments: []any{new(*struct { - From *uint32 `ic:"from,omitempty" json:"from,omitempty"` - To *uint32 `ic:"to,omitempty" json:"to,omitempty"` - })}, - Handler: func(request mock.Request) ([]any, error) { - return []any{[]wallet.Event{{ - *new(uint32), - *new(uint64), - wallet.EventKind{ - CyclesSent: idl.Ptr(struct { - To principal.Principal `ic:"to" json:"to"` - Amount uint64 `ic:"amount" json:"amount"` - Refund uint64 `ic:"refund" json:"refund"` - }{ - *new(principal.Principal), - *new(uint64), - *new(uint64), - }), - }, - }}}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - var a0 = *new(*struct { - From *uint32 `ic:"from,omitempty" json:"from,omitempty"` - To *uint32 `ic:"to,omitempty" json:"to,omitempty"` - }) - if _, err := a.GetEvents(a0); err != nil { - t.Fatal(err) - } - -} - -// Test_GetEvents128 tests the "get_events128" method on the "wallet" canister. -func Test_GetEvents128(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "get_events128", - Arguments: []any{new(*struct { - From *uint32 `ic:"from,omitempty" json:"from,omitempty"` - To *uint32 `ic:"to,omitempty" json:"to,omitempty"` - })}, - Handler: func(request mock.Request) ([]any, error) { - return []any{[]wallet.Event128{{ - *new(uint32), - *new(uint64), - wallet.EventKind128{ - CyclesSent: idl.Ptr(struct { - To principal.Principal `ic:"to" json:"to"` - Amount idl.Nat `ic:"amount" json:"amount"` - Refund idl.Nat `ic:"refund" json:"refund"` - }{ - *new(principal.Principal), - idl.NewNat(uint(0)), - idl.NewNat(uint(0)), - }), - }, - }}}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - var a0 = *new(*struct { - From *uint32 `ic:"from,omitempty" json:"from,omitempty"` - To *uint32 `ic:"to,omitempty" json:"to,omitempty"` - }) - if _, err := a.GetEvents128(a0); err != nil { - t.Fatal(err) - } - -} - -// Test_GetManagedCanisterEvents tests the "get_managed_canister_events" method on the "wallet" canister. -func Test_GetManagedCanisterEvents(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "get_managed_canister_events", - Arguments: []any{new(struct { - Canister principal.Principal `ic:"canister" json:"canister"` - From *uint32 `ic:"from,omitempty" json:"from,omitempty"` - To *uint32 `ic:"to,omitempty" json:"to,omitempty"` - })}, - Handler: func(request mock.Request) ([]any, error) { - return []any{*new(*[]wallet.ManagedCanisterEvent)}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - var a0 = struct { - Canister principal.Principal `ic:"canister" json:"canister"` - From *uint32 `ic:"from,omitempty" json:"from,omitempty"` - To *uint32 `ic:"to,omitempty" json:"to,omitempty"` - }{ - *new(principal.Principal), - *new(*uint32), - *new(*uint32), - } - if _, err := a.GetManagedCanisterEvents(a0); err != nil { - t.Fatal(err) - } - -} - -// Test_GetManagedCanisterEvents128 tests the "get_managed_canister_events128" method on the "wallet" canister. -func Test_GetManagedCanisterEvents128(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "get_managed_canister_events128", - Arguments: []any{new(struct { - Canister principal.Principal `ic:"canister" json:"canister"` - From *uint32 `ic:"from,omitempty" json:"from,omitempty"` - To *uint32 `ic:"to,omitempty" json:"to,omitempty"` - })}, - Handler: func(request mock.Request) ([]any, error) { - return []any{*new(*[]wallet.ManagedCanisterEvent128)}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - var a0 = struct { - Canister principal.Principal `ic:"canister" json:"canister"` - From *uint32 `ic:"from,omitempty" json:"from,omitempty"` - To *uint32 `ic:"to,omitempty" json:"to,omitempty"` - }{ - *new(principal.Principal), - *new(*uint32), - *new(*uint32), - } - if _, err := a.GetManagedCanisterEvents128(a0); err != nil { - t.Fatal(err) - } - -} - -// Test_HttpRequest tests the "http_request" method on the "wallet" canister. -func Test_HttpRequest(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "http_request", - Arguments: []any{new(wallet.HttpRequest)}, - Handler: func(request mock.Request) ([]any, error) { - return []any{wallet.HttpResponse{ - *new(uint16), - []wallet.HeaderField{{ - *new(string), - *new(string), - }}, - *new([]byte), - *new(*wallet.StreamingStrategy), - }}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - var a0 = wallet.HttpRequest{ - *new(string), - *new(string), - []wallet.HeaderField{{ - *new(string), - *new(string), - }}, - *new([]byte), - } - if _, err := a.HttpRequest(a0); err != nil { - t.Fatal(err) - } - -} - -// Test_ListAddresses tests the "list_addresses" method on the "wallet" canister. -func Test_ListAddresses(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "list_addresses", - Arguments: []any{}, - Handler: func(request mock.Request) ([]any, error) { - return []any{[]wallet.AddressEntry{{ - *new(principal.Principal), - *new(*string), - wallet.Kind{ - Unknown: new(idl.Null), - }, - wallet.Role{ - Contact: new(idl.Null), - }, - }}}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - if _, err := a.ListAddresses(); err != nil { - t.Fatal(err) - } - -} - -// Test_ListManagedCanisters tests the "list_managed_canisters" method on the "wallet" canister. -func Test_ListManagedCanisters(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "list_managed_canisters", - Arguments: []any{new(struct { - From *uint32 `ic:"from,omitempty" json:"from,omitempty"` - To *uint32 `ic:"to,omitempty" json:"to,omitempty"` - })}, - Handler: func(request mock.Request) ([]any, error) { - return []any{[]wallet.ManagedCanisterInfo{{ - *new(principal.Principal), - *new(*string), - *new(uint64), - }}, *new(uint32)}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - var a0 = struct { - From *uint32 `ic:"from,omitempty" json:"from,omitempty"` - To *uint32 `ic:"to,omitempty" json:"to,omitempty"` - }{ - *new(*uint32), - *new(*uint32), - } - if _, _, err := a.ListManagedCanisters(a0); err != nil { - t.Fatal(err) - } - -} - -// Test_Name tests the "name" method on the "wallet" canister. -func Test_Name(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "name", - Arguments: []any{}, - Handler: func(request mock.Request) ([]any, error) { - return []any{*new(*string)}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - if _, err := a.Name(); err != nil { - t.Fatal(err) - } - -} - -// Test_RemoveAddress tests the "remove_address" method on the "wallet" canister. -func Test_RemoveAddress(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "remove_address", - Arguments: []any{new(principal.Principal)}, - Handler: func(request mock.Request) ([]any, error) { - return []any{wallet.WalletResult{ - Ok: new(idl.Null), - }}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - var a0 = *new(principal.Principal) - if _, err := a.RemoveAddress(a0); err != nil { - t.Fatal(err) - } - -} - -// Test_RemoveController tests the "remove_controller" method on the "wallet" canister. -func Test_RemoveController(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "remove_controller", - Arguments: []any{new(principal.Principal)}, - Handler: func(request mock.Request) ([]any, error) { - return []any{wallet.WalletResult{ - Ok: new(idl.Null), - }}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - var a0 = *new(principal.Principal) - if _, err := a.RemoveController(a0); err != nil { - t.Fatal(err) - } - -} - -// Test_SetName tests the "set_name" method on the "wallet" canister. -func Test_SetName(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "set_name", - Arguments: []any{new(string)}, - Handler: func(request mock.Request) ([]any, error) { - return []any{}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - var a0 = *new(string) - if err := a.SetName(a0); err != nil { - t.Fatal(err) - } - -} - -// Test_SetShortName tests the "set_short_name" method on the "wallet" canister. -func Test_SetShortName(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "set_short_name", - Arguments: []any{new(principal.Principal), new(*string)}, - Handler: func(request mock.Request) ([]any, error) { - return []any{*new(*wallet.ManagedCanisterInfo)}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - var a0 = *new(principal.Principal) - var a1 = *new(*string) - if _, err := a.SetShortName(a0, a1); err != nil { - t.Fatal(err) - } - -} - -// Test_WalletApiVersion tests the "wallet_api_version" method on the "wallet" canister. -func Test_WalletApiVersion(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "wallet_api_version", - Arguments: []any{}, - Handler: func(request mock.Request) ([]any, error) { - return []any{*new(string)}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - if _, err := a.WalletApiVersion(); err != nil { - t.Fatal(err) - } - -} - -// Test_WalletBalance tests the "wallet_balance" method on the "wallet" canister. -func Test_WalletBalance(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "wallet_balance", - Arguments: []any{}, - Handler: func(request mock.Request) ([]any, error) { - return []any{struct { - Amount uint64 `ic:"amount" json:"amount"` - }{ - *new(uint64), - }}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - if _, err := a.WalletBalance(); err != nil { - t.Fatal(err) - } - -} - -// Test_WalletBalance128 tests the "wallet_balance128" method on the "wallet" canister. -func Test_WalletBalance128(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "wallet_balance128", - Arguments: []any{}, - Handler: func(request mock.Request) ([]any, error) { - return []any{struct { - Amount idl.Nat `ic:"amount" json:"amount"` - }{ - idl.NewNat(uint(0)), - }}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - if _, err := a.WalletBalance128(); err != nil { - t.Fatal(err) - } - -} - -// Test_WalletCall tests the "wallet_call" method on the "wallet" canister. -func Test_WalletCall(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "wallet_call", - Arguments: []any{new(struct { - Canister principal.Principal `ic:"canister" json:"canister"` - MethodName string `ic:"method_name" json:"method_name"` - Args []byte `ic:"args" json:"args"` - Cycles uint64 `ic:"cycles" json:"cycles"` - })}, - Handler: func(request mock.Request) ([]any, error) { - return []any{wallet.WalletResultCall{ - Ok: idl.Ptr(struct { - Return []byte `ic:"return" json:"return"` - }{ - *new([]byte), - }), - }}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - var a0 = struct { - Canister principal.Principal `ic:"canister" json:"canister"` - MethodName string `ic:"method_name" json:"method_name"` - Args []byte `ic:"args" json:"args"` - Cycles uint64 `ic:"cycles" json:"cycles"` - }{ - *new(principal.Principal), - *new(string), - *new([]byte), - *new(uint64), - } - if _, err := a.WalletCall(a0); err != nil { - t.Fatal(err) - } - -} - -// Test_WalletCall128 tests the "wallet_call128" method on the "wallet" canister. -func Test_WalletCall128(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "wallet_call128", - Arguments: []any{new(struct { - Canister principal.Principal `ic:"canister" json:"canister"` - MethodName string `ic:"method_name" json:"method_name"` - Args []byte `ic:"args" json:"args"` - Cycles idl.Nat `ic:"cycles" json:"cycles"` - })}, - Handler: func(request mock.Request) ([]any, error) { - return []any{wallet.WalletResultCall{ - Ok: idl.Ptr(struct { - Return []byte `ic:"return" json:"return"` - }{ - *new([]byte), - }), - }}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - var a0 = struct { - Canister principal.Principal `ic:"canister" json:"canister"` - MethodName string `ic:"method_name" json:"method_name"` - Args []byte `ic:"args" json:"args"` - Cycles idl.Nat `ic:"cycles" json:"cycles"` - }{ - *new(principal.Principal), - *new(string), - *new([]byte), - idl.NewNat(uint(0)), - } - if _, err := a.WalletCall128(a0); err != nil { - t.Fatal(err) - } - -} - -// Test_WalletCallWithMaxCycles tests the "wallet_call_with_max_cycles" method on the "wallet" canister. -func Test_WalletCallWithMaxCycles(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "wallet_call_with_max_cycles", - Arguments: []any{new(struct { - Canister principal.Principal `ic:"canister" json:"canister"` - MethodName string `ic:"method_name" json:"method_name"` - Args []byte `ic:"args" json:"args"` - })}, - Handler: func(request mock.Request) ([]any, error) { - return []any{wallet.WalletResultCallWithMaxCycles{ - Ok: idl.Ptr(struct { - Return []byte `ic:"return" json:"return"` - AttachedCycles idl.Nat `ic:"attached_cycles" json:"attached_cycles"` - }{ - *new([]byte), - idl.NewNat(uint(0)), - }), - }}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - var a0 = struct { - Canister principal.Principal `ic:"canister" json:"canister"` - MethodName string `ic:"method_name" json:"method_name"` - Args []byte `ic:"args" json:"args"` - }{ - *new(principal.Principal), - *new(string), - *new([]byte), - } - if _, err := a.WalletCallWithMaxCycles(a0); err != nil { - t.Fatal(err) - } - -} - -// Test_WalletCreateCanister tests the "wallet_create_canister" method on the "wallet" canister. -func Test_WalletCreateCanister(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "wallet_create_canister", - Arguments: []any{new(wallet.CreateCanisterArgs)}, - Handler: func(request mock.Request) ([]any, error) { - return []any{wallet.WalletResultCreate{ - Ok: idl.Ptr(struct { - CanisterId principal.Principal `ic:"canister_id" json:"canister_id"` - }{ - *new(principal.Principal), - }), - }}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - var a0 = wallet.CreateCanisterArgs{ - *new(uint64), - wallet.CanisterSettings{ - *new(*principal.Principal), - *new(*[]principal.Principal), - *new(*idl.Nat), - *new(*idl.Nat), - *new(*idl.Nat), - }, - } - if _, err := a.WalletCreateCanister(a0); err != nil { - t.Fatal(err) - } - -} - -// Test_WalletCreateCanister128 tests the "wallet_create_canister128" method on the "wallet" canister. -func Test_WalletCreateCanister128(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "wallet_create_canister128", - Arguments: []any{new(wallet.CreateCanisterArgs128)}, - Handler: func(request mock.Request) ([]any, error) { - return []any{wallet.WalletResultCreate{ - Ok: idl.Ptr(struct { - CanisterId principal.Principal `ic:"canister_id" json:"canister_id"` - }{ - *new(principal.Principal), - }), - }}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - var a0 = wallet.CreateCanisterArgs128{ - idl.NewNat(uint(0)), - wallet.CanisterSettings{ - *new(*principal.Principal), - *new(*[]principal.Principal), - *new(*idl.Nat), - *new(*idl.Nat), - *new(*idl.Nat), - }, - } - if _, err := a.WalletCreateCanister128(a0); err != nil { - t.Fatal(err) - } - -} - -// Test_WalletCreateWallet tests the "wallet_create_wallet" method on the "wallet" canister. -func Test_WalletCreateWallet(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "wallet_create_wallet", - Arguments: []any{new(wallet.CreateCanisterArgs)}, - Handler: func(request mock.Request) ([]any, error) { - return []any{wallet.WalletResultCreate{ - Ok: idl.Ptr(struct { - CanisterId principal.Principal `ic:"canister_id" json:"canister_id"` - }{ - *new(principal.Principal), - }), - }}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - var a0 = wallet.CreateCanisterArgs{ - *new(uint64), - wallet.CanisterSettings{ - *new(*principal.Principal), - *new(*[]principal.Principal), - *new(*idl.Nat), - *new(*idl.Nat), - *new(*idl.Nat), - }, - } - if _, err := a.WalletCreateWallet(a0); err != nil { - t.Fatal(err) - } - -} - -// Test_WalletCreateWallet128 tests the "wallet_create_wallet128" method on the "wallet" canister. -func Test_WalletCreateWallet128(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "wallet_create_wallet128", - Arguments: []any{new(wallet.CreateCanisterArgs128)}, - Handler: func(request mock.Request) ([]any, error) { - return []any{wallet.WalletResultCreate{ - Ok: idl.Ptr(struct { - CanisterId principal.Principal `ic:"canister_id" json:"canister_id"` - }{ - *new(principal.Principal), - }), - }}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - var a0 = wallet.CreateCanisterArgs128{ - idl.NewNat(uint(0)), - wallet.CanisterSettings{ - *new(*principal.Principal), - *new(*[]principal.Principal), - *new(*idl.Nat), - *new(*idl.Nat), - *new(*idl.Nat), - }, - } - if _, err := a.WalletCreateWallet128(a0); err != nil { - t.Fatal(err) - } - -} - -// Test_WalletReceive tests the "wallet_receive" method on the "wallet" canister. -func Test_WalletReceive(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "wallet_receive", - Arguments: []any{new(*wallet.ReceiveOptions)}, - Handler: func(request mock.Request) ([]any, error) { - return []any{}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - var a0 = *new(*wallet.ReceiveOptions) - if err := a.WalletReceive(a0); err != nil { - t.Fatal(err) - } - -} - -// Test_WalletSend tests the "wallet_send" method on the "wallet" canister. -func Test_WalletSend(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "wallet_send", - Arguments: []any{new(struct { - Canister principal.Principal `ic:"canister" json:"canister"` - Amount uint64 `ic:"amount" json:"amount"` - })}, - Handler: func(request mock.Request) ([]any, error) { - return []any{wallet.WalletResult{ - Ok: new(idl.Null), - }}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - var a0 = struct { - Canister principal.Principal `ic:"canister" json:"canister"` - Amount uint64 `ic:"amount" json:"amount"` - }{ - *new(principal.Principal), - *new(uint64), - } - if _, err := a.WalletSend(a0); err != nil { - t.Fatal(err) - } - -} - -// Test_WalletSend128 tests the "wallet_send128" method on the "wallet" canister. -func Test_WalletSend128(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "wallet_send128", - Arguments: []any{new(struct { - Canister principal.Principal `ic:"canister" json:"canister"` - Amount idl.Nat `ic:"amount" json:"amount"` - })}, - Handler: func(request mock.Request) ([]any, error) { - return []any{wallet.WalletResult{ - Ok: new(idl.Null), - }}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - var a0 = struct { - Canister principal.Principal `ic:"canister" json:"canister"` - Amount idl.Nat `ic:"amount" json:"amount"` - }{ - *new(principal.Principal), - idl.NewNat(uint(0)), - } - if _, err := a.WalletSend128(a0); err != nil { - t.Fatal(err) - } - -} - -// Test_WalletStoreWalletWasm tests the "wallet_store_wallet_wasm" method on the "wallet" canister. -func Test_WalletStoreWalletWasm(t *testing.T) { - a, err := newAgent([]mock.Method{ - { - Name: "wallet_store_wallet_wasm", - Arguments: []any{new(struct { - WasmModule []byte `ic:"wasm_module" json:"wasm_module"` - })}, - Handler: func(request mock.Request) ([]any, error) { - return []any{}, nil - }, - }, - }) - if err != nil { - t.Fatal(err) - } - - var a0 = struct { - WasmModule []byte `ic:"wasm_module" json:"wasm_module"` - }{ - *new([]byte), - } - if err := a.WalletStoreWalletWasm(a0); err != nil { - t.Fatal(err) - } - -} - -// newAgent creates a new agent with the given (mock) methods. -// Runs a mock replica in the background. -func newAgent(methods []mock.Method) (*wallet.Agent, error) { - replica := mock.NewReplica() - canisterId := principal.Principal{Raw: []byte("wallet")} - replica.AddCanister(canisterId, methods) - s := httptest.NewServer(replica) - u, _ := url.Parse(s.URL) - a, err := wallet.NewAgent(canisterId, agent.Config{ - ClientConfig: &agent.ClientConfig{Host: u}, - FetchRootKey: true, - }) - if err != nil { - return nil, err - } - return a, nil -} diff --git a/mock/replica.go b/mock/replica.go deleted file mode 100644 index 63441f2..0000000 --- a/mock/replica.go +++ /dev/null @@ -1,265 +0,0 @@ -package mock - -import ( - "bytes" - "encoding/hex" - "io" - "net/http" - "strings" - - "github.com/aviate-labs/agent-go" - "github.com/aviate-labs/agent-go/candid/idl" - "github.com/aviate-labs/agent-go/certification" - "github.com/aviate-labs/agent-go/certification/bls" - "github.com/aviate-labs/agent-go/certification/hashtree" - "github.com/aviate-labs/agent-go/principal" - - "github.com/fxamacker/cbor/v2" -) - -type Canister struct { - Id principal.Principal - Methods map[string]Method -} - -type HandlerFunc func(request Request) ([]any, error) - -type Method struct { - Name string // Name is the name of the method. - Arguments []any // Arguments is a list of pointers to the arguments, that will be filled by the agent. - Handler HandlerFunc // Handler is the function that will be called when the method is called. -} - -type Replica struct { - rootKey *bls.SecretKey - Canisters map[string]Canister - Requests map[string]agent.Request -} - -func NewReplica() *Replica { - return &Replica{ - rootKey: bls.NewSecretKeyByCSPRNG(), - Canisters: make(map[string]Canister), - Requests: make(map[string]agent.Request), - } -} - -// AddCanister adds a canister to the replica. -func (r *Replica) AddCanister( - id principal.Principal, - methods []Method, -) { - ms := make(map[string]Method) - for _, m := range methods { - ms[m.Name] = m - } - r.Canisters[id.String()] = Canister{ - Id: id, - Methods: ms, - } -} - -func (r *Replica) ServeHTTP(writer http.ResponseWriter, request *http.Request) { - if !strings.HasPrefix(request.URL.Path, "/api/v2/") { - writer.WriteHeader(http.StatusNotFound) - return - } - - path := strings.Split(request.URL.Path, "/")[3:] - switch path[0] { - case "canister": - if request.Method != http.MethodPost { - writer.WriteHeader(http.StatusMethodNotAllowed) - return - } - body, _ := io.ReadAll(request.Body) - r.handleCanister(writer, path[1], path[2], body) - case "status": - if request.Method != http.MethodGet { - writer.WriteHeader(http.StatusMethodNotAllowed) - return - } - r.handleStatus(writer) - default: - writer.WriteHeader(http.StatusNotFound) - } -} - -func (r *Replica) handleCanister(writer http.ResponseWriter, canisterId, typ string, body []byte) { - canister, ok := r.Canisters[canisterId] - if !ok { - writer.WriteHeader(http.StatusNotFound) - _, _ = writer.Write([]byte("canister not found: " + canisterId)) - return - } - var envelope agent.Envelope - if err := cbor.Unmarshal(body, &envelope); err != nil { - writer.WriteHeader(http.StatusBadRequest) - _, _ = writer.Write([]byte(err.Error())) - return - } - // TODO: validate sender + signatures, ... - req := envelope.Content - - switch typ { - case "call": - if req.Type != agent.RequestTypeCall { - writer.WriteHeader(http.StatusBadRequest) - _, _ = writer.Write([]byte("expected call request")) - return - } - requestID := agent.NewRequestID(req) - requestIDHex := hex.EncodeToString(requestID[:]) - r.Requests[requestIDHex] = req - writer.WriteHeader(http.StatusAccepted) - case "query": - if req.Type != agent.RequestTypeQuery { - writer.WriteHeader(http.StatusBadRequest) - _, _ = writer.Write([]byte("expected query request")) - return - } - - method, ok := canister.Methods[req.MethodName] - if !ok { - writer.WriteHeader(http.StatusNotFound) - _, _ = writer.Write([]byte("method not defined in replica: " + req.MethodName)) - return - } - - values, err := method.Handler(fromAgentRequest(req, method.Arguments)) - if err != nil { - writer.WriteHeader(http.StatusInternalServerError) - _, _ = writer.Write([]byte(err.Error())) - return - } - - rawReply, err := idl.Marshal(values) - if err != nil { - writer.WriteHeader(http.StatusInternalServerError) - _, _ = writer.Write([]byte(err.Error())) - return - } - - reply := make(map[string][]byte) - reply["arg"] = rawReply - resp := agent.Response{ - Status: "replied", - Reply: reply, - } - - writer.WriteHeader(http.StatusOK) - raw, err := cbor.Marshal(resp) - if err != nil { - writer.WriteHeader(http.StatusInternalServerError) - _, _ = writer.Write([]byte(err.Error())) - return - } - _, _ = writer.Write(raw) - case "read_state": - if !bytes.Equal(req.Paths[0][0], []byte("request_status")) { - writer.WriteHeader(http.StatusBadRequest) - _, _ = writer.Write([]byte("expected request_status")) - return - } - requestID := req.Paths[0][1] - requestIDHex := hex.EncodeToString(requestID) - req, ok := r.Requests[requestIDHex] - if !ok { - writer.WriteHeader(http.StatusNotFound) - _, _ = writer.Write([]byte("request not found: " + requestIDHex)) - return - } - - method, ok := canister.Methods[req.MethodName] - if !ok { - writer.WriteHeader(http.StatusNotFound) - _, _ = writer.Write([]byte("method not defined in replica: " + req.MethodName)) - return - } - - values, err := method.Handler(fromAgentRequest(req, method.Arguments)) - if err != nil { - writer.WriteHeader(http.StatusInternalServerError) - _, _ = writer.Write([]byte(err.Error())) - return - } - - rawReply, err := idl.Marshal(values) - if err != nil { - writer.WriteHeader(http.StatusInternalServerError) - _, _ = writer.Write([]byte(err.Error())) - return - } - - t := hashtree.NewHashTree(hashtree.Fork{ - LeftTree: hashtree.Labeled{ - Label: []byte("request_status"), - Tree: hashtree.Labeled{ - Label: requestID, - Tree: hashtree.Fork{ - LeftTree: hashtree.Labeled{ - Label: []byte("reply"), - Tree: hashtree.Leaf(rawReply), - }, - RightTree: hashtree.Labeled{ - Label: []byte("status"), - Tree: hashtree.Leaf("replied"), - }, - }, - }, - }, - RightTree: hashtree.Empty{}, - }) - d := t.Digest() - m := make(map[string][]byte) - s := r.rootKey.Sign(string(append(hashtree.DomainSeparator("ic-state-root"), d[:]...))) - cert := certification.Cert{ - Tree: t, - Signature: s.Serialize(), - } - rawCert, _ := cbor.Marshal(cert) - m["certificate"] = rawCert - - rawTree, _ := cbor.Marshal(t) - m["tree"] = rawTree - - writer.WriteHeader(http.StatusOK) - raw, err := cbor.Marshal(m) - if err != nil { - writer.WriteHeader(http.StatusInternalServerError) - _, _ = writer.Write([]byte(err.Error())) - return - } - _, _ = writer.Write(raw) - default: - writer.WriteHeader(http.StatusNotFound) - } -} - -func (r *Replica) handleStatus(writer http.ResponseWriter) { - publicKey := r.rootKey.GetPublicKey().Serialize() - status := agent.Status{ - Version: "golang-mock", - RootKey: publicKey, - } - raw, _ := cbor.Marshal(status) - writer.WriteHeader(http.StatusOK) - _, _ = writer.Write(raw) -} - -type Request struct { - Type agent.RequestType - Sender principal.Principal - Arguments []any -} - -func fromAgentRequest(request agent.Request, arguments []any) Request { - if err := idl.Unmarshal(request.Arguments, arguments); err != nil { - panic(err) - } - return Request{ - Type: request.Type, - Sender: request.Sender, - Arguments: arguments, - } -} diff --git a/mock/replica_test.go b/mock/replica_test.go deleted file mode 100644 index a226bbc..0000000 --- a/mock/replica_test.go +++ /dev/null @@ -1,92 +0,0 @@ -package mock_test - -import ( - "bytes" - "fmt" - "github.com/aviate-labs/agent-go" - "github.com/aviate-labs/agent-go/mock" - "github.com/aviate-labs/agent-go/principal" - "net/http/httptest" - "net/url" - "testing" -) - -func TestNewReplica(t *testing.T) { - replica := mock.NewReplica() - var canisterId principal.Principal - replica.AddCanister( - canisterId, - []mock.Method{ - { - Name: "test", - Handler: func(request mock.Request) ([]any, error) { - if !bytes.Equal(request.Sender.Raw, principal.AnonymousID.Raw) { - t.Error("unexpected sender") - } - if len(request.Arguments) != 0 { - t.Error("unexpected arguments") - } - return []any{"hello"}, nil - }, - }, - }, - ) - - s := httptest.NewServer(replica) - u, _ := url.Parse(s.URL) - a, _ := agent.New(agent.Config{ - ClientConfig: &agent.ClientConfig{Host: u}, - FetchRootKey: true, - }) - - t.Run("call", func(t *testing.T) { - var result string - if err := a.Call(canisterId, "test", nil, []any{&result}); err != nil { - t.Error(err) - } - if result != "hello" { - t.Error("unexpected result") - } - }) - - t.Run("query", func(t *testing.T) { - var result string - if err := a.Query(canisterId, "test", nil, []any{&result}); err != nil { - t.Error(err) - } - if result != "hello" { - t.Error("unexpected result") - } - }) -} - -func TestNewReplica_error(t *testing.T) { - replica := mock.NewReplica() - var canisterId principal.Principal - replica.AddCanister( - canisterId, - []mock.Method{ - { - Name: "test", - Handler: func(request mock.Request) ([]any, error) { - return nil, fmt.Errorf("oops") - }, - }, - }, - ) - - s := httptest.NewServer(replica) - u, _ := url.Parse(s.URL) - a, _ := agent.New(agent.Config{ - ClientConfig: &agent.ClientConfig{Host: u}, - FetchRootKey: true, - }) - - t.Run("call", func(t *testing.T) { - var result string - err := a.Call(canisterId, "test", nil, []any{&result}) - if err == nil || err.Error() != "(500) 500 Internal Server Error: oops" { - t.Error("unexpected error") - } - }) -}