Files
magpie/internal/agent/deveco_test.go
T
yetone e528aeb3ce agent: DevEco Code, Huawei's OpenCode, as an agent (#1885)
foobra asked for DevEco Code (npm @deveco/deveco-code), Huawei's
HarmonyOS coding agent. It is OpenCode underneath, so it joins through
openCodeLike as MiMo Code did (03a01a548):

- Config in ~/.config/deveco. magpie writes the first of deveco.jsonc,
  deveco.json, config.json there (the one DevEco reads last wins its
  merge), or DEVECO_CONFIG_DIR when that is an absolute folder; agentenv
  carries DEVECO_CONFIG_DIR. Auth at ~/.local/share/deveco/auth.json.
- Its provider block carries the key magpie-deveco, not the plain one.
  0.2.3 sends "deveco/0.2.3 ai-sdk/provider-utils/4.0.23 runtime/bun/1.4.2"
  on chat, but parts of its code still send opencode/<version>, so the key
  keeps its usage DevEco's whichever it sends. Check flags the plain key,
  and Sync writes DevEco's back. OpenCode's own key is unchanged.
- The user's own provider, small_model, $schema and comments stay; the
  model they were on comes back on Disconnect.
- Library: AGENTS.md instructions (with the "reads Claude Code's
  CLAUDE.md" note, as OpenCode and MiMo Code), MCP in OpenCode's shape,
  skills. No WSL folder.
- Install/update: npm @deveco/deveco-code on macOS and Windows only, as the
  package has no Linux binary (new cliSpec.only); the self installer under
  ~/.local/share/deveco/versions updates with `deveco upgrade`.
- Icon: DevEco's own logo, byte for byte from its IntelliJ plugin's
  pluginIcon.svg. Model picker /models. Route hue, READMEs (en, zh), site
  agent lists (en, zh, ja), docs/reference.md, subsystem pages, CLI help.

What was run
- gofmt -l clean; go vet darwin, GOOS=linux -tags nogui, GOOS=windows.
- go test -tags nogui: internal/library, agentenv, proc, cli, gui, usage
  pass; internal/agent passes in 559s with -timeout 40m;
  its first run, at the default 10m with load average ~40 from other
  sessions, was cut off by the timeout (no test had failed).
- New deveco_test.go: config lookup and DEVECO_CONFIG_DIR, the key, and
  Connect/Check/Sync/Disconnect against a deveco.jsonc holding the user's
  own model, provider, small_model, $schema and a comment. Made
  openCodeKeyAt return the plain key by hand: TestDevEcoKeyNamesIt and
  TestDevEcoConnectKeepsTheUsersOwn go red.
- GUI: new library-deveco.test.cjs (the note in en, zh, zh-TW, ja, de,
  and the icon draws), chromium and webkit; red with deveco taken out of
  library.js's note. library-instr-sets, library-instr-busy,
  panel-routing, agent-connect, library-desktop-skills, getting-started,
  filter-ime, routing-account-identity pass in both engines.
- Real use, sandbox HOME: npm-installed @deveco/deveco-code 0.2.3,
  `deveco auth consent --accept` (sandbox only, no Huawei sign-in). magpie
  serve on 127.0.0.1:3592 with a fake upstream; `magpie deveco fake/fake-1`
  wrote provider magpie (magpie-deveco, http://127.0.0.1:3592/v1) beside
  the user's own and kept the file's comment; `deveco run "Reply with one
  word: ping"` answered from the fake, and `magpie usage today` counted it
  under DevEco Code. The User-Agent above is what it sent.

Not tried
- Windows, and DevEco Studio's plugin mode; a Huawei-signed-in DevEco.
- `magpie deveco <model>` then `magpie deveco default` doesn't bring back
  the model the user was on before: the CLI set path doesn't stash. It is
  the same for OpenCode today; Connect/Disconnect in the GUI does.

Reported-by: foobra
2026-10-13 10:57:32 +08:00

144 lines
4.7 KiB
Go

package agent
import (
"path/filepath"
"strings"
"testing"
"github.com/yetone/magpie/internal/edit"
"github.com/yetone/magpie/internal/gateway"
)
// DevEco Code (#1885) keeps OpenCode's config shape in ~/.config/deveco:
// the first of deveco.jsonc, deveco.json and config.json there is the one
// it reads last, so the one magpie writes; DEVECO_CONFIG_DIR, read after
// all of them, when set to an absolute folder.
func TestDevEcoConfigAsItFindsIt(t *testing.T) {
home := syncHome(t)
cfg := filepath.Join(home, ".config")
dir := filepath.Join(cfg, "deveco")
if a := deveco(home, cfg); a.Path != filepath.Join(dir, "deveco.json") {
t.Fatalf("with no config, %s", a.Path)
}
writeFile(t, filepath.Join(dir, "config.json"), `{}`)
if a := deveco(home, cfg); a.Path != filepath.Join(dir, "config.json") {
t.Fatalf("with config.json only, %s", a.Path)
}
// the one DevEco Code makes on its first start, which wins its merge
writeFile(t, filepath.Join(dir, "deveco.jsonc"), `{"$schema":"https://opencode.ai/config.json"}`)
if a := deveco(home, cfg); a.Path != filepath.Join(dir, "deveco.jsonc") {
t.Fatalf("with deveco.jsonc, %s", a.Path)
}
moved := filepath.Join(home, "deveco-cfg")
t.Setenv("DEVECO_CONFIG_DIR", moved)
if a := deveco(home, cfg); a.Path != filepath.Join(moved, "deveco.json") {
t.Fatalf("with DEVECO_CONFIG_DIR, config at %s", a.Path)
}
t.Setenv("DEVECO_CONFIG_DIR", "deveco-cfg")
if a := deveco(home, cfg); a.Path != filepath.Join(dir, "deveco.jsonc") {
t.Fatalf("with a relative DEVECO_CONFIG_DIR, config at %s", a.Path)
}
}
// Parts of DevEco Code's code still send OpenCode's own User-Agent
// (opencode/<version>), so the key magpie writes for it names it: its usage
// is DevEco Code's, not OpenCode's, whichever it sends. OpenCode's own
// stays the gateway's plain key.
func TestDevEcoKeyNamesIt(t *testing.T) {
syncHome(t)
key := func(id string) any {
return magpieProviderJSONFor("opencode", id).(map[string]any)["options"].(map[string]any)["apiKey"]
}
if got := key("deveco"); got != gateway.TokenFor("deveco") {
t.Errorf("deveco's key %v", got)
}
if got := key("opencode"); got != gateway.Token {
t.Errorf("opencode's key %v", got)
}
}
// Connected, magpie's provider goes in beside the user's own, with
// DevEco Code's own key; its built-in provider, the user's own provider,
// their small model and the file's $schema stay as they were. Disconnected,
// the model they were on comes back.
func TestDevEcoConnectKeepsTheUsersOwn(t *testing.T) {
home := syncHome(t)
cfg := filepath.Join(home, ".config", "deveco", "deveco.jsonc")
writeFile(t, cfg, `{
"$schema": "https://opencode.ai/config.json",
// mine
"model": "deveco/glm-5",
"small_model": "mine/x",
"provider": {"mine": {"name": "mine", "options": {"baseURL": "http://127.0.0.1:9/v1", "apiKey": "sk-own"}}}
}`)
a, err := Find("deveco")
if err != nil {
t.Fatal(err)
}
if a.Path != cfg {
t.Fatalf("config at %s", a.Path)
}
if a.Wired() {
t.Fatal("wired before connecting")
}
if err := a.Connect(); err != nil {
t.Fatal(err)
}
get := func(k string) string { v, _ := edit.GetJSON(cfg, k); return v }
if m := get("model"); !strings.HasPrefix(m, magpieID+"/") {
t.Fatalf("model %q after connecting", m)
}
if k := get("provider.magpie.options.apiKey"); k != gateway.TokenFor("deveco") {
t.Fatalf("apiKey %q", k)
}
if u := get("provider.magpie.options.baseURL"); u != gateway.URL()+"/v1" {
t.Fatalf("baseURL %q", u)
}
if k := get("provider.mine.options.apiKey"); k != "sk-own" {
t.Fatalf("own provider %q", k)
}
if s := get("small_model"); s != "mine/x" {
t.Fatalf("small_model %q", s)
}
if s := get("$schema"); s != "https://opencode.ai/config.json" {
t.Fatalf("$schema %q", s)
}
if !a.Wired() {
t.Fatal("not wired after connecting")
}
if c := a.Check(); c != "" {
t.Fatalf("check after connecting: %s", c)
}
// a key that isn't DevEco Code's is told as off
if err := edit.SetJSON(cfg, edit.KV{Path: "provider.magpie.options.apiKey", Value: gateway.Token}); err != nil {
t.Fatal(err)
}
if a.Check() == "" {
t.Fatal("check passes the plain key")
}
// and a sync puts DevEco Code's back
if err := a.Sync(); err != nil {
t.Fatal(err)
}
if k := get("provider.magpie.options.apiKey"); k != gateway.TokenFor("deveco") {
t.Fatalf("apiKey %q after a sync", k)
}
if err := a.Disconnect(); err != nil {
t.Fatal(err)
}
if m := get("model"); m != "deveco/glm-5" {
t.Fatalf("model %q after disconnecting", m)
}
if s := get("small_model"); s != "mine/x" {
t.Fatalf("small_model %q after disconnecting", s)
}
if k := get("provider.mine.options.apiKey"); k != "sk-own" {
t.Fatalf("own provider %q after disconnecting", k)
}
if _, ok := edit.GetJSON(cfg, "provider.magpie"); ok {
t.Fatalf("magpie's provider left: %s", readFile(cfg))
}
}