-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpolyscript_extism_test.go
More file actions
230 lines (190 loc) · 5.69 KB
/
polyscript_extism_test.go
File metadata and controls
230 lines (190 loc) · 5.69 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
package polyscript_test
import (
"context"
"os"
"path/filepath"
"testing"
"github.com/robbyt/go-polyscript"
"github.com/robbyt/go-polyscript/engines/extism/wasmdata"
"github.com/robbyt/go-polyscript/platform/constants"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
var wasmData = wasmdata.TestModule
func TestFromExtismBytes(t *testing.T) {
t.Parallel()
t.Run("success with embedded wasmdata", func(t *testing.T) {
// Execute
evaluator, err := polyscript.FromExtismBytes(
wasmdata.TestModule,
nil,
wasmdata.EntrypointGreet,
)
// Verify
require.NoError(t, err)
require.NotNil(t, evaluator)
})
t.Run("empty bytes", func(t *testing.T) {
// Execute
evaluator, err := polyscript.FromExtismBytes([]byte{}, nil, wasmdata.EntrypointGreet)
// Verify
require.Error(t, err)
require.Nil(t, evaluator)
})
}
func TestFromExtismBytesWithData(t *testing.T) {
t.Parallel()
t.Run("success with static data", func(t *testing.T) {
// Setup
staticData := map[string]any{
"version": "1.0.0",
"config": map[string]any{
"timeout": 30,
"retry": true,
},
}
// Execute
evaluator, err := polyscript.FromExtismBytesWithData(
wasmdata.TestModule,
staticData,
nil,
wasmdata.EntrypointGreet,
)
// Verify
require.NoError(t, err)
require.NotNil(t, evaluator)
// Test that we can actually run the evaluator with the embedded WASM
// Add runtime data with required "input" field
runtimeData := map[string]any{
"input": "test user",
}
ctx, err := evaluator.AddDataToContext(t.Context(), runtimeData)
require.NoError(t, err)
response, err := evaluator.Eval(ctx)
require.NoError(t, err)
require.NotNil(t, response)
})
}
func TestFromExtismFileLoaders(t *testing.T) {
t.Parallel()
// Create a temporary directory for test files
tmpDir := t.TempDir()
wasmPath := filepath.Join(tmpDir, "main.wasm")
// Write the embedded WASM data to the temporary file
err := os.WriteFile(wasmPath, wasmData, 0o644)
require.NoError(t, err)
t.Run("FromExtismFile - Valid", func(t *testing.T) {
evaluator, err := polyscript.FromExtismFile(wasmPath, nil, wasmdata.EntrypointGreet)
require.NoError(t, err)
require.NotNil(t, evaluator)
// For Extism, we need to test with correct input data
// Create a context with the input data directly
ctx := context.WithValue(t.Context(), constants.EvalData, map[string]any{
"input": "Test User",
})
// Evaluate with the context containing input data
result, err := evaluator.Eval(ctx)
require.NoError(t, err)
require.NotNil(t, result)
})
t.Run("FromExtismFile - Invalid Path", func(t *testing.T) {
_, err := polyscript.FromExtismFile("non-existent-file.wasm", nil, wasmdata.EntrypointGreet)
require.Error(t, err)
})
t.Run("FromExtismFileWithData - Valid", func(t *testing.T) {
staticData := map[string]any{
"input": "Test User", // Required for WASM execution
}
evaluator, err := polyscript.FromExtismFileWithData(
wasmPath,
staticData,
nil,
wasmdata.EntrypointGreet,
)
require.NoError(t, err)
require.NotNil(t, evaluator)
})
}
func TestExtismDataIntegrationScenarios(t *testing.T) {
t.Parallel()
// Create a temporary directory for test files
tmpDir := t.TempDir()
wasmPath := filepath.Join(tmpDir, "main.wasm")
// Write the embedded WASM data to the temporary file
err := os.WriteFile(wasmPath, wasmData, 0o644)
require.NoError(t, err)
// Common test data
staticData := map[string]any{
"app_version": "1.0.0",
"config": map[string]any{
"timeout": 30,
},
}
t.Run("ExtismWithData", func(t *testing.T) {
// Create evaluator with static data that includes input
staticDataWithInput := map[string]any{
"input": "Test User",
}
extismEval, err := polyscript.FromExtismFileWithData(
wasmPath,
staticDataWithInput,
nil,
wasmdata.EntrypointGreet,
)
require.NoError(t, err)
require.NotNil(t, extismEval)
// Evaluate
result, err := extismEval.Eval(t.Context())
require.NoError(t, err)
require.NotNil(t, result)
// Check result
resultMap, ok := result.Interface().(map[string]any)
require.True(t, ok, "Result should be a map")
require.Contains(t, resultMap, "greeting")
assert.Equal(t, "Hello, Test User!", resultMap["greeting"])
// Test evaluator with no input (should fail)
// Create a copy of staticData without input field
configOnlyData := map[string]any{
"app_version": staticData["app_version"],
"config": staticData["config"],
}
extismEvalNoInput, err := polyscript.FromExtismFileWithData(
wasmPath,
configOnlyData,
nil,
wasmdata.EntrypointGreet,
)
require.NoError(t, err)
require.NotNil(t, extismEvalNoInput)
_, err = extismEvalNoInput.Eval(t.Context())
require.Error(t, err)
assert.Contains(t, err.Error(), "input string is empty")
})
}
func TestFromExtismFile(t *testing.T) {
t.Parallel()
// Create a temporary directory for the WASM file
tmpDir := t.TempDir()
wasmPath := filepath.Join(tmpDir, "main.wasm")
// Write the embedded WASM data to the temporary file
err := os.WriteFile(wasmPath, wasmData, 0o644)
require.NoError(t, err)
// Create an evaluator with file loader and static data
evaluator, err := polyscript.FromExtismFileWithData(
wasmPath,
map[string]any{"input": "Test User"},
nil,
wasmdata.EntrypointGreet,
)
require.NoError(t, err)
require.NotNil(t, evaluator)
// Test evaluation
result, err := evaluator.Eval(t.Context())
require.NoError(t, err)
require.NotNil(t, result)
// The greet function returns a JSON with a greeting field
resultMap, ok := result.Interface().(map[string]any)
require.True(t, ok, "Result should be a map")
require.Contains(t, resultMap, "greeting")
assert.Equal(t, "Hello, Test User!", resultMap["greeting"])
}