224 lines
10 KiB
Go
224 lines
10 KiB
Go
package chat
|
|
|
|
import (
|
|
"context"
|
|
"io"
|
|
"net/http"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/Tencent/WeKnora/internal/types"
|
|
)
|
|
|
|
const anthropicTestMessageStart = "data: {\"type\":\"message_start\",\"message\":{\"usage\":{\"input_tokens\":1,\"output_tokens\":0}}}\n\n"
|
|
const anthropicTestBlockStart0 = "data: {\"type\":\"content_block_start\",\"index\":0,\"content_block\":{\"type\":\"text\",\"text\":\"\"}}\n\n"
|
|
const anthropicTestBlockDelta0 = "data: {\"type\":\"content_block_delta\",\"index\":0,\"delta\":{\"type\":\"text_delta\",\"text\":\"x\"}}\n\n"
|
|
const anthropicTestBlockStop0 = "data: {\"type\":\"content_block_stop\",\"index\":0}\n\n"
|
|
|
|
func invalidAnthropicBlockPayloads() map[string]string {
|
|
return map[string]string{
|
|
"stop before start": anthropicTestMessageStart + anthropicTestBlockStop0,
|
|
"delta before start": anthropicTestMessageStart + anthropicTestBlockDelta0,
|
|
"delta after stop": anthropicTestMessageStart + anthropicTestBlockStart0 + anthropicTestBlockStop0 + anthropicTestBlockDelta0,
|
|
"duplicate start": anthropicTestMessageStart + anthropicTestBlockStart0 + anthropicTestBlockStart0,
|
|
"duplicate stop": anthropicTestMessageStart + anthropicTestBlockStart0 + anthropicTestBlockStop0 + anthropicTestBlockStop0,
|
|
"mismatched index": anthropicTestMessageStart + anthropicTestBlockStart0 + "data: {\"type\":\"content_block_delta\",\"index\":1,\"delta\":{\"type\":\"text_delta\",\"text\":\"x\"}}\n\n",
|
|
"index gap": anthropicTestMessageStart + "data: {\"type\":\"content_block_start\",\"index\":1,\"content_block\":{\"type\":\"text\"}}\n\n",
|
|
"concurrent block start": anthropicTestMessageStart + anthropicTestBlockStart0 + "data: {\"type\":\"content_block_start\",\"index\":1,\"content_block\":{\"type\":\"text\"}}\n\n",
|
|
"mismatched delta type": anthropicTestMessageStart + anthropicTestBlockStart0 + "data: {\"type\":\"content_block_delta\",\"index\":0,\"delta\":{\"type\":\"input_json_delta\"}}\n\n",
|
|
}
|
|
}
|
|
|
|
func TestAnthropicContentBlockLifecycleRejectedByBothParsers(t *testing.T) {
|
|
for name, payload := range invalidAnthropicBlockPayloads() {
|
|
t.Run(name+"/aggregate", func(t *testing.T) {
|
|
if _, err := parseAnthropicSSE(strings.NewReader(payload)); err == nil {
|
|
t.Fatal("aggregate parser accepted invalid content-block lifecycle")
|
|
}
|
|
})
|
|
t.Run(name+"/stream", func(t *testing.T) {
|
|
resp := &http.Response{Body: io.NopCloser(strings.NewReader(payload))}
|
|
stream := make(chan types.StreamResponse, 8)
|
|
go processAnthropicStream(context.Background(), "test", resp, stream)
|
|
sawError := false
|
|
for item := range stream {
|
|
if item.Done && item.ResponseType == types.ResponseTypeAnswer {
|
|
t.Fatalf("stream accepted invalid content-block lifecycle: %#v", item)
|
|
}
|
|
if item.Done && item.ResponseType == types.ResponseTypeError {
|
|
sawError = true
|
|
}
|
|
}
|
|
if !sawError {
|
|
t.Fatal("stream did not emit an error terminal")
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestAnthropicContentBlockLifecycleAcceptsSequentialBlocks(t *testing.T) {
|
|
payload := anthropicTestMessageStart + anthropicTestBlockStart0 + anthropicTestBlockDelta0 + anthropicTestBlockStop0 +
|
|
"data: {\"type\":\"content_block_start\",\"index\":1,\"content_block\":{\"type\":\"thinking\",\"thinking\":\"\"}}\n\n" +
|
|
"data: {\"type\":\"content_block_delta\",\"index\":1,\"delta\":{\"type\":\"thinking_delta\"}}\n\n" +
|
|
"data: {\"type\":\"content_block_stop\",\"index\":1}\n\n" +
|
|
"data: {\"type\":\"message_delta\",\"delta\":{\"stop_reason\":\"end_turn\"}}\n\n" +
|
|
"data: {\"type\":\"message_stop\"}\n\n"
|
|
response, err := parseAnthropicSSE(strings.NewReader(payload))
|
|
if err != nil || response.FinishReason != "end_turn" || response.Content != "x" {
|
|
t.Fatalf("valid sequential blocks rejected: response=%#v err=%v", response, err)
|
|
}
|
|
|
|
resp := &http.Response{Body: io.NopCloser(strings.NewReader(payload))}
|
|
stream := make(chan types.StreamResponse, 8)
|
|
go processAnthropicStream(context.Background(), "test", resp, stream)
|
|
successTerminals := 0
|
|
for item := range stream {
|
|
if item.Done && item.ResponseType == types.ResponseTypeError {
|
|
t.Fatalf("valid stream rejected: %#v", item)
|
|
}
|
|
if item.Done && item.ResponseType == types.ResponseTypeAnswer {
|
|
successTerminals++
|
|
}
|
|
}
|
|
if successTerminals != 1 {
|
|
t.Fatalf("success terminals=%d, want 1", successTerminals)
|
|
}
|
|
}
|
|
|
|
func TestAnthropicProtocolAcceptsOfficialStopReasons(t *testing.T) {
|
|
reasons := []string{"end_turn", "max_tokens", "stop_sequence", "tool_use", "pause_turn", "refusal", "model_context_window_exceeded"}
|
|
for _, reason := range reasons {
|
|
t.Run(reason, func(t *testing.T) {
|
|
var state anthropicStreamProtocolState
|
|
start := anthropicStreamEvent{Type: "message_start"}
|
|
if err := state.observe(&start); err != nil {
|
|
t.Fatalf("message_start: %v", err)
|
|
}
|
|
delta := anthropicStreamEvent{Type: "message_delta"}
|
|
delta.Delta = &struct {
|
|
Type string `json:"type"`
|
|
Text string `json:"text"`
|
|
StopReason string `json:"stop_reason"`
|
|
}{StopReason: reason}
|
|
if err := state.observe(&delta); err != nil {
|
|
t.Fatalf("message_delta: %v", err)
|
|
}
|
|
stop := anthropicStreamEvent{Type: "message_stop"}
|
|
if err := state.observe(&stop); err != nil || !state.complete() {
|
|
t.Fatalf("message_stop err=%v complete=%v", err, state.complete())
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestParseAnthropicSSERejectsPrematureEOF(t *testing.T) {
|
|
payload := anthropicTestMessageStart + anthropicTestBlockStart0 + "event: content_block_delta\n" +
|
|
"data: {\"type\":\"content_block_delta\",\"index\":0,\"delta\":{\"type\":\"text_delta\",\"text\":\"partial\"}}\n\n"
|
|
if _, err := parseAnthropicSSE(strings.NewReader(payload)); err == nil {
|
|
t.Fatal("expected premature Anthropic EOF to fail closed")
|
|
}
|
|
}
|
|
|
|
func TestParseAnthropicSSERejectsMalformedPacketAfterFinishReason(t *testing.T) {
|
|
payload := anthropicTestMessageStart + "event: message_delta\n" +
|
|
"data: {\"type\":\"message_delta\",\"delta\":{\"stop_reason\":\"end_turn\"}}\n\n" +
|
|
"event: broken\n" +
|
|
"data: {not-json}\n\n"
|
|
if _, err := parseAnthropicSSE(strings.NewReader(payload)); err == nil {
|
|
t.Fatal("expected trailing malformed packet to override finish reason")
|
|
}
|
|
}
|
|
|
|
func TestParseAnthropicSSERejectsInvalidOrOutOfOrderCompletion(t *testing.T) {
|
|
cases := map[string]string{
|
|
"invalid stop reason": anthropicTestMessageStart + "data: {\"type\":\"message_delta\",\"delta\":{\"stop_reason\":\"made_up\"}}\n\n" +
|
|
"data: {\"type\":\"message_stop\"}\n\n",
|
|
"message stop first": "data: {\"type\":\"message_stop\"}\n\n" +
|
|
"data: {\"type\":\"message_delta\",\"delta\":{\"stop_reason\":\"end_turn\"}}\n\n",
|
|
"stop reason wrong event": anthropicTestMessageStart + "data: {\"type\":\"content_block_delta\",\"delta\":{\"stop_reason\":\"end_turn\"}}\n\n" +
|
|
"data: {\"type\":\"message_stop\"}\n\n",
|
|
"empty provider error after completion": anthropicTestMessageStart + "data: {\"type\":\"message_delta\",\"delta\":{\"stop_reason\":\"end_turn\"}}\n\n" +
|
|
"data: {\"type\":\"message_stop\"}\n\n" +
|
|
"data: {\"type\":\"error\",\"error\":{\"type\":\"api_error\",\"message\":\"\"}}\n\n",
|
|
}
|
|
for name, payload := range cases {
|
|
t.Run(name, func(t *testing.T) {
|
|
if _, err := parseAnthropicSSE(strings.NewReader(payload)); err == nil {
|
|
t.Fatal("expected invalid Anthropic completion protocol to fail")
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestProcessAnthropicStreamRejectsPrematureDone(t *testing.T) {
|
|
payload := anthropicTestMessageStart + anthropicTestBlockStart0 + anthropicTestBlockDelta0 +
|
|
"data: [DONE]\n\n"
|
|
resp := &http.Response{Body: io.NopCloser(strings.NewReader(payload))}
|
|
stream := make(chan types.StreamResponse, 4)
|
|
go processAnthropicStream(context.Background(), "test", resp, stream)
|
|
for item := range stream {
|
|
if item.Done && item.ResponseType == types.ResponseTypeAnswer {
|
|
t.Fatalf("premature [DONE] produced success: %#v", item)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestProcessAnthropicStreamRejectsEmptyProviderErrorAfterCompletion(t *testing.T) {
|
|
payload := anthropicTestMessageStart + "data: {\"type\":\"message_delta\",\"delta\":{\"stop_reason\":\"end_turn\"}}\n\n" +
|
|
"data: {\"type\":\"message_stop\"}\n\n" +
|
|
"data: {\"type\":\"error\",\"error\":{\"type\":\"api_error\",\"message\":\"\"}}\n\n"
|
|
resp := &http.Response{Body: io.NopCloser(strings.NewReader(payload))}
|
|
stream := make(chan types.StreamResponse, 4)
|
|
go processAnthropicStream(context.Background(), "test", resp, stream)
|
|
for item := range stream {
|
|
if item.Done && item.ResponseType == types.ResponseTypeAnswer {
|
|
t.Fatalf("empty-message provider error was ignored: %#v", item)
|
|
}
|
|
if item.Done && item.ResponseType == types.ResponseTypeError {
|
|
return
|
|
}
|
|
}
|
|
t.Fatal("expected provider error terminal")
|
|
}
|
|
|
|
func TestProcessAnthropicStreamRejectsPrematureEOF(t *testing.T) {
|
|
payload := anthropicTestMessageStart + anthropicTestBlockStart0 + "event: content_block_delta\n" +
|
|
"data: {\"type\":\"content_block_delta\",\"index\":0,\"delta\":{\"type\":\"text_delta\",\"text\":\"partial\"}}\n\n"
|
|
resp := &http.Response{Body: io.NopCloser(strings.NewReader(payload))}
|
|
stream := make(chan types.StreamResponse, 4)
|
|
go processAnthropicStream(context.Background(), "test", resp, stream)
|
|
|
|
successTerminals, errorTerminals := 0, 0
|
|
for item := range stream {
|
|
if item.Done && item.ResponseType == types.ResponseTypeAnswer {
|
|
successTerminals++
|
|
}
|
|
if item.Done && item.ResponseType == types.ResponseTypeError {
|
|
errorTerminals++
|
|
}
|
|
}
|
|
if successTerminals != 0 || errorTerminals != 1 {
|
|
t.Fatalf("terminal counts success=%d error=%d; want 0/1", successTerminals, errorTerminals)
|
|
}
|
|
}
|
|
|
|
func TestProcessAnthropicStreamRejectsMalformedPacketAfterFinishReason(t *testing.T) {
|
|
payload := anthropicTestMessageStart + "event: message_delta\n" +
|
|
"data: {\"type\":\"message_delta\",\"delta\":{\"stop_reason\":\"end_turn\"}}\n\n" +
|
|
"event: broken\n" +
|
|
"data: {not-json}\n\n"
|
|
resp := &http.Response{Body: io.NopCloser(strings.NewReader(payload))}
|
|
stream := make(chan types.StreamResponse, 4)
|
|
go processAnthropicStream(context.Background(), "test", resp, stream)
|
|
|
|
for item := range stream {
|
|
if item.Done && item.ResponseType == types.ResponseTypeAnswer {
|
|
t.Fatalf("unexpected success terminal before trailing malformed packet: %#v", item)
|
|
}
|
|
if item.ResponseType == types.ResponseTypeError {
|
|
return
|
|
}
|
|
}
|
|
t.Fatal("expected terminal stream error")
|
|
}
|