Files
magpie/internal/gui/sessions_relocate_test.go
T
Jianyu c984adfbc7 sessions: a renamed or moved Claude Code project folder is relocated with its sessions, from the Sessions page or the CLI (#1131)
Renaming or moving a project folder left Claude Code's transcripts under
the old encoded project path: magpie listed them with a resume command
for a folder that no longer exists, and Claude Code in the new folder
didn't find them. A Claude Code folder row on the Sessions page now has
Relocate project, which opens magpie's own dialog with the new absolute
path, a read-only preview and a confirm that carries the preview's
token; `magpie sessions relocate-claude --from --to [--confirm <token>]`
does the same in a terminal (inside WSL too).

The whole project store moves, session ids, subagents, tool results and
memory with it; the transcripts' top-level cwd and the sessions index
are rewritten, message text is not. The originals and a recovery
manifest are kept outside Claude's project list, writes are staged
before the originals are renamed, a change since the preview is
refused, and a failed publish rolls back. An existing target store,
recently written files, a malformed transcript, a symlink, an ambiguous
encoded path or a long hashed name are refused. Desktop's stores are
left alone. Recovery is in docs/session-relocation.md.

Jianyu's PR head e8263cde, which conflicted with main: the button moved
into main's folder() row, the CLI test into internal/cli beside
sessions_cli.go, and both sides of sessions_manage.go and i18n.js kept.

Run: see the next commit, which adds the zh-TW/ja/de strings and the
GUI test.
2026-10-13 13:02:09 +08:00

76 lines
2.2 KiB
Go

package gui
import (
"bytes"
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"regexp"
"testing"
"time"
"github.com/yetone/magpie/internal/sessions"
)
func TestSessionsRelocateClaudeRoute(t *testing.T) {
h := sandboxHome(t)
root := filepath.Join(h, ".claude")
t.Setenv("CLAUDE_CONFIG_DIR", root)
sessions.Reset()
t.Cleanup(sessions.Reset)
from, to := filepath.Join(h, "old"), filepath.Join(h, "new")
name := regexp.MustCompile(`[^a-zA-Z0-9]`).ReplaceAllString(from, "-")
source := filepath.Join(root, "projects", name)
os.MkdirAll(source, 0o700)
os.Mkdir(to, 0o700)
const id = "11111111-2222-3333-4444-555555555555"
b, _ := json.Marshal(map[string]any{
"type": "user", "cwd": from, "sessionId": id,
"timestamp": "2026-01-01T00:00:00Z", "message": map[string]string{"role": "user", "content": "hello"},
})
os.WriteFile(filepath.Join(source, id+".jsonl"), append(b, '\n'), 0o600)
old := time.Now().Add(-time.Hour)
os.Chtimes(filepath.Join(source, id+".jsonl"), old, old)
os.Chtimes(source, old, old)
if got := sessions.ListAgent("claude"); len(got) != 1 || got[0].Cwd != from {
t.Fatalf("before %+v", got)
}
mux := http.NewServeMux()
sessionManageRoutes(mux, folderOnly{})
post := func(body any) *httptest.ResponseRecorder {
t.Helper()
b, _ := json.Marshal(body)
w := httptest.NewRecorder()
mux.ServeHTTP(w, httptest.NewRequest("POST", "/api/sessions/relocate-claude", bytes.NewReader(b)))
return w
}
in := sessions.ClaudeRelocation{From: from, To: to}
w := post(in)
if w.Code != http.StatusOK {
t.Fatal(w.Body.String())
}
var out sessions.ClaudeRelocationResult
json.Unmarshal(w.Body.Bytes(), &out)
if out.Token == "" || out.Backup != "" || out.Sessions != 1 {
t.Fatalf("preview %+v", out)
}
in.Token = "stale"
if w := post(in); w.Code != http.StatusBadRequest {
t.Fatalf("stale preview accepted: %s", w.Body)
}
in.Token = out.Token
w = post(in)
if w.Code != http.StatusOK {
t.Fatal(w.Body.String())
}
json.Unmarshal(w.Body.Bytes(), &out)
if out.Backup == "" {
t.Fatal("no recovery backup")
}
if got := sessions.ListAgent("claude"); len(got) != 1 || got[0].Cwd != to || got[0].ID != id {
t.Fatalf("stale listing after relocation: %+v", got)
}
}