Files
coder/coderd/x/chatd/chaterror/message_test.go
T
Ethan 46a60e6d5d refactor: move chat error kinds into codersdk (#24955)
Moves the chat error kind taxonomy from `coderd/x/chatd/chaterror` into
`codersdk.ChatErrorKind` and types `ChatError.Kind` /
`ChatStreamRetry.Kind` so generated TypeScript exposes an SDK-owned
union, including `usage_limit`. Backend chat classification now
references the SDK constants directly while preserving the existing JSON
string values.

Keeps chat usage-limit admission failures on their existing 409 response
shape. The frontend maps structured usage-limit responses to the
SDK-owned `usage_limit` kind, uses generated `TypesGen.ChatErrorKind`
directly, and removes the local string union and alias.
2026-05-06 11:57:48 +10:00

101 lines
2.6 KiB
Go

package chaterror_test
import (
"testing"
"github.com/stretchr/testify/require"
"golang.org/x/xerrors"
"github.com/coder/coder/v2/coderd/x/chatd/chaterror"
"github.com/coder/coder/v2/codersdk"
)
// TestTerminalMessage covers the per-provider "temporarily
// unavailable" copy, the startup-timeout copy, and the generic
// fallback string for its intended (unclassified, non-retryable)
// path.
func TestTerminalMessage(t *testing.T) {
t.Parallel()
tests := []struct {
name string
kind codersdk.ChatErrorKind
provider string
retryable bool
statusCode int
want string
}{
{
name: "Timeout_Retryable_Anthropic",
kind: codersdk.ChatErrorKindTimeout,
provider: "anthropic",
retryable: true,
want: "Anthropic is temporarily unavailable.",
},
{
name: "Timeout_Retryable_OpenAI",
kind: codersdk.ChatErrorKindTimeout,
provider: "openai",
retryable: true,
want: "OpenAI is temporarily unavailable.",
},
{
name: "Timeout_Retryable_UnknownProvider",
kind: codersdk.ChatErrorKindTimeout,
provider: "",
retryable: true,
want: "The AI provider is temporarily unavailable.",
},
{
name: "Timeout_NotRetryable_NoStatus",
kind: codersdk.ChatErrorKindTimeout,
provider: "",
retryable: false,
want: "The request timed out before it completed.",
},
{
name: "StartupTimeout_Anthropic",
kind: codersdk.ChatErrorKindStartupTimeout,
provider: "anthropic",
retryable: true,
want: "Anthropic did not start responding in time.",
},
{
name: "StartupTimeout_OpenAI",
kind: codersdk.ChatErrorKindStartupTimeout,
provider: "openai",
retryable: true,
want: "OpenAI did not start responding in time.",
},
{
// Generic fallback reserved for genuinely
// unclassified non-retryable failures.
name: "Generic_NotRetryable_NoStatus",
kind: codersdk.ChatErrorKindGeneric,
provider: "",
retryable: false,
want: "The chat request failed unexpectedly.",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
classified := chaterror.ClassifiedError{
Kind: tt.kind,
Provider: tt.provider,
Retryable: tt.retryable,
StatusCode: tt.statusCode,
}
// terminalMessage is unexported; round-trip through
// WithClassification + Classify to exercise it.
wrapped := chaterror.WithClassification(
xerrors.New(tt.name),
classified,
)
require.Equal(t, tt.want, chaterror.Classify(wrapped).Message)
})
}
}