Files
magpie/internal/gateway/plugin_zed_test.go
T
yetone 21252f6a2c provider: Zed's built-in is retired; its plugin serves it
The first built-in taken out (the owner, 10-16: 陆续把内置Provider下线掉,
一定要下线完毕). @magpie-community/opencode-zed-auth serves Zed for
everyone now; built-in accounts not moved yet are moved by the hourly
loop, or from the Providers list's line (the commit before).

Removed: the built-in's sign-in (zed_signin.go and its loopback paste
branch), usage (zed_usage.go), its login list, switch, forget and
account cases, its own-backend case in asksOwnBackend, the gateway's
serveZed and zed.go, internal/zed, and the tests of only those.

Kept, because the mover and the plugin path use them: zed.go's
credential and login readers (migrate_zed.go's move), zed_body.go
(plugins.go), and Zed in accountIDs, side_logins, cards_kept,
plugin_usage's movedCards, the gateway's estimatedMoved and the CLI's
account names, which name the plugin's accounts too. Zed the editor as
an agent (internal/agent, the library's MCP target) is untouched.

Tests changed: TestMovedBuiltinQuiet is TestRetiredBuiltinQuiet (no Zed
card, its sign-in refused, the move still offered with the account);
TestZedMoveCarriesItsModels compares the models without internal/zed;
TestLoopbackSignInsTakePastedAddress and asksOwnBackend's set lose Zed.
AGENTS.md and gateway-routing.md no longer name Zed's built-in client.

Run on this tree over af08ecb27's parent tree:
- go test -tags nogui ./... -count=1 -timeout 90m: 64 packages ok,
  internal/gui red only in TestCoreScriptBudget (the Move line; the
  budget is raised in af08ecb27). Rebased onto it:
  go test -tags nogui ./internal/gui -count=1: ok.
- Earlier on wt6 before the three fixes: provider red in
  TestAsksOwnBackendAgreesWithTheGatewayDispatch and
  TestLoopbackSignInsTakePastedAddress (Zed still named), fixed here.
- gofmt -l internal: nothing; GOOS=linux and GOOS=windows
  go vet -tags nogui ./...: clean.
- GUI: retired-move, plugin-move, deprecation-notice and
  deprecated-subscriptions, both engines, en and zh: 20 pass.
2026-10-17 23:10:59 +08:00

140 lines
5.5 KiB
Go

package gateway
import (
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"strings"
"sync"
"testing"
)
// zedParses is what Zed's cloud asks of an OpenAI Responses request
// (zed-industries/zed, crates/open_ai/src/responses.rs) before it goes on,
// "" when it would take it.
func zedParses(q map[string]any) string {
items, _ := q["input"].([]any)
for _, it := range items {
im, _ := it.(map[string]any)
switch ty, _ := im["type"].(string); ty {
case "message":
switch im["role"] {
case "user", "assistant", "system", "tool":
default:
return "unknown variant `" + im["role"].(string) + "`, expected one of `user`, `assistant`, `system`, `tool`"
}
if _, ok := im["content"].([]any); !ok {
return "invalid type: expected a sequence"
}
case "reasoning":
if c, ok := im["content"]; ok && c == nil {
return "invalid type: null, expected a sequence"
}
case "function_call", "function_call_output", "custom_tool_call", "custom_tool_call_output", "compaction", "compaction_trigger":
default:
return "unknown variant `" + ty + "`"
}
}
tools, _ := q["tools"].([]any)
for _, t := range tools {
if ty := t.(map[string]any)["type"]; ty != "function" && ty != "custom" {
return "unknown variant `" + ty.(string) + "`, expected `function` or `custom`"
}
}
inc, _ := q["include"].([]any)
for _, v := range inc {
if v != "reasoning.encrypted_content" {
return "unknown variant `" + v.(string) + "`"
}
}
return ""
}
// A Codex request on an OpenAI model of Zed's plugin reaches Zed as its
// cloud reads it: the developer messages Codex sends at the start and
// mid-conversation (a model switched) as system ones where they stand,
// reasoning with a null content without it, a web search an OpenAI model
// ran left out, and the namespaced tools flat (nico on Discord: "failed to
// parse OpenAI Responses API request: unknown variant `developer`").
func TestZedPluginGetsNoDeveloperRole(t *testing.T) {
var mu sync.Mutex
var sent []map[string]any
pid := besideFake(t, "zed", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
b, _ := io.ReadAll(r.Body)
var q map[string]any
json.Unmarshal(b, &q)
mu.Lock()
sent = append(sent, q)
mu.Unlock()
if why := zedParses(q); why != "" {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(400)
io.WriteString(w, `{"error":{"message":"failed to parse OpenAI Responses API request: `+why+`"}}`)
return
}
done := `{"id":"r1","object":"response","status":"completed","output":[{"type":"message","role":"assistant","content":[{"type":"output_text","text":"ok"}]}],"usage":{"input_tokens":5,"output_tokens":1}}`
if q["stream"] != true {
w.Header().Set("Content-Type", "application/json")
io.WriteString(w, done)
return
}
w.Header().Set("Content-Type", "text/event-stream")
io.WriteString(w, sse(
`event: response.created`+"\n"+`data: {"type":"response.created","response":{"id":"r1","object":"response","status":"in_progress","output":[]}}`,
`event: response.output_text.delta`+"\n"+`data: {"type":"response.output_text.delta","item_id":"m1","output_index":0,"delta":"ok"}`,
`event: response.completed`+"\n"+`data: {"type":"response.completed","response":`+done+`}`))
}))
body := `{"model":"` + pid + `/fake-resp","stream":true,"instructions":"You are Codex.",` + namespacedTools + `,
"include":["reasoning.encrypted_content"],"reasoning":{"effort":"medium","summary":"auto"},"input":[
{"type":"message","role":"developer","content":[{"type":"input_text","text":"<permissions instructions>sandboxed</permissions instructions>"}]},
{"type":"message","role":"user","content":[{"type":"input_text","text":"hi"}]},
{"type":"reasoning","summary":[{"type":"summary_text","text":"thinking"}],"content":null,"encrypted_content":"gAAAAAx"},
{"type":"web_search_call","id":"ws_1","status":"completed","action":{"type":"search","query":"magpie"}},
{"type":"message","role":"assistant","content":[{"type":"output_text","text":"hello"}]},
{"type":"message","role":"developer","content":[{"type":"input_text","text":"<model_switch>now another model</model_switch>"}]},
{"type":"message","role":"user","content":[{"type":"input_text","text":"again"}]}]}`
rec := httptest.NewRecorder()
New().Handler().ServeHTTP(rec, httptest.NewRequest("POST", "/v1/responses", strings.NewReader(body)))
if rec.Code != 200 {
t.Fatalf("%d %s", rec.Code, rec.Body)
}
mu.Lock()
defer mu.Unlock()
if len(sent) != 1 {
t.Fatalf("asked %d times", len(sent))
}
q := sent[0]
var roles []string
for _, it := range q["input"].([]any) {
im := it.(map[string]any)
if im["type"] == "message" {
roles = append(roles, im["role"].(string))
}
}
if got := strings.Join(roles, ","); got != "system,user,assistant,system,user" {
t.Fatalf("roles %s", got)
}
in := q["input"].([]any)
if len(in) != 6 {
t.Fatalf("input %v", in)
}
if s := in[4].(map[string]any)["content"].([]any)[0].(map[string]any)["text"]; s != "<model_switch>now another model</model_switch>" {
t.Fatalf("the model switch went as %v", in[4])
}
if r := in[2].(map[string]any); r["type"] != "reasoning" || r["encrypted_content"] != "gAAAAAx" {
t.Fatalf("reasoning went as %v", r)
}
if q["instructions"] != "You are Codex." {
t.Fatalf("instructions %v", q["instructions"])
}
var names []string
for _, tl := range q["tools"].([]any) {
tm := tl.(map[string]any)
names = append(names, tm["type"].(string)+":"+tm["name"].(string))
}
if got := strings.Join(names, ","); got != "function:exec_command,function:collaboration__spawn_agent" {
t.Fatalf("Zed was offered %s", got)
}
}