Files
magpie/internal/gateway/draw_copilot_test.go
T
yetone e0a7664b53 image: GPT Image 2.6's complexity and 2.5's quality xhigh/max go through
StarBits on X asked whether magpie draws with the newest GPT Image
(v0.1.3055/3056). This follows it: the images API and both image MCPs
take GPT Image 2.6's `complexity` (low, medium, high) and pass it on, and
the tools' quality text names xhigh and max (GPT Image 2.5 and later).

- draw.go: complexity is read from JSON and form bodies (drawingRead) and
  sent, as given, to an images API that takes OpenAI's fields:
  generations (JSON and form) and multipart edits. Not sent to a
  ChatGPT (Codex) account, Copilot, OpenRouter, WorkBuddy, Grok or
  Volcengine. Volcengine used to get an unread complexity through Extra;
  it is now read, so it no longer goes there.
- A ChatGPT account's image backend ignores it. Tried with a real
  account today: complexity none, low and a bogus value drew alike, and
  quality high, xhigh and max all came back quality "low" at the same
  size and tokens. So its request stays the one Codex CLI sends.
- image_mcp.go, imagemcp.go: a `complexity` argument, forwarded; quality
  text lists xhigh and max.

Run on mote at this tree (rebased on origin/main e8df78f33):
- New: TestDrawComplexityAndQualityReachTheVendor (JSON, form, edit,
  none given = none sent), TestCodexDrawSendsNoComplexity; Copilot's
  test and both MCP tests now pass complexity and check it.
- Broken by hand: with the three code files at HEAD, the new test, the
  gateway MCP test and imagemcp's TestGenerateTakesTheModel go red; with
  complexity sent to Codex too, TestCodexDrawSendsNoComplexity goes red.
- Full suite at 11df525f0 + this change (go vet on darwin, linux nogui
  and windows, the three builds, go test -timeout 45m ./...): 85
  packages ok; internal/gui red only on TestMainWindowCorners (#3949,
  macOS 27), internal/provider red only on TestRenewLoginsKeepsClaudeReading
  (#5481, a fixed date, fixed in the next commit).
- Rebased on c6b7bbedf (3 commits, none in gateway or imagemcp): vet on
  all three OSes and the builds ok, internal/gateway (-timeout 45m),
  internal/imagemcp and internal/provider ok.

Not tried: a real OpenAI key drawing with complexity or quality xhigh/max
(no key with GPT Image 2.5/2.6 here).
2026-11-06 18:47:24 +08:00

104 lines
3.7 KiB
Go

package gateway
import (
"encoding/base64"
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"sync"
"testing"
"time"
"github.com/yetone/magpie/internal/provider"
)
// A Copilot account's image model (GPT Image 2.5 Flare in Copilot CLI) is
// listed by its /models as an image model, never offered to an agent's chat,
// and draws at {api}/images/generations with the model, prompt, size and
// quality Copilot CLI sends.
func TestCopilotDrawsItsImageModel(t *testing.T) {
fresh(t)
cfg := os.Getenv("XDG_CONFIG_HOME")
os.MkdirAll(filepath.Join(cfg, "github-copilot"), 0o755)
os.WriteFile(filepath.Join(cfg, "github-copilot", "apps.json"), mustJSON(map[string]any{
"github.com:Iv1.x": map[string]any{"user": "u", "oauth_token": "gho_img"},
}), 0o600)
var mu sync.Mutex
var paths, bodies []string
var sess string
api := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
b, _ := io.ReadAll(r.Body)
switch r.URL.Path {
case "/models":
io.WriteString(w, `{"data":[
{"id":"gpt-4.1","name":"GPT-4.1","model_picker_enabled":true,"is_chat_default":true,"policy":{"state":"enabled"},"supported_endpoints":["/chat/completions"],"capabilities":{"type":"chat"}},
{"id":"gpt-image-2.5-flare","name":"GPT Image 2.5 Flare","model_picker_enabled":true,"policy":{"state":"enabled"},"capabilities":{"type":"image"}}]}`)
case "/images/generations":
mu.Lock()
paths, bodies, sess = append(paths, r.URL.Path), append(bodies, string(b)), r.Header.Get("Authorization")
mu.Unlock()
io.WriteString(w, `{"data":[{"b64_json":"`+base64.StdEncoding.EncodeToString(pngBytes)+`"}],"usage":{"input_tokens":5,"output_tokens":100}}`)
default:
w.WriteHeader(404)
}
}))
defer api.Close()
gh := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if strings.HasSuffix(r.URL.Path, "/token") {
json.NewEncoder(w).Encode(map[string]any{"token": "sess", "expires_at": time.Now().Add(time.Hour).Unix(), "endpoints": map[string]string{"api": api.URL}})
return
}
io.WriteString(w, `{"copilot_plan":"individual"}`)
}))
defer gh.Close()
oldTok, oldUser := provider.CopilotTokenURL, provider.CopilotUserURL
provider.CopilotTokenURL, provider.CopilotUserURL = gh.URL+"/token", gh.URL+"/user"
defer func() { provider.CopilotTokenURL, provider.CopilotUserURL = oldTok, oldUser }()
p, err := provider.Find("copilot")
if err != nil {
t.Fatal(err)
}
if _, err := p.Fetch(t.Context()); err != nil {
t.Fatal(err)
}
p, _ = provider.Find("copilot")
var drawers []string
for _, m := range Drawers(*p) {
drawers = append(drawers, m.ID)
}
if strings.Join(drawers, ",") != "gpt-image-2.5-flare" {
t.Fatalf("Copilot draws with %v", drawers)
}
for _, m := range p.Available() {
if m.Draws {
continue
}
if strings.Contains(m.ID, "image") {
t.Fatalf("an image model among chat models: %v", m)
}
}
s := New()
code, a, raw := postImages(t, s, "/v1/images/generations", "application/json",
`{"model":"copilot/gpt-image-2.5-flare","prompt":"a magpie","size":"1024x1024","quality":"high","background":"transparent","output_format":"png","complexity":"low"}`)
if code != 200 || len(a.Data) != 1 {
t.Fatalf("%d %s", code, raw)
}
mu.Lock()
defer mu.Unlock()
sent := bodies[0]
if paths[0] != "/images/generations" || !strings.Contains(sent, `"model":"gpt-image-2.5-flare"`) ||
!strings.Contains(sent, `"size":"1024x1024"`) || !strings.Contains(sent, `"quality":"high"`) ||
strings.Contains(sent, "background") || strings.Contains(sent, "output_format") || strings.Contains(sent, "complexity") {
t.Fatalf("asked %v %s", paths, sent)
}
if sess != "Bearer sess" {
t.Fatalf("Authorization %q", sess)
}
}