-
Notifications
You must be signed in to change notification settings - Fork 77
Expand file tree
/
Copy pathvm_runner_graphql_query_test.go
More file actions
259 lines (224 loc) · 6.99 KB
/
vm_runner_graphql_query_test.go
File metadata and controls
259 lines (224 loc) · 6.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
package taskengine
import (
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/AvaProtocol/EigenLayer-AVS/core/testutil"
"github.com/AvaProtocol/EigenLayer-AVS/model"
"github.com/AvaProtocol/EigenLayer-AVS/pkg/gow"
avsproto "github.com/AvaProtocol/EigenLayer-AVS/protobuf"
)
// Test GraphQL node processing against a local mock server
func TestGraphlQlNodeSimpleQuery(t *testing.T) {
// Mock GraphQL server that returns a response matching The Graph subgraph format
mockServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
fmt.Fprint(w, `{
"data": {
"markets": [
{
"id": "0x794a61358d6845594f94dc1db02a252b5b4814ad-0x82af49447d8a07e3bd95bd0d56f35241523fbab1-0xa97684ead0e402dc232d5a977953df7ecbab3cdb",
"name": "Aave Arbitrum WETH",
"inputToken": {
"symbol": "WETH",
"decimals": 18
},
"totalValueLockedUSD": "150000000.50"
},
{
"id": "0x794a61358d6845594f94dc1db02a252b5b4814ad-0xaf88d065e77c8cc2239327c5edb3a432268e5831-0xa97684ead0e402dc232d5a977953df7ecbab3cdb",
"name": "Aave Arbitrum USDC",
"inputToken": {
"symbol": "USDC",
"decimals": 6
},
"totalValueLockedUSD": "200000000.25"
}
]
}
}`)
}))
defer mockServer.Close()
node := &avsproto.GraphQLQueryNode{
Config: &avsproto.GraphQLQueryNode_Config{
Url: mockServer.URL,
Query: `
query AaveMarkets {
markets(first: 2, orderBy: totalValueLockedUSD, orderDirection: desc) {
id
name
inputToken {
symbol
decimals
}
totalValueLockedUSD
}
}
`,
},
}
nodes := []*avsproto.TaskNode{
{
Id: "123abc",
Name: "graphqlQuery",
TaskType: &avsproto.TaskNode_GraphqlQuery{
GraphqlQuery: node,
},
},
}
trigger := &avsproto.TaskTrigger{
Id: "triggertest",
Name: "triggertest",
}
edges := []*avsproto.TaskEdge{
{
Id: "e1",
Source: trigger.Id,
Target: "123abc",
},
}
vm, err := NewVMWithData(&model.Task{
Task: &avsproto.Task{
Id: "123abc",
Nodes: nodes,
Edges: edges,
Trigger: trigger,
},
}, nil, testutil.GetTestSmartWalletConfig(), nil)
if err != nil {
t.Fatalf("failed to create VM: %v", err)
}
n, _ := NewGraphqlQueryProcessor(vm)
step, _, err := n.Execute("123abc", node)
if err != nil {
t.Fatalf("expected graphql node to run successfully but got error: %v", err)
}
if !step.Success {
t.Fatalf("expected graphql node to run successfully but failed")
}
if !strings.Contains(step.Log, "Executing 'graphqlQuery'") {
t.Errorf("expected log contains request trace data but not found. Log data is: %s", step.Log)
}
if step.Error != "" {
t.Errorf("expected no error but got: %s", step.Error)
}
graphqlResult := step.GetGraphql()
if graphqlResult == nil || graphqlResult.Data == nil {
t.Fatal("expected graphql data but got nil")
}
var output struct {
Markets []struct {
ID string `json:"id"`
Name string `json:"name"`
InputToken struct {
Symbol string `json:"symbol"`
Decimals int `json:"decimals"`
} `json:"inputToken"`
TotalValueLockedUSD string `json:"totalValueLockedUSD"`
} `json:"markets"`
}
dataMap := gow.ValueToMap(graphqlResult.Data)
if dataMap == nil {
t.Fatal("expected graphql data map but got nil")
}
jsonBytes, err := json.Marshal(dataMap)
if err != nil {
t.Fatalf("failed to marshal data map: %v", err)
}
err = json.Unmarshal(jsonBytes, &output)
if err != nil {
t.Fatalf("expected the data output in json format, but failed to decode: %v", err)
}
if len(output.Markets) != 2 {
t.Errorf("expected 2 markets but found %d", len(output.Markets))
}
if output.Markets[0].Name != "Aave Arbitrum WETH" {
t.Errorf("name doesn't match. expected %s got %s", "Aave Arbitrum WETH", output.Markets[0].Name)
}
if output.Markets[0].InputToken.Symbol != "WETH" {
t.Errorf("symbol doesn't match. expected %s got %s", "WETH", output.Markets[0].InputToken.Symbol)
}
if output.Markets[1].Name != "Aave Arbitrum USDC" {
t.Errorf("name doesn't match. expected %s got %s", "Aave Arbitrum USDC", output.Markets[1].Name)
}
if output.Markets[1].InputToken.Decimals != 6 {
t.Errorf("decimals doesn't match. expected %d got %d", 6, output.Markets[1].InputToken.Decimals)
}
}
// Test that Loop node with GraphQL runner correctly extracts iteration results.
// This is a regression test for https://github.com/AvaProtocol/EigenLayer-AVS/issues/523
// where extractResultData was missing the GraphQL output case, causing all GraphQL
// loop iterations to be counted as "failed" even though the HTTP requests succeeded.
func TestLoopWithGraphQLRunner(t *testing.T) {
requestCount := 0
mockServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
requestCount++
w.Header().Set("Content-Type", "application/json")
fmt.Fprint(w, `{
"data": {
"country": {
"name": "TestCountry",
"code": "TC"
}
}
}`)
}))
defer mockServer.Close()
// Build a loop node with GraphQL runner
loopConfig := map[string]interface{}{
"inputVariable": "{{countryCodes}}",
"iterVal": "value",
"iterKey": "index",
"runner": map[string]interface{}{
"type": "graphqlDataQuery",
"config": map[string]interface{}{
"url": mockServer.URL,
"query": `query { country(code: "{{value}}") { name code } }`,
},
},
}
node, err := CreateNodeFromType(NodeTypeLoop, loopConfig, "loop_graphql_test")
if err != nil {
t.Fatalf("failed to create loop node: %v", err)
}
inputVariables := map[string]interface{}{
"countryCodes": []interface{}{"US", "JP"},
}
vm, err := NewVMWithData(nil, nil, testutil.GetTestSmartWalletConfig(), nil)
if err != nil {
t.Fatalf("failed to create VM: %v", err)
}
vm.WithLogger(testutil.GetLogger())
step, err := vm.RunNodeWithInputs(node, inputVariables)
if err != nil {
t.Fatalf("expected loop to succeed but got error: %v", err)
}
if !step.Success {
t.Fatalf("expected loop step to succeed but got failure: %s", step.Error)
}
// Verify GraphQL HTTP requests were actually made
if requestCount != 2 {
t.Errorf("expected 2 GraphQL HTTP requests but got %d", requestCount)
}
// Verify loop output data contains results from both iterations
loopOutput := step.GetLoop()
if loopOutput == nil || loopOutput.Data == nil {
t.Fatal("expected loop output data but got nil")
}
outputArray, ok := loopOutput.Data.AsInterface().([]interface{})
if !ok {
t.Fatalf("expected loop output to be an array, got %T", loopOutput.Data.AsInterface())
}
if len(outputArray) != 2 {
t.Fatalf("expected 2 results but got %d", len(outputArray))
}
// Verify each iteration result is non-nil (the fix for issue #523)
for i, result := range outputArray {
if result == nil {
t.Errorf("iteration %d result is nil - extractResultData is not handling GraphQL output", i)
}
}
}