Files
magpie/internal/library/rtk_wsl_test.go
T
yetone 3ce2a9819f library: RTK for agents in WSL, with the distro's own rtk (lex on Discord)
lex: Claude Code and other agents in WSL couldn't use the RTK of magpie
on Windows. rtkAgents looked an agent up in rtkSpecs by its whole id, so
claude@wsl:Ubuntu never matched and WSL agents weren't on the RTK page at
all. Even matched, the specs read this machine's home, and Windows' rtk.exe
is no command inside the distro, where the hook runs.

Now an agent is looked up by its kind (rtkKind). One in WSL:
- has its files read and written under the distro's home as magpie opens
  it (rtkClaudeDir, rtkCodexDir, rtkHome), with blocked/version checks
  against the distro's rtk and OpenCode;
- uses the distro's own rtk: rtkDistroOf asks the distro (wsl.exe -d <d>
  -e sh -lc) where rtk is on its shells' PATH, else in ~/.local/bin or
  ~/.cargo/bin (offPath), kept 15s; ReadRTK lists one RTKDistro per distro;
- switching on runs that rtk there (rtk init -g ..., its folder on PATH);
- Install RTK in WSL <distro> (POST /api/library/rtk/install {distro},
  magpie library rtk install <distro>) runs RTK's own Linux installer in
  the distro (curl, or wget).
A distro that isn't running is never started (#3896): its agents are
listed asleep with the switch off, and switching or installing says to
start it. The page has a card per distro (install, off-PATH, asleep) and
uses the distro's rtk for each WSL row's tags and switch; strings in zh,
zh-TW, ja and de. The install route now refuses a body it can't read
instead of installing on this computer.

Run:
- mote (macOS 27), at origin/main ec8fe29b8 plus this: gofmt -l clean;
  go vet darwin, GOOS=linux -tags nogui, GOOS=windows for internal/library,
  internal/gui, internal/cli: ok. go test ./internal/library ./internal/cli
  ok; ./internal/gui: TestMainWindowCorners/macOS_26_SDK red (#3949, open,
  mote's macOS 27), everything else ok; -run Library ok after the route
  change. node --test library-*.test.cjs + i18n-keys in Chromium and WebKit:
  828 tests, 3 red, all library-row-remove (#3099, open; 1 of 2 runs alone
  is red the same); library-rtk-*.test.cjs + i18n-keys at the pushed base:
  180 pass. New library-rtk-wsl.test.cjs: en, zh, zh-TW, ja, de at 980 and
  380px, both engines.
- New TestRTKWSL / TestRTKWSLAsleep (fake wsl.exe runs the real probe
  script with sh, the distro's home as HOME). Broken by hand: the old
  rtkSpecs[a.ID] lookup makes TestRTKWSL red (claude@wsl not on the page).
  Also ok on Windows (win-run) and Linux (wsl-run), -run 'RTK|WSL'.
- Real WSL, xiaomi-gpu with magpie-desktop (Ubuntu 24.04): from Windows,
  through real wsl.exe and the exact rtkWSLScript/rtkWSLProbe/rtkInitWSL
  strings, with HOME set to a throwaway folder in the distro: RTK's
  installer put rtk 1.1.0 in ~/.local/bin, the probe found it (on:0, not
  on that bare PATH), rtk init -g --auto-patch wrote "rtk hook claude" into
  ~/.claude/settings.json, and sp.has read it back over
  \\wsl.localhost\magpie-desktop\.... ReadRTK on Windows lists
  claude@wsl, codex@wsl and pi@wsl:magpie-desktop with an install offered.

Not tried: a real Claude Code session in WSL running a command through
the hook; the install and switch in the distro's real home (only the
throwaway HOME); the real GUI window on Windows; OpenCode, Gemini, agy,
Hermes, OMP in WSL (same code path, not run for real).
2026-10-29 19:58:40 +08:00

170 lines
5.4 KiB
Go

package library
import (
"errors"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"sync"
"testing"
"time"
"github.com/yetone/magpie/internal/agent"
"github.com/yetone/magpie/internal/testenv"
)
// fakeDistroRTK stands in for wsl.exe -d <distro> -e sh -lc <script> on the
// distros of wslSandbox: the script runs here, with the distro's home as
// HOME and a PATH with no rtk on it, except RTK's installer, which isn't
// fetched (a test never reaches the network): it puts fakeRTK where the
// real one puts rtk, ~/.local/bin. It counts what ran in each distro.
func fakeDistroRTK(t *testing.T, home string) *sync.Map {
t.Helper()
ran := &sync.Map{}
old := rtkWSLRun
rtkWSLRun = func(_ time.Duration, args ...string) ([]byte, error) {
if len(args) != 6 || args[0] != "-d" || args[2] != "-e" || args[3] != "sh" || args[4] != "-lc" {
return nil, errors.New("unexpected wsl.exe " + strings.Join(args, " "))
}
n, _ := ran.LoadOrStore(args[1], new(int))
*n.(*int)++
if args[5] == rtkWSLScript+" 2>&1" {
bin := filepath.Join(home, ".local", "bin", "rtk")
if err := os.MkdirAll(filepath.Dir(bin), 0o755); err != nil {
return nil, err
}
return []byte("installed\n"), os.WriteFile(bin, []byte(fakeRTK), 0o755)
}
cmd := exec.Command("/bin/sh", "-c", args[5])
cmd.Env = []string{"HOME=" + home, "PATH=/usr/bin:/bin"}
return cmd.Output()
}
forget := func() {
rtkDistros_.Lock()
rtkDistros_.at, rtkDistros_.got = nil, nil
rtkDistros_.Unlock()
}
forget()
t.Cleanup(func() { rtkWSLRun = old; forget() })
return ran
}
func runsIn(ran *sync.Map, distro string) int {
if n, ok := ran.Load(distro); ok {
return *n.(*int)
}
return 0
}
func rtkRow(t *testing.T, v *RTKView, id string) RTKAgent {
t.Helper()
for _, a := range v.Agents {
if a.ID == id {
return a
}
}
t.Fatalf("%s isn't on the RTK page: %+v", id, v.Agents)
return RTKAgent{}
}
// Claude Code in a WSL distro gets RTK (lex on Discord): listed, with the
// distro's own rtk, installed there by RTK's installer and run there for
// the hook, which lands in the distro's ~/.claude, not this machine's.
func TestRTKWSL(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("the fake distro runs sh")
}
h := wslSandbox(t)
ran := fakeDistroRTK(t, h)
const distro = "Ubuntu-24.04"
hooked := func(dir string) bool {
b, _ := os.ReadFile(filepath.Join(dir, "settings.json"))
return strings.Contains(string(b), "rtk hook claude")
}
v := ReadRTK()
if a := rtkRow(t, v, wslClaude); a.WSL != distro || a.On || a.Asleep {
t.Fatalf("WSL Claude Code's row: %+v", a)
}
if len(v.WSL) != 1 || v.WSL[0].Name != distro || v.WSL[0].Path != "" || v.WSL[0].Install != rtkWSLShown || v.WSL[0].Asleep {
t.Fatalf("the distro's RTK, with none installed: %+v", v.WSL)
}
// no rtk in the distro: nothing to switch it on with
if _, err := SetRTK(wslClaude, true); err == nil || !strings.Contains(err.Error(), "isn't installed in WSL "+distro) {
t.Fatalf("switched on with no rtk in the distro: %v", err)
}
if _, err := InstallRTKIn("no-such-distro"); err == nil {
t.Fatal("installed in a distro no agent is in")
}
// the distro's name as the user types it
v, err := InstallRTKIn(strings.ToLower(distro))
if err != nil {
t.Fatal(err)
}
bin := filepath.Join(h, ".local", "bin", "rtk")
if d := v.WSL[0]; d.Path != bin || d.Version != "9.9.9" || !d.OffPath || d.Install != "" {
t.Fatalf("the distro's RTK after the install: %+v", d)
}
if _, err := SetRTK(wslClaude, true); err != nil {
t.Fatal(err)
}
if !hooked(filepath.Join(h, ".claude")) {
t.Fatal("no hook in the distro's ~/.claude/settings.json")
}
if hooked(filepath.Join(os.Getenv("HOME"), ".claude")) {
t.Fatal("the hook went into this machine's Claude Code")
}
if a := rtkRow(t, ReadRTK(), wslClaude); !a.On {
t.Fatalf("switched on, yet the row is off: %+v", a)
}
// this machine's Claude Code is its own
if a := rtkRow(t, ReadRTK(), "claude"); a.On || a.WSL != "" {
t.Fatalf("this machine's Claude Code: %+v", a)
}
if _, err := SetRTK(wslClaude, false); err != nil {
t.Fatal(err)
}
if hooked(filepath.Join(h, ".claude")) {
t.Fatal("switched off, the hook is still there")
}
if runsIn(ran, distro) == 0 {
t.Fatal("nothing ran in the distro")
}
}
// A distro that isn't running is never started for RTK (#3896): its agents
// are listed asleep, and switching or installing says to start it.
func TestRTKWSLAsleep(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("the fake distro runs sh")
}
h := wslSandbox(t)
ran := fakeDistroRTK(t, h)
const distro = "Ubuntu-24.04"
testenv.Program(t, filepath.Join(h, ".local", "bin", "rtk"), fakeRTK)
// seen while it ran, then stopped
if a := rtkRow(t, ReadRTK(), wslClaude); a.Asleep {
t.Fatalf("running, yet asleep: %+v", a)
}
agent.StopFakeWSL(distro)
before := runsIn(ran, distro)
v := ReadRTK()
if a := rtkRow(t, v, wslClaude); !a.Asleep || a.On || a.WSL != distro {
t.Fatalf("a stopped distro's Claude Code: %+v", a)
}
if len(v.WSL) != 1 || !v.WSL[0].Asleep || v.WSL[0].Path != "" {
t.Fatalf("a stopped distro's RTK: %+v", v.WSL)
}
if _, err := SetRTK(wslClaude, true); err == nil || !strings.Contains(err.Error(), "isn't running") {
t.Fatalf("switched in a stopped distro: %v", err)
}
if _, err := InstallRTKIn(distro); err == nil || !strings.Contains(err.Error(), "isn't running") {
t.Fatalf("installed in a stopped distro: %v", err)
}
if n := runsIn(ran, distro) - before; n != 0 {
t.Fatalf("%d commands ran in the stopped distro, each starting it", n)
}
}