mirror of
https://github.com/runkids/skillshare.git
synced 2026-11-20 20:34:19 +00:00
fix(sync): fail only the target whose config is invalid
Any per-target config problem, such as a skills path that is a file or
an unknown mode, made `skillshare sync`, `sync -p` and the dashboard
refuse to sync anything. That contradicts the policy that sync runs
every target and reports the ones that failed. Sync now validates the
config as a whole first and still stops on global problems (source,
global mode or naming, git_root, extras); a target whose own settings
are invalid is skipped for skills and agents and reported as failed
("invalid config" in CLI output and --json details, part "config" in
the dashboard's failed list). Config validation on save is unchanged.
This commit is contained in:
@@ -3,8 +3,10 @@ package config
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"maps"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"slices"
|
||||
"strings"
|
||||
)
|
||||
|
||||
@@ -14,6 +16,14 @@ const errAgentsEnabled = "agents.enabled is not supported (only skills.enabled)"
|
||||
// ValidateConfig validates a global config semantically (after YAML parsing).
|
||||
// Returns warnings (non-fatal) and error (fatal, should return 400).
|
||||
func ValidateConfig(cfg *Config) (warnings []string, err error) {
|
||||
warnings, invalid, err := ValidateConfigForSync(cfg)
|
||||
return warnings, joinTargetErrors(err, invalid)
|
||||
}
|
||||
|
||||
// ValidateConfigForSync validates a global config for sync, which runs every
|
||||
// target it can: invalid maps each target whose own settings are wrong to its
|
||||
// problems, and err holds the problems that stop the whole sync.
|
||||
func ValidateConfigForSync(cfg *Config) (warnings []string, invalid map[string]error, err error) {
|
||||
var errs []string
|
||||
|
||||
// Source path validation checks the effective source, including the default
|
||||
@@ -50,48 +60,75 @@ func ValidateConfig(cfg *Config) (warnings []string, err error) {
|
||||
errs = append(errs, err.Error())
|
||||
}
|
||||
|
||||
// Per-target validation
|
||||
for name, target := range cfg.Targets {
|
||||
if err := ValidateTargetInstructions(target.Instructions, false); err != nil {
|
||||
errs = append(errs, fmt.Sprintf("target %q: %v", name, err))
|
||||
}
|
||||
if target.Agents != nil && target.Agents.Enabled != nil {
|
||||
errs = append(errs, fmt.Sprintf("target %q: %s", name, errAgentsEnabled))
|
||||
}
|
||||
sc := target.SkillsConfig()
|
||||
if !IsValidSyncMode(sc.Mode) {
|
||||
errs = append(errs, fmt.Sprintf("target %q: invalid sync mode %q (valid: %s)", name, sc.Mode, strings.Join(ValidSyncModes, ", ")))
|
||||
continue
|
||||
}
|
||||
if !IsValidTargetNaming(sc.TargetNaming) {
|
||||
errs = append(errs, fmt.Sprintf("target %q: invalid target naming %q (valid: %s)", name, sc.TargetNaming, strings.Join(ValidTargetNamings, ", ")))
|
||||
continue
|
||||
}
|
||||
path := sc.Path
|
||||
if path == "" {
|
||||
// Known built-in targets get their path from targets.yaml at runtime;
|
||||
// custom targets must specify a path explicitly.
|
||||
if _, known := LookupGlobalTarget(name); !known && target.Agent == "" {
|
||||
errs = append(errs, fmt.Sprintf("target %q: missing path (custom targets require skills.path)", name))
|
||||
continue
|
||||
}
|
||||
} else {
|
||||
errs = append(errs, validateTargetPath(name, ExpandPath(path))...)
|
||||
}
|
||||
}
|
||||
|
||||
if err := cfg.ValidateExtras(); err != nil {
|
||||
errs = append(errs, err.Error())
|
||||
}
|
||||
|
||||
if len(errs) > 0 {
|
||||
return warnings, errors.New(strings.Join(errs, "; "))
|
||||
invalid = map[string]error{}
|
||||
for name, target := range cfg.Targets {
|
||||
if problems := validateGlobalTarget(name, target); len(problems) > 0 {
|
||||
invalid[name] = errors.New(strings.Join(problems, "; "))
|
||||
}
|
||||
}
|
||||
return warnings, nil
|
||||
|
||||
if len(errs) > 0 {
|
||||
err = errors.New(strings.Join(errs, "; "))
|
||||
}
|
||||
return warnings, invalid, err
|
||||
}
|
||||
|
||||
// validateGlobalTarget returns the problems of one global target's settings.
|
||||
func validateGlobalTarget(name string, target TargetConfig) []string {
|
||||
var problems []string
|
||||
if err := ValidateTargetInstructions(target.Instructions, false); err != nil {
|
||||
problems = append(problems, err.Error())
|
||||
}
|
||||
if target.Agents != nil && target.Agents.Enabled != nil {
|
||||
problems = append(problems, errAgentsEnabled)
|
||||
}
|
||||
sc := target.SkillsConfig()
|
||||
if !IsValidSyncMode(sc.Mode) {
|
||||
return append(problems, fmt.Sprintf("invalid sync mode %q (valid: %s)", sc.Mode, strings.Join(ValidSyncModes, ", ")))
|
||||
}
|
||||
if !IsValidTargetNaming(sc.TargetNaming) {
|
||||
return append(problems, fmt.Sprintf("invalid target naming %q (valid: %s)", sc.TargetNaming, strings.Join(ValidTargetNamings, ", ")))
|
||||
}
|
||||
if sc.Path == "" {
|
||||
// Known built-in targets get their path from targets.yaml at runtime;
|
||||
// custom targets must specify a path explicitly.
|
||||
if _, known := LookupGlobalTarget(name); !known && target.Agent == "" {
|
||||
problems = append(problems, "missing path (custom targets require skills.path)")
|
||||
}
|
||||
} else if err := validateTargetPath(ExpandPath(sc.Path)); err != nil {
|
||||
problems = append(problems, err.Error())
|
||||
}
|
||||
return problems
|
||||
}
|
||||
|
||||
// joinTargetErrors folds the per-target problems into err, as one error that
|
||||
// names each target, for callers that reject the whole config.
|
||||
func joinTargetErrors(err error, invalid map[string]error) error {
|
||||
var errs []string
|
||||
if err != nil {
|
||||
errs = append(errs, err.Error())
|
||||
}
|
||||
for _, name := range slices.Sorted(maps.Keys(invalid)) {
|
||||
errs = append(errs, fmt.Sprintf("target %q: %v", name, invalid[name]))
|
||||
}
|
||||
if len(errs) == 0 {
|
||||
return nil
|
||||
}
|
||||
return errors.New(strings.Join(errs, "; "))
|
||||
}
|
||||
|
||||
// ValidateProjectConfig validates a project config semantically.
|
||||
func ValidateProjectConfig(cfg *ProjectConfig, projectRoot string) (warnings []string, err error) {
|
||||
warnings, invalid, err := ValidateProjectConfigForSync(cfg, projectRoot)
|
||||
return warnings, joinTargetErrors(err, invalid)
|
||||
}
|
||||
|
||||
// ValidateProjectConfigForSync is ValidateConfigForSync for a project config.
|
||||
func ValidateProjectConfigForSync(cfg *ProjectConfig, projectRoot string) (warnings []string, invalid map[string]error, err error) {
|
||||
var errs []string
|
||||
|
||||
sourcePath := cfg.EffectiveSkillsSource(projectRoot)
|
||||
@@ -108,64 +145,78 @@ func ValidateProjectConfig(cfg *ProjectConfig, projectRoot string) (warnings []s
|
||||
errs = append(errs, fmt.Sprintf("source path is not a directory: %s", sourcePath))
|
||||
}
|
||||
|
||||
// Target validation
|
||||
if !IsValidTargetNaming(cfg.TargetNaming) {
|
||||
errs = append(errs, fmt.Sprintf("invalid project target naming %q (valid: %s)", cfg.TargetNaming, strings.Join(ValidTargetNamings, ", ")))
|
||||
}
|
||||
for _, entry := range cfg.Targets {
|
||||
if err := ValidateTargetInstructions(entry.Instructions, true); err != nil {
|
||||
errs = append(errs, fmt.Sprintf("target %q: %v", entry.Name, err))
|
||||
}
|
||||
if entry.Agents != nil && entry.Agents.Enabled != nil {
|
||||
errs = append(errs, fmt.Sprintf("target %q: %s", entry.Name, errAgentsEnabled))
|
||||
}
|
||||
sc := entry.SkillsConfig()
|
||||
if !IsValidSyncMode(sc.Mode) {
|
||||
errs = append(errs, fmt.Sprintf("target %q: invalid sync mode %q (valid: %s)", entry.Name, sc.Mode, strings.Join(ValidSyncModes, ", ")))
|
||||
continue
|
||||
}
|
||||
if !IsValidTargetNaming(sc.TargetNaming) {
|
||||
errs = append(errs, fmt.Sprintf("target %q: invalid target naming %q (valid: %s)", entry.Name, sc.TargetNaming, strings.Join(ValidTargetNamings, ", ")))
|
||||
continue
|
||||
}
|
||||
|
||||
var skillsBuiltin string
|
||||
if t, ok := LookupProjectTarget(entry.Name); ok {
|
||||
skillsBuiltin = t.Path
|
||||
}
|
||||
if sc.Path == "" && skillsBuiltin == "" {
|
||||
// Known built-in targets are resolved from targets.yaml;
|
||||
// custom targets must have an explicit path.
|
||||
errs = append(errs, fmt.Sprintf("target %q: missing path (custom targets require skills.path)", entry.Name))
|
||||
} else {
|
||||
skillsTargetPath := resolveProjectTargetPath(projectRoot, sc.Path, skillsBuiltin)
|
||||
if sc.Path != "" {
|
||||
errs = append(errs, validateTargetPath(entry.Name, skillsTargetPath)...)
|
||||
}
|
||||
if skillsTargetPath != "" && pathsOverlap(sourcePath, skillsTargetPath) {
|
||||
errs = append(errs, fmt.Sprintf("target %q: skills target path %s overlaps skills source %s — sync --force could destroy the source", entry.Name, skillsTargetPath, sourcePath))
|
||||
}
|
||||
}
|
||||
|
||||
ac := entry.AgentsConfig()
|
||||
var agentsBuiltin string
|
||||
if t, ok := LookupProjectAgentTarget(entry.Name); ok {
|
||||
agentsBuiltin = t.Path
|
||||
}
|
||||
agentsTargetPath := resolveProjectTargetPath(projectRoot, ac.Path, agentsBuiltin)
|
||||
if agentsTargetPath != "" && pathsOverlap(agentsSourcePath, agentsTargetPath) {
|
||||
errs = append(errs, fmt.Sprintf("target %q: agents target path %s overlaps agents source %s — sync --force could destroy the source", entry.Name, agentsTargetPath, agentsSourcePath))
|
||||
}
|
||||
}
|
||||
|
||||
if err := cfg.ValidateExtras(projectRoot); err != nil {
|
||||
errs = append(errs, err.Error())
|
||||
}
|
||||
|
||||
if len(errs) > 0 {
|
||||
return warnings, errors.New(strings.Join(errs, "; "))
|
||||
problems := map[string][]string{}
|
||||
for _, entry := range cfg.Targets {
|
||||
problems[entry.Name] = append(problems[entry.Name], validateProjectTarget(entry, projectRoot, sourcePath, agentsSourcePath)...)
|
||||
}
|
||||
return warnings, nil
|
||||
invalid = map[string]error{}
|
||||
for name, p := range problems {
|
||||
if len(p) > 0 {
|
||||
invalid[name] = errors.New(strings.Join(p, "; "))
|
||||
}
|
||||
}
|
||||
|
||||
if len(errs) > 0 {
|
||||
err = errors.New(strings.Join(errs, "; "))
|
||||
}
|
||||
return warnings, invalid, err
|
||||
}
|
||||
|
||||
// validateProjectTarget returns the problems of one project target's settings.
|
||||
func validateProjectTarget(entry ProjectTargetEntry, projectRoot, sourcePath, agentsSourcePath string) []string {
|
||||
var problems []string
|
||||
if err := ValidateTargetInstructions(entry.Instructions, true); err != nil {
|
||||
problems = append(problems, err.Error())
|
||||
}
|
||||
if entry.Agents != nil && entry.Agents.Enabled != nil {
|
||||
problems = append(problems, errAgentsEnabled)
|
||||
}
|
||||
sc := entry.SkillsConfig()
|
||||
if !IsValidSyncMode(sc.Mode) {
|
||||
return append(problems, fmt.Sprintf("invalid sync mode %q (valid: %s)", sc.Mode, strings.Join(ValidSyncModes, ", ")))
|
||||
}
|
||||
if !IsValidTargetNaming(sc.TargetNaming) {
|
||||
return append(problems, fmt.Sprintf("invalid target naming %q (valid: %s)", sc.TargetNaming, strings.Join(ValidTargetNamings, ", ")))
|
||||
}
|
||||
|
||||
var skillsBuiltin string
|
||||
if t, ok := LookupProjectTarget(entry.Name); ok {
|
||||
skillsBuiltin = t.Path
|
||||
}
|
||||
if sc.Path == "" && skillsBuiltin == "" {
|
||||
// Known built-in targets are resolved from targets.yaml;
|
||||
// custom targets must have an explicit path.
|
||||
problems = append(problems, "missing path (custom targets require skills.path)")
|
||||
} else {
|
||||
skillsTargetPath := resolveProjectTargetPath(projectRoot, sc.Path, skillsBuiltin)
|
||||
if sc.Path != "" {
|
||||
if err := validateTargetPath(skillsTargetPath); err != nil {
|
||||
problems = append(problems, err.Error())
|
||||
}
|
||||
}
|
||||
if skillsTargetPath != "" && pathsOverlap(sourcePath, skillsTargetPath) {
|
||||
problems = append(problems, fmt.Sprintf("skills target path %s overlaps skills source %s — sync --force could destroy the source", skillsTargetPath, sourcePath))
|
||||
}
|
||||
}
|
||||
|
||||
ac := entry.AgentsConfig()
|
||||
var agentsBuiltin string
|
||||
if t, ok := LookupProjectAgentTarget(entry.Name); ok {
|
||||
agentsBuiltin = t.Path
|
||||
}
|
||||
agentsTargetPath := resolveProjectTargetPath(projectRoot, ac.Path, agentsBuiltin)
|
||||
if agentsTargetPath != "" && pathsOverlap(agentsSourcePath, agentsTargetPath) {
|
||||
problems = append(problems, fmt.Sprintf("agents target path %s overlaps agents source %s — sync --force could destroy the source", agentsTargetPath, agentsSourcePath))
|
||||
}
|
||||
return problems
|
||||
}
|
||||
|
||||
// ValidateTargetInstructions checks a user-set instruction file: a file path,
|
||||
@@ -227,7 +278,7 @@ func pathsOverlap(a, b string) bool {
|
||||
|
||||
// validateTargetPath checks a single target's path is accessible and is a directory.
|
||||
// Missing paths are accepted — sync will auto-create them with a visible notification.
|
||||
func validateTargetPath(name, expandedPath string) []string {
|
||||
func validateTargetPath(expandedPath string) error {
|
||||
if expandedPath == "" {
|
||||
return nil // path resolved by target registry; skip filesystem check
|
||||
}
|
||||
@@ -237,10 +288,10 @@ func validateTargetPath(name, expandedPath string) []string {
|
||||
if os.IsNotExist(statErr) {
|
||||
return nil // sync will auto-create and notify
|
||||
}
|
||||
return []string{fmt.Sprintf("target %q: cannot access path: %v", name, statErr)}
|
||||
return fmt.Errorf("cannot access path: %v", statErr)
|
||||
}
|
||||
if !info.IsDir() {
|
||||
return []string{fmt.Sprintf("target %q: path is not a directory: %s", name, expandedPath)}
|
||||
return fmt.Errorf("path is not a directory: %s", expandedPath)
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
@@ -473,3 +473,57 @@ func TestValidateConfig_RejectsRelativeInstructionsPath(t *testing.T) {
|
||||
t.Fatalf("err = %v, want instructions.path error", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateConfigForSync_TargetProblemFailsOnlyThatTarget(t *testing.T) {
|
||||
file := filepath.Join(t.TempDir(), "skills-file")
|
||||
if err := os.WriteFile(file, []byte("x"), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
cfg := &Config{
|
||||
Source: t.TempDir(),
|
||||
Targets: map[string]TargetConfig{
|
||||
"broken": {Skills: &ResourceTargetConfig{Path: file}},
|
||||
"good": {Skills: &ResourceTargetConfig{Path: filepath.Join(t.TempDir(), "skills")}},
|
||||
},
|
||||
}
|
||||
_, invalid, err := ValidateConfigForSync(cfg)
|
||||
if err != nil || len(invalid) != 1 || invalid["broken"] == nil || !strings.Contains(invalid["broken"].Error(), "path is not a directory") {
|
||||
t.Fatalf("err = %v, invalid = %v; want only broken invalid", err, invalid)
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateConfigForSync_GlobalProblemStillFails(t *testing.T) {
|
||||
cfg := &Config{Source: t.TempDir(), Mode: "bogus", Targets: map[string]TargetConfig{}}
|
||||
if _, _, err := ValidateConfigForSync(cfg); err == nil || !strings.Contains(err.Error(), "invalid global sync mode") {
|
||||
t.Fatalf("err = %v, want invalid global sync mode", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateConfig_TargetPathIsFileStillRejected(t *testing.T) {
|
||||
file := filepath.Join(t.TempDir(), "skills-file")
|
||||
if err := os.WriteFile(file, []byte("x"), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
cfg := &Config{Source: t.TempDir(), Targets: map[string]TargetConfig{"broken": {Skills: &ResourceTargetConfig{Path: file}}}}
|
||||
if _, err := ValidateConfig(cfg); err == nil || !strings.Contains(err.Error(), `target "broken": path is not a directory`) {
|
||||
t.Fatalf("err = %v, want target path error", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateProjectConfigForSync_TargetProblemFailsOnlyThatTarget(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
if err := os.MkdirAll(filepath.Join(root, ".skillshare", "skills"), 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(root, "skills-file"), []byte("x"), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
cfg := &ProjectConfig{Targets: []ProjectTargetEntry{
|
||||
{Name: "broken", Skills: &ResourceTargetConfig{Path: "skills-file"}},
|
||||
{Name: "claude"},
|
||||
}}
|
||||
_, invalid, err := ValidateProjectConfigForSync(cfg, root)
|
||||
if err != nil || len(invalid) != 1 || invalid["broken"] == nil || !strings.Contains(invalid["broken"].Error(), "path is not a directory") {
|
||||
t.Fatalf("err = %v, invalid = %v; want only broken invalid", err, invalid)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -44,7 +44,7 @@ func ignorePayload(stats *skillignore.IgnoreStats) map[string]any {
|
||||
// failures apart from the other warnings.
|
||||
type syncFailure struct {
|
||||
Target string `json:"target"`
|
||||
Part string `json:"part"` // "skill" or "agent"
|
||||
Part string `json:"part"` // "skill", "agent", or "config"
|
||||
Error string `json:"error"`
|
||||
Message string `json:"message"`
|
||||
Conflict bool `json:"conflict,omitempty"` // a symlink points elsewhere; force replaces it
|
||||
@@ -173,21 +173,45 @@ func (s *Server) syncResources(start time.Time, dryRun, force bool, kind, projec
|
||||
}
|
||||
|
||||
// Pre-check warnings via shared config validation
|
||||
warnings, validErr := config.ValidateConfig(s.cfg)
|
||||
warnings, invalid, validErr := config.ValidateConfigForSync(s.cfg)
|
||||
if validErr != nil {
|
||||
return nil, http.StatusBadRequest, validErr
|
||||
}
|
||||
|
||||
conflicts, overlap := folderConflicts(s.cfg.Targets, s.IsProjectMode())
|
||||
|
||||
if !dryRun {
|
||||
warnings = append(warnings, s.backupBeforeSync(targets, kind != kindAgent, kind != kindSkill)...)
|
||||
}
|
||||
|
||||
results := make([]syncTargetResult, 0)
|
||||
failed := make([]syncFailure, 0)
|
||||
skillFailed := 0
|
||||
|
||||
// A target with invalid settings fails alone: it is skipped for skills and
|
||||
// agents, and counts as a failed skill target when skills are synced.
|
||||
var firstErr error
|
||||
var firstFailed string
|
||||
runTargets := maps.Clone(targets)
|
||||
for _, name := range slices.Sorted(maps.Keys(invalid)) {
|
||||
target, ok := targets[name]
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
delete(runTargets, name)
|
||||
err := invalid[name]
|
||||
msg := name + ": invalid config: " + err.Error()
|
||||
warnings = append(warnings, msg)
|
||||
failed = append(failed, syncFailure{Target: name, Part: "config", Error: err.Error(), Message: msg})
|
||||
if kind != kindAgent && target.SkillsConfig().IsEnabled() {
|
||||
skillFailed++
|
||||
if firstErr == nil {
|
||||
firstErr, firstFailed = fmt.Errorf("invalid config for %s: %w", name, err), name
|
||||
}
|
||||
}
|
||||
}
|
||||
skillRan := skillFailed
|
||||
|
||||
if !dryRun {
|
||||
warnings = append(warnings, s.backupBeforeSync(runTargets, kind != kindAgent, kind != kindSkill)...)
|
||||
}
|
||||
|
||||
var ignoreStats *skillignore.IgnoreStats
|
||||
var allSkills []ssync.DiscoveredSkill
|
||||
ignorePatterns := ssync.EffectiveFileIgnorePatterns(s.cfg.Ignore)
|
||||
@@ -209,8 +233,6 @@ func (s *Server) syncResources(start time.Time, dryRun, force bool, kind, projec
|
||||
// for installed skills whose files may be missing from disk.
|
||||
|
||||
// A failed target adds a warning and no results row; the rest still sync.
|
||||
var firstErr error
|
||||
var firstFailed string
|
||||
failTarget := func(name string, err error) {
|
||||
skillFailed++
|
||||
if firstErr == nil {
|
||||
@@ -224,8 +246,7 @@ func (s *Server) syncResources(start time.Time, dryRun, force bool, kind, projec
|
||||
Source: s.cfg.EffectiveSkillsSource(), ProjectRoot: s.projectRoot, IgnorePatterns: ignorePatterns,
|
||||
DryRun: dryRun, Force: force,
|
||||
}
|
||||
skillRan := 0
|
||||
for name, target := range targets {
|
||||
for name, target := range runTargets {
|
||||
sc := target.SkillsConfig()
|
||||
if !sc.IsEnabled() {
|
||||
continue // skills off: agents, extras and MCP below still run
|
||||
@@ -299,8 +320,8 @@ func (s *Server) syncResources(start time.Time, dryRun, force bool, kind, projec
|
||||
agents := discoverActiveAgents(agentsSource)
|
||||
builtinAgents := s.builtinAgentTargets()
|
||||
|
||||
agentTargets := make([]ssync.AgentTarget, 0, len(targets))
|
||||
for name, target := range targets {
|
||||
agentTargets := make([]ssync.AgentTarget, 0, len(runTargets))
|
||||
for name, target := range runTargets {
|
||||
agentTargets = append(agentTargets, ssync.AgentTarget{
|
||||
Name: name,
|
||||
Path: resolveAgentPath(target, builtinAgents, name, s.IsProjectMode()),
|
||||
|
||||
@@ -771,3 +771,58 @@ func TestHandleSync_SymlinkConflictReplacedWithForce(t *testing.T) {
|
||||
t.Fatalf("expected %s linked to the source after force", linkPath)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleSync_InvalidTargetConfigFailsOnlyThatTarget(t *testing.T) {
|
||||
s, src, _ := newAgentSyncServer(t)
|
||||
addSkill(t, src, "alpha")
|
||||
file := filepath.Join(t.TempDir(), "skills-file")
|
||||
if err := os.WriteFile(file, []byte("x"), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
brokenAgents := filepath.Join(t.TempDir(), "broken-agents")
|
||||
s.cfg.Targets["broken"] = config.TargetConfig{
|
||||
Skills: &config.ResourceTargetConfig{Path: file},
|
||||
Agents: &config.ResourceTargetConfig{Path: brokenAgents},
|
||||
}
|
||||
s.cfg.Targets["claude"] = config.TargetConfig{
|
||||
Skills: &config.ResourceTargetConfig{Path: filepath.Join(t.TempDir(), "claude-skills")},
|
||||
Agents: &config.ResourceTargetConfig{Path: filepath.Join(t.TempDir(), "claude-agents")},
|
||||
}
|
||||
if err := s.cfg.Save(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
req := httptest.NewRequest(http.MethodPost, "/api/sync", strings.NewReader(`{}`))
|
||||
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 {
|
||||
Results []syncTargetResult `json:"results"`
|
||||
Warnings []string `json:"warnings"`
|
||||
Failed []syncFailure `json:"failed"`
|
||||
}
|
||||
if err := json.Unmarshal(rr.Body.Bytes(), &resp); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(resp.Results) != 1 || resp.Results[0].Target != "claude" || !slices.Equal(resp.Results[0].Linked, []string{"alpha", "tutor.md"}) {
|
||||
t.Fatalf("expected only the claude row with alpha and tutor.md, got %+v", resp.Results)
|
||||
}
|
||||
if len(resp.Failed) != 1 || resp.Failed[0].Target != "broken" || resp.Failed[0].Part != "config" ||
|
||||
!strings.Contains(resp.Failed[0].Error, "path is not a directory") || !slices.Contains(resp.Warnings, resp.Failed[0].Message) {
|
||||
t.Fatalf("expected one config failure for broken also in warnings, got failed %+v warnings %v", resp.Failed, resp.Warnings)
|
||||
}
|
||||
if _, err := os.Stat(brokenAgents); !os.IsNotExist(err) {
|
||||
t.Fatalf("expected no agent sync into the invalid target, stat err = %v", err)
|
||||
}
|
||||
entries, err := oplog.Read(config.ConfigPath(), oplog.OpsFile, 1)
|
||||
if err != nil || len(entries) != 1 {
|
||||
t.Fatalf("read ops log: %v (%d entries)", err, len(entries))
|
||||
}
|
||||
got, _ := json.Marshal(entries[0].Args["failed_targets"])
|
||||
if e := entries[0]; e.Status != "partial" || e.Args["targets_failed"] != float64(1) || string(got) != `["broken"]` {
|
||||
t.Fatalf("expected partial sync with broken failed, got status %q args %v", e.Status, e.Args)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user