Files
magpie/internal/gui/sessions_uninstalled_test.go
T
yetone fe20e4dbd8 sessions: an uninstalled agent's sessions can be deleted and are not offered Resume (_SekiBetu_ on X)
OpenCode (and Cline, Grok, Kimi, Hermes) keep sessions in their own
database, so magpie never deletes them and they stay listed after the
agent is uninstalled, with only Resume. When the agent's command is gone
from PATH, its rows now get Delete (Forget: hidden through the gateway
hidden list, a trash note marked forgot, the agent's files untouched,
Restore unhides) and no resume command. Installed agents are unchanged.

Run on mote: go test -count=1 ./internal/sessions ./internal/gui ok;
go vet darwin, windows, linux -tags nogui ok; gofmt clean; GUI files
sessions-forget (new, 5 languages x chromium+webkit), sessions-gateway-delete,
sessions-manage, sessions-folder-delete, sessions-purge, sessions-row-narrow,
i18n-keys, i18n-en-no-cjk all pass.
Broken by hand: removing the hidden-row filter turned the gui Go test red;
the old client.js turned sessions-forget red (5 of 5 chromium).
Not tried: a real uninstalled OpenCode on a user's machine; the Usage
page's /api/sessions list still shows a forgotten row (manage page only);
the whole GUI suite and full go suite run after the tag.
2026-11-12 00:09:56 +08:00

79 lines
2.7 KiB
Go

package gui
import (
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"
"github.com/yetone/magpie/internal/sessions"
)
// _SekiBetu_ on X: OpenCode was uninstalled and its sessions stayed on the
// management page with a Resume and no Delete. With its command gone, the
// page lists them with a Delete that takes them off the list (OpenCode's
// files are not touched) and no Resume; Restore in the trash lists them
// again. With the command there, as before: no Delete.
func TestManageDeleteOfAnUninstalledAgentTakesItOffTheList(t *testing.T) {
h := sandboxHome(t)
t.Setenv("XDG_DATA_HOME", "")
if err := os.CopyFS(filepath.Join(h, ".local", "share", "opencode"), os.DirFS("../sessions/testdata/opencode")); err != nil {
t.Fatal(err)
}
sessions.Reset()
t.Cleanup(sessions.Reset)
mux := http.NewServeMux()
sessionManageRoutes(mux, folderOnly{})
type manage struct {
Agents []struct {
Agent string `json:"agent"`
Deletable bool `json:"deletable"`
} `json:"agents"`
Sessions []struct {
ID string `json:"id"`
Resume string `json:"resume"`
Deletable bool `json:"deletable"`
Forget bool `json:"forget"`
} `json:"sessions"`
Trash []struct {
Key string `json:"key"`
Forgot bool `json:"forgot"`
} `json:"trash"`
}
call := func(method, path, body string) (m manage) {
t.Helper()
w := httptest.NewRecorder()
mux.ServeHTTP(w, httptest.NewRequest(method, path, strings.NewReader(body)))
if w.Code != 200 {
t.Fatalf("%s %s: %d %s", method, path, w.Code, w.Body)
}
json.Unmarshal(w.Body.Bytes(), &m)
return m
}
m := call("GET", "/api/sessions/manage?agent=opencode", "")
// the fixtures' two: one with a conversation, one nothing was said in
if len(m.Sessions) != 2 || m.Sessions[0].Deletable || m.Sessions[0].Resume == "" {
t.Fatalf("installed: %+v", m.Sessions)
}
id := m.Sessions[0].ID
other := m.Sessions[1].ID
t.Cleanup(sessions.ForceCLIGone("opencode"))
m = call("GET", "/api/sessions/manage?agent=opencode", "")
if len(m.Sessions) != 2 || !m.Sessions[0].Deletable || !m.Sessions[0].Forget || m.Sessions[0].Resume != "" || !m.Agents[0].Deletable {
t.Fatalf("uninstalled: %+v %+v", m.Agents, m.Sessions)
}
call("POST", "/api/sessions/delete", `{"agent":"opencode","ids":["`+id+`"]}`)
m = call("GET", "/api/sessions/manage?agent=opencode", "")
if len(m.Sessions) != 1 || m.Sessions[0].ID != other || len(m.Trash) != 1 || !m.Trash[0].Forgot {
t.Fatalf("after delete: %+v trash %+v", m.Sessions, m.Trash)
}
call("POST", "/api/sessions/restore", `{"key":"`+m.Trash[0].Key+`"}`)
if m = call("GET", "/api/sessions/manage?agent=opencode", ""); len(m.Sessions) != 2 {
t.Fatalf("after restore: %+v", m.Sessions)
}
}