Files
skillshare/internal/server/handler_instructions_setup_test.go
Willie b8291895e1 feat(ui): share one AGENTS.md across tools from the dashboard
Claude Code now reads AGENTS.md, but every tool still keeps its standing
instructions in its own file (CLAUDE.md, GEMINI.md, AGENTS.md), and Claude
has no user-level AGENTS.md at all. Keeping several tools on the same
instructions meant copying text between those files by hand.

Each target page gets a tab named after the file it reads, with its read
order, an editor, and a convert flow from CLAUDE.md to AGENTS.md. In global
mode the Extras page gets an AGENTS.md tab where several shared files can
coexist and each target picks one or keeps its own. Shared files are
single-file extras: tools that follow @imports get a managed import block
and keep their own lines, others get a link in place of their file.
Projects get small shims instead, since every tool there reads ./AGENTS.md.
Custom targets can declare which file they read.

Restore records the target's state when it is first attached (file,
symlink, or no file) and always returns to it. Edits made after attaching
are kept as drift backups instead of becoming the restore point, so sync,
reapply and mode switches can no longer lose the user's original file.
2026-09-27 03:02:51 +08:00

118 lines
5.3 KiB
Go

package server
import (
"net/http"
"os"
"path/filepath"
"strings"
"testing"
"skillshare/internal/config"
)
func getTargetInstructions(t *testing.T, s *Server, name string) instructionsFileResponse {
t.Helper()
return decodeBody[instructionsFileResponse](t, instructionsRequest(t, s, http.MethodGet, "/api/targets/"+name+"/instructions", ""))
}
func TestInstructionsSetup_CustomTargetBecomesSupported(t *testing.T) {
s, home := newInstructionsServer(t, "myagent")
if got := getTargetInstructions(t, s, "myagent"); got.Supported {
t.Fatalf("before setup: %+v, want unsupported", got)
}
rr := instructionsRequest(t, s, http.MethodPut, "/api/targets/myagent/instructions/setup", `{"path":"~/.myagent/AGENTS.md","import":true}`)
if rr.Code != http.StatusOK {
t.Fatalf("setup: %d %s", rr.Code, rr.Body.String())
}
got := getTargetInstructions(t, s, "myagent")
if !got.Supported || !got.Custom || !got.Import || got.Path != filepath.Join(home, ".myagent", "AGENTS.md") || got.Setup.Path != "~/.myagent/AGENTS.md" {
t.Errorf("after setup: %+v", got)
}
}
func TestInstructionsSetup_RejectsRelativeGlobalPath(t *testing.T) {
s, _ := newInstructionsServer(t, "myagent")
rr := instructionsRequest(t, s, http.MethodPut, "/api/targets/myagent/instructions/setup", `{"path":"AGENTS.md"}`)
if rr.Code != http.StatusBadRequest {
t.Fatalf("setup relative: %d %s, want 400", rr.Code, rr.Body.String())
}
}
func TestInstructionsSetup_CustomTargetCanUseSharedFile(t *testing.T) {
s, home := newInstructionsServer(t, "myagent")
instructionsRequest(t, s, http.MethodPut, "/api/targets/myagent/instructions/setup", `{"path":"~/.myagent/AGENTS.md","import":true}`)
instructionsRequest(t, s, http.MethodPost, "/api/instructions", `{"name":"personal","content":"p\n"}`)
list := decodeBody[struct {
Targets []sharedInstructionsTarget `json:"targets"`
}](t, instructionsRequest(t, s, http.MethodGet, "/api/instructions", ""))
if len(list.Targets) != 1 || list.Targets[0].Name != "myagent" {
t.Fatalf("grouping targets = %+v, want myagent", list.Targets)
}
res := decodeBody[map[string]any](t, instructionsRequest(t, s, http.MethodPost, "/api/instructions/assign", `{"targets":["myagent"],"extras":["personal"]}`))
if res["success"] != true {
t.Fatalf("assign: %v", res)
}
if got := readFile(t, filepath.Join(home, ".myagent", "AGENTS.md")); !strings.Contains(got, "@") {
t.Errorf("AGENTS.md = %q, want an import of the shared file", got)
}
}
func TestInstructionsSetup_RemoveRefusedWhileShared(t *testing.T) {
s, _ := newInstructionsServer(t, "myagent")
instructionsRequest(t, s, http.MethodPut, "/api/targets/myagent/instructions/setup", `{"path":"~/.myagent/AGENTS.md","import":true}`)
instructionsRequest(t, s, http.MethodPost, "/api/instructions", `{"name":"personal","content":"p\n"}`)
instructionsRequest(t, s, http.MethodPost, "/api/instructions/assign", `{"targets":["myagent"],"extras":["personal"]}`)
rr := instructionsRequest(t, s, http.MethodDelete, "/api/targets/myagent/instructions/setup", "")
if res := decodeBody[map[string]any](t, rr); rr.Code != http.StatusConflict || res["error_code"] != errInstructionsInUse {
t.Fatalf("remove while shared: %d %v, want 409 %s", rr.Code, res, errInstructionsInUse)
}
rr = instructionsRequest(t, s, http.MethodPut, "/api/targets/myagent/instructions/setup", `{"path":"~/.other/AGENTS.md"}`)
if rr.Code != http.StatusConflict {
t.Fatalf("move while shared: %d %s, want 409", rr.Code, rr.Body.String())
}
instructionsRequest(t, s, http.MethodPost, "/api/instructions/personal/restore", `{"target":"myagent"}`)
if rr := instructionsRequest(t, s, http.MethodDelete, "/api/targets/myagent/instructions/setup", ""); rr.Code != http.StatusOK {
t.Fatalf("remove: %d %s", rr.Code, rr.Body.String())
}
if got := getTargetInstructions(t, s, "myagent"); got.Supported || got.Custom {
t.Errorf("after remove: %+v, want unsupported", got)
}
}
func TestInstructionsSetup_ProjectPathIsRelativeToRoot(t *testing.T) {
s, root := newTestProjectServerWithExtras(t, nil)
s.projectCfg.Targets = []config.ProjectTargetEntry{{Name: "myagent", Skills: &config.ResourceTargetConfig{Path: ".myagent/skills"}}}
if err := s.projectCfg.Save(root); err != nil {
t.Fatal(err)
}
if err := s.reloadConfig(); err != nil {
t.Fatal(err)
}
rr := instructionsRequest(t, s, http.MethodPut, "/api/targets/myagent/instructions/setup", `{"path":".myagent/AGENTS.md"}`)
if rr.Code != http.StatusOK {
t.Fatalf("setup: %d %s", rr.Code, rr.Body.String())
}
if got := getTargetInstructions(t, s, "myagent"); !got.Custom || got.Path != filepath.Join(root, ".myagent", "AGENTS.md") {
t.Errorf("project setup: %+v", got)
}
data, err := os.ReadFile(config.ProjectConfigPath(root))
if err != nil || !strings.Contains(string(data), "instructions:") {
t.Errorf("project config = %q, %v; want instructions saved", data, err)
}
}
// The dashboard prefills its "change" form from the raw JSON keys.
func TestInstructionsSetup_ResponseUsesJSONKeys(t *testing.T) {
s, _ := newInstructionsServer(t, "myagent")
instructionsRequest(t, s, http.MethodPut, "/api/targets/myagent/instructions/setup", `{"path":"~/.myagent/AGENTS.md","import":true}`)
body := instructionsRequest(t, s, http.MethodGet, "/api/targets/myagent/instructions", "").Body.String()
if !strings.Contains(body, `"setup":{"path":"~/.myagent/AGENTS.md","import":true}`) {
t.Errorf("setup JSON = %s", body)
}
}