-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient_test.go
More file actions
198 lines (181 loc) · 5.6 KB
/
client_test.go
File metadata and controls
198 lines (181 loc) · 5.6 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
package hcs11
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
func TestProfileBuilders(t *testing.T) {
agentBuilder := NewAgentBuilder().
SetName("Support Agent").
SetType(AIAgentTypeAutonomous).
SetCapabilities([]AIAgentCapability{AIAgentCapabilityTextGeneration}).
SetModel("gpt-4o")
agentProfile, err := agentBuilder.Build()
if err != nil {
t.Fatalf("agent builder failed: %v", err)
}
if agentProfile.Type != ProfileTypeAIAgent {
t.Fatalf("unexpected agent profile type: %d", agentProfile.Type)
}
personProfile, err := NewPersonBuilder().
SetName("Jane Doe").
SetBio("profile").Build()
if err != nil {
t.Fatalf("person builder failed: %v", err)
}
if personProfile.Type != ProfileTypePersonal {
t.Fatalf("unexpected person profile type: %d", personProfile.Type)
}
floraProfile, err := NewFloraBuilder().
SetDisplayName("Flora").
SetMembers([]FloraMember{{AccountID: "0.0.1001"}}).
SetThreshold(1).
SetTopics(FloraTopics{
Communication: "0.0.2001",
Transaction: "0.0.2002",
State: "0.0.2003",
}).
Build()
if err != nil {
t.Fatalf("flora builder failed: %v", err)
}
if floraProfile.Type != ProfileTypeFlora {
t.Fatalf("unexpected flora profile type: %d", floraProfile.Type)
}
}
func TestClientValidateAndSerializeProfile(t *testing.T) {
client, err := NewClient(ClientConfig{Network: "testnet"})
if err != nil {
t.Fatalf("failed to initialize client: %v", err)
}
profile, err := client.CreateAIAgentProfile(
"Agent",
AIAgentTypeManual,
[]AIAgentCapability{AIAgentCapabilityTextGeneration},
"gpt-4o",
nil,
)
if err != nil {
t.Fatalf("failed to create profile: %v", err)
}
validation := client.ValidateProfile(profile)
if !validation.Valid {
t.Fatalf("profile should be valid: %+v", validation)
}
encoded, err := client.ProfileToJSONString(profile)
if err != nil {
t.Fatalf("profile serialization failed: %v", err)
}
decoded, err := client.ParseProfileFromString(encoded)
if err != nil {
t.Fatalf("profile parse failed: %v", err)
}
if decoded.DisplayName != "Agent" {
t.Fatalf("unexpected decoded display name: %s", decoded.DisplayName)
}
}
func TestClientFetchProfileByAccountID(t *testing.T) {
profile := HCS11Profile{
Version: "1.0",
Type: ProfileTypeAIAgent,
DisplayName: "Agent",
InboundTopicID: "0.0.1001",
OutboundTopicID: "0.0.1002",
AIAgent: &AIAgentDetails{
Type: AIAgentTypeManual,
Capabilities: []AIAgentCapability{AIAgentCapabilityTextGeneration},
Model: "gpt-4o",
},
}
profileBytes, err := json.Marshal(profile)
if err != nil {
t.Fatalf("failed to marshal profile: %v", err)
}
server := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
switch {
case strings.HasPrefix(request.URL.Path, "/api/v1/accounts/"):
_, _ = writer.Write([]byte(`{"account":"0.0.1234","memo":"hcs-11:hcs://1/0.0.987654"}`))
case strings.HasPrefix(request.URL.Path, "/api/inscription-cdn/0.0.987654"):
_, _ = writer.Write(profileBytes)
default:
writer.WriteHeader(http.StatusNotFound)
}
}))
defer server.Close()
client, err := NewClient(ClientConfig{
Network: "testnet",
MirrorBaseURL: server.URL,
InscriberBaseURL: server.URL,
})
if err != nil {
t.Fatalf("failed to initialize client: %v", err)
}
response, err := client.FetchProfileByAccountID(context.Background(), "0.0.1234", "testnet")
if err != nil {
t.Fatalf("FetchProfileByAccountID failed: %v", err)
}
if !response.Success {
t.Fatalf("fetch failed: %s", response.Error)
}
if response.Profile == nil || response.Profile.DisplayName != "Agent" {
t.Fatalf("unexpected fetched profile: %+v", response.Profile)
}
if response.TopicInfo == nil || response.TopicInfo.ProfileTopicID != "0.0.987654" {
t.Fatalf("unexpected topic info: %+v", response.TopicInfo)
}
}
func TestClientGetCapabilitiesFromTags(t *testing.T) {
client, err := NewClient(ClientConfig{Network: "testnet"})
if err != nil {
t.Fatalf("failed to initialize client: %v", err)
}
capabilities := client.GetCapabilitiesFromTags([]string{"text_generation"})
if len(capabilities) != 1 || capabilities[0] != int(AIAgentCapabilityTextGeneration) {
t.Fatalf("unexpected capabilities: %+v", capabilities)
}
}
func TestClientGetCapabilitiesFromTagsExpanded(t *testing.T) {
client, err := NewClient(ClientConfig{Network: "testnet"})
if err != nil {
t.Fatalf("failed to initialize client: %v", err)
}
capabilities := client.GetCapabilitiesFromTags([]string{
"image_generation",
"workflow_automation",
"unknown",
})
if len(capabilities) != 2 {
t.Fatalf("expected two mapped capabilities, got %+v", capabilities)
}
if capabilities[0] != int(AIAgentCapabilityImageGeneration) {
t.Fatalf("expected image generation capability, got %d", capabilities[0])
}
if capabilities[1] != int(AIAgentCapabilityWorkflowAutomation) {
t.Fatalf("expected workflow automation capability, got %d", capabilities[1])
}
}
func TestClientValidateProfileRejectsInvalidMCPTransport(t *testing.T) {
client, err := NewClient(ClientConfig{Network: "testnet"})
if err != nil {
t.Fatalf("failed to initialize client: %v", err)
}
profile, createErr := client.CreateMCPServerProfile(
"MCP",
MCPServerDetails{
Version: "2026-01-01",
ConnectionInfo: MCPServerConnectionInfo{
URL: "https://example.com",
Transport: "invalid",
},
Services: []MCPServerCapability{MCPServerCapabilityToolProvider},
Description: "server",
},
nil,
)
if createErr == nil {
t.Fatalf("expected profile creation to fail due to invalid transport, got profile %+v", profile)
}
}