-
Notifications
You must be signed in to change notification settings - Fork 119
Expand file tree
/
Copy pathserver_test.go
More file actions
112 lines (101 loc) · 3.13 KB
/
server_test.go
File metadata and controls
112 lines (101 loc) · 3.13 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
package httpapi_test
import (
"context"
"encoding/json"
"fmt"
"io"
"log/slog"
"net/http"
"net/http/httptest"
"os"
"sort"
"testing"
"github.com/coder/agentapi/lib/httpapi"
"github.com/coder/agentapi/lib/logctx"
"github.com/coder/agentapi/lib/msgfmt"
"github.com/stretchr/testify/require"
)
func normalizeSchema(t *testing.T, schema any) any {
t.Helper()
switch val := (schema).(type) {
case *any:
normalizeSchema(t, *val)
case []any:
for i := range val {
normalizeSchema(t, &val[i])
}
sort.SliceStable(val, func(i, j int) bool {
return fmt.Sprintf("%v", val[i]) < fmt.Sprintf("%v", val[j])
})
case map[string]any:
for k := range val {
valUnderKey := val[k]
normalizeSchema(t, &valUnderKey)
val[k] = valUnderKey
}
}
return schema
}
// Ensure the OpenAPI schema on disk is up to date.
// To update the schema, run `go run main.go server --print-openapi dummy > openapi.json`.
func TestOpenAPISchema(t *testing.T) {
t.Parallel()
ctx := logctx.WithLogger(context.Background(), slog.New(slog.NewTextHandler(os.Stdout, nil)))
srv := httpapi.NewServer(ctx, msgfmt.AgentTypeClaude, nil, 0, "/chat")
currentSchemaStr := srv.GetOpenAPI()
var currentSchema any
if err := json.Unmarshal([]byte(currentSchemaStr), ¤tSchema); err != nil {
t.Fatalf("failed to unmarshal current schema: %s", err)
}
diskSchemaFile, err := os.OpenFile("../../openapi.json", os.O_RDONLY, 0)
if err != nil {
t.Fatalf("failed to open disk schema: %s", err)
}
defer func() {
_ = diskSchemaFile.Close()
}()
diskSchemaBytes, err := io.ReadAll(diskSchemaFile)
if err != nil {
t.Fatalf("failed to read disk schema: %s", err)
}
var diskSchema any
if err := json.Unmarshal(diskSchemaBytes, &diskSchema); err != nil {
t.Fatalf("failed to unmarshal disk schema: %s", err)
}
normalizeSchema(t, ¤tSchema)
normalizeSchema(t, &diskSchema)
require.Equal(t, currentSchema, diskSchema)
}
func TestServer_redirectToChat(t *testing.T) {
cases := []struct {
name string
chatBasePath string
expectedResponseCode int
expectedLocation string
}{
{"default base path", "/chat", http.StatusTemporaryRedirect, "/chat/embed"},
{"custom base path", "/custom", http.StatusTemporaryRedirect, "/custom/embed"},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
tCtx := logctx.WithLogger(context.Background(), slog.New(slog.NewTextHandler(os.Stdout, nil)))
s := httpapi.NewServer(tCtx, msgfmt.AgentTypeClaude, nil, 0, tc.chatBasePath)
tsServer := httptest.NewServer(s.Handler())
t.Cleanup(tsServer.Close)
client := &http.Client{
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
},
}
resp, err := client.Get(tsServer.URL + "/")
require.NoError(t, err, "unexpected error making GET request")
t.Cleanup(func() {
_ = resp.Body.Close()
})
require.Equal(t, tc.expectedResponseCode, resp.StatusCode, "expected %d status code", tc.expectedResponseCode)
loc := resp.Header.Get("Location")
require.Equal(t, tc.expectedLocation, loc, "expected Location %q, got %q", tc.expectedLocation, loc)
})
}
}