108 lines
3.3 KiB
Go
108 lines
3.3 KiB
Go
package vision
|
|
|
|
import (
|
|
"context"
|
|
"encoding/base64"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os/exec"
|
|
"strings"
|
|
"testing"
|
|
|
|
"cyberstrike-ai/internal/agentruntime"
|
|
"cyberstrike-ai/internal/config"
|
|
)
|
|
|
|
const fixturePNG = "iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAIAAAD8GO2jAAAAKElEQVR4nO3NsQ0AAAzCMP5/un0CNkuZ41wybXsHAAAAAAAAAAAAxR4yw/wuPL6QkAAAAABJRU5ErkJggg=="
|
|
|
|
func TestAnalyzeRoutesImageThroughPi(t *testing.T) {
|
|
if _, err := exec.LookPath("node"); err != nil {
|
|
t.Skip("Node not installed")
|
|
}
|
|
if _, err := agentruntime.ResolveWorker(""); err != nil {
|
|
t.Skip("build runtime/pi before Pi integration tests")
|
|
}
|
|
raw, err := base64.StdEncoding.DecodeString(fixturePNG)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
var userText, imageURL string
|
|
var tools json.RawMessage
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
var body struct {
|
|
Messages []struct {
|
|
Role string `json:"role"`
|
|
Content json.RawMessage `json:"content"`
|
|
} `json:"messages"`
|
|
Tools json.RawMessage `json:"tools"`
|
|
}
|
|
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
|
|
t.Error(err)
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
return
|
|
}
|
|
tools = body.Tools
|
|
for _, message := range body.Messages {
|
|
if message.Role != "user" {
|
|
continue
|
|
}
|
|
var blocks []struct {
|
|
Type string `json:"type"`
|
|
Text string `json:"text"`
|
|
ImageURL struct {
|
|
URL string `json:"url"`
|
|
} `json:"image_url"`
|
|
}
|
|
if json.Unmarshal(message.Content, &blocks) != nil {
|
|
continue
|
|
}
|
|
for _, block := range blocks {
|
|
switch block.Type {
|
|
case "text":
|
|
userText += block.Text
|
|
case "image_url":
|
|
imageURL = block.ImageURL.URL
|
|
}
|
|
}
|
|
}
|
|
w.Header().Set("Content-Type", "text/event-stream")
|
|
for _, choice := range []map[string]any{
|
|
{"index": 0, "delta": map[string]any{"role": "assistant", "content": "A7K2"}, "finish_reason": nil},
|
|
{"index": 0, "delta": map[string]any{}, "finish_reason": "stop"},
|
|
} {
|
|
b, _ := json.Marshal(map[string]any{"id": "fixture", "object": "chat.completion.chunk", "model": "fixture", "choices": []any{choice}})
|
|
fmt.Fprintf(w, "data: %s\n\n", b)
|
|
}
|
|
fmt.Fprint(w, "data: [DONE]\n\n")
|
|
}))
|
|
defer server.Close()
|
|
|
|
client := NewClient(config.VisionConfig{Enabled: true, Model: "fixture-vl", APIKey: "fixture-key", BaseURL: server.URL + "/v1"},
|
|
config.OpenAIConfig{}, config.AgentRuntimeConfig{})
|
|
got, err := client.Analyze(context.Background(), ImagePayload{Bytes: raw, MIMEType: "image/png"}, "识别验证码,只输出字符")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got != "A7K2" {
|
|
t.Fatalf("analysis = %q", got)
|
|
}
|
|
if want := "data:image/png;base64," + fixturePNG; imageURL != want {
|
|
t.Fatalf("image reached the model as %q", imageURL)
|
|
}
|
|
if !strings.Contains(userText, "识别验证码") || !strings.Contains(userText, "仅输出你辨认出的字符序列") {
|
|
t.Fatalf("captcha prompt missing: %q", userText)
|
|
}
|
|
if len(tools) != 0 {
|
|
t.Fatalf("vision call exposed tools: %s", tools)
|
|
}
|
|
}
|
|
|
|
func TestAnalyzeRejectsEmptyPayload(t *testing.T) {
|
|
client := NewClient(config.VisionConfig{Enabled: true, Model: "fixture-vl", APIKey: "fixture-key"}, config.OpenAIConfig{}, config.AgentRuntimeConfig{})
|
|
if _, err := client.Analyze(context.Background(), ImagePayload{}, "describe"); err == nil {
|
|
t.Fatal("empty payload accepted")
|
|
}
|
|
} |