-
Notifications
You must be signed in to change notification settings - Fork 394
Expand file tree
/
Copy pathhosted_agent_configs_test.go
More file actions
224 lines (192 loc) · 8.21 KB
/
Copy pathhosted_agent_configs_test.go
File metadata and controls
224 lines (192 loc) · 8.21 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
package godo
import (
"encoding/json"
"fmt"
"net/http"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
var hostedAgentConfig = HostedAgentConfig{
ID: "019fb39c-14d9-7080-933e-b9b90e25acda",
Name: "support-agent",
AgentSpecSchemaVersion: "agents.digitalocean.com/v1alpha1",
Manifest: json.RawMessage(`{"apiVersion":"agents.digitalocean.com/v1alpha1","kind":"Agent"}`),
ContentHash: "75803fef24dc731824ecd4a1853c76153c7d8503534092b94da3ca3f31a882f4",
CreatedBy: "user-1",
CreatedAt: Timestamp{Time: time.Date(2026, 8, 1, 12, 0, 0, 0, time.UTC)},
UpdatedAt: Timestamp{Time: time.Date(2026, 8, 1, 12, 0, 0, 0, time.UTC)},
Credentials: []HostedAgentConfigCredentialSlot{
{Name: "OPENAI_API_KEY", Source: "tenantSecret", Provider: "openai"},
},
}
const hostedAgentConfigJSON = `
{
"id": "019fb39c-14d9-7080-933e-b9b90e25acda",
"name": "support-agent",
"agentspec_schema_version": "agents.digitalocean.com/v1alpha1",
"manifest": {"apiVersion":"agents.digitalocean.com/v1alpha1","kind":"Agent"},
"content_hash": "75803fef24dc731824ecd4a1853c76153c7d8503534092b94da3ca3f31a882f4",
"created_by": "user-1",
"created_at": "2026-08-01T12:00:00Z",
"updated_at": "2026-08-01T12:00:00Z",
"credentials": [
{"name":"OPENAI_API_KEY","source":"tenantSecret","provider":"openai"}
]
}
`
func TestHostedAgents_ListAgentConfigs(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v2/agents/configs", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
assert.Equal(t, "10", r.URL.Query().Get("page_size"))
fmt.Fprint(w, `{"configs":[{"id":"019fb39c-14d9-7080-933e-b9b90e25acda","name":"support-agent","agentspec_schema_version":"agents.digitalocean.com/v1alpha1","content_hash":"75803fef24dc731824ecd4a1853c76153c7d8503534092b94da3ca3f31a882f4","created_by":"user-1","created_at":"2026-08-01T12:00:00Z","updated_at":"2026-08-01T12:00:00Z"}],"next_page_token":"abc"}`)
})
got, resp, err := client.HostedAgents.ListAgentConfigs(ctx, &HostedAgentConfigListOptions{PageSize: 10})
require.NoError(t, err)
assert.Equal(t, http.StatusOK, resp.StatusCode)
require.Len(t, got.Configs, 1)
assert.Equal(t, "support-agent", got.Configs[0].Name)
assert.Equal(t, "abc", got.NextPageToken)
}
func TestHostedAgents_GetAgentConfig(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v2/agents/configs/019fb39c-14d9-7080-933e-b9b90e25acda", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
fmt.Fprintf(w, `{"config":%s}`, hostedAgentConfigJSON)
})
got, resp, err := client.HostedAgents.GetAgentConfig(ctx, hostedAgentConfig.ID)
require.NoError(t, err)
assert.Equal(t, http.StatusOK, resp.StatusCode)
assert.Equal(t, hostedAgentConfig.ID, got.ID)
assert.Equal(t, hostedAgentConfig.Name, got.Name)
require.Len(t, got.Credentials, 1)
assert.Equal(t, "OPENAI_API_KEY", got.Credentials[0].Name)
assert.Equal(t, "tenantSecret", got.Credentials[0].Source)
assert.NotContains(t, string(mustMarshal(t, got)), "plaintext")
assert.NotContains(t, string(mustMarshal(t, got)), `"configured"`)
}
func TestHostedAgents_CreateEnvironment(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v2/agents/configs", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodPost)
var body HostedEnvironmentCreateRequest
require.NoError(t, json.NewDecoder(r.Body).Decode(&body))
assert.Equal(t, "support-agent", body.Name)
assert.Contains(t, body.ManifestYAML, "agent:")
w.WriteHeader(http.StatusCreated)
fmt.Fprintf(w, `{"config":%s}`, hostedAgentConfigJSON)
})
got, resp, err := client.HostedAgents.CreateEnvironment(ctx, &HostedEnvironmentCreateRequest{
Name: "support-agent",
ManifestYAML: "name: support-agent\n" +
"agent: claude-code\n" +
"secrets:\n" +
" ANTHROPIC_API_KEY: sk-test\n",
})
require.NoError(t, err)
assert.Equal(t, http.StatusCreated, resp.StatusCode)
assert.Equal(t, hostedAgentConfig.ID, got.ID)
}
func TestHostedAgents_ListEnvironments(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v2/agents/configs", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
fmt.Fprint(w, `{"configs":[{"id":"019fb39c-14d9-7080-933e-b9b90e25acda","name":"support-agent","agentspec_schema_version":"agents.digitalocean.com/v1alpha1","content_hash":"75803fef24dc731824ecd4a1853c76153c7d8503534092b94da3ca3f31a882f4","created_by":"user-1","created_at":"2026-08-01T12:00:00Z","updated_at":"2026-08-01T12:00:00Z"}],"next_page_token":""}`)
})
got, resp, err := client.HostedAgents.ListEnvironments(ctx, &HostedEnvironmentListOptions{PageSize: 5})
require.NoError(t, err)
assert.Equal(t, http.StatusOK, resp.StatusCode)
require.Len(t, got.Configs, 1)
assert.Equal(t, "support-agent", got.Configs[0].Name)
}
func TestHostedAgents_GetEnvironment(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v2/agents/configs/019fb39c-14d9-7080-933e-b9b90e25acda", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
fmt.Fprintf(w, `{"config":%s}`, hostedAgentConfigJSON)
})
got, resp, err := client.HostedAgents.GetEnvironment(ctx, hostedAgentConfig.ID)
require.NoError(t, err)
assert.Equal(t, http.StatusOK, resp.StatusCode)
assert.Equal(t, hostedAgentConfig.ID, got.ID)
}
func TestHostedAgents_DeleteEnvironment(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v2/agents/configs/019fb39c-14d9-7080-933e-b9b90e25acda", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodDelete)
w.WriteHeader(http.StatusNoContent)
})
resp, err := client.HostedAgents.DeleteEnvironment(ctx, hostedAgentConfig.ID)
require.NoError(t, err)
assert.Equal(t, http.StatusNoContent, resp.StatusCode)
}
func TestHostedAgents_CreateAgentConfig(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v2/agents/configs", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodPost)
var body HostedAgentConfigCreateRequest
require.NoError(t, json.NewDecoder(r.Body).Decode(&body))
assert.Equal(t, "support-agent", body.Name)
assert.Contains(t, body.ManifestYAML, "apiVersion:")
// Credentials are declared inline in the manifest under spec.secrets;
// there is no separate secrets field on the request body anymore.
assert.Contains(t, body.ManifestYAML, "spec:")
w.WriteHeader(http.StatusCreated)
fmt.Fprintf(w, `{"config":%s}`, hostedAgentConfigJSON)
})
got, resp, err := client.HostedAgents.CreateAgentConfig(ctx, &HostedAgentConfigCreateRequest{
Name: "support-agent",
ManifestYAML: "apiVersion: agents.digitalocean.com/v1alpha1\n" +
"kind: Agent\n" +
"spec:\n" +
" secrets:\n" +
" - name: OPENAI_API_KEY\n" +
" source: tenantSecret\n" +
" value: sk-test\n",
})
require.NoError(t, err)
assert.Equal(t, http.StatusCreated, resp.StatusCode)
assert.Equal(t, hostedAgentConfig.ID, got.ID)
}
func TestHostedAgents_DeleteAgentConfig(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v2/agents/configs/019fb39c-14d9-7080-933e-b9b90e25acda", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodDelete)
w.WriteHeader(http.StatusNoContent)
})
resp, err := client.HostedAgents.DeleteAgentConfig(ctx, hostedAgentConfig.ID)
require.NoError(t, err)
assert.Equal(t, http.StatusNoContent, resp.StatusCode)
}
func TestHostedAgents_ListAgentConfigSessions(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v2/agents/configs/019fb39c-14d9-7080-933e-b9b90e25acda/sessions", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
assert.Equal(t, "SESSION_STATUS_READY", r.URL.Query().Get("status"))
fmt.Fprintf(w, `{"sessions":[%s],"next_page_token":""}`, hostedAgentSessionJSON)
})
got, resp, err := client.HostedAgents.ListAgentConfigSessions(ctx, hostedAgentConfig.ID, &HostedAgentSessionListOptions{
Status: HostedAgentSessionStatusReady,
})
require.NoError(t, err)
assert.Equal(t, http.StatusOK, resp.StatusCode)
require.Len(t, got.Sessions, 1)
assert.Equal(t, "sess-abc123", got.Sessions[0].SessionID)
}
func mustMarshal(t *testing.T, v any) []byte {
t.Helper()
b, err := json.Marshal(v)
require.NoError(t, err)
return b
}