-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathmain_test.go
138 lines (116 loc) · 3.46 KB
/
main_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
package inferable
import (
"testing"
"github.com/inferablehq/inferable/sdk-go/internal/util"
)
type EchoInput struct {
Input string
}
func echo(input EchoInput, ctx ContextInput) string {
return input.Input
}
type ReverseInput struct {
Input string
}
func reverse(input ReverseInput, ctx ContextInput) string {
runes := []rune(input.Input)
for i, j := 0, len(runes)-1; i < j; i, j = i+1, j-1 {
runes[i], runes[j] = runes[j], runes[i]
}
return string(runes)
}
func TestInferableFunctions(t *testing.T) {
machineSecret, _, _, apiEndpoint := util.GetTestVars()
inferableInstance, err := New(InferableOptions{
APIEndpoint: apiEndpoint,
APISecret: machineSecret,
})
if err != nil {
t.Fatalf("Error creating Inferable instance: %v", err)
}
err = inferableInstance.Tools.Register(Tool{
Func: echo,
Description: "Echoes the input string",
Name: "echo",
})
if err != nil {
t.Fatalf("Error registering echo function: %v", err)
}
err = inferableInstance.Tools.Register(Tool{
Func: reverse,
Description: "Reverses the input string",
Name: "reverse",
})
if err != nil {
t.Fatalf("Error registering reverse function: %v", err)
}
if err != nil {
t.Fatalf("Error generating JSON definition: %v", err)
}
t.Run("Echo Function", func(t *testing.T) {
testInput := EchoInput{Input: "Hello, Inferable!"}
result, err := inferableInstance.callFunc("echo", testInput, ContextInput{})
if err != nil {
t.Fatalf("Error calling echo function: %v", err)
}
if len(result) != 1 {
t.Fatalf("Expected 1 return value, got %d", len(result))
}
returnedString := result[0].Interface().(string)
if returnedString != testInput.Input {
t.Errorf("Echo function returned incorrect result. Expected: %s, Got: %s", testInput.Input, returnedString)
}
})
t.Run("Reverse Function", func(t *testing.T) {
testInput := ReverseInput{Input: "Hello, Inferable!"}
result, err := inferableInstance.callFunc("reverse", testInput, ContextInput{})
if err != nil {
t.Fatalf("Error calling reverse function: %v", err)
}
if len(result) != 1 {
t.Fatalf("Expected 1 return value, got %d", len(result))
}
returnedString := result[0].Interface().(string)
if returnedString != "!elbarefnI ,olleH" {
t.Errorf("Reverse function returned incorrect result. Expected: %s, Got: %s", testInput.Input, returnedString)
}
})
t.Run("Server Health Check", func(t *testing.T) {
err := inferableInstance.serverOk()
if err != nil {
t.Fatalf("Server health check failed: %v", err)
}
t.Log("Server health check passed")
})
t.Run("Machine ID Generation", func(t *testing.T) {
machineID := inferableInstance.machineID
if machineID == "" {
t.Error("Machine ID is empty")
}
t.Logf("Generated Machine ID: %s", machineID)
})
t.Run("Machine ID Consistency", func(t *testing.T) {
machineSecret, _, _, apiEndpoint := util.GetTestVars()
instance1, err := New(InferableOptions{
APIEndpoint: apiEndpoint,
APISecret: machineSecret,
})
if err != nil {
t.Fatalf("Error creating first Inferable instance: %v", err)
}
id1 := instance1.machineID
instance2, err := New(InferableOptions{
APIEndpoint: apiEndpoint,
APISecret: machineSecret,
})
if err != nil {
t.Fatalf("Error creating second Inferable instance: %v", err)
}
id2 := instance2.machineID
if id1 != id2 {
t.Errorf("Machine IDs are not consistent. First: %s, Second: %s", id1, id2)
} else {
t.Logf("Machine ID is consistent: %s", id1)
}
})
}