mirror of
https://github.com/runkids/skillshare.git
synced 2026-11-18 18:04:17 +00:00
* feat(memory): add host-owned review and verification workflows Share scoped memory tools between Dashboard and Memory, and route retrospective and Dreaming requests through the built-in skill. Keep agent execution in the host, require approval for reviewed edits, and preserve versioned CLI write safeguards. Confirm scope before exposing prompts and include the resolved root in recent memory responses. Update provenance guidance and localized documentation, with regressions for unavailable scope and protected review prompts. * fix(memory): distinguish trial copy controls for screen readers Include the localized step title in each trial copy control accessible name while keeping the short visible label. Update the existing clipboard test selector for the distinct names; no new test or local test rerun was requested for this label-only fix. * fix(memory): point connection hints to the trial tools Replace stale verification-prompt directions in all eleven locales with the current Memory tools and Try across sessions labels. Interpolate the existing localized labels so future naming changes remain aligned. No new tests or local test reruns for this copy-only correction, as requested by the maintainer.
292 lines
10 KiB
Go
292 lines
10 KiB
Go
package server
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"skillshare/internal/config"
|
|
"skillshare/internal/memory"
|
|
)
|
|
|
|
func TestMemoryAPI_ScopesAndConflict(t *testing.T) {
|
|
for _, project := range []bool{false, true} {
|
|
t.Run(map[bool]string{false: "global", true: "project"}[project], func(t *testing.T) {
|
|
var s *Server
|
|
var root string
|
|
if project {
|
|
s, root = newTestProjectServerWithExtras(t, nil)
|
|
root = filepath.Join(root, ".skillshare/extras/memory")
|
|
} else {
|
|
s, _ = newTestServerWithExtras(t, nil, "")
|
|
root, _ = memory.GlobalRoot(s.cfg)
|
|
}
|
|
request := func(method, path, body string) *httptest.ResponseRecorder {
|
|
rr := httptest.NewRecorder()
|
|
s.mux.ServeHTTP(rr, httptest.NewRequest(method, path, strings.NewReader(body)))
|
|
return rr
|
|
}
|
|
rr := request("POST", "/api/extras/memory/init", "")
|
|
if rr.Code != http.StatusOK {
|
|
t.Fatalf("init: %d %s", rr.Code, rr.Body)
|
|
}
|
|
for _, path := range []string{"INDEX.md", "LEARNED.md"} {
|
|
if _, err := os.Stat(filepath.Join(root, path)); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
body := `{"path":"build.md","content":"# Build\nUse the devcontainer.","version":""}`
|
|
rr = request("PUT", "/api/extras/memory/notes/content", body)
|
|
if rr.Code != http.StatusOK {
|
|
t.Fatalf("write: %d %s", rr.Code, rr.Body)
|
|
}
|
|
var note memory.Note
|
|
json.Unmarshal(rr.Body.Bytes(), ¬e)
|
|
if note.ModifiedAt == "" || note.Starter {
|
|
t.Fatalf("written note metadata: %+v", note)
|
|
}
|
|
os.WriteFile(filepath.Join(root, "build.md"), []byte("# External"), 0644)
|
|
encoded, _ := json.Marshal(map[string]string{"path": "build.md", "content": "# Lost", "version": note.Version})
|
|
rr = request("PUT", "/api/extras/memory/notes/content", string(encoded))
|
|
if rr.Code != http.StatusConflict {
|
|
t.Fatalf("stale edit: %d %s", rr.Code, rr.Body)
|
|
}
|
|
rr = request("GET", "/api/extras/memory/notes?search=External", "")
|
|
if rr.Code != http.StatusOK || !strings.Contains(rr.Body.String(), "build.md") {
|
|
t.Fatalf("search: %d %s", rr.Code, rr.Body)
|
|
}
|
|
rr = request("GET", "/api/extras/memory/notes/content?path=../outside.md", "")
|
|
if rr.Code != http.StatusBadRequest {
|
|
t.Fatalf("traversal: %d %s", rr.Code, rr.Body)
|
|
}
|
|
encoded, _ = json.Marshal(map[string]string{"path": "build.md", "version": note.Version})
|
|
rr = request("DELETE", "/api/extras/memory/notes/content", string(encoded))
|
|
if rr.Code != http.StatusConflict {
|
|
t.Fatalf("stale delete: %d %s", rr.Code, rr.Body)
|
|
}
|
|
current, err := memory.Read(root, "build.md")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
encoded, _ = json.Marshal(map[string]string{"path": "build.md", "version": current.Version})
|
|
rr = request("DELETE", "/api/extras/memory/notes/content", string(encoded))
|
|
if rr.Code != http.StatusOK {
|
|
t.Fatalf("delete: %d %s", rr.Code, rr.Body)
|
|
}
|
|
rr = request("GET", "/api/extras/memory/notes/content?path=build.md", "")
|
|
if rr.Code != http.StatusNotFound {
|
|
t.Fatalf("deleted note: %d %s", rr.Code, rr.Body)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestMemoryAPI_RecentScopes(t *testing.T) {
|
|
for _, project := range []bool{false, true} {
|
|
t.Run(fmt.Sprintf("project=%t", project), func(t *testing.T) {
|
|
var s *Server
|
|
if project {
|
|
s, _ = newTestProjectServerWithExtras(t, nil)
|
|
} else {
|
|
s, _ = newTestServerWithExtras(t, nil, "")
|
|
}
|
|
initial := decodeBody[memory.Overview](t, serveJSON(t, s, http.MethodGet, "/api/extras/memory/recent", ""))
|
|
if initial.Initialized || initial.Count != 0 {
|
|
t.Fatalf("initial: %+v", initial)
|
|
}
|
|
if rr := serveJSON(t, s, http.MethodPost, "/api/extras/memory/init", ""); rr.Code != http.StatusOK {
|
|
t.Fatalf("init: %d %s", rr.Code, rr.Body)
|
|
}
|
|
root, err := s.memoryRoot()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for i := 0; i < 8; i++ {
|
|
if _, err := memory.Write(root, fmt.Sprintf("note-%d.md", i), "# Note\n", ""); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
rr := serveJSON(t, s, http.MethodGet, "/api/extras/memory/recent", "")
|
|
result := decodeBody[memory.Overview](t, rr)
|
|
if result.Root != root {
|
|
t.Fatalf("recent root: %q, want %q", result.Root, root)
|
|
}
|
|
if rr.Code != http.StatusOK || !result.Initialized || result.Count != 10 || len(result.Notes) != 4 {
|
|
t.Fatalf("recent: %d %+v", rr.Code, result)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestMemoryAPI_UsesExtraSourceAndPreservesIndex(t *testing.T) {
|
|
root := t.TempDir()
|
|
s, _ := newTestServerWithExtras(t, []config.ExtraConfig{{Name: "memory", Source: root}}, "")
|
|
os.WriteFile(filepath.Join(root, "INDEX.md"), []byte("# Personal index"), 0644)
|
|
rr := httptest.NewRecorder()
|
|
s.mux.ServeHTTP(rr, httptest.NewRequest("POST", "/api/extras/memory/init", nil))
|
|
if rr.Code != http.StatusOK {
|
|
t.Fatalf("init: %d %s", rr.Code, rr.Body)
|
|
}
|
|
data, _ := os.ReadFile(filepath.Join(root, "INDEX.md"))
|
|
if string(data) != "# Personal index" {
|
|
t.Fatal("index overwritten")
|
|
}
|
|
}
|
|
|
|
func TestMemoryAPI_MoveScopesAndConflict(t *testing.T) {
|
|
for _, project := range []bool{false, true} {
|
|
t.Run(map[bool]string{false: "global", true: "project"}[project], func(t *testing.T) {
|
|
var s *Server
|
|
var root string
|
|
if project {
|
|
s, root = newTestProjectServerWithExtras(t, nil)
|
|
root = filepath.Join(root, ".skillshare/extras/memory")
|
|
} else {
|
|
s, _ = newTestServerWithExtras(t, nil, "")
|
|
root, _ = memory.GlobalRoot(s.cfg)
|
|
}
|
|
note, err := memory.Write(root, "wiki/source.md", "# Source\n", "")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
request := func(path, newPath, version string) *httptest.ResponseRecorder {
|
|
body, _ := json.Marshal(map[string]string{"path": path, "new_path": newPath, "version": version})
|
|
rr := httptest.NewRecorder()
|
|
s.mux.ServeHTTP(rr, httptest.NewRequest("POST", "/api/extras/memory/move", strings.NewReader(string(body))))
|
|
return rr
|
|
}
|
|
for _, version := range []string{"", "stale"} {
|
|
if rr := request(note.Path, "new.md", version); rr.Code != http.StatusConflict {
|
|
t.Fatalf("stale: %d %s", rr.Code, rr.Body)
|
|
}
|
|
}
|
|
if rr := request(note.Path, "../outside.md", note.Version); rr.Code != http.StatusBadRequest {
|
|
t.Fatalf("unsafe: %d %s", rr.Code, rr.Body)
|
|
}
|
|
if rr := request(note.Path, "renamed.md", note.Version); rr.Code != http.StatusOK {
|
|
t.Fatalf("rename: %d %s", rr.Code, rr.Body)
|
|
}
|
|
rr := request("renamed.md", "projects/deep/moved.md", note.Version)
|
|
if rr.Code != http.StatusOK {
|
|
t.Fatalf("move: %d %s", rr.Code, rr.Body)
|
|
}
|
|
var moved memory.Note
|
|
if err := json.Unmarshal(rr.Body.Bytes(), &moved); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if moved.Path != "projects/deep/moved.md" || moved.Content != note.Content {
|
|
t.Fatalf("response: %+v", moved)
|
|
}
|
|
if _, err := memory.Read(root, note.Path); !os.IsNotExist(err) {
|
|
t.Fatalf("source exists: %v", err)
|
|
}
|
|
if _, err := memory.Write(root, "existing.md", "# Keep\n", ""); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if rr := request(moved.Path, "existing.md", moved.Version); rr.Code != http.StatusConflict {
|
|
t.Fatalf("collision: %d %s", rr.Code, rr.Body)
|
|
}
|
|
current, err := memory.Read(root, "existing.md")
|
|
if err != nil || current.Content != "# Keep\n" {
|
|
t.Fatalf("destination overwritten: %+v %v", current, err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestMemoryAPI_InitializationRequiresStarters(t *testing.T) {
|
|
for _, project := range []bool{false, true} {
|
|
for _, state := range []string{"empty", "missing", "index-only", "learned-only", "ready"} {
|
|
t.Run(fmt.Sprintf("project=%t/%s", project, state), func(t *testing.T) {
|
|
root := filepath.Join(t.TempDir(), "memory")
|
|
extra := []config.ExtraConfig{{Name: "memory", Source: root}}
|
|
var s *Server
|
|
if project {
|
|
s, _ = newTestProjectServerWithExtras(t, []config.ExtraConfig{{Name: "memory"}})
|
|
s.projectCfg.Sources.Extras = filepath.Dir(root)
|
|
if err := s.saveConfig(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.MkdirAll(root, 0755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
} else {
|
|
s, _ = newTestServerWithExtras(t, extra, "")
|
|
}
|
|
if state == "missing" {
|
|
if err := os.Remove(root); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
for _, path := range []string{"INDEX.md", "LEARNED.md"} {
|
|
if state == "ready" || state == "index-only" && path == "INDEX.md" || state == "learned-only" && path == "LEARNED.md" {
|
|
if err := os.WriteFile(filepath.Join(root, path), []byte("# My notes"), 0644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
}
|
|
rr := serveJSON(t, s, http.MethodGet, "/api/extras/memory/notes", "")
|
|
var list struct {
|
|
Initialized bool `json:"initialized"`
|
|
}
|
|
if err := json.Unmarshal(rr.Body.Bytes(), &list); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if rr.Code != http.StatusOK || list.Initialized != (state == "ready") {
|
|
t.Fatalf("state: %d %s", rr.Code, rr.Body)
|
|
}
|
|
rr = serveJSON(t, s, http.MethodPost, "/api/extras/memory/init", "")
|
|
if rr.Code != http.StatusOK {
|
|
t.Fatalf("init: %d %s", rr.Code, rr.Body)
|
|
}
|
|
for _, path := range []string{"INDEX.md", "LEARNED.md"} {
|
|
if _, err := memory.Read(root, path); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestMemoryAPI_InitLogsPartialFailures(t *testing.T) {
|
|
for _, failure := range []string{"starter", "config"} {
|
|
t.Run(failure, func(t *testing.T) {
|
|
s, _ := newTestServerWithExtras(t, nil, "")
|
|
root, err := s.memoryRoot()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if failure == "starter" {
|
|
if err := os.MkdirAll(filepath.Join(root, "LEARNED.md"), 0755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
} else {
|
|
if err := os.Remove(s.configPath()); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.Mkdir(s.configPath(), 0755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
rr := httptest.NewRecorder()
|
|
s.handleMemoryInit(rr, httptest.NewRequest(http.MethodPost, "/api/extras/memory/init", nil))
|
|
if rr.Code < 400 {
|
|
t.Fatalf("expected failure: %d %s", rr.Code, rr.Body)
|
|
}
|
|
if _, err := memory.Read(root, "INDEX.md"); err != nil {
|
|
t.Fatalf("no partial mutation: %v", err)
|
|
}
|
|
entry := latestOpsEntry(t, s)
|
|
if entry.Command != "memory-init" || entry.Status != "error" || entry.Message == "" {
|
|
t.Fatalf("oplog = %+v", entry)
|
|
}
|
|
})
|
|
}
|
|
}
|