No test reached the "handler returned both streaming and non-streaming
responses" and "handler returned empty response" branches of the
chattest fakes, and the next PR in the stack rewrites them. This PR adds
`TestFakeResponseShapeErrors`, which posts to the OpenAI
`/chat/completions`, OpenAI `/responses` and Anthropic `/v1/messages`
fakes with a handler that returns both response shapes or neither, and
checks the 500 status and the exact error body. It is the test-first
layer for that rewrite, so it adds 77 lines and deletes none.
**Why it is safe**
- Test only, and it passes on `main`.
- I deleted each of the six cases in turn with `go test -overlay`. Each
mutant fails exactly its own row, and the rest of the package still
passes on all six, so no other test covered these branches.
**Review focus**
The test sends plain `net/http` requests with a `{}` body, so it asserts
the fake's own output (`http.Error` writes the message and a newline)
with no provider SDK in between.
**Validation**
- `go test ./coderd/x/chatd/chattest/ -run TestFakeResponseShapeErrors
-count=1 -v`: all six rows pass
- `go vet ./coderd/x/chatd/chattest/`: ok
- `golangci-lint run --fix=false ./coderd/x/chatd/chattest/`: 0 issues
- pre-commit hook: passed
**Stack**
The stack makes the chattest fake tests fail on wrong output, then drops
the fakes' write-only state and shares their response-shape check.
1. #30948 table the OpenAI fake positive tests and fail on error streams
2. #30949 pin the fake response-shape errors (this PR)
3. #30950 drop write-only fake state and share the shape check
> Xum, an AI agent, opened this PR for @ibetitsmike and wrote this
description.
78 lines
2.2 KiB
Go
78 lines
2.2 KiB
Go
package chattest_test
|
|
|
|
import (
|
|
"io"
|
|
"net/http"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/coder/coder/v2/coderd/x/chatd/chattest"
|
|
"github.com/coder/coder/v2/testutil"
|
|
)
|
|
|
|
func TestFakeResponseShapeErrors(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
openAI := func(path string) func(t *testing.T, both bool) string {
|
|
return func(t *testing.T, both bool) string {
|
|
return chattest.NewOpenAI(t, func(*chattest.OpenAIRequest) chattest.OpenAIResponse {
|
|
if !both {
|
|
return chattest.OpenAIResponse{}
|
|
}
|
|
resp := chattest.OpenAINonStreamingResponse("")
|
|
resp.StreamingChunks = chattest.OpenAIStreamingResponse().StreamingChunks
|
|
return resp
|
|
}) + path
|
|
}
|
|
}
|
|
servers := []struct {
|
|
name string
|
|
start func(t *testing.T, both bool) string
|
|
}{
|
|
{name: "OpenAICompletions", start: openAI("/chat/completions")},
|
|
{name: "OpenAIResponses", start: openAI("/responses")},
|
|
{name: "Anthropic", start: func(t *testing.T, both bool) string {
|
|
return chattest.NewAnthropic(t, func(*chattest.AnthropicRequest) chattest.AnthropicResponse {
|
|
if !both {
|
|
return chattest.AnthropicResponse{}
|
|
}
|
|
resp := chattest.AnthropicNonStreamingResponse("")
|
|
resp.StreamingChunks = chattest.AnthropicStreamingResponse().StreamingChunks
|
|
return resp
|
|
}) + "/v1/messages"
|
|
}},
|
|
}
|
|
shapes := []struct {
|
|
name string
|
|
both bool
|
|
wantMsg string
|
|
}{
|
|
{name: "Both", both: true, wantMsg: "handler returned both streaming and non-streaming responses"},
|
|
{name: "Empty", wantMsg: "handler returned empty response"},
|
|
}
|
|
|
|
for _, server := range servers {
|
|
t.Run(server.name, func(t *testing.T) {
|
|
t.Parallel()
|
|
for _, shape := range shapes {
|
|
t.Run(shape.name, func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
ctx := testutil.Context(t, testutil.WaitShort)
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodPost, server.start(t, shape.both), strings.NewReader("{}"))
|
|
require.NoError(t, err)
|
|
resp, err := http.DefaultClient.Do(req)
|
|
require.NoError(t, err)
|
|
defer resp.Body.Close()
|
|
body, err := io.ReadAll(resp.Body)
|
|
require.NoError(t, err)
|
|
require.Equal(t, http.StatusInternalServerError, resp.StatusCode)
|
|
require.Equal(t, shape.wantMsg+"\n", string(body))
|
|
})
|
|
}
|
|
})
|
|
}
|
|
}
|