Files
magpie/internal/agent/wsl_sessions_test.go
T
yetone 643264c24f sessions: the Sessions page and local usage read the agents in WSL too, and resume one there through wsl.exe (lgtm on Discord: magpie can't detect the session lists of agents inside WSL?) — on Windows magpie already finds the WSL distros an agent is set up in (internal/agent's wslDistros, the distro user's home as \\wsl.localhost\<distro>\home\<user>), but the session reader only looked at this computer's own ~/.claude, ~/.codex and ~/.pi. Now internal/agent hands those homes to the sessions package (sessions.WSLHomes, set in init, no second discovery), and the sessions of Claude Code, Codex and Pi in each distro are listed beside this computer's, with Session.WSL naming the distro: a "WSL <distro>" badge on the Sessions page's rows and the management page, a WSL line in the details, " · WSL <distro>" in the API's name. Resume runs in the distro: wsl.exe -d '<distro>' --cd '<cwd>' -e sh -lc 'exec ${SHELL:-sh} -lic ''claude --resume <id>''' (the user's own shell, so the agent's PATH is there; no double quotes, which PowerShell 5.1 mangles). Their calls count in local usage (callFiles). Reading a distro over \\wsl.localhost is slow and opening a file in a stopped distro starts it, so the files are listed in the background, at most every 30 s, the listing kept in sessions-wsl.json; the page waits for it only the first time ever (up to 3 s); a stopped distro's sessions stay listed as last read and nothing in it is opened (the parse cache and call shards keep them). magpie doesn't delete a session in WSL: the checkbox and Delete are off for them, with a note to delete them there, and sessions.Delete refuses. OpenCode in WSL isn't read (its SQLite over \\wsl.localhost isn't reliable). zh strings added. TestWSLSessionsListed, TestWSLStoppedDistroKept and TestWSLNoneWithoutHomes (internal/sessions) and TestWSLSessionsOfTheDistro (internal/agent, FakeWSL: a Claude Code session in a fake Ubuntu is listed with its distro and the wsl.exe resume line) fail on the old code and pass. go vet, builds (darwin, GOOS=linux, GOOS=windows) and go test -tags nogui -count=1 ./... under a temp HOME pass. Not tried on a real WSL with agent sessions: the Windows test box's distros have no Claude Code, Codex or Pi sessions; the resume line wasn't run in PowerShell, and the pages weren't opened in a browser.
2026-10-03 23:55:18 +08:00

52 lines
2.0 KiB
Go

package agent
import (
"os"
"path/filepath"
"strings"
"testing"
"github.com/yetone/magpie/internal/sessions"
)
// The Sessions page reads the sessions of Claude Code in a WSL distro magpie
// found it in (lgtm on Discord: magpie can't see the session lists of agents
// inside WSL), at the distro user's home as the agent is set up there, and
// resumes one through wsl.exe in the distro.
func TestWSLSessionsOfTheDistro(t *testing.T) {
syncHome(t)
root := t.TempDir()
home := filepath.Join(root, "home", "me")
id := "33333333-2222-3333-4444-555555555555"
project := filepath.Join(home, ".claude", "projects", "-home-me-app")
if err := os.MkdirAll(project, 0o755); err != nil {
t.Fatal(err)
}
lines := `{"type":"user","message":{"role":"user","content":"Tidy the README"},"uuid":"u1","timestamp":"2026-09-20T10:00:01.000Z","cwd":"/home/me/app","sessionId":"` + id + `"}
{"parentUuid":"u1","message":{"model":"claude-opus-5-5","id":"msg_1","type":"message","role":"assistant","content":[{"type":"text","text":"Done."}],"usage":{"input_tokens":10,"output_tokens":5}},"requestId":"req_1","type":"assistant","uuid":"a1","timestamp":"2026-09-20T10:00:05.000Z","cwd":"/home/me/app","sessionId":"` + id + `"}
`
if err := os.WriteFile(filepath.Join(project, id+".jsonl"), []byte(lines), 0o644); err != nil {
t.Fatal(err)
}
t.Cleanup(FakeWSL(map[string]string{"Ubuntu": "home:/home/me\ndir:.claude\n"}, map[string]string{"Ubuntu": root}))
sessions.Reset()
t.Cleanup(sessions.Reset)
var got *sessions.Session
for _, s := range sessions.List(0) {
if s.ID == id {
got = &s
}
}
if got == nil {
t.Fatal("the distro's Claude Code session isn't listed")
}
if got.WSL != "Ubuntu" || got.Agent != "claude" || got.Cwd != "/home/me/app" || got.Title != "Tidy the README" || got.Output != 5 {
t.Fatalf("%+v", *got)
}
if !strings.HasPrefix(got.Resume, "wsl.exe -d 'Ubuntu' --cd '/home/me/app' ") || !strings.Contains(got.Resume, "''claude --resume "+id+"''") {
t.Fatalf("resume %q", got.Resume)
}
noOwnClaude(t)
}