Files
Cyrene_v2/internal/runtime/embedding/api_test.go
T
AskaEth 66dda084cd fix(runtime): 上游错误体统一走 trace.UpstreamError,正文不进 error
M41 的「LLM / embedding / 视觉 上游错误体」一族(M38 #5 #6 #7 #8 #9 #14):
非 2xx 时把响应体截断后拼进 error,而 error 会被 chat run / loop step /
retrieve_fallback 打日志,等于把她的提问、记忆正文、要念的台词写进日志。

- model:openai/anthropic 两处 upstreamError 收口到 upstream.go 的
  providerUpstreamError;anthropic 流内 error 事件的 evt.Error.Message
  (原本无上限)整段丢弃,只保留令牌形状的 type
- embedding:readErrorBody 删除,改走 helper(input 就是她的记忆正文)
- vision:错误路径不再沿用取图的 50 MiB 上限,改为 4 KiB 有界读且不拼原文
- voice:classifyDashscopeStatus 只解 code/request_id,正文只留在内存里做
  分类判据;错误体读取同样改为 4 KiB 有界
- 测试:五个上游各一条「日志/error 里搜不到哨兵正文」用例,且能变异打红
2026-10-26 13:44:32 +08:00

233 lines
6.7 KiB
Go

package embedding
import (
"context"
"encoding/json"
"errors"
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
"git.yeij.top/AskaEth/Cyrene_v2/internal/config"
)
// roundTripFunc adapts a function to http.RoundTripper, letting tests inject a
// mock transport without starting a network server.
type roundTripFunc func(*http.Request) (*http.Response, error)
func (f roundTripFunc) RoundTrip(r *http.Request) (*http.Response, error) { return f(r) }
func TestAPIEmbedder_EmbedsFromServer(t *testing.T) {
t.Parallel()
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
t.Errorf("method = %s, want POST", r.Method)
}
if got := r.Header.Get("Content-Type"); got != "application/json" {
t.Errorf("Content-Type = %q, want application/json", got)
}
if got := r.Header.Get("Authorization"); got != "Bearer test-key" {
t.Errorf("Authorization = %q, want Bearer test-key", got)
}
var req openAICompatibleRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
t.Errorf("decode request: %v", err)
w.WriteHeader(http.StatusBadRequest)
return
}
if req.Model != "text-embedding-3-small" {
t.Errorf("request model = %q, want text-embedding-3-small", req.Model)
}
vec := make([]float32, 4)
for i := range vec {
vec[i] = float32(i) + 0.1
}
resp := openAICompatibleResponse{Data: make([]openAICompatibleDatum, 0, len(req.Input))}
for i := range req.Input {
resp.Data = append(resp.Data, openAICompatibleDatum{Index: i, Embedding: vec})
}
_ = json.NewEncoder(w).Encode(resp)
}))
defer srv.Close()
cfg := config.EmbeddingConfig{
APIURL: srv.URL + "/v1/embeddings",
APIKey: "test-key",
Model: "text-embedding-3-small",
}
emb := NewAPI(cfg)
vectors, err := emb.Embed(context.Background(), []string{"hello", "world"})
if err != nil {
t.Fatalf("Embed() error: %v", err)
}
if len(vectors) != 2 {
t.Fatalf("got %d vectors, want 2", len(vectors))
}
for i, v := range vectors {
if len(v) != 4 {
t.Errorf("vector %d length = %d, want 4", i, len(v))
}
for j, x := range v {
if want := float32(j) + 0.1; x != want {
t.Errorf("vector %d[%d] = %v, want %v", i, j, x, want)
}
}
}
if emb.Dim() != 4 {
t.Errorf("Dim() = %d, want 4", emb.Dim())
}
}
func TestAPIEmbedder_RoundTripperInjection(t *testing.T) {
t.Parallel()
var seenPath string
rt := roundTripFunc(func(r *http.Request) (*http.Response, error) {
seenPath = r.URL.Path
body := `{"data":[{"index":0,"embedding":[0.1,0.2,0.3]}]}`
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(strings.NewReader(body)),
Header: make(http.Header),
}, nil
})
emb := NewAPI(
config.EmbeddingConfig{APIURL: "http://example.com/v1/embeddings"},
WithRoundTripper(rt),
)
vectors, err := emb.Embed(context.Background(), []string{"hi"})
if err != nil {
t.Fatalf("Embed() error: %v", err)
}
if seenPath != "/v1/embeddings" {
t.Errorf("request path = %q, want /v1/embeddings", seenPath)
}
if len(vectors) != 1 || len(vectors[0]) != 3 {
t.Fatalf("vectors = %v, want 1 vector of length 3", vectors)
}
}
func TestAPIEmbedder_Non200Errors(t *testing.T) {
t.Parallel()
rt := roundTripFunc(func(r *http.Request) (*http.Response, error) {
return &http.Response{
StatusCode: http.StatusInternalServerError,
Body: io.NopCloser(strings.NewReader("server exploded")),
Header: make(http.Header),
}, nil
})
emb := NewAPI(
config.EmbeddingConfig{APIURL: "http://example.com/v1/embeddings"},
WithRoundTripper(rt),
)
if _, err := emb.Embed(context.Background(), []string{"hi"}); err == nil {
t.Fatal("expected error on 500, got nil")
}
}
// TestAPIEmbedder_Non200DropsEchoedInput 覆盖 M41 里最敏感的一处:/embeddings 的
// **input 就是她的记忆正文**,而兼容层常把 input 原样回显在错误体里。旧实现读
// 4096 字节响应体、整个拼进 error,而 error 会被 retrieve_fallback 等位置打日志。
//
// 断言的是「文本里搜不到正文」,不是「error 非空」—— 后者在旧实现下同样成立,
// 打不红。
func TestAPIEmbedder_Non200DropsEchoedInput(t *testing.T) {
t.Parallel()
const memo = "SENTINEL-MEMORY-INPUT-4a7d"
rt := roundTripFunc(func(r *http.Request) (*http.Response, error) {
body := `{"error":{"code":"invalid_request_error","param":"input",` +
`"message":"Invalid value: '` + memo + `'"}}`
return &http.Response{
StatusCode: http.StatusBadRequest,
Body: io.NopCloser(strings.NewReader(body)),
Header: http.Header{"Content-Type": []string{"application/json"}},
}, nil
})
emb := NewAPI(
config.EmbeddingConfig{APIURL: "https://embed.example.com/v1/embeddings"},
WithRoundTripper(rt),
)
_, err := emb.Embed(context.Background(), []string{memo})
if err == nil {
t.Fatal("expected error on 400, got nil")
}
if strings.Contains(err.Error(), memo) {
t.Errorf("error text echoes the memory text: %v", err)
}
if !strings.Contains(err.Error(), "400") {
t.Errorf("err = %v, want the status kept for triage", err)
}
}
func TestAPIEmbedder_MismatchedCountErrors(t *testing.T) {
t.Parallel()
rt := roundTripFunc(func(r *http.Request) (*http.Response, error) {
body := `{"data":[{"index":0,"embedding":[0.1,0.2]}]}`
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(strings.NewReader(body)),
Header: make(http.Header),
}, nil
})
emb := NewAPI(
config.EmbeddingConfig{APIURL: "http://example.com/v1/embeddings"},
WithRoundTripper(rt),
)
// 2 texts sent, only 1 returned -> error.
if _, err := emb.Embed(context.Background(), []string{"a", "b"}); err == nil {
t.Fatal("expected error on count mismatch, got nil")
}
}
func TestAPIEmbedder_BlankURLErrors(t *testing.T) {
t.Parallel()
emb := NewAPI(config.EmbeddingConfig{})
_, err := emb.Embed(context.Background(), []string{"hello"})
if err == nil {
t.Fatal("expected error on blank URL, got nil")
}
if !errors.Is(err, ErrNoAPIURL) {
t.Errorf("error = %v, want ErrNoAPIURL", err)
}
}
func TestAPIEmbedder_EmptyInput(t *testing.T) {
t.Parallel()
emb := NewAPI(config.EmbeddingConfig{APIURL: "http://example.invalid"})
vectors, err := emb.Embed(context.Background(), nil)
if err != nil {
t.Fatalf("Embed(nil) error: %v", err)
}
if len(vectors) != 0 {
t.Errorf("got %d vectors, want 0", len(vectors))
}
}
func TestAPIEmbedder_CanceledContext(t *testing.T) {
t.Parallel()
emb := NewAPI(config.EmbeddingConfig{APIURL: "http://example.invalid"})
ctx, cancel := context.WithCancel(context.Background())
cancel()
if _, err := emb.Embed(ctx, []string{"hello"}); err == nil {
t.Fatal("expected error on canceled context, got nil")
}
}