mirror of
https://github.com/runkids/skillshare.git
synced 2026-11-18 18:04:17 +00:00
* docs(proposals): propose agent-specific skill variants Tracked repositories can ship different files for the same skill across agents. Counting and syncing each copy independently sends incompatible instructions to every target unless users maintain routing filters. Propose repository-scoped logical identity and exact variant routing while preserving source files, explicit controls, and existing naming behavior. Leave activation and compatibility decisions for maintainer review. Refs #648 * docs(proposals): clarify variant routing compatibility Explain how recognition affects universal-only targets and how existing per-variant overrides preserve the desired routing. Fix the precedence between variant identity and generic host-style path inference. Include analyze, move, and catalog consumers while preserving physical identifiers and actual context-cost accounting. Refs #648 * docs(proposals): clarify activation assumptions and scope Make acceptance cases conditional on maintainer-approved recognition being enabled for the tracked source. Separate the provisional implementation estimate from this proposal-only PR so its size is clear. Refs #648 * docs(proposals): qualify naming and opt-in assumptions State that the reproduced routing behavior uses default flat target naming. Leave any opt-in surface with the maintainer's activation decision without ruling out future CLI or config fields. Refs #648 * feat: recognize and route tracked agent skill variants Count tracked variants as logical skills in source and repository summaries, while preserving physical paths, sync operations, and context costs. Select matching canonical agent variants, including account targets, through shared discovery and selection with existing explicit routing precedence. Refs #648 * fix: count logical skills in sync inventory summaries Use shared logical and physical inventory counts in global and project sync closing lines while preserving each target's operation totals. Refs #648 * fix: preserve variant routing diagnostics across previews Resolve target selections once so CLI and server previews keep ambiguity reasons and physical skip counts. Explain managed variants removed by automatic agent routing, and report logical inventory in doctor. Keep warning rows read-only, document upgrade recovery in translated filtering references, and remove the optional README-only addition. Refs #648 * fix: align sync matrix previews with shared variant selection Classify the full source selection once per target so matrix previews honor canonical aliases, account identity, exact-agent routing, and ambiguous variant skips. Preserve each rejected physical path's existing diagnostic and draft filter disambiguation; skipped preview rows remain read-only. Refs #648 * test: expect singular skill wording in doctor source checks Keep the undiscovered-link count assertion aligned with logical source inventory formatting for one skill in global and project mode. Refs #648 * fix: retain selection diagnostics in catalog and diff verdicts Report warning-only diff targets as requiring attention while preserving local-only folder semantics. Reuse shared per-path selection diagnostics in target-scoped catalogs without applying target naming to served paths. Compute source counts once in summary callers and require variant counts in the source status formatter. Refs #648 * test: tighten tracked variant preview fixtures Use a real committed tracked repository for status counts, verify sync preview banners retain server warnings, and use emitted ambiguity reason text in target preview fixtures. Refs #648 * fix(sync): keep agent variants in shared target folders Compatible targets sharing a skills folder need a combined selection so Codex and universal do not prune one another in merge or copy mode. Carry every shared reader through selection, pruning and previews, including project targets, account identities and path aliases. Require a matching skill suffix under two distinct agents before inferring variants, keeping ordinary agent-named categories unrestricted. Separate naming warnings from ambiguous selection in diff and explain ambiguous removals. Add global/project regression coverage for repeated sync, target-only sync, status JSON, diff and API consumers. * fix(variants): align consumer counts, hints and diagnostics Use logical source counts in list, tracked install and init summaries while matching audit caches against physical variants. Restore localized dashboard skip hints and explain symlink routing limits in status and doctor, including project and JSON output. Document paired-agent detection and compatible shared-folder selection in the proposal, built-in skill and English plus translated command pages. Add focused regressions for the affected consumers. * fix(sync): cover initial sync and ignored variant diagnostics Init syncs a fresh in-memory config, so resolve shared target identities before its first sync as well as when loading saved configuration. A whole-directory symlink exposes ignored skills too. Diagnose agent variants using physical inventory when active discovery has none, while keeping logical counts and merge/copy selection restricted to enabled skills. Cover global/project JSON status and doctor with regressions. Reuse sourcewalk directory and canonical-path helpers so traversal guards remain enforced and path resolution has a single existing owner. * fix(variants): address conversion, counts and selection diagnostics Ignore runtime agent identities when comparing convertible project targets and count logical skills during init. Keep selection warnings separate from naming warnings and preserve agent-named parent skills as ordinary skills. Cover account sync/prune, project ambiguity and overrides in merge/copy modes, and init/status agreement. Focused verification ran in an isolated Linux Codex container, not the devcontainer, as authorized. * refactor(variants): remove obsolete selection helpers Remove the unused FilterSkillsByTarget wrapper and prune agent parameter, cache canonical target aliases once, and restore catalog skip ordering to avoid an unrelated behavior change. Completes the optional cleanup from PR 649 review. * fix(ui): expose localized variant selection diagnostics Show target warnings with source paths, localize ambiguity across preview and detail consumers, and let skipped preview rows add a physical exclude pattern. Show tracked repository variant counts alongside logical skills. Drop display-only rendered cases while retaining mutation protection and plain TypeScript logic coverage. Inspect target states in Clean/Playful light/dark and Traditional Chinese. * docs(variants): explain diagnostics and add sync runbook Document doctor variant_routing and logical/physical counts, diff selection skips, and the nested parent-skill boundary. Keep all four command translations aligned and add isolated account/project E2E scenarios. Both runbook steps passed in the authorized Linux Codex container with separate HOME/XDG/TMPDIR fixtures. Website typecheck and all five locale builds passed. * test(config): use native paths in project conversion fixture The shared-agent regression fixture appended slash-separated suffixes to Windows temporary paths, so project path matching returned no candidates. Build each path with filepath.Join and preserve all conversion assertions. Focused project configuration tests passed in the authorized isolated Linux Codex container, not the devcontainer. Windows CI verifies the platform-specific fixture correction. * fix(ui): recompute shared skill routing for filter previews Evaluate edited include/exclude settings in the complete target map before classifying the draft, so a formerly shared folder no longer keeps its old union when the filters differ. Preserve the saved target configuration. * fix(mcp): keep parent skills when peer variants route elsewhere Do not treat an otherwise eligible variant for another agent as an unserved nested skill. Continue blocking parent publication for explicitly filtered, disabled, ambiguous, or invalid children so filters still hide their files. * fix(ui): distinguish agent selection from naming skips Expose separate naming and selection skip counts on target summaries. Show each applicable remedy in target details and sync previews so ambiguity keeps its filter/override guidance alongside naming and collision hints. * fix(list): include disabled rows in tracked repository counts Count the complete inventory that list displays, including ignored skills and physical variants, without changing the enabled counts used by status, install, or sync. Clone rows before counting to preserve disabled state. * docs: align variant counts and routing recovery guidance Show logical skill and physical variant counts in list/install documentation and keep the four translations aligned with the filtering examples. Explain that agent-named skill parents keep ordinary nested skills and retain the devcontainer-only runbook environment. Removal warnings now recommend a source-local override, since include filters cannot undo agent routing. * fix(mcp): preserve explicit target restrictions on nested variants Limit the differently routed child exemption to automatic layout inference. Frontmatter targets and dashboard source-local overrides must still prevent a parent from publishing files explicitly restricted to another target. * fix(ui): fill target name in naming warning guidance Pass the current target name when rendering localized warning hints so the same naming guidance shown beside the controls does not expose a placeholder in the warning banner. Verified in the populated target preview. --------- Co-authored-by: Willie <egg358358@gmail.com>
739 lines
27 KiB
Go
739 lines
27 KiB
Go
package server
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"skillshare/internal/config"
|
|
ssync "skillshare/internal/sync"
|
|
)
|
|
|
|
func TestHandleListTargets_Empty(t *testing.T) {
|
|
s, _ := newTestServer(t)
|
|
req := httptest.NewRequest(http.MethodGet, "/api/targets", 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 {
|
|
Targets []any `json:"targets"`
|
|
}
|
|
json.Unmarshal(rr.Body.Bytes(), &resp)
|
|
if len(resp.Targets) != 0 {
|
|
t.Errorf("expected 0 targets, got %d", len(resp.Targets))
|
|
}
|
|
}
|
|
|
|
func TestHandleListTargets_WithTargets(t *testing.T) {
|
|
tgtPath := filepath.Join(t.TempDir(), "claude-skills")
|
|
s, _ := newTestServerWithTargets(t, map[string]string{"claude": tgtPath})
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/api/targets", 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 {
|
|
Targets []map[string]any `json:"targets"`
|
|
}
|
|
json.Unmarshal(rr.Body.Bytes(), &resp)
|
|
if len(resp.Targets) != 1 {
|
|
t.Fatalf("expected 1 target, got %d", len(resp.Targets))
|
|
}
|
|
if resp.Targets[0]["name"] != "claude" {
|
|
t.Errorf("expected target name 'claude', got %v", resp.Targets[0]["name"])
|
|
}
|
|
}
|
|
|
|
func TestHandleAddTarget_Success(t *testing.T) {
|
|
s, _ := newTestServer(t)
|
|
body := `{"name":"test-target","path":"/tmp/test-target"}`
|
|
req := httptest.NewRequest(http.MethodPost, "/api/targets", strings.NewReader(body))
|
|
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 map[string]any
|
|
json.Unmarshal(rr.Body.Bytes(), &resp)
|
|
if resp["success"] != true {
|
|
t.Error("expected success true")
|
|
}
|
|
}
|
|
|
|
func TestHandleAddTarget_SavesInstructionFile(t *testing.T) {
|
|
s, _ := newTestServer(t)
|
|
body := `{"name":"myagent","path":"/tmp/myagent/skills","instructions":{"path":"~/.myagent/AGENTS.md","import":true}}`
|
|
req := httptest.NewRequest(http.MethodPost, "/api/targets", strings.NewReader(body))
|
|
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 got := s.cfg.Targets["myagent"].Instructions; got == nil || got.Path != "~/.myagent/AGENTS.md" || !got.Import {
|
|
t.Errorf("instructions = %+v, want ~/.myagent/AGENTS.md with import", got)
|
|
}
|
|
}
|
|
|
|
func TestHandleAddTarget_RejectsRelativeInstructionFile(t *testing.T) {
|
|
s, _ := newTestServer(t)
|
|
body := `{"name":"myagent","path":"/tmp/myagent/skills","instructions":{"path":"AGENTS.md"}}`
|
|
req := httptest.NewRequest(http.MethodPost, "/api/targets", strings.NewReader(body))
|
|
rr := httptest.NewRecorder()
|
|
s.handler.ServeHTTP(rr, req)
|
|
|
|
if rr.Code != http.StatusBadRequest {
|
|
t.Fatalf("expected 400, got %d: %s", rr.Code, rr.Body.String())
|
|
}
|
|
if _, exists := s.cfg.Targets["myagent"]; exists {
|
|
t.Error("target was added despite the invalid instruction file")
|
|
}
|
|
}
|
|
|
|
func TestHandleAddTarget_MissingName(t *testing.T) {
|
|
s, _ := newTestServer(t)
|
|
body := `{"path":"/tmp/test"}`
|
|
req := httptest.NewRequest(http.MethodPost, "/api/targets", strings.NewReader(body))
|
|
rr := httptest.NewRecorder()
|
|
s.handler.ServeHTTP(rr, req)
|
|
|
|
if rr.Code != http.StatusBadRequest {
|
|
t.Errorf("expected 400 for missing name, got %d", rr.Code)
|
|
}
|
|
}
|
|
|
|
func TestHandleAddTarget_Duplicate(t *testing.T) {
|
|
tgtPath := filepath.Join(t.TempDir(), "claude-skills")
|
|
s, _ := newTestServerWithTargets(t, map[string]string{"claude": tgtPath})
|
|
|
|
body := `{"name":"claude","path":"/tmp/another"}`
|
|
req := httptest.NewRequest(http.MethodPost, "/api/targets", strings.NewReader(body))
|
|
rr := httptest.NewRecorder()
|
|
s.handler.ServeHTTP(rr, req)
|
|
|
|
if rr.Code != http.StatusConflict {
|
|
t.Errorf("expected 409 for duplicate target, got %d", rr.Code)
|
|
}
|
|
}
|
|
|
|
func TestHandleRemoveTarget_Success(t *testing.T) {
|
|
tgtPath := filepath.Join(t.TempDir(), "claude-skills")
|
|
s, _ := newTestServerWithTargets(t, map[string]string{"claude": tgtPath})
|
|
|
|
req := httptest.NewRequest(http.MethodDelete, "/api/targets/claude", 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())
|
|
}
|
|
}
|
|
|
|
func TestHandleRemoveTarget_CleanupFailureKeepsTarget(t *testing.T) {
|
|
tgtPath := filepath.Join(t.TempDir(), "claude-skills")
|
|
s, sourceDir := newTestServerWithTargets(t, map[string]string{"claude": tgtPath})
|
|
addSkill(t, sourceDir, "alpha")
|
|
|
|
if err := os.Symlink(filepath.Join(sourceDir, "alpha"), filepath.Join(tgtPath, "alpha")); err != nil {
|
|
t.Fatalf("create symlink: %v", err)
|
|
}
|
|
origDetachSkillsTarget := detachSkillsTarget
|
|
detachSkillsTarget = func(path string, opts ssync.DetachOptions) (ssync.DetachReport, error) {
|
|
if path != tgtPath || opts.Source != sourceDir || opts.DryRun {
|
|
t.Fatalf("wrong detach inputs: %s, %+v", path, opts)
|
|
}
|
|
return ssync.DetachReport{}, errors.New("remove denied")
|
|
}
|
|
defer func() { detachSkillsTarget = origDetachSkillsTarget }()
|
|
|
|
req := httptest.NewRequest(http.MethodDelete, "/api/targets/claude", nil)
|
|
rr := httptest.NewRecorder()
|
|
s.handler.ServeHTTP(rr, req)
|
|
|
|
if rr.Code != http.StatusInternalServerError {
|
|
t.Fatalf("expected 500, got %d: %s", rr.Code, rr.Body.String())
|
|
}
|
|
if _, ok := s.cfg.Targets["claude"]; !ok {
|
|
t.Fatal("target should remain in memory after cleanup failure")
|
|
}
|
|
|
|
diskCfg, err := config.Load()
|
|
if err != nil {
|
|
t.Fatalf("failed to load config from disk: %v", err)
|
|
}
|
|
if _, ok := diskCfg.Targets["claude"]; !ok {
|
|
t.Fatal("target should remain on disk after cleanup failure")
|
|
}
|
|
}
|
|
|
|
// Codex and universal share ~/.agents/skills. Removing one must leave the links
|
|
// the other still syncs there, not turn them into copies it no longer manages.
|
|
func TestHandleRemoveTarget_KeepsSkillsAnotherTargetShares(t *testing.T) {
|
|
shared := filepath.Join(t.TempDir(), "agents-skills")
|
|
s, sourceDir := newTestServerWithTargets(t, map[string]string{"universal": shared, "codex": shared})
|
|
addSkill(t, sourceDir, "alpha")
|
|
link := filepath.Join(shared, "alpha")
|
|
if err := os.Symlink(filepath.Join(sourceDir, "alpha"), link); err != nil {
|
|
t.Fatalf("create symlink: %v", err)
|
|
}
|
|
|
|
rr := httptest.NewRecorder()
|
|
s.handler.ServeHTTP(rr, httptest.NewRequest(http.MethodDelete, "/api/targets/codex", nil))
|
|
if rr.Code != http.StatusOK {
|
|
t.Fatalf("expected 200, got %d: %s", rr.Code, rr.Body.String())
|
|
}
|
|
if info, err := os.Lstat(link); err != nil || info.Mode()&os.ModeSymlink == 0 {
|
|
t.Errorf("universal's link was not kept: %v", err)
|
|
}
|
|
}
|
|
|
|
// Removing an account the MCP config still selects succeeds and says where its name
|
|
// is left behind, so the next mcp sync does not fail on a target it cannot resolve.
|
|
func TestHandleRemoveTarget_WarnsWhenMCPStillNamesTheAccount(t *testing.T) {
|
|
s, sourceDir := newTestServer(t)
|
|
work := filepath.Join(t.TempDir(), "claude-work")
|
|
if err := os.MkdirAll(filepath.Join(work, "skills"), 0755); err != nil {
|
|
t.Fatalf("create account directory: %v", err)
|
|
}
|
|
raw := "source: " + sourceDir + "\nmode: merge\ntargets:\n claude-work:\n agent: claude\n config_dir: " + work +
|
|
"\nmcp:\n targets: [claude-work]\n servers:\n docs:\n url: https://example.com/mcp\n targets: [claude-work]\n"
|
|
if err := os.WriteFile(os.Getenv("SKILLSHARE_CONFIG"), []byte(raw), 0644); err != nil {
|
|
t.Fatalf("write config: %v", err)
|
|
}
|
|
cfg, err := config.Load()
|
|
if err != nil {
|
|
t.Fatalf("failed to load config: %v", err)
|
|
}
|
|
s = New(cfg, "127.0.0.1:0", "", "")
|
|
|
|
req := httptest.NewRequest(http.MethodDelete, "/api/targets/claude-work", 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 {
|
|
Warnings []string `json:"warnings"`
|
|
}
|
|
json.Unmarshal(rr.Body.Bytes(), &resp)
|
|
if len(resp.Warnings) != 1 || !strings.Contains(resp.Warnings[0], "mcp.targets, mcp.servers.docs still name claude-work") {
|
|
t.Fatalf("warnings: %v", resp.Warnings)
|
|
}
|
|
}
|
|
|
|
func TestHandleRemoveTarget_NotFound(t *testing.T) {
|
|
s, _ := newTestServer(t)
|
|
req := httptest.NewRequest(http.MethodDelete, "/api/targets/nonexistent", nil)
|
|
rr := httptest.NewRecorder()
|
|
s.handler.ServeHTTP(rr, req)
|
|
|
|
if rr.Code != http.StatusNotFound {
|
|
t.Errorf("expected 404, got %d", rr.Code)
|
|
}
|
|
}
|
|
|
|
func TestHandleUpdateTarget_Mode(t *testing.T) {
|
|
tgtPath := filepath.Join(t.TempDir(), "claude-skills")
|
|
s, _ := newTestServerWithTargets(t, map[string]string{"claude": tgtPath})
|
|
|
|
body := `{"mode":"symlink"}`
|
|
req := httptest.NewRequest(http.MethodPatch, "/api/targets/claude", strings.NewReader(body))
|
|
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())
|
|
}
|
|
}
|
|
|
|
func TestHandleUpdateTarget_PrefixedNamingNeedsCopyMode(t *testing.T) {
|
|
tgtPath := filepath.Join(t.TempDir(), "claude-skills")
|
|
s, _ := newTestServerWithTargets(t, map[string]string{"claude": tgtPath})
|
|
|
|
for _, tc := range []struct {
|
|
body string
|
|
want int
|
|
}{
|
|
{`{"target_naming":"prefixed"}`, http.StatusBadRequest}, // inherits merge
|
|
{`{"mode":"copy","target_naming":"prefixed"}`, http.StatusOK},
|
|
{`{"mode":"merge"}`, http.StatusBadRequest}, // would leave prefixed on merge
|
|
} {
|
|
req := httptest.NewRequest(http.MethodPatch, "/api/targets/claude", strings.NewReader(tc.body))
|
|
rr := httptest.NewRecorder()
|
|
s.handler.ServeHTTP(rr, req)
|
|
if rr.Code != tc.want {
|
|
t.Fatalf("%s: expected %d, got %d: %s", tc.body, tc.want, rr.Code, rr.Body.String())
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestHandleUpdateTarget_NotFound(t *testing.T) {
|
|
s, _ := newTestServer(t)
|
|
body := `{"mode":"merge"}`
|
|
req := httptest.NewRequest(http.MethodPatch, "/api/targets/nonexistent", strings.NewReader(body))
|
|
rr := httptest.NewRecorder()
|
|
s.handler.ServeHTTP(rr, req)
|
|
|
|
if rr.Code != http.StatusNotFound {
|
|
t.Errorf("expected 404, got %d", rr.Code)
|
|
}
|
|
}
|
|
|
|
func TestHandleUpdateTarget_IncludeExclude_Persisted(t *testing.T) {
|
|
tgtPath := filepath.Join(t.TempDir(), "claude-skills")
|
|
s, _ := newTestServerWithTargets(t, map[string]string{"claude": tgtPath})
|
|
|
|
// PATCH include/exclude
|
|
body := `{"include":["my-skill","other-*"],"exclude":["tmp-*"]}`
|
|
req := httptest.NewRequest(http.MethodPatch, "/api/targets/claude", strings.NewReader(body))
|
|
rr := httptest.NewRecorder()
|
|
s.handler.ServeHTTP(rr, req)
|
|
if rr.Code != http.StatusOK {
|
|
t.Fatalf("PATCH expected 200, got %d: %s", rr.Code, rr.Body.String())
|
|
}
|
|
|
|
// Verify disk persistence
|
|
diskCfg, err := config.Load()
|
|
if err != nil {
|
|
t.Fatalf("failed to load config from disk: %v", err)
|
|
}
|
|
tgt, ok := diskCfg.Targets["claude"]
|
|
if !ok {
|
|
t.Fatal("target 'claude' not found in disk config")
|
|
}
|
|
diskSc := tgt.SkillsConfig()
|
|
if len(diskSc.Include) != 2 || diskSc.Include[0] != "my-skill" || diskSc.Include[1] != "other-*" {
|
|
t.Errorf("disk include mismatch: got %v", diskSc.Include)
|
|
}
|
|
if len(diskSc.Exclude) != 1 || diskSc.Exclude[0] != "tmp-*" {
|
|
t.Errorf("disk exclude mismatch: got %v", diskSc.Exclude)
|
|
}
|
|
|
|
// Verify in-memory state was reloaded correctly
|
|
memTgt, ok := s.cfg.Targets["claude"]
|
|
if !ok {
|
|
t.Fatal("target 'claude' not in in-memory config")
|
|
}
|
|
memSc := memTgt.SkillsConfig()
|
|
if len(memSc.Include) != 2 || memSc.Include[0] != "my-skill" {
|
|
t.Errorf("in-memory include mismatch: got %v", memSc.Include)
|
|
}
|
|
if len(memSc.Exclude) != 1 || memSc.Exclude[0] != "tmp-*" {
|
|
t.Errorf("in-memory exclude mismatch: got %v", memSc.Exclude)
|
|
}
|
|
|
|
// Verify GET /api/targets returns the filters
|
|
req2 := httptest.NewRequest(http.MethodGet, "/api/targets", nil)
|
|
rr2 := httptest.NewRecorder()
|
|
s.handler.ServeHTTP(rr2, req2)
|
|
|
|
var resp struct {
|
|
Targets []struct {
|
|
Name string `json:"name"`
|
|
Include []string `json:"include"`
|
|
Exclude []string `json:"exclude"`
|
|
} `json:"targets"`
|
|
}
|
|
json.Unmarshal(rr2.Body.Bytes(), &resp)
|
|
if len(resp.Targets) != 1 {
|
|
t.Fatalf("expected 1 target, got %d", len(resp.Targets))
|
|
}
|
|
if len(resp.Targets[0].Include) != 2 {
|
|
t.Errorf("GET include mismatch: got %v", resp.Targets[0].Include)
|
|
}
|
|
if len(resp.Targets[0].Exclude) != 1 {
|
|
t.Errorf("GET exclude mismatch: got %v", resp.Targets[0].Exclude)
|
|
}
|
|
}
|
|
|
|
func TestHandleUpdateTarget_ClearFilters(t *testing.T) {
|
|
tgtPath := filepath.Join(t.TempDir(), "claude-skills")
|
|
s, _ := newTestServerWithTargets(t, map[string]string{"claude": tgtPath})
|
|
|
|
// First set filters
|
|
body := `{"include":["a"],"exclude":["b"]}`
|
|
req := httptest.NewRequest(http.MethodPatch, "/api/targets/claude", strings.NewReader(body))
|
|
rr := httptest.NewRecorder()
|
|
s.handler.ServeHTTP(rr, req)
|
|
if rr.Code != http.StatusOK {
|
|
t.Fatalf("set filters: expected 200, got %d: %s", rr.Code, rr.Body.String())
|
|
}
|
|
|
|
// Clear filters by sending empty arrays
|
|
body2 := `{"include":[],"exclude":[]}`
|
|
req2 := httptest.NewRequest(http.MethodPatch, "/api/targets/claude", strings.NewReader(body2))
|
|
rr2 := httptest.NewRecorder()
|
|
s.handler.ServeHTTP(rr2, req2)
|
|
if rr2.Code != http.StatusOK {
|
|
t.Fatalf("clear filters: expected 200, got %d: %s", rr2.Code, rr2.Body.String())
|
|
}
|
|
|
|
// Verify disk config has empty filters
|
|
diskCfg, err := config.Load()
|
|
if err != nil {
|
|
t.Fatalf("failed to load config from disk: %v", err)
|
|
}
|
|
tgt := diskCfg.Targets["claude"]
|
|
if len(tgt.Include) != 0 {
|
|
t.Errorf("expected empty include after clear, got %v", tgt.Include)
|
|
}
|
|
if len(tgt.Exclude) != 0 {
|
|
t.Errorf("expected empty exclude after clear, got %v", tgt.Exclude)
|
|
}
|
|
|
|
// Verify GET also returns empty filters
|
|
req3 := httptest.NewRequest(http.MethodGet, "/api/targets", nil)
|
|
rr3 := httptest.NewRecorder()
|
|
s.handler.ServeHTTP(rr3, req3)
|
|
|
|
var resp struct {
|
|
Targets []struct {
|
|
Include []string `json:"include"`
|
|
Exclude []string `json:"exclude"`
|
|
} `json:"targets"`
|
|
}
|
|
json.Unmarshal(rr3.Body.Bytes(), &resp)
|
|
if len(resp.Targets) != 1 {
|
|
t.Fatalf("expected 1 target, got %d", len(resp.Targets))
|
|
}
|
|
if len(resp.Targets[0].Include) != 0 {
|
|
t.Errorf("GET include should be empty after clear, got %v", resp.Targets[0].Include)
|
|
}
|
|
if len(resp.Targets[0].Exclude) != 0 {
|
|
t.Errorf("GET exclude should be empty after clear, got %v", resp.Targets[0].Exclude)
|
|
}
|
|
}
|
|
|
|
func TestHandleUpdateTarget_AgentIncludeExclude_Persisted(t *testing.T) {
|
|
tgtPath := filepath.Join(t.TempDir(), "claude-skills")
|
|
s, _ := newTestServerWithTargets(t, map[string]string{"claude": tgtPath})
|
|
|
|
// PATCH agent include/exclude
|
|
body := `{"agent_include":["review-*"],"agent_exclude":["draft-*"]}`
|
|
req := httptest.NewRequest(http.MethodPatch, "/api/targets/claude", strings.NewReader(body))
|
|
rr := httptest.NewRecorder()
|
|
s.handler.ServeHTTP(rr, req)
|
|
if rr.Code != http.StatusOK {
|
|
t.Fatalf("PATCH expected 200, got %d: %s", rr.Code, rr.Body.String())
|
|
}
|
|
|
|
// Verify disk persistence
|
|
diskCfg, err := config.Load()
|
|
if err != nil {
|
|
t.Fatalf("failed to load config from disk: %v", err)
|
|
}
|
|
tgt, ok := diskCfg.Targets["claude"]
|
|
if !ok {
|
|
t.Fatal("target 'claude' not found in disk config")
|
|
}
|
|
ac := tgt.AgentsConfig()
|
|
if len(ac.Include) != 1 || ac.Include[0] != "review-*" {
|
|
t.Errorf("disk agent include mismatch: got %v", ac.Include)
|
|
}
|
|
if len(ac.Exclude) != 1 || ac.Exclude[0] != "draft-*" {
|
|
t.Errorf("disk agent exclude mismatch: got %v", ac.Exclude)
|
|
}
|
|
|
|
// Verify in-memory state
|
|
memTgt := s.cfg.Targets["claude"]
|
|
memAc := memTgt.AgentsConfig()
|
|
if len(memAc.Include) != 1 || memAc.Include[0] != "review-*" {
|
|
t.Errorf("in-memory agent include mismatch: got %v", memAc.Include)
|
|
}
|
|
if len(memAc.Exclude) != 1 || memAc.Exclude[0] != "draft-*" {
|
|
t.Errorf("in-memory agent exclude mismatch: got %v", memAc.Exclude)
|
|
}
|
|
}
|
|
|
|
func TestHandleUpdateTarget_AgentInclude_InvalidPattern(t *testing.T) {
|
|
tgtPath := filepath.Join(t.TempDir(), "claude-skills")
|
|
s, _ := newTestServerWithTargets(t, map[string]string{"claude": tgtPath})
|
|
|
|
body := `{"agent_include":["[unclosed"]}`
|
|
req := httptest.NewRequest(http.MethodPatch, "/api/targets/claude", strings.NewReader(body))
|
|
rr := httptest.NewRecorder()
|
|
s.handler.ServeHTTP(rr, req)
|
|
if rr.Code != http.StatusBadRequest {
|
|
t.Errorf("expected 400 for invalid agent include pattern, got %d", rr.Code)
|
|
}
|
|
}
|
|
|
|
func TestHandleUpdateTarget_AgentExclude_InvalidPattern(t *testing.T) {
|
|
tgtPath := filepath.Join(t.TempDir(), "claude-skills")
|
|
s, _ := newTestServerWithTargets(t, map[string]string{"claude": tgtPath})
|
|
|
|
body := `{"agent_exclude":["[bad"]}`
|
|
req := httptest.NewRequest(http.MethodPatch, "/api/targets/claude", strings.NewReader(body))
|
|
rr := httptest.NewRecorder()
|
|
s.handler.ServeHTTP(rr, req)
|
|
if rr.Code != http.StatusBadRequest {
|
|
t.Errorf("expected 400 for invalid agent exclude pattern, got %d", rr.Code)
|
|
}
|
|
}
|
|
|
|
func patchTarget(t *testing.T, s *Server, body string) *httptest.ResponseRecorder {
|
|
t.Helper()
|
|
req := httptest.NewRequest(http.MethodPatch, "/api/targets/claude", strings.NewReader(body))
|
|
rr := httptest.NewRecorder()
|
|
s.handler.ServeHTTP(rr, req)
|
|
return rr
|
|
}
|
|
|
|
func TestHandleUpdateTarget_AgentExtension_ForcesCopy(t *testing.T) {
|
|
tgtPath := filepath.Join(t.TempDir(), "claude-skills")
|
|
s, _ := newTestServerWithTargets(t, map[string]string{"claude": tgtPath})
|
|
|
|
if rr := patchTarget(t, s, `{"agent_mode":"merge"}`); rr.Code != http.StatusOK {
|
|
t.Fatalf("PATCH mode expected 200, got %d: %s", rr.Code, rr.Body.String())
|
|
}
|
|
if rr := patchTarget(t, s, `{"agent_extension":"opencode-agents"}`); rr.Code != http.StatusOK {
|
|
t.Fatalf("PATCH extension expected 200, got %d: %s", rr.Code, rr.Body.String())
|
|
}
|
|
|
|
diskCfg, err := config.Load()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
tgt := diskCfg.Targets["claude"]
|
|
if tgt.Agents == nil || tgt.Agents.Extension != "opencode-agents" || tgt.Agents.Mode != "copy" {
|
|
t.Errorf("disk agents = %+v, want extension opencode-agents in copy mode", tgt.Agents)
|
|
}
|
|
}
|
|
|
|
func TestHandleUpdateTarget_AgentExtension_RejectsConflictingMode(t *testing.T) {
|
|
tgtPath := filepath.Join(t.TempDir(), "claude-skills")
|
|
s, _ := newTestServerWithTargets(t, map[string]string{"claude": tgtPath})
|
|
|
|
patchTarget(t, s, `{"agent_mode":"merge"}`) // agents block now exists and is shared
|
|
rr := patchTarget(t, s, `{"agent_extension":"opencode-agents","agent_mode":"symlink"}`)
|
|
if rr.Code != http.StatusBadRequest {
|
|
t.Fatalf("expected 400, got %d: %s", rr.Code, rr.Body.String())
|
|
}
|
|
memTgt := s.cfg.Targets["claude"]
|
|
if mode := memTgt.AgentsConfig().Mode; mode == "symlink" {
|
|
t.Error("a rejected request must not change the agent mode")
|
|
}
|
|
}
|
|
|
|
func TestHandleUpdateTarget_AgentExtension_Clear(t *testing.T) {
|
|
tgtPath := filepath.Join(t.TempDir(), "claude-skills")
|
|
s, _ := newTestServerWithTargets(t, map[string]string{"claude": tgtPath})
|
|
|
|
patchTarget(t, s, `{"agent_extension":"opencode-agents"}`)
|
|
if rr := patchTarget(t, s, `{"agent_extension":""}`); rr.Code != http.StatusOK {
|
|
t.Fatalf("expected 200, got %d: %s", rr.Code, rr.Body.String())
|
|
}
|
|
|
|
memTgt := s.cfg.Targets["claude"]
|
|
if ext := memTgt.AgentsConfig().Extension; ext != "" {
|
|
t.Errorf("extension = %q, want cleared", ext)
|
|
}
|
|
}
|
|
|
|
func TestHandleUpdateTarget_RejectedRequestChangesNothing(t *testing.T) {
|
|
tgtPath := filepath.Join(t.TempDir(), "claude-skills")
|
|
s, _ := newTestServerWithTargets(t, map[string]string{"claude": tgtPath})
|
|
patchTarget(t, s, `{"mode":"merge","agent_mode":"merge"}`) // skills and agents blocks now exist
|
|
|
|
rr := patchTarget(t, s, `{"mode":"copy","agent_mode":"copy","agent_include":["["]}`)
|
|
if rr.Code != http.StatusBadRequest {
|
|
t.Fatalf("expected 400, got %d: %s", rr.Code, rr.Body.String())
|
|
}
|
|
memTgt := s.cfg.Targets["claude"]
|
|
if got := memTgt.SkillsConfig().Mode + "/" + memTgt.AgentsConfig().Mode; got != "merge/merge" {
|
|
t.Errorf("modes = %s, want merge/merge after a rejected request", got)
|
|
}
|
|
}
|
|
|
|
// A target can be another config directory of a built-in Agent, such as a second account.
|
|
func TestHandleAddTarget_AgentConfigDir(t *testing.T) {
|
|
s, _ := newTestServer(t)
|
|
dir := filepath.Join(t.TempDir(), ".claude-work")
|
|
add := func(body string) *httptest.ResponseRecorder {
|
|
rr := httptest.NewRecorder()
|
|
s.handler.ServeHTTP(rr, httptest.NewRequest(http.MethodPost, "/api/targets", strings.NewReader(body)))
|
|
return rr
|
|
}
|
|
if rr := add(`{"name":"claude-work","agent":"claude","configDir":"` + dir + `"}`); rr.Code != http.StatusOK {
|
|
t.Fatalf("expected 200, got %d: %s", rr.Code, rr.Body.String())
|
|
}
|
|
added := s.cfg.Targets["claude-work"]
|
|
if got := added.SkillsConfig().Path; got != filepath.Join(dir, "skills") {
|
|
t.Errorf("skills path = %q", got)
|
|
}
|
|
saved, err := os.ReadFile(config.ConfigPath())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !strings.Contains(string(saved), "agent: claude") || strings.Contains(string(saved), "path:") {
|
|
t.Errorf("the paths should stay derived:\n%s", saved)
|
|
}
|
|
if rr := add(`{"name":"cursor-work","agent":"cursor","configDir":"` + dir + `2"}`); rr.Code != http.StatusBadRequest {
|
|
t.Errorf("expected 400 for an Agent without a config directory, got %d", rr.Code)
|
|
}
|
|
}
|
|
|
|
// cli names the compatible executable for the account's plugin commands, such as omo for Pi.
|
|
func TestHandleAddTarget_AgentConfigDirCLI(t *testing.T) {
|
|
s, _ := newTestServer(t)
|
|
dir := filepath.Join(t.TempDir(), ".omo", "agent")
|
|
rr := httptest.NewRecorder()
|
|
s.handler.ServeHTTP(rr, httptest.NewRequest(http.MethodPost, "/api/targets", strings.NewReader(`{"name":"omo","agent":"pi","configDir":"`+dir+`","cli":"omo"}`)))
|
|
if rr.Code != http.StatusOK {
|
|
t.Fatalf("expected 200, got %d: %s", rr.Code, rr.Body.String())
|
|
}
|
|
if saved, _ := os.ReadFile(config.ConfigPath()); !strings.Contains(string(saved), "cli: omo") {
|
|
t.Errorf("cli was not saved:\n%s", saved)
|
|
}
|
|
rr = httptest.NewRecorder()
|
|
s.handler.ServeHTTP(rr, httptest.NewRequest(http.MethodGet, "/api/targets", nil))
|
|
if !strings.Contains(rr.Body.String(), `"cli":"omo"`) {
|
|
t.Errorf("cli was not listed: %s", rr.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestHandleAvailableTargets_NamesTheConfigDir(t *testing.T) {
|
|
s, _ := newTestServer(t)
|
|
rr := httptest.NewRecorder()
|
|
s.handler.ServeHTTP(rr, httptest.NewRequest(http.MethodGet, "/api/config/available-targets", nil))
|
|
var resp struct {
|
|
Targets []struct{ Name, ConfigDir string } `json:"targets"`
|
|
}
|
|
json.Unmarshal(rr.Body.Bytes(), &resp)
|
|
accounts := map[string]bool{"claude": true, "codex": true, "opencode": true, "pi": true, "omp": true}
|
|
for _, target := range resp.Targets {
|
|
if accounts[target.Name] != (target.ConfigDir != "") {
|
|
t.Errorf("%s: configDir = %q", target.Name, target.ConfigDir)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestHandleAddTarget_ProjectPrefixedNamingUsesCopyMode(t *testing.T) {
|
|
s, _ := newTestProjectServerWithExtras(t, nil)
|
|
s.projectCfg.TargetNaming = "prefixed"
|
|
if err := s.projectCfg.Save(s.projectRoot); err != nil { // requests reload the config from disk
|
|
t.Fatal(err)
|
|
}
|
|
rr := httptest.NewRecorder()
|
|
s.handler.ServeHTTP(rr, httptest.NewRequest(http.MethodPost, "/api/targets", strings.NewReader(`{"name":"cursor"}`)))
|
|
if rr.Code != http.StatusOK {
|
|
t.Fatalf("add: got %d %s", rr.Code, rr.Body.String())
|
|
}
|
|
if _, invalid, err := config.ValidateProjectConfigForSync(s.projectCfg, s.projectRoot); err != nil || invalid["cursor"] != nil {
|
|
t.Fatalf("added target invalid: err = %v, invalid = %v", err, invalid)
|
|
}
|
|
}
|
|
|
|
func TestHandleAddTarget_GlobalPrefixedNamingUnderMergeUsesCopyMode(t *testing.T) {
|
|
s, _ := newTestServer(t)
|
|
s.cfg.Mode, s.cfg.TargetNaming = "merge", "prefixed" // valid while every target overrides copy
|
|
if err := s.saveConfig(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
rr := httptest.NewRecorder()
|
|
body := `{"name":"cursor","path":"` + filepath.ToSlash(filepath.Join(t.TempDir(), "skills")) + `"}`
|
|
s.handler.ServeHTTP(rr, httptest.NewRequest(http.MethodPost, "/api/targets", strings.NewReader(body)))
|
|
if rr.Code != http.StatusOK {
|
|
t.Fatalf("add: got %d %s", rr.Code, rr.Body.String())
|
|
}
|
|
if tc := s.cfg.Targets["cursor"]; tc.SkillsConfig().Mode != "copy" {
|
|
mode := tc.SkillsConfig().Mode
|
|
t.Fatalf("new target mode = %q, want copy", mode)
|
|
}
|
|
|
|
rr = httptest.NewRecorder()
|
|
body = `{"name":"claude-work","agent":"claude","configDir":"` + filepath.ToSlash(filepath.Join(t.TempDir(), ".claude-work")) + `"}`
|
|
s.handler.ServeHTTP(rr, httptest.NewRequest(http.MethodPost, "/api/targets", strings.NewReader(body)))
|
|
if tc := s.cfg.Targets["claude-work"]; rr.Code != http.StatusOK || tc.SkillsConfig().Mode != "copy" {
|
|
t.Fatalf("add config dir: got %d %s, mode %q; want copy", rr.Code, rr.Body.String(), tc.SkillsConfig().Mode)
|
|
}
|
|
}
|
|
|
|
func TestHandleUpdateTarget_ReenablingSkillsChecksPrefixedNaming(t *testing.T) {
|
|
s, _ := newTestServer(t)
|
|
skills := &config.ResourceTargetConfig{Path: filepath.Join(t.TempDir(), "skills"), TargetNaming: "prefixed"}
|
|
skills.SetEnabled(false) // allowed while off: it syncs no skill
|
|
s.cfg.Mode = "merge"
|
|
s.cfg.Targets = map[string]config.TargetConfig{"claude": {Skills: skills}}
|
|
if err := s.saveConfig(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
rr := httptest.NewRecorder()
|
|
s.handler.ServeHTTP(rr, httptest.NewRequest(http.MethodPatch, "/api/targets/claude", strings.NewReader(`{"skills_enabled":true}`)))
|
|
if rr.Code != http.StatusBadRequest || !strings.Contains(rr.Body.String(), "requires copy mode") {
|
|
t.Fatalf("re-enable under merge: got %d %s, want 400", rr.Code, rr.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestHandleUpdateTarget_DisablingSkillsSkipsNamingCheck(t *testing.T) {
|
|
s, _ := newTestServer(t)
|
|
s.cfg.Targets = map[string]config.TargetConfig{"claude": {Skills: &config.ResourceTargetConfig{
|
|
Path: filepath.Join(t.TempDir(), "skills"), Mode: "copy", TargetNaming: "prefixed",
|
|
}}}
|
|
if err := s.saveConfig(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
rr := httptest.NewRecorder()
|
|
s.handler.ServeHTTP(rr, httptest.NewRequest(http.MethodPatch, "/api/targets/claude", strings.NewReader(`{"skills_enabled":false,"mode":"merge"}`)))
|
|
if rr.Code != http.StatusOK {
|
|
t.Fatalf("disable with merge: got %d %s, want 200", rr.Code, rr.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestHandleListTargets_SeparatesSkipRemedies(t *testing.T) {
|
|
s, src := newTestServerWithTargets(t, map[string]string{"claude": t.TempDir()})
|
|
target := s.cfg.Targets["claude"]
|
|
target.Skills = &config.ResourceTargetConfig{Path: target.Path, TargetNaming: "standard"}
|
|
s.cfg.Targets["claude"] = target
|
|
if err := s.cfg.Save(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, path := range []string{"_repo/skills/claude/fix", "_repo/skills/claude-code/fix", "_repo/skills/codex/fix", "one/same", "two/same", "bad"} {
|
|
name := filepath.Base(path)
|
|
if name == "bad" {
|
|
name = "different"
|
|
}
|
|
dir := filepath.Join(src, filepath.FromSlash(path))
|
|
if err := os.MkdirAll(dir, 0755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(filepath.Join(dir, "SKILL.md"), []byte("---\nname: "+name+"\n---\n# Instructions"), 0644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
rr := httptest.NewRecorder()
|
|
s.handler.ServeHTTP(rr, httptest.NewRequest(http.MethodGet, "/api/targets", nil))
|
|
if rr.Code != http.StatusOK {
|
|
t.Fatal(rr.Body.String())
|
|
}
|
|
var response struct {
|
|
Targets []targetItem `json:"targets"`
|
|
}
|
|
if err := json.Unmarshal(rr.Body.Bytes(), &response); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(response.Targets) != 1 {
|
|
t.Fatal(rr.Body.String())
|
|
}
|
|
got := response.Targets[0]
|
|
if got.NamingSkippedSkillCount != 1 || got.SelectionSkippedSkillCount != 2 || got.CollisionCount != 1 || got.SkippedSkillCount != 5 {
|
|
t.Fatalf("skip remedies: %s", rr.Body.String())
|
|
}
|
|
}
|