Files
magpie/internal/agent/dsh_fresh_test.go
T
yetone 5cc2eea2fe dsh: a dsh CLI that has booted no profile yet is wired in its web profile, not config.yaml (liby233)
liby233 on Discord: in WSL magpie wired dsh in ~/.dsh/config.yaml, and dsh
saw magpie's models only once they were written to its settings.yaml.
With no ~/.dsh/profiles, dshAt fell back to the "pre-0.1.5" config.yaml.
No dsh on npm reads that file. 0.0.1-rc.1 (the first), 0.0.1-rc.5,
0.1.0-rc.8, 0.1.1-rc.1/rc.2, 0.1.2-rc.1, 0.1.3-alpha.2 and 0.2.1-rc.5 all
boot $DSH_HOME/profiles/<name> (grepped from their npm tarballs). A dsh
makes no profile until one is first booted, so a fresh CLI install,
in a distro or not, was wired in a file that nothing reads.

- With no profile, and a dsh that is not from before profiles, magpie
  writes profiles/web/cordis.patch.yml as dsh's template, as it does
  for Desktop (#2279). dsh's initProfile adds package.json,
  pnpm-workspace.yaml and cordis.yml next to it and keeps the file.
- "Before profiles" is judged by the CLI's version where it is known
  (locally from its package.json; in a distro the WSL probe now asks
  dsh --version). Otherwise config.yaml is used only when it holds an
  entry that isn't magpie's. That keeps the legacy path for a genuinely
  old dsh.
- The sync carries a magpie model already wired in config.yaml (the
  reporter's state) into the profiles and drops magpie's entries from
  config.yaml. It does this whether or not dsh has since booted a bare
  web profile, and only where no profile has a model or wiring of its
  own.
- Disconnecting before dsh ever booted the web profile removes what
  magpie made, as for Desktop.

Tests: TestDshCLIWithNoProfileYet and TestDshConfigYamlCarriedToProfiles
(new), plus wslMore's dsh row, now profiles/web. All three are red on the
old dsh.go: "names .../.dsh/config.yaml, not the web profile's patch
list", and the web profile is left as dsh's bare template after the sync.
Tests pin dshVersion, so a dsh on the test machine decides nothing.

Run (go test -timeout 45m, logs teed and grepped):
- gofmt -l internal/agent: clean. go vet ./internal/agent on darwin,
  GOOS=linux and GOOS=windows (mote): ok.
- go test -run 'TestDsh|WSL' ./internal/agent: ok on mote (macOS),
  magpie-desktop (WSL Ubuntu 24.04, nogui) and xiaomi-gpu (Windows 11).
- Full suite `go test ./...` on mote at this change on origin/main
  8680ffa41: 82 packages ok; internal/gui red only in
  TestSettingsSaveKeepsWhatItDoesNotSend (#4514) and
  TestMainWindowCorners/macOS_26_SDK (#3949), both filed before this and
  in code this change doesn't reach.
- Rebased again onto origin/main 70d50254c (it changed internal/agent's
  new_session_effort and rename_group tests). At this pushed head: vet
  (darwin, linux, windows) ok, and internal/agent, internal/cli ok;
  internal/gui has the same two reds and no others.
- Rebased once more onto e484b1e93, which touches internal/provider only.
  At that head (the pushed one): go build ./..., go vet internal/agent and
  internal/provider, and go test -run 'TestDsh|WSL' ./internal/agent all
  ok on mote.
- Real dsh 0.2.1-rc.5 from npm in magpie-desktop, in sandbox HOMEs under
  ~/dshprobe (never the live ~/.dsh), with this commit's CLI built there
  (nogui):
  A. Empty HOME, `magpie dsh magpie/relay/glm-4.6`: only
     profiles/web/cordis.patch.yml was written, holding llm-pi-ai's
     magpie route, agent-default-model and web-search-deepseek.
     `dsh --profile web --dump-config` shows llm-pi-ai baseURL
     127.0.0.1:3425/v1 and agent-default-model relay/glm-4.6. dsh added
     package.json, pnpm-workspace.yaml and cordis.yml and kept magpie's
     entries. `magpie dsh default` then left dsh's bare template.
  B. origin/main's CLI wired config.yaml (the reporter's state). The
     dump showed no magpie route. `dsh --profile web --dump-config`
     booted web, then this CLI's `magpie sync` moved the route and model
     into profiles/web and emptied config.yaml, and the dump showed them.
  C. Wired and never booted: `magpie dsh default` removed profiles/.
Not tried: the reporter's own machine and dsh version, a running
`dsh web` session picking the model up live, the desktop app, and a
pre-npm dsh that reads config.yaml (none could be found).
2026-10-31 19:50:39 +08:00

145 lines
5.2 KiB
Go

package agent
import (
"os"
"path/filepath"
"strings"
"testing"
)
// A dsh CLI on the machine running the tests never decides which file a
// test's dsh is wired in; a test that needs a version says it.
func init() { dshVersion = func() string { return "" } }
// dshVersionIs holds the dsh CLI's version on this machine at v.
func dshVersionIs(t *testing.T, v string) {
t.Helper()
was := dshVersion
dshVersion = func() string { return v }
t.Cleanup(func() { dshVersion = was })
}
// dshNoApp holds DeepSeek Harness Desktop uninstalled.
func dshNoApp(t *testing.T) {
t.Helper()
was := appFolders
appFolders = func() []string { return nil }
t.Cleanup(func() { appFolders = was })
}
// liby233 on Discord: in WSL, a dsh 0.2 that had booted no profile yet was
// wired in ~/.dsh/config.yaml, which no dsh on npm reads (0.0.1-rc.1 on;
// 0.2.1-rc.5 makes $DSH_HOME/profiles/web on `dsh web`, as dsh's
// initProfile, and reads no config.yaml). magpie now makes the web
// profile's patch list as dsh's template with its route in it, which dsh
// keeps on its first boot; a dsh before profiles still gets config.yaml.
func TestDshCLIWithNoProfileYet(t *testing.T) {
home, dir, web := dshRouteHome(t)
if err := os.RemoveAll(dir); err != nil {
t.Fatal(err)
}
dshNoApp(t)
dshVersionIs(t, "0.2.1-rc.5")
legacy := filepath.Join(dir, "config.yaml")
read := func(p string) string { b, _ := os.ReadFile(p); return string(b) }
a := dsh(home)
if a.Path != web {
t.Fatalf("names %s, not the web profile's patch list", a.Path)
}
if err := a.Pick("model", "magpie/deepseek/flash"); err != nil {
t.Fatal(err)
}
s := read(web)
if !strings.HasPrefix(s, strings.TrimSuffix(dshDesktopPatch, "[]\n")) || !strings.Contains(s, "apiKeyEnv: "+dshKeyRef) || !strings.Contains(s, "provider: "+dshRoute) || !strings.Contains(s, `model: "deepseek/flash"`) {
t.Fatalf("the web profile magpie made:\n%s", s)
}
if _, err := os.Stat(legacy); err == nil {
t.Fatalf("wrote config.yaml, which this dsh doesn't read:\n%s", read(legacy))
}
a = dsh(home)
if !a.Wired() || a.Check() != "" || a.Drift() != nil {
t.Fatalf("not wired: wired %v, check %q, drift %+v", a.Wired(), a.Check(), a.Drift())
}
// Disconnect before dsh booted it: what magpie made goes
if err := a.Disconnect(); err != nil {
t.Fatal(err)
}
if _, err := os.Stat(filepath.Join(dir, "profiles")); !os.IsNotExist(err) {
t.Fatalf("the profile magpie made outlived Disconnect: %v\n%s", err, read(web))
}
// a dsh before profiles keeps config.yaml
dshVersionIs(t, "0.0.0")
a = dsh(home)
if a.Path != legacy {
t.Fatalf("an old dsh names %s", a.Path)
}
if err := a.Pick("model", "magpie/deepseek/flash"); err != nil {
t.Fatal(err)
}
if s := read(legacy); !strings.Contains(s, "- id: llm-deepseek # magpie") {
t.Fatalf("an old dsh's config.yaml:\n%s", s)
}
if _, err := os.Stat(filepath.Join(dir, "profiles")); !os.IsNotExist(err) {
t.Fatal("made a profile for a dsh before profiles")
}
// its version not known: config.yaml with an entry of the user's says
// it is one before profiles; one with magpie's alone says nothing
dshVersionIs(t, "")
if dshBeforeProfiles(dir, "", false) {
t.Fatal("magpie's entries alone taken for an old dsh's config")
}
if err := os.WriteFile(legacy, []byte("- id: tools\n config:\n disabled: []\n"+read(legacy)), 0o644); err != nil {
t.Fatal(err)
}
if !dshBeforeProfiles(dir, "", false) || dsh(home).Path != legacy {
t.Fatal("an old dsh's own config.yaml not taken for one")
}
}
// liby233's state: magpie wired a dsh 0.2 in config.yaml before it booted a
// profile, and its first boot made the web profile as its template. The
// sync carries magpie's model over to the profile, and config.yaml loses
// magpie's entries; with no profile yet, it makes the web one.
func TestDshConfigYamlCarriedToProfiles(t *testing.T) {
for _, booted := range []bool{true, false} {
home, dir, web := dshRouteHome(t)
if err := os.RemoveAll(dir); err != nil {
t.Fatal(err)
}
dshNoApp(t)
legacy := filepath.Join(dir, "config.yaml")
read := func(p string) string { b, _ := os.ReadFile(p); return string(b) }
// what magpie wrote before, for a dsh it took for an old one
dshVersionIs(t, "0.0.0")
if err := dsh(home).Pick("model", "magpie/deepseek/pro"); err != nil {
t.Fatal(err)
}
if !strings.Contains(read(legacy), "- id: llm-deepseek # magpie") {
t.Fatalf("config.yaml:\n%s", read(legacy))
}
dshVersionIs(t, "0.2.1-rc.5")
if booted {
os.MkdirAll(filepath.Dir(web), 0o755)
os.WriteFile(web, []byte(dshDesktopPatch), 0o644)
os.WriteFile(filepath.Join(filepath.Dir(web), "package.json"), []byte("{\n \"name\": \"dsh-profile-web\"\n}\n"), 0o644)
}
a := dsh(home)
if err := a.Sync(); err != nil {
t.Fatal(err)
}
s := read(web)
if !strings.Contains(s, "- id: llm-pi-ai # magpie") || !strings.Contains(s, "provider: "+dshRoute) || !strings.Contains(s, `model: "deepseek/pro"`) {
t.Fatalf("booted %v: the web profile after the sync:\n%s", booted, s)
}
if s := read(legacy); strings.Contains(s, "# magpie") {
t.Fatalf("booted %v: config.yaml kept magpie's entries:\n%s", booted, s)
}
if a = dsh(home); a.Field("model").Get() != "magpie/deepseek/pro" || a.Check() != "" {
t.Fatalf("booted %v: model %q, check %q", booted, a.Field("model").Get(), a.Check())
}
}
}