Files
magpie/internal/gateway/draw_workbuddy_test.go
T
yetone b7ceb53fe4 provider: WorkBuddy's built-in is retired; its plugin serves it
workbuddy and workbuddy-ai join retiredBuiltins, as Zed did in c0605abc2
and Qoder in 43cc3171d. @magpie-community/opencode-workbuddy-auth serves
both, moved or not.

Removed (built-in only):
- provider: workbuddy_models.go (the model list); in workbuddy.go the
  sign-in, token renewal, usage/credits reading, request signing and the
  site's model fetch; the WorkBuddy cases in account.go (Accounts),
  logins_on.go, logins.go (list/switch/forget), login_usage.go,
  subscription_usage.go and signin.go; WorkBuddyBaseForTest, now unused.
- The built-in tests: workbuddy_test, workbuddy_signin_test,
  workbuddy_accounts_test, workbuddy_ai_test, workbuddy_body_test,
  workbuddy_enterprise_test, workbuddy_free_test, workbuddy_names_test,
  workbuddy_spare_free_test, workbuddy_refused_test (provider), and
  gateway/workbuddy_refused_test.

Kept, and why:
- migrate_workbuddy.go (the mover) with what it reads: wbSite, wbCreds,
  wbOwn/wbAuthPath (the desktop's file, sent as a marker), wbLogins,
  wbTokens (back drops a renewed pair).
- workbuddy_checkin.go: the daily check-in now walks the plugin's
  accounts only and sends through the plugin's fetch (a.via); wbCallVia,
  wbNum and the site's api() serve it, and plugin_usage's host.
- WBRestricted/WBRestrictedHint: the gateway reads the plugin's 11140 by
  them. textOnly: applied to the plugin's lists.
- draw_workbuddy and search_workbuddy in the gateway: they serve the
  plugin's provider. The PLUGIN-SERVED notice in workbuddy.go.
- The WorkBuddy agent target (internal/agent) is untouched.

Tests changed in meaning:
- TestFetchNewAccount, TestFetchNewRetriedSoon and
  TestFetchingNewOnceStarted used WorkBuddy as the built-in signed in;
  they use Codex now (codexSignedIn, codexListedAt), same assertions.
- TestFetchPlanWithoutList uses local models; TestWorkBuddyFetchModels,
  TestWorkBuddyAIGLMIsTextOnly and TestOwnStoreSignInAgainDropsTheReading
  (WorkBuddy case) are gone with the code.
- Check-in: TestWorkBuddyCheckinSignedIn is gone; the fixture's account
  is a plugin account signing through via; PluginUnderItsOwnID became
  TestWorkBuddyCheckinPluginAccounts (plugin accounts listed, a built-in
  login left out). TestCheckinAccountsListed reads a moved plugin.
- TestWorkBuddyMover seeds the desktop's logins.json row: retired, the
  built-in no longer writes it, and an existing user's row is what moves
  (red without the row).
- Gateway: the WorkBuddy draw and search tests run through the fake
  plugin (besideFake), without the built-in's header checks;
  TestWorkBuddySearchesForItsModels folded into
  TestWorkBuddyPluginSearches (refusal and search-off checks kept);
  TestLoopEndsWorkBuddysReply is the plugin route only;
  TestWorkBuddyRestrictedRestsHours lost its built-in translation case.

Run:
- gofmt -l internal: empty
- go vet -tags nogui ./...; GOOS=linux and GOOS=windows the same: clean
- go test -tags nogui -count=1 -timeout 45m -run 'WorkBuddy|workbuddy|Checkin|CheckIn|FetchNew|FetchingNew|FetchPlan|Migrat|migrat|Mover|Moved|Retired|Retire|SignIn|Plugin|Draw|Search|Loop|Redact|Restricted|Images|TextOnly|Qoder|BuiltinsSay|AsksOwn|Loopback' ./internal/provider ./internal/gateway ./internal/cli ./internal/gui ./internal/qoder
  first run: gateway, cli, gui ok; provider red on TestWorkBuddyMover
  (above); after the fix provider ok (99s).
- Not run: whole packages, the full suite, the GUI node suite.
2026-10-17 23:11:00 +08:00

261 lines
9.8 KiB
Go

package gateway
import (
"context"
"encoding/base64"
"encoding/json"
"io"
"net/http"
"strings"
"sync"
"sync/atomic"
"testing"
"time"
"github.com/yetone/magpie/internal/provider"
)
// wbStudio is WorkBuddy's API as far as drawing goes: its product config,
// which lists image models by tag among its chat models, and its images
// API, which answers OpenAI's shape inside WorkBuddy's {code, msg, data}.
type wbStudio struct {
models string // the config's models
mu sync.Mutex
asked []string // method and path
bodies map[string]map[string]any
heads map[string]http.Header
answer func(path string, body map[string]any) string
}
func (s *wbStudio) ServeHTTP(w http.ResponseWriter, r *http.Request) {
b, _ := io.ReadAll(r.Body)
var body map[string]any
json.Unmarshal(b, &body)
s.mu.Lock()
s.asked = append(s.asked, r.Method+" "+r.URL.Path)
if s.bodies == nil {
s.bodies, s.heads = map[string]map[string]any{}, map[string]http.Header{}
}
s.bodies[r.URL.Path], s.heads[r.URL.Path] = body, r.Header.Clone()
s.mu.Unlock()
w.Header().Set("Content-Type", "application/json")
if r.URL.Path == "/v3/config" {
io.WriteString(w, `{"code":0,"msg":"OK","data":{"agents":[{"name":"cli","models":["hy3"]}],"models":`+s.models+`}}`)
return
}
io.WriteString(w, s.answer(r.URL.Path, body))
}
func (s *wbStudio) got(path string) (map[string]any, http.Header) {
s.mu.Lock()
defer s.mu.Unlock()
return s.bodies[path], s.heads[path]
}
func (s *wbStudio) count(what string) int {
s.mu.Lock()
defer s.mu.Unlock()
n := 0
for _, a := range s.asked {
if a == what {
n++
}
}
return n
}
const wbHunyuanModels = `[{"id":"hy3","name":"Hy3","tags":[]},
{"id":"hunyuan-image-alpha","name":"Hunyuan Image Alpha","tags":["text-to-image"]},
{"id":"hunyuan-image-alpha-edit","name":"Hunyuan Image Alpha Edit","tags":["image-to-image"]},
{"id":"hunyuan-image-v3.0-art","name":"Hunyuan-Image-v3.0-art","tags":["text-to-image"]}]`
const wbGPTImageModels = `[{"id":"gpt-5.5","name":"GPT-5.5"},
{"id":"gpt-image-2.5-sunburst","name":"GPT-Image-2.5-Sunburst","tags":["text-to-image","image-to-image"]},
{"id":"seedance-2.5","name":"Seedance-2.5","tags":["text-to-video","image-to-video"]}]`
// wbSignedIn installs the fake plugin as WorkBuddy's (id), signed in, its
// API at the studio, and gives magpie's provider for it: the retired
// built-in's id is the plugin's.
func wbSignedIn(t *testing.T, id string, st http.Handler) provider.Provider {
t.Helper()
wbImages.Lock()
wbImages.m = map[string]*wbImageList{}
wbImages.Unlock()
pid := besideFake(t, id, st)
if pid != id {
t.Fatalf("WorkBuddy's plugin under %q, not %q", pid, id)
}
p, err := provider.Find(pid)
if err != nil {
t.Fatal(err)
}
return *p
}
// A WorkBuddy plan's image models are those its config tags as drawing, not
// a list of magpie's; an edit goes to the model that edits beside the one
// named; the image comes back as WorkBuddy gave it (a URL) out of its
// envelope; AutoDrawer never picks one, as each image costs credits.
func TestWorkBuddyDrawsWithItsConfigsModels(t *testing.T) {
const kept = "https://cos.example/img-1.png?sign=x"
st := &wbStudio{models: wbHunyuanModels, answer: func(path string, body map[string]any) string {
if path == "/v1/images/edits" {
return `{"code":0,"msg":"OK","data":{"created":1,"data":[{"b64_json":"` + base64.StdEncoding.EncodeToString(pngBytes) + `"}],"usage":{"input_tokens":3,"output_tokens":7}}}`
}
return `{"code":0,"msg":"OK","requestId":"r1","data":{"created":1,"data":[{"url":"` + kept + `"}],"usage":{"input_tokens":333,"output_tokens":612,"credit":5.71}}}`
}}
p := wbSignedIn(t, "workbuddy", st)
// the first look asks the config in the background; the next has it
Drawers(p)
deadline := time.Now().Add(5 * time.Second)
var ds []string
for time.Now().Before(deadline) {
ds = ds[:0]
for _, m := range Drawers(p) {
ds = append(ds, m.ID)
}
if len(ds) > 0 {
break
}
time.Sleep(20 * time.Millisecond)
}
if strings.Join(ds, ",") != "hunyuan-image-alpha,hunyuan-image-v3.0-art" {
t.Fatalf("drawers %v", ds)
}
for _, m := range Drawers(p) {
if m.Images != (m.ID == "hunyuan-image-alpha") || m.Provider != "workbuddy" {
t.Fatalf("drawer %+v", m)
}
}
Drawers(p)
if n := st.count("GET /v3/config"); n != 1 {
t.Fatalf("the config was asked %d times", n)
}
if a := AutoDrawer(); a != "" {
t.Fatalf("AutoDrawer picks %q", a)
}
s := New()
code, _, raw := postImages(t, s, "/v1/images/generations", "application/json", `{"model":"workbuddy/hunyuan-image-v3.0-art","prompt":"a magpie","size":"1024x1024","n":1,"quality":"high"}`)
if code != 200 || !strings.Contains(raw, `"url":"`+kept+`"`) {
t.Fatalf("%d %s", code, raw)
}
body, _ := st.got("/v1/images/generations")
if body["model"] != "hunyuan-image-v3.0-art" || body["size"] != "1024x1024" || body["n"] != 1.0 || body["prompt"] != "a magpie" {
t.Fatalf("sent %v", body)
}
if _, ok := body["response_format"]; ok {
t.Fatalf("Hunyuan was sent OpenAI's fields: %v", body)
}
if _, ok := body["quality"]; ok {
t.Fatalf("Hunyuan was sent OpenAI's fields: %v", body)
}
png := "data:image/png;base64," + base64.StdEncoding.EncodeToString(pngBytes)
code, a, raw := postImages(t, s, "/v1/images/edits", "application/json", `{"model":"workbuddy/hunyuan-image-alpha","prompt":"make it blue","image":"`+png+`"}`)
if code != 200 || len(a.Data) != 1 || a.Data[0].B64 == "" || a.Usage.Output != 7 {
t.Fatalf("%d %s", code, raw)
}
body, _ = st.got("/v1/images/edits")
imgs, _ := body["image"].([]any)
if body["model"] != "hunyuan-image-alpha-edit" || len(imgs) != 1 || imgs[0] != png {
t.Fatalf("edit sent %v", body)
}
// one with no model to edit with says so, and asks nothing
code, _, raw = postImages(t, s, "/v1/images/edits", "application/json", `{"model":"workbuddy/hunyuan-image-v3.0-art","prompt":"make it blue","image":"`+png+`"}`)
if code != 400 || !strings.Contains(raw, "no model to edit") {
t.Fatalf("%d %s", code, raw)
}
}
// WorkBuddy AI's GPT Image is asked as OpenAI's, b64_json and all, and edits
// itself; WorkBuddy's refusal in its envelope is an error with its message.
func TestWorkBuddyAIDrawsGPTImage(t *testing.T) {
var refuse atomic.Bool
st := &wbStudio{models: wbGPTImageModels, answer: func(path string, body map[string]any) string {
if refuse.Load() {
return `{"code":14018,"msg":"credits used up","data":null}`
}
return `{"code":0,"msg":"OK","data":{"created":1,"data":[{"b64_json":"` + base64.StdEncoding.EncodeToString(pngBytes) + `","revised_prompt":"a magpie, painted"}]}}`
}}
p := wbSignedIn(t, provider.WorkBuddyAIID, st)
if _, err := wbImageModelsOf(context.Background(), wbConfigClient, p, true); err != nil {
t.Fatal(err)
}
ds := Drawers(p)
if len(ds) != 1 || ds[0].ID != "gpt-image-2.5-sunburst" || ds[0].Name != "GPT-Image-2.5-Sunburst" || !ds[0].Images {
t.Fatalf("drawers %+v", ds)
}
s := New()
code, a, raw := postImages(t, s, "/v1/images/generations", "application/json", `{"model":"workbuddy-ai/gpt-image-2.5-sunburst","prompt":"a magpie","size":"1024x1024","quality":"low"}`)
if code != 200 || len(a.Data) != 1 || a.Data[0].B64 == "" || !strings.Contains(raw, "a magpie, painted") {
t.Fatalf("%d %s", code, raw)
}
body, _ := st.got("/v1/images/generations")
if body["response_format"] != "b64_json" || body["quality"] != "low" || body["model"] != "gpt-image-2.5-sunburst" {
t.Fatalf("sent %v", body)
}
png := "data:image/png;base64," + base64.StdEncoding.EncodeToString(pngBytes)
code, _, raw = postImages(t, s, "/v1/images/edits", "application/json", `{"model":"workbuddy-ai/gpt-image-2.5-sunburst","prompt":"make it blue","image":["`+png+`"]}`)
if code != 200 {
t.Fatalf("%d %s", code, raw)
}
if body, _ = st.got("/v1/images/edits"); body["model"] != "gpt-image-2.5-sunburst" || body["response_format"] != "b64_json" {
t.Fatalf("edit sent %v", body)
}
refuse.Store(true)
code, _, raw = postImages(t, s, "/v1/images/generations", "application/json", `{"model":"workbuddy-ai/gpt-image-2.5-sunburst","prompt":"a magpie"}`)
if code != 502 || !strings.Contains(raw, "credits used up (14018)") {
t.Fatalf("%d %s", code, raw)
}
}
// A WorkBuddy moved onto its plugin draws the same way: its config and its
// images are asked through the plugin's fetch, which signs them; the
// config, outside the plugin's /v2 base, at the plugin's own host.
func TestWorkBuddyPluginDraws(t *testing.T) {
wbImages.Lock()
wbImages.m = map[string]*wbImageList{}
wbImages.Unlock()
st := &wbStudio{models: wbHunyuanModels, answer: func(path string, body map[string]any) string {
return `{"code":0,"msg":"OK","data":{"data":[{"b64_json":"` + base64.StdEncoding.EncodeToString(pngBytes) + `"}]}}`
}}
pid := besideFake(t, "workbuddy", st)
p, err := provider.Find(pid)
if err != nil {
t.Fatal(err)
}
if !drawsWorkBuddy(*p) {
t.Fatal("the WorkBuddy plugin doesn't draw")
}
if _, err := wbImageModelsOf(context.Background(), wbConfigClient, *p, true); err != nil {
t.Fatal(err)
}
if ds := Drawers(*p); len(ds) != 2 || ds[0].ID != "hunyuan-image-alpha" || ds[0].Provider != pid {
t.Fatalf("drawers %+v", ds)
}
code, a, raw := postImages(t, New(), "/v1/images/generations", "application/json", `{"model":"`+pid+`/hunyuan-image-alpha","prompt":"a magpie"}`)
if code != 200 || len(a.Data) != 1 {
t.Fatalf("%d %s", code, raw)
}
if st.count("GET /v3/config") != 1 || st.count("POST /v1/images/generations") != 1 {
st.mu.Lock()
defer st.mu.Unlock()
t.Fatalf("asked %v", st.asked)
}
if body, _ := st.got("/v1/images/generations"); body["model"] != "hunyuan-image-alpha" {
t.Fatalf("sent %v", body)
}
// the plugin signs a URL of its own host only, never another's
req, _ := http.NewRequest(http.MethodGet, "https://elsewhere.example/v3/config", nil)
if res, err := p.Do(http.DefaultClient, req); err == nil || !strings.Contains(err.Error(), "not elsewhere.example") {
if res != nil {
res.Body.Close()
}
t.Fatalf("sent elsewhere: %v", err)
}
}