forked from gptscript-ai/gptscript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcredential_test.go
155 lines (149 loc) · 4.08 KB
/
credential_test.go
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
package types
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestParseCredentialArgs(t *testing.T) {
tests := []struct {
name string
toolName string
input string
expectedName string
expectedAlias string
expectedArgs map[string]string
wantErr bool
}{
{
name: "empty",
toolName: "",
expectedName: "",
expectedAlias: "",
},
{
name: "tool name only",
toolName: "myCredentialTool",
expectedName: "myCredentialTool",
expectedAlias: "",
},
{
name: "tool name and alias",
toolName: "myCredentialTool as myAlias",
expectedName: "myCredentialTool",
expectedAlias: "myAlias",
},
{
name: "tool name with one arg",
toolName: "myCredentialTool with value1 as arg1",
expectedName: "myCredentialTool",
expectedAlias: "",
expectedArgs: map[string]string{
"arg1": "value1",
},
},
{
name: "tool name with two args",
toolName: "myCredentialTool with value1 as arg1 and value2 as arg2",
expectedName: "myCredentialTool",
expectedAlias: "",
expectedArgs: map[string]string{
"arg1": "value1",
"arg2": "value2",
},
},
{
name: "tool name with alias and one arg",
toolName: "myCredentialTool as myAlias with value1 as arg1",
expectedName: "myCredentialTool",
expectedAlias: "myAlias",
expectedArgs: map[string]string{
"arg1": "value1",
},
},
{
name: "tool name with alias and two args",
toolName: "myCredentialTool as myAlias with value1 as arg1 and value2 as arg2",
expectedName: "myCredentialTool",
expectedAlias: "myAlias",
expectedArgs: map[string]string{
"arg1": "value1",
"arg2": "value2",
},
},
{
name: "tool name with quoted args",
toolName: `myCredentialTool with "value one" as arg1 and "value two" as arg2`,
expectedName: "myCredentialTool",
expectedAlias: "",
expectedArgs: map[string]string{
"arg1": "value one",
"arg2": "value two",
},
},
{
name: "tool name with arg references",
toolName: `myCredentialTool with ${var1} as arg1 and ${var2} as arg2`,
input: `{"var1": "value1", "var2": "value2"}`,
expectedName: "myCredentialTool",
expectedAlias: "",
expectedArgs: map[string]string{
"arg1": "value1",
"arg2": "value2",
},
},
{
name: "tool name with alias but no 'as' (invalid)",
toolName: "myCredentialTool myAlias",
wantErr: true,
},
{
name: "tool name with 'as' but no alias (invalid)",
toolName: "myCredentialTool as",
wantErr: true,
},
{
name: "tool with 'with' but no args (invalid)",
toolName: "myCredentialTool with",
wantErr: true,
},
{
name: "tool with args but no 'with' (invalid)",
toolName: "myCredentialTool value1 as arg1",
wantErr: true,
},
{
name: "tool with trailing 'and' (invalid)",
toolName: "myCredentialTool with value1 as arg1 and",
wantErr: true,
},
{
name: "tool with quoted arg but the quote is unterminated (invalid)",
toolName: `myCredentialTool with "value one" as arg1 and "value two as arg2`,
wantErr: true,
},
{
name: "invalid input",
toolName: "myCredentialTool",
input: `{"asdf":"asdf"`,
expectedName: "myCredentialTool",
expectedAlias: "",
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
originalName, alias, args, err := ParseCredentialArgs(tt.toolName, tt.input)
if tt.wantErr {
require.Error(t, err, "expected an error but got none")
return
}
require.NoError(t, err, "did not expect an error but got one")
require.Equal(t, tt.expectedName, originalName, "unexpected original name")
require.Equal(t, tt.expectedAlias, alias, "unexpected alias")
require.Equal(t, len(tt.expectedArgs), len(args), "unexpected number of args")
for k, v := range tt.expectedArgs {
assert.Equal(t, v, args[k], "unexpected value for args[%s]", k)
}
})
}
}