-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenvelope_test.go
More file actions
87 lines (77 loc) · 2.39 KB
/
envelope_test.go
File metadata and controls
87 lines (77 loc) · 2.39 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
package arcp_test
import (
"encoding/json"
"testing"
arcp "github.com/agentruntimecontrolprotocol/go-sdk"
"github.com/agentruntimecontrolprotocol/go-sdk/messages"
_ "github.com/agentruntimecontrolprotocol/go-sdk/messages"
"github.com/stretchr/testify/require"
)
func TestEnvelopeRoundTrip(t *testing.T) {
hello := messages.SessionHello{
Client: messages.ClientInfo{Name: "test", Version: "0.1"},
Auth: messages.AuthInfo{Scheme: "bearer", Token: "abc"},
Capabilities: messages.HelloCapabilities{
Encodings: []string{"json"},
Features: []string{"heartbeat", "ack"},
},
}
env, err := arcp.NewEnvelope(messages.TypeSessionHello, &hello)
require.NoError(t, err)
env.SessionID = "sess_test"
body, err := json.Marshal(env)
require.NoError(t, err)
var got arcp.Envelope
require.NoError(t, json.Unmarshal(body, &got))
require.Equal(t, env.Type, got.Type)
require.Equal(t, env.SessionID, got.SessionID)
require.Equal(t, arcp.ProtocolVersion, got.ARCP)
v := arcp.NewPayloadForType(got.Type)
require.NotNil(t, v)
require.NoError(t, json.Unmarshal(got.Payload, v))
}
func TestEnvelopeValidate(t *testing.T) {
env := arcp.Envelope{}
require.Error(t, env.Validate())
env.ARCP = arcp.ProtocolVersion
require.Error(t, env.Validate())
env.ID = "x"
require.Error(t, env.Validate())
env.Type = "session.hello"
require.NoError(t, env.Validate())
}
func TestIntersectFeatures(t *testing.T) {
a := []string{"heartbeat", "ack", "subscribe"}
b := []string{"subscribe", "ack", "progress"}
got := arcp.IntersectFeatures(a, b)
require.Equal(t, []string{"ack", "subscribe"}, got)
}
func TestCodeAndIsRetryable(t *testing.T) {
require.Equal(t, arcp.CodeLeaseExpired, arcp.Code(arcp.ErrLeaseExpired))
require.False(t, arcp.IsRetryable(arcp.ErrLeaseExpired))
require.True(t, arcp.IsRetryable(arcp.ErrInternalError))
require.False(t, arcp.IsRetryable(arcp.ErrBudgetExhausted))
}
func TestParseBudgetAmount(t *testing.T) {
for _, tc := range []struct {
in string
ok bool
cur arcp.Currency
val float64
}{
{"USD:5.00", true, "USD", 5.00},
{"credits:1000", true, "credits", 1000},
{"USD:-1", false, "", 0},
{"USD:", false, "", 0},
{":1.00", false, "", 0},
} {
amt, err := arcp.ParseBudgetAmount(tc.in)
if !tc.ok {
require.Error(t, err, tc.in)
continue
}
require.NoError(t, err, tc.in)
require.Equal(t, tc.cur, amt.Currency)
require.InDelta(t, tc.val, amt.Value, 0.0001)
}
}