Files
ARTEX/third_party/norma/llm/nonstream_test.go
T
wangdefa f782b612ec build: 内置 norma 模块,构建不再依赖上游仓库
把 github.com/Autumn-27/norma v0.4.3(commit 320ae32)的源码复制到
third_party/norma,并用 go.mod 的 replace 指向本地副本。上游删除仓库或
改写 tag 都不再影响本仓库构建。

- 已验证 GOPROXY=off 下可完整编译并打出发行包
- go.sum 中该模块的两条校验记录随之删除(本地 replace 不需要哈希)
- 升级方法与授权注意事项见 third_party/README.md
2026-10-20 21:10:41 -07:00

117 lines
4.6 KiB
Go

package llm
import (
"context"
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
// TestOpenAICompleteNonStreaming verifies the real non-streaming path: the wire
// carries stream:false (and no stream_options), and a single JSON response is
// parsed into text + thinking + tool_use with usage and stop reason.
func TestOpenAICompleteNonStreaming(t *testing.T) {
var gotBody map[string]any
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/chat/completions" {
t.Errorf("path=%s", r.URL.Path)
}
raw, _ := io.ReadAll(r.Body)
_ = json.Unmarshal(raw, &gotBody)
w.Header().Set("content-type", "application/json")
w.Write([]byte(`{"choices":[{"message":{"content":"Hello","reasoning_content":"let me think","tool_calls":[{"id":"c1","type":"function","function":{"name":"Read","arguments":"{\"file_path\":\"a\"}"}}]},"finish_reason":"tool_calls"}],"usage":{"prompt_tokens":12,"completion_tokens":8,"prompt_tokens_details":{"cached_tokens":4}}}`))
}))
defer srv.Close()
p, _ := NewProvider(Config{Format: FormatOpenAI, BaseURL: srv.URL, APIKey: "k", Model: "m"})
msg, stop, usage, err := p.Complete(context.Background(), CompletionRequest{Messages: []Message{UserText("go")}})
if err != nil {
t.Fatalf("Complete err: %v", err)
}
// Wire must be non-streaming.
if gotBody["stream"] != false {
t.Fatalf("stream=%v, want false", gotBody["stream"])
}
if _, ok := gotBody["stream_options"]; ok {
t.Fatal("stream_options must be omitted on a non-streaming request")
}
if msg.Text() != "Hello" {
t.Fatalf("text=%q", msg.Text())
}
// reasoning_content → thinking block.
var thinking string
for _, b := range msg.Content {
if b.Type == BlockThinking {
thinking = b.Thinking
}
}
if thinking != "let me think" {
t.Fatalf("thinking=%q", thinking)
}
if u := msg.ToolUses(); len(u) != 1 || u[0].Name != "Read" || string(u[0].Input) != `{"file_path":"a"}` {
t.Fatalf("tooluse=%+v", u)
}
if stop != "tool_use" { // mapped from finish_reason "tool_calls"
t.Fatalf("stop=%q", stop)
}
if usage.InputTokens != 12 || usage.OutputTokens != 8 || usage.CacheReadTokens != 4 {
t.Fatalf("usage=%+v", usage)
}
}
// A 200 response whose body is an error object must surface as an error, not an
// empty completion — this is the failure mode that made flaky gateways look like
// a silently "completed" empty turn on the streaming path.
func TestOpenAICompleteErrorBody(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("content-type", "application/json")
w.Write([]byte(`{"error":{"message":"content filtered","type":"invalid_request_error"}}`))
}))
defer srv.Close()
p, _ := NewProvider(Config{Format: FormatOpenAI, BaseURL: srv.URL, APIKey: "k", Model: "m"})
_, _, _, err := p.Complete(context.Background(), CompletionRequest{Messages: []Message{UserText("go")}})
if err == nil || !strings.Contains(err.Error(), "content filtered") {
t.Fatalf("want error surfacing the body, got %v", err)
}
}
// TestAnthropicCompleteNonStreaming verifies the Messages API non-streaming path.
func TestAnthropicCompleteNonStreaming(t *testing.T) {
var gotBody map[string]any
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/v1/messages" {
t.Errorf("path=%s", r.URL.Path)
}
raw, _ := io.ReadAll(r.Body)
_ = json.Unmarshal(raw, &gotBody)
w.Header().Set("content-type", "application/json")
w.Write([]byte(`{"type":"message","content":[{"type":"thinking","thinking":"hmm","signature":"sig"},{"type":"text","text":"Hi"},{"type":"tool_use","id":"t1","name":"Bash","input":{"command":"ls"}}],"stop_reason":"tool_use","usage":{"input_tokens":10,"output_tokens":5,"cache_read_input_tokens":2}}`))
}))
defer srv.Close()
p, _ := NewProvider(Config{Format: FormatAnthropic, BaseURL: srv.URL, APIKey: "k", Model: "m", APIVersion: "2023-06-01"})
msg, stop, usage, err := p.Complete(context.Background(), CompletionRequest{Messages: []Message{UserText("go")}})
if err != nil {
t.Fatalf("Complete err: %v", err)
}
if gotBody["stream"] != false {
t.Fatalf("stream=%v, want false", gotBody["stream"])
}
if msg.Text() != "Hi" {
t.Fatalf("text=%q", msg.Text())
}
if u := msg.ToolUses(); len(u) != 1 || u[0].Name != "Bash" || string(u[0].Input) != `{"command":"ls"}` {
t.Fatalf("tooluse=%+v", u)
}
if stop != "tool_use" {
t.Fatalf("stop=%q", stop)
}
// InputTokens includes cache-read (OpenAI-style total input semantics).
if usage.InputTokens != 12 || usage.OutputTokens != 5 || usage.CacheReadTokens != 2 {
t.Fatalf("usage=%+v", usage)
}
}