Files
magpie/internal/imagemcp/wsl_test.go
T
yetone c426994196 library, imagemcp: magpie's own image server is given to agents in WSL, started through WSL's interop with the paths crossing over (#900, cjyrainbow) — on Windows the Library names magpie-image by this machine's binary ('D:\tools\Magpie\magpie-windows-amd64.exe' mcp image), and turning it on for "Codex · WSL Ubuntu" or "Claude Code · WSL Ubuntu" failed: a Windows program was refused for every agent in WSL. WSL's interop starts a Windows .exe from inside the distro, so magpie's own server (args mcp image, whatever its binary) is now written for an agent in WSL by the binary's path in the distro, asked of the distro itself (wslpath -u, so a custom automount root is followed; asked once in ten minutes; /mnt/<drive>/ when it can't be asked), with --wsl <distro>, --mount where the drives aren't under /mnt/, --home the distro's $HOME, and WSLENV naming the variables it is given (MAGPIE_ADDR), which a Windows program otherwise doesn't get. A distro whose interop is off (no WSLInterop in binfmt_misc) is told so: turn it on in /etc/wsl.conf. A user's own Windows program is still refused, as before: its paths and variables wouldn't cross over. A Linux magpie in the distro isn't looked for: the Windows one reaches the gateway at 127.0.0.1 and reads this machine's settings, the same version as the app, which a Linux one would need the gateway's address from inside WSL for. With --wsl, magpie mcp image opens the paths the agent gives (its roots, reference images, where to save, ~/) as the distro's — /home/me/x through \\wsl.localhost\<distro>\home\me\x (\\wsl$\ where that isn't), /mnt/c/x as C:\x — answers the saved paths as the distro's (/home/me/app/generated-images/…, /mnt/c/…), and with no project falls back to the distro's ~/Pictures/Magpie. Tested on the Windows 11 test box: in a WSL2 Ubuntu 24.04 distro the library's probe gave /mnt/c/…/magpie.exe and interop:on, and this build, started from the distro as the library writes it (the .exe's /mnt/c path, mcp image --wsl, MAGPIE_GATEWAY through WSLENV to a stand-in gateway in the distro), answered initialize and generate_image with a root of /home/yeton/m900proj: a relative path and an absolute reference image (sent as /v1/images/edits), ~/m900proj/home.png and /mnt/c/… were each saved there and answered as the distro's paths. TestWSLMagpieImage (the /mnt/d command, --wsl, WSLENV, nothing rewritten or asked again at the next sync, a custom mount, interop off as a problem) and TestLinuxHome in internal/library, TestWSLPaths in internal/imagemcp are new and fail without the change; TestWSLServers (npx.cmd still refused) passes. go vet, go build (darwin, GOOS=linux nogui, GOOS=windows) and go test -tags nogui ./... under a temp HOME pass (internal/provider failed once under load and passes alone).
2026-10-05 20:09:34 +08:00

53 lines
2.0 KiB
Go

package imagemcp
import "testing"
// Started for an agent in WSL (#900), the paths it gives are opened on
// Windows' side of the distro, and those answered are the distro's again.
func TestWSLPaths(t *testing.T) {
w, err := options([]string{"--wsl", "Ubuntu", "--home", "/home/me"})
if err != nil || w.distro != "Ubuntu" || w.mount != "/mnt/" || w.home != "/home/me" {
t.Fatalf("options: %+v %v", w, err)
}
w.share = `\\wsl.localhost\`
for in, want := range map[string]string{
"/home/me/app": `\\wsl.localhost\Ubuntu\home\me\app`,
"~/app/hero.png": `\\wsl.localhost\Ubuntu\home\me\app\hero.png`,
"/mnt/d/work/a.png": `D:\work\a.png`,
"/mnt/c": `C:\`,
"/mnt/wsl/x": `\\wsl.localhost\Ubuntu\mnt\wsl\x`,
"assets/hero.png": "assets/hero.png",
`D:\work\a.png`: `D:\work\a.png`,
} {
if got := w.local(in); got != want {
t.Errorf("local(%s) = %s, want %s", in, got, want)
}
}
for in, want := range map[string]string{
`\\wsl.localhost\Ubuntu\home\me\app\generated-images\a.png`: "/home/me/app/generated-images/a.png",
`\\wsl$\ubuntu\home\me\a.png`: "/home/me/a.png",
`D:\work\a.png`: "/mnt/d/work/a.png",
`\\wsl.localhost\Debian\home\x.png`: `\\wsl.localhost\Debian\home\x.png`,
} {
if got := w.shown(in); got != want {
t.Errorf("shown(%s) = %s, want %s", in, got, want)
}
}
// a distro mounting the drives elsewhere
w, _ = options([]string{"--wsl", "Ubuntu", "--mount", "/win"})
if got := w.local("/win/e/x.png"); got != `E:\x.png` {
t.Errorf("custom mount: %s", got)
}
if got := w.shown(`E:\x.png`); got != "/win/e/x.png" {
t.Errorf("custom mount shown: %s", got)
}
for _, bad := range [][]string{{"--wsl"}, {"--home", "/home/me"}, {"--nope", "x"}} {
if _, err := options(bad); err == nil {
t.Errorf("options(%v) took it", bad)
}
}
if w, err := options(nil); w != nil || err != nil {
t.Errorf("no options: %+v %v", w, err)
}
}