mirror of
https://github.com/runkids/skillshare.git
synced 2026-11-18 18:04:17 +00:00
skillshare backs up AGENTS.md, CLAUDE.md and shared-file locations before
rewriting them, but nothing could list or restore those copies, and a
target snapshot could not be deleted on its own. The file backups now
record why they were taken in their name (<time>.<reason>.bak; older ones
have none), and the server and CLI can list them, show a version and
restore it, saving the current content first. A link is only replaced
when the caller asks for it.
Also:
- DELETE /api/backups/{timestamp} and `backup --delete`.
- Agents snapshots (<target>-agents) could not be restored from the
dashboard: the name was looked up as a target.
- Project mode uses the project's backups folder for snapshots and lists
only file backups inside the project.
- MCP backups report which servers they changed and when.
Refs #299
129 lines
4.7 KiB
Go
129 lines
4.7 KiB
Go
package server
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/url"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
syncpkg "skillshare/internal/sync"
|
|
)
|
|
|
|
// seedFileHistory backs up path's current content with reason, then writes
|
|
// next as the file's new content.
|
|
func seedFileHistory(t *testing.T, path, content, reason, next string) {
|
|
t.Helper()
|
|
os.MkdirAll(filepath.Dir(path), 0755)
|
|
if err := os.WriteFile(path, []byte(content), 0644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := syncpkg.BackupFile(path, reason); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(path, []byte(next), 0644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func TestFileBackupsAPI_ListVersionsRestore(t *testing.T) {
|
|
s, _ := newTestServer(t)
|
|
path := filepath.Join(t.TempDir(), "CLAUDE.md")
|
|
seedFileHistory(t, path, "# before edit\n", syncpkg.BackupReasonEdit, "# now\n")
|
|
|
|
rr := serveJSON(t, s, http.MethodGet, "/api/file-backups", "")
|
|
var list struct {
|
|
Files []struct {
|
|
Path string `json:"path"`
|
|
Versions int `json:"versions"`
|
|
Latest string `json:"latest"`
|
|
} `json:"files"`
|
|
}
|
|
json.Unmarshal(rr.Body.Bytes(), &list)
|
|
if rr.Code != http.StatusOK || len(list.Files) != 1 || list.Files[0].Path != path || list.Files[0].Versions != 1 || list.Files[0].Latest == "" {
|
|
t.Fatalf("list: %d %s", rr.Code, rr.Body.String())
|
|
}
|
|
|
|
q := "?path=" + url.QueryEscape(path)
|
|
rr = serveJSON(t, s, http.MethodGet, "/api/file-backups/versions"+q, "")
|
|
var versions struct {
|
|
Current struct{ Exists bool } `json:"current"`
|
|
Versions []struct {
|
|
ID string `json:"id"`
|
|
Kind string `json:"kind"`
|
|
Reason string `json:"reason"`
|
|
Preview string `json:"preview"`
|
|
} `json:"versions"`
|
|
}
|
|
json.Unmarshal(rr.Body.Bytes(), &versions)
|
|
if rr.Code != http.StatusOK || !versions.Current.Exists || len(versions.Versions) != 1 {
|
|
t.Fatalf("versions: %d %s", rr.Code, rr.Body.String())
|
|
}
|
|
v := versions.Versions[0]
|
|
if v.Kind != "history" || v.Reason != "edit" || v.Preview != "# before edit" {
|
|
t.Fatalf("version = %+v", v)
|
|
}
|
|
|
|
rr = serveJSON(t, s, http.MethodGet, "/api/file-backups/version"+q+"&id="+url.QueryEscape(v.ID), "")
|
|
if rr.Code != http.StatusOK || !strings.Contains(rr.Body.String(), `"content":"# before edit\n"`) || !strings.Contains(rr.Body.String(), `"current":"# now\n"`) {
|
|
t.Fatalf("version: %d %s", rr.Code, rr.Body.String())
|
|
}
|
|
|
|
body, _ := json.Marshal(map[string]string{"path": path, "id": v.ID})
|
|
rr = serveJSON(t, s, http.MethodPost, "/api/file-backups/restore", string(body))
|
|
if rr.Code != http.StatusOK || !strings.Contains(rr.Body.String(), `"backup_id"`) {
|
|
t.Fatalf("restore: %d %s", rr.Code, rr.Body.String())
|
|
}
|
|
if data, _ := os.ReadFile(path); string(data) != "# before edit\n" {
|
|
t.Fatalf("content = %q", data)
|
|
}
|
|
}
|
|
|
|
func TestFileBackupsAPI_Errors(t *testing.T) {
|
|
s, _ := newTestServer(t)
|
|
tmp := t.TempDir()
|
|
path := filepath.Join(tmp, "CLAUDE.md")
|
|
seedFileHistory(t, path, "old", syncpkg.BackupReasonEdit, "now")
|
|
shared := filepath.Join(tmp, "AGENTS.md")
|
|
os.WriteFile(shared, []byte("shared"), 0644)
|
|
os.Remove(path)
|
|
if err := os.Symlink(shared, path); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
versions, _ := syncpkg.FileBackupVersions(path)
|
|
|
|
body, _ := json.Marshal(map[string]string{"path": path, "id": versions[0].ID})
|
|
rr := serveJSON(t, s, http.MethodPost, "/api/file-backups/restore", string(body))
|
|
if rr.Code != http.StatusConflict || !strings.Contains(rr.Body.String(), `"file_backup_is_link"`) {
|
|
t.Fatalf("link: %d %s", rr.Code, rr.Body.String())
|
|
}
|
|
|
|
rr = serveJSON(t, s, http.MethodGet, "/api/file-backups/versions?path="+url.QueryEscape(filepath.Join(tmp, "other.md")), "")
|
|
if rr.Code != http.StatusNotFound || !strings.Contains(rr.Body.String(), `"file_backup_not_found"`) {
|
|
t.Fatalf("unknown path: %d %s", rr.Code, rr.Body.String())
|
|
}
|
|
rr = serveJSON(t, s, http.MethodGet, "/api/file-backups/version?path="+url.QueryEscape(path)+"&id=../../etc", "")
|
|
if rr.Code != http.StatusNotFound {
|
|
t.Fatalf("bad id: %d %s", rr.Code, rr.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestFileBackupsAPI_ProjectModeHidesOutsideFiles(t *testing.T) {
|
|
s, projectRoot := newTestProjectServerWithExtras(t, nil)
|
|
inside := filepath.Join(projectRoot, "CLAUDE.md")
|
|
outside := filepath.Join(t.TempDir(), "CLAUDE.md")
|
|
seedFileHistory(t, inside, "in", syncpkg.BackupReasonShim, "in2")
|
|
seedFileHistory(t, outside, "out", syncpkg.BackupReasonEdit, "out2")
|
|
|
|
rr := serveJSON(t, s, http.MethodGet, "/api/file-backups", "")
|
|
if !strings.Contains(rr.Body.String(), inside) || strings.Contains(rr.Body.String(), outside) {
|
|
t.Fatalf("list: %s", rr.Body.String())
|
|
}
|
|
rr = serveJSON(t, s, http.MethodGet, "/api/file-backups/versions?path="+url.QueryEscape(outside), "")
|
|
if rr.Code != http.StatusForbidden || !strings.Contains(rr.Body.String(), `"file_backup_outside_project"`) {
|
|
t.Fatalf("outside: %d %s", rr.Code, rr.Body.String())
|
|
}
|
|
}
|