-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient_test.go
More file actions
213 lines (202 loc) · 7.21 KB
/
Copy pathclient_test.go
File metadata and controls
213 lines (202 loc) · 7.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
package cursor
import (
"context"
"encoding/json"
"errors"
"net/http"
"net/http/httptest"
"testing"
)
func TestClientCreateAgentUsesBasicAuthAndJSON(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/v1/agents" {
t.Fatalf("unexpected path: %s", r.URL.Path)
}
if r.Method != http.MethodPost {
t.Fatalf("unexpected method: %s", r.Method)
}
user, pass, ok := r.BasicAuth()
if !ok || user != "test-key" || pass != "" {
t.Fatalf("unexpected basic auth: %q %q %v", user, pass, ok)
}
var request CreateAgentRequest
if err := json.NewDecoder(r.Body).Decode(&request); err != nil {
t.Fatal(err)
}
if request.Prompt.Text != "hello" {
t.Fatalf("unexpected prompt: %q", request.Prompt.Text)
}
if request.Model == nil || request.Model.ID != "composer-2" {
t.Fatalf("unexpected model: %#v", request.Model)
}
if len(request.Repos) != 1 || request.Repos[0].StartingRef != "main" {
t.Fatalf("unexpected repos: %#v", request.Repos)
}
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(`{
"agent":{"id":"bc-1","name":"hello","status":"ACTIVE","latestRunId":"run-1"},
"run":{"id":"run-1","agentId":"bc-1","status":"CREATING"}
}`))
}))
defer server.Close()
client, err := NewClient("test-key", WithBaseURL(server.URL))
if err != nil {
t.Fatal(err)
}
response, err := client.CreateAgent(context.Background(), CreateAgentRequest{
Prompt: PromptInput{Text: "hello"},
Model: &ModelSelection{ID: "composer-2"},
Repos: []Repo{{URL: "https://github.com/acme/repo", StartingRef: "main"}},
})
if err != nil {
t.Fatal(err)
}
if response.Agent.ID != "bc-1" || response.Run.ID != "run-1" {
t.Fatalf("unexpected response: %#v", response)
}
if !response.Run.Supports(RunOperationWait) {
t.Fatalf("returned run should support wait: %s", response.Run.UnsupportedReason(RunOperationWait))
}
}
func TestClientListAgentsQueryAndError(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if got := r.URL.Query().Get("limit"); got != "50" {
t.Fatalf("limit query = %q", got)
}
if got := r.URL.Query().Get("cursor"); got != "abc" {
t.Fatalf("cursor query = %q", got)
}
if got := r.URL.Query().Get("prUrl"); got != "https://github.com/acme/repo/pull/1" {
t.Fatalf("prUrl query = %q", got)
}
if got := r.URL.Query().Get("includeArchived"); got != "false" {
t.Fatalf("includeArchived query = %q", got)
}
http.Error(w, `{"error":"Too Many Requests","message":"slow down"}`, http.StatusTooManyRequests)
}))
defer server.Close()
client, err := NewClient("test-key", WithBaseURL(server.URL))
if err != nil {
t.Fatal(err)
}
_, err = client.ListAgents(context.Background(), ListAgentsOptions{
Limit: 50,
Cursor: "abc",
PRURL: "https://github.com/acme/repo/pull/1",
IncludeArchived: Bool(false),
})
var apiErr *APIError
if !errors.As(err, &apiErr) {
t.Fatalf("expected APIError, got %T: %v", err, err)
}
if apiErr.StatusCode != http.StatusTooManyRequests || apiErr.Code != "Too Many Requests" || apiErr.Message != "slow down" {
t.Fatalf("unexpected api error: %#v", apiErr)
}
if !apiErr.Retryable() {
t.Fatal("429 should be retryable")
}
}
func TestCreateSubTokenUsesBearerAuth(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if got := r.Header.Get("Authorization"); got != "Bearer service-key" {
t.Fatalf("authorization = %q", got)
}
var request CreateSubTokenRequest
if err := json.NewDecoder(r.Body).Decode(&request); err != nil {
t.Fatal(err)
}
if request.ForUserEmail != "alice@example.com" {
t.Fatalf("unexpected request: %#v", request)
}
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(`{"accessToken":"eyJ","expiresAt":"2026-04-24T19:00:00.000Z","userId":42,"teamId":456}`))
}))
defer server.Close()
client, err := NewClient("service-key", WithBaseURL(server.URL))
if err != nil {
t.Fatal(err)
}
token, err := client.CreateSubToken(context.Background(), CreateSubTokenRequest{ForUserEmail: "alice@example.com"})
if err != nil {
t.Fatal(err)
}
if token.AccessToken != "eyJ" || token.UserID != 42 {
t.Fatalf("unexpected token: %#v", token)
}
}
func TestListModelsAcceptsStringsAndObjects(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(`{"items":["gpt-5.2",{"id":"composer-2","displayName":"Composer"}]}`))
}))
defer server.Close()
client, err := NewClient("test-key", WithBaseURL(server.URL))
if err != nil {
t.Fatal(err)
}
models, err := client.ListModels(context.Background())
if err != nil {
t.Fatal(err)
}
if len(models) != 2 || models[0].ID != "gpt-5.2" || models[1].DisplayName != "Composer" {
t.Fatalf("unexpected models: %#v", models)
}
}
func TestArtifactsAndLifecycleEndpoints(t *testing.T) {
var lifecycle []string
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch {
case r.Method == http.MethodGet && r.URL.Path == "/v1/agents/bc-1/artifacts":
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(`{"items":[{"path":"artifacts/screenshot.png","sizeBytes":12345,"updatedAt":"2026-04-13T18:45:00.000Z"}]}`))
case r.Method == http.MethodGet && r.URL.Path == "/v1/agents/bc-1/artifacts/download":
if got := r.URL.Query().Get("path"); got != "artifacts/screenshot.png" {
t.Fatalf("artifact path query = %q", got)
}
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(`{"url":"https://example.com/signed","expiresAt":"2026-04-13T19:00:00.000Z"}`))
case r.Method == http.MethodPost && r.URL.Path == "/v1/agents/bc-1/archive":
lifecycle = append(lifecycle, "archive")
w.WriteHeader(http.StatusNoContent)
case r.Method == http.MethodPost && r.URL.Path == "/v1/agents/bc-1/unarchive":
lifecycle = append(lifecycle, "unarchive")
w.WriteHeader(http.StatusNoContent)
case r.Method == http.MethodDelete && r.URL.Path == "/v1/agents/bc-1":
lifecycle = append(lifecycle, "delete")
w.WriteHeader(http.StatusNoContent)
default:
t.Fatalf("unexpected request: %s %s", r.Method, r.URL.String())
}
}))
defer server.Close()
client, err := NewClient("test-key", WithBaseURL(server.URL))
if err != nil {
t.Fatal(err)
}
artifacts, err := client.ListArtifacts(context.Background(), "bc-1")
if err != nil {
t.Fatal(err)
}
if len(artifacts) != 1 || artifacts[0].SizeBytes != 12345 {
t.Fatalf("unexpected artifacts: %#v", artifacts)
}
download, err := client.DownloadArtifact(context.Background(), "bc-1", artifacts[0].Path)
if err != nil {
t.Fatal(err)
}
if download.URL != "https://example.com/signed" {
t.Fatalf("unexpected download: %#v", download)
}
if err := client.ArchiveAgent(context.Background(), "bc-1"); err != nil {
t.Fatal(err)
}
if err := client.UnarchiveAgent(context.Background(), "bc-1"); err != nil {
t.Fatal(err)
}
if err := client.DeleteAgent(context.Background(), "bc-1"); err != nil {
t.Fatal(err)
}
if got := len(lifecycle); got != 3 {
t.Fatalf("expected 3 lifecycle calls, got %d", got)
}
}