Skip to content

Commit a2ec32d

Browse files
committed
Add test for noop edit
Signed-off-by: David Gageot <david.gageot@docker.com>
1 parent 4dcb38d commit a2ec32d

1 file changed

Lines changed: 47 additions & 6 deletions

File tree

pkg/server/server_test.go

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"github.com/docker/cagent/pkg/api"
1919
"github.com/docker/cagent/pkg/config"
2020
latest "github.com/docker/cagent/pkg/config/v2"
21+
v2 "github.com/docker/cagent/pkg/config/v2"
2122
"github.com/docker/cagent/pkg/session"
2223
)
2324

@@ -113,6 +114,27 @@ func TestServer_GetSetYaml(t *testing.T) {
113114
assert.Equal(t, []byte(`version: "2"`), httpGET(t, ctx, lnPath, url))
114115
}
115116

117+
func TestServer_Edit_Noop(t *testing.T) {
118+
// t.Parallel()
119+
t.Setenv("OPENAI_API_KEY", "dummy")
120+
121+
ctx := t.Context()
122+
lnPath := startServer(t, ctx, prepareAgentsDir(t, "pirate.yaml"))
123+
124+
edit := api.EditAgentConfigRequest{
125+
Filename: "pirate.yaml",
126+
AgentConfig: v2.Config{},
127+
}
128+
httpPUT(t, ctx, lnPath, "/api/agents/config", edit)
129+
130+
buf := httpGET(t, ctx, lnPath, "/api/agents/pirate.yaml")
131+
var cfg latest.Config
132+
unmarshal(t, buf, &cfg)
133+
assert.NotEmpty(t, cfg.Version)
134+
require.NotEmpty(t, cfg.Agents)
135+
assert.Contains(t, cfg.Agents["root"].Instruction, "pirate")
136+
}
137+
116138
func TestServer_ListSessions(t *testing.T) {
117139
t.Parallel()
118140

@@ -169,20 +191,39 @@ func startServer(t *testing.T, ctx context.Context, agentsDir string) string {
169191

170192
func httpGET(t *testing.T, ctx context.Context, socketPath, path string) []byte {
171193
t.Helper()
172-
return httpDo(t, ctx, http.MethodGet, socketPath, path, http.NoBody)
194+
return httpDo(t, ctx, http.MethodGet, socketPath, path, nil)
173195
}
174196

175-
func httpPUT(t *testing.T, ctx context.Context, socketPath, path string, payload []byte) {
197+
func httpPUT(t *testing.T, ctx context.Context, socketPath, path string, payload any) []byte {
176198
t.Helper()
177-
httpDo(t, ctx, http.MethodPut, socketPath, path, bytes.NewReader(payload))
199+
return httpDo(t, ctx, http.MethodPut, socketPath, path, payload)
178200
}
179201

180-
func httpDo(t *testing.T, ctx context.Context, method, socketPath, path string, payload io.Reader) []byte {
202+
func httpDo(t *testing.T, ctx context.Context, method, socketPath, path string, payload any) []byte {
181203
t.Helper()
182204

183-
req, err := http.NewRequestWithContext(ctx, method, "http://_"+path, payload)
205+
var body io.Reader
206+
var contentType string
207+
if payload == nil {
208+
body = http.NoBody
209+
} else if text, ok := payload.([]byte); ok {
210+
body = bytes.NewReader(text)
211+
} else if text, ok := payload.(string); ok {
212+
body = strings.NewReader(text)
213+
} else {
214+
buf, err := json.Marshal(payload)
215+
require.NoError(t, err)
216+
body = bytes.NewReader(buf)
217+
contentType = "application/json"
218+
}
219+
220+
req, err := http.NewRequestWithContext(ctx, method, "http://_"+path, body)
184221
require.NoError(t, err)
185222

223+
if contentType != "" {
224+
req.Header.Set("Content-Type", contentType)
225+
}
226+
186227
client := &http.Client{
187228
Transport: &http.Transport{
188229
DialContext: func(ctx context.Context, _, _ string) (net.Conn, error) {
@@ -193,12 +234,12 @@ func httpDo(t *testing.T, ctx context.Context, method, socketPath, path string,
193234
}
194235
resp, err := client.Do(req)
195236
require.NoError(t, err)
196-
require.Less(t, resp.StatusCode, 400)
197237
defer resp.Body.Close()
198238

199239
buf, err := io.ReadAll(resp.Body)
200240
require.NoError(t, err)
201241

242+
require.Less(t, resp.StatusCode, 400, string(buf))
202243
return buf
203244
}
204245

0 commit comments

Comments
 (0)