Files
Willie 78a8dc7c0d fix(trash): keep install metadata when restoring from the trash (#680)
* fix(trash): keep install metadata when restoring from the trash

Uninstall pruned a skill's, tracked repo's or agent's entry from the
central .metadata.json, and only the files went to the trash, so a
restore brought back a remote install as a local one: update and check
no longer knew its source (#679).

Uninstall now saves the entries it prunes, with their audit acceptances
and target overrides, beside the trash entry, re-keyed to the trash
name so they match where restore puts the item. uninstall.Restore and
RestoreAgent merge them back after the files return, and every restore
path (CLI, trash TUI, dashboard) goes through them; the dashboard
passes its cached store so it does not go stale. The saved copy is
taken with the same rule RemoveByNames uses and copies each entry, so
legacy repo-group entries are kept and re-keying never touches the
live store.

In project mode uninstall also drops the skill's .gitignore rule and
lock pin, so a skill restore reconciles the project skills afterwards.

trash.Delete removes an entry with its saved metadata, and replaces the
direct RemoveAll calls in delete, empty and the 7-day cleanup. Items
trashed before this change have no saved metadata and still restore as
local.

* fix(trash): keep live records of another path when restoring metadata

A legacy store keys a grouped skill by its basename, so the saved copy of
frontend/foo was keyed "foo". If a different skill was installed as
root-level foo while frontend/foo sat in the trash, restoring it replaced
the root skill's record and lost its source.

Extract now writes full source-relative keys (legacy repo-group entries
keep theirs, since their Group is not their path), and Merge keeps a live
entry whose key names a different path instead of overwriting it. Merge
is shared by skill and agent restore, so both are covered.
2026-10-31 17:25:04 +08:00

161 lines
4.9 KiB
Go

package server
import (
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"testing"
"skillshare/internal/install"
"skillshare/internal/trash"
)
func TestHandleListTrash_Empty(t *testing.T) {
s, _ := newTestServer(t)
req := httptest.NewRequest(http.MethodGet, "/api/trash", nil)
rr := httptest.NewRecorder()
s.handler.ServeHTTP(rr, req)
if rr.Code != http.StatusOK {
t.Fatalf("expected 200, got %d: %s", rr.Code, rr.Body.String())
}
var resp struct {
Items []any `json:"items"`
TotalSize int64 `json:"totalSize"`
}
json.Unmarshal(rr.Body.Bytes(), &resp)
if len(resp.Items) != 0 {
t.Errorf("expected 0 trash items, got %d", len(resp.Items))
}
if resp.TotalSize != 0 {
t.Errorf("expected totalSize 0, got %d", resp.TotalSize)
}
}
func TestHandleRestoreTrash_NotFound(t *testing.T) {
s, _ := newTestServer(t)
req := httptest.NewRequest(http.MethodPost, "/api/trash/nonexistent/restore", nil)
rr := httptest.NewRecorder()
s.handler.ServeHTTP(rr, req)
if rr.Code != http.StatusNotFound {
t.Errorf("expected 404, got %d: %s", rr.Code, rr.Body.String())
}
}
func TestHandleRestoreTrash_AgentKind(t *testing.T) {
s, _ := newTestServer(t)
agentsDir := s.cfg.EffectiveAgentsSource()
if err := os.MkdirAll(agentsDir, 0755); err != nil {
t.Fatalf("failed to create agents dir: %v", err)
}
agentFile := filepath.Join(agentsDir, "tutor.md")
if err := os.WriteFile(agentFile, []byte("# Tutor agent"), 0644); err != nil {
t.Fatalf("failed to seed agent: %v", err)
}
if _, err := trash.MoveAgentToTrash(agentFile, "", "tutor", s.agentTrashBase()); err != nil {
t.Fatalf("failed to move agent to trash: %v", err)
}
req := httptest.NewRequest(http.MethodPost, "/api/trash/tutor/restore?kind=agent", nil)
rr := httptest.NewRecorder()
s.handler.ServeHTTP(rr, req)
if rr.Code != http.StatusOK {
t.Fatalf("expected 200, got %d: %s", rr.Code, rr.Body.String())
}
if _, err := os.Stat(filepath.Join(agentsDir, "tutor.md")); err != nil {
t.Fatalf("expected restored agent file, got: %v", err)
}
if entry := trash.FindByName(s.agentTrashBase(), "tutor"); entry != nil {
t.Fatalf("expected agent trash entry to be removed after restore")
}
}
func TestHandleRestoreTrash_KeepsRemoteSource(t *testing.T) {
s, src := newTestServer(t)
addSkill(t, src, "remote-skill")
s.skillsStore.Set("remote-skill", &install.MetadataEntry{Source: "github.com/acme/remote-skill"})
if err := s.skillsStore.Save(src); err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
s.handler.ServeHTTP(rr, httptest.NewRequest(http.MethodDelete, "/api/resources/remote-skill", nil))
if rr.Code != http.StatusOK || s.skillsStore.Has("remote-skill") {
t.Fatalf("uninstall should prune the entry: %d %s", rr.Code, rr.Body.String())
}
rr = httptest.NewRecorder()
s.handler.ServeHTTP(rr, httptest.NewRequest(http.MethodPost, "/api/trash/remote-skill/restore", nil))
if rr.Code != http.StatusOK {
t.Fatalf("expected 200, got %d: %s", rr.Code, rr.Body.String())
}
if e := s.skillsStore.Get("remote-skill"); e == nil || e.Source != "github.com/acme/remote-skill" {
t.Errorf("cached store should hold the restored source, got %+v", e)
}
}
func TestHandleDeleteTrash_NotFound(t *testing.T) {
s, _ := newTestServer(t)
req := httptest.NewRequest(http.MethodDelete, "/api/trash/nonexistent", nil)
rr := httptest.NewRecorder()
s.handler.ServeHTTP(rr, req)
if rr.Code != http.StatusNotFound {
t.Errorf("expected 404, got %d: %s", rr.Code, rr.Body.String())
}
}
func TestHandleEmptyTrash(t *testing.T) {
s, _ := newTestServer(t)
addSkill(t, s.skillsSource(), "trash-skill")
skillDir := filepath.Join(s.skillsSource(), "trash-skill")
if _, err := trash.MoveToTrash(skillDir, "trash-skill", s.trashBase()); err != nil {
t.Fatalf("failed to trash skill: %v", err)
}
agentsDir := s.cfg.EffectiveAgentsSource()
if err := os.MkdirAll(agentsDir, 0755); err != nil {
t.Fatalf("failed to create agents dir: %v", err)
}
agentFile := filepath.Join(agentsDir, "tutor.md")
if err := os.WriteFile(agentFile, []byte("# Tutor agent"), 0644); err != nil {
t.Fatalf("failed to seed agent: %v", err)
}
if _, err := trash.MoveAgentToTrash(agentFile, "", "tutor", s.agentTrashBase()); err != nil {
t.Fatalf("failed to trash agent: %v", err)
}
req := httptest.NewRequest(http.MethodPost, "/api/trash/empty", nil)
rr := httptest.NewRecorder()
s.handler.ServeHTTP(rr, req)
if rr.Code != http.StatusOK {
t.Fatalf("expected 200, got %d: %s", rr.Code, rr.Body.String())
}
var resp struct {
Success bool `json:"success"`
Removed int `json:"removed"`
}
json.Unmarshal(rr.Body.Bytes(), &resp)
if !resp.Success {
t.Error("expected success true")
}
if resp.Removed != 2 {
t.Errorf("expected 2 removed, got %d", resp.Removed)
}
if len(trash.List(s.trashBase())) != 0 {
t.Errorf("expected skill trash to be empty after empty")
}
if len(trash.List(s.agentTrashBase())) != 0 {
t.Errorf("expected agent trash to be empty after empty")
}
}