50 lines
1.9 KiB
Go
50 lines
1.9 KiB
Go
package extraction
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
"time"
|
|
|
|
"data-terminal-cloud/internal/outbox"
|
|
)
|
|
|
|
func TestArkGeneratorProducesValidatedContent(t *testing.T) {
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.Header.Get("Authorization") != "Bearer test-key" || r.URL.Path != "/api/v3/chat/completions" {
|
|
t.Fatalf("unexpected Ark request path or authorization")
|
|
}
|
|
var body map[string]any
|
|
if err := json.NewDecoder(r.Body).Decode(&body); err != nil || body["model"] != "doubao-test" {
|
|
t.Fatalf("request body=%v err=%v", body, err)
|
|
}
|
|
_ = json.NewEncoder(w).Encode(map[string]any{"choices": []any{map[string]any{"message": map[string]any{"content": `{"summary":"谈到河边小学","open_topic":"下次可以聊聊同学","story_title":"河边小学的回忆","story_content":"小时候在河边小学读书。","fact_content":"曾在河边小学读书","confidence":0.82}`}}}})
|
|
}))
|
|
defer server.Close()
|
|
generator, err := NewArkGenerator(server.URL+"/api/v3", "test-key", "doubao-test", time.Second)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
result, err := generator.Generate(context.Background(), GenerationInput{Content: "小时候在河边小学读书。"})
|
|
if err != nil || result.ModelRef != "ark:doubao-test" || result.Confidence != 0.82 || !result.AllowStory {
|
|
t.Fatalf("result=%+v err=%v", result, err)
|
|
}
|
|
}
|
|
|
|
func TestArkGeneratorRetriesTemporaryFailure(t *testing.T) {
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
|
http.Error(w, "busy", http.StatusTooManyRequests)
|
|
}))
|
|
defer server.Close()
|
|
generator, err := NewArkGenerator(server.URL, "test-key", "doubao-test", time.Second)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
_, err = generator.Generate(context.Background(), GenerationInput{Content: "测试内容"})
|
|
if !outbox.IsRetryable(err) {
|
|
t.Fatalf("429 should be retryable: %v", err)
|
|
}
|
|
}
|