Skip to content

mission control for agent skills

28.8 KB
Raw Blame
package scan

import (
	"encoding/json"
	"os"
	"path/filepath"
	"strings"
	"testing"

	"github.com/alvinunreal/lazyskills/internal/agents"
	"github.com/alvinunreal/lazyskills/internal/model"
)

func writeSkill(t *testing.T, dir, name, desc string) {
	t.Helper()
	if err := os.MkdirAll(dir, 0o755); err != nil {
		t.Fatal(err)
	}
	content := "---\nname: " + name + "\ndescription: " + desc + "\n---\n\n# " + name + "\n"
	if err := os.WriteFile(filepath.Join(dir, "SKILL.md"), []byte(content), 0o644); err != nil {
		t.Fatal(err)
	}
}

func withHome(t *testing.T) string {
	t.Helper()
	home := t.TempDir()
	t.Setenv("HOME", home)
	t.Setenv("XDG_CONFIG_HOME", filepath.Join(home, ".config"))
	t.Setenv("XDG_STATE_HOME", filepath.Join(home, ".local", "state"))
	t.Setenv("CLAUDE_CONFIG_DIR", filepath.Join(home, ".claude"))
	t.Setenv("CODEX_HOME", filepath.Join(home, ".codex"))
	return home
}

func TestUniversalProjectVisibilityForUniversalAgents(t *testing.T) {
	withHome(t)
	cwd := t.TempDir()
	writeSkill(t, filepath.Join(cwd, ".agents", "skills", "build"), "Build Tools", "Use build tools")

	res, err := Run(cwd)
	if err != nil {
		t.Fatal(err)
	}
	if len(res.Skills) != 1 {
		t.Fatalf("expected 1 skill, got %d", len(res.Skills))
	}
	seen := map[string]bool{}
	for _, p := range res.Skills[0].ObservedPaths {
		seen[p.Agent] = true
	}
	for _, agent := range []string{"universal", "opencode", "cursor", "codex"} {
		if !seen[agent] {
			t.Fatalf("expected %s to observe .agents/skills", agent)
		}
	}
	if seen["claude-code"] {
		t.Fatalf("claude-code should not observe project .agents/skills")
	}
}

func TestOpenCodeNativeProjectSkillsAndDiagnostics(t *testing.T) {
	withHome(t)
	cwd := t.TempDir()
	nativeRoot := filepath.Join(cwd, ".opencode", "skills")
	sharedRoot := filepath.Join(cwd, ".agents", "skills")
	writeSkill(t, filepath.Join(nativeRoot, "shared-name"), "Shared Name", "native copy")
	writeSkill(t, filepath.Join(sharedRoot, "shared-name"), "Shared Name", "universal copy")
	if err := os.MkdirAll(filepath.Join(nativeRoot, "missing"), 0o755); err != nil {
		t.Fatal(err)
	}
	if err := os.MkdirAll(filepath.Join(nativeRoot, "invalid"), 0o755); err != nil {
		t.Fatal(err)
	}
	if err := os.WriteFile(filepath.Join(nativeRoot, "invalid", "SKILL.md"), []byte("---\nname: 123\n---\n"), 0o644); err != nil {
		t.Fatal(err)
	}

	res, err := Run(cwd)
	if err != nil {
		t.Fatal(err)
	}
	var shared, missing, invalid *model.Skill
	for i := range res.Skills {
		switch res.Skills[i].Name {
		case "Shared Name":
			shared = res.Skills[i]
		case "missing":
			missing = res.Skills[i]
		case "invalid":
			invalid = res.Skills[i]
		}
	}
	if shared == nil || missing == nil || invalid == nil {
		t.Fatalf("expected native, missing, and invalid skills, got %#v", res.Skills)
	}
	if got, ok := observedForAgent(shared, "opencode"); !ok || got.Path != filepath.Join(nativeRoot, "shared-name") {
		t.Fatalf("expected native path to be preferred for OpenCode, got %#v, %v", got, ok)
	}
	if len(shared.ObservedPaths) < 2 {
		t.Fatalf("expected universal and native observations to be retained, got %#v", shared.ObservedPaths)
	}
	if !hasIssue(missing.HealthIssues, "missing_skill_md") {
		t.Fatalf("expected missing native SKILL.md diagnostic, got %#v", missing.HealthIssues)
	}
	if !hasIssue(invalid.HealthIssues, "invalid_frontmatter") {
		t.Fatalf("expected invalid native SKILL.md diagnostic, got %#v", invalid.HealthIssues)
	}
}

func TestClaudeProjectDir(t *testing.T) {
	withHome(t)
	cwd := t.TempDir()
	writeSkill(t, filepath.Join(cwd, ".claude", "skills", "review"), "Review", "Review code")
	res, err := Run(cwd)
	if err != nil {
		t.Fatal(err)
	}
	if len(res.Skills) != 1 {
		t.Fatalf("expected 1 skill, got %d", len(res.Skills))
	}
	if got := res.Skills[0].ObservedPaths[0].Agent; got != "claude-code" {
		t.Fatalf("expected claude-code observation, got %s", got)
	}
}

func TestHiddenSkillDirectoriesAreIgnored(t *testing.T) {
	home := withHome(t)
	cwd := t.TempDir()
	if err := os.MkdirAll(filepath.Join(home, ".codex", "skills", ".system"), 0o755); err != nil {
		t.Fatal(err)
	}
	writeSkill(t, filepath.Join(home, ".codex", "skills", "review"), "Review", "Review code")
	res, err := Run(cwd)
	if err != nil {
		t.Fatal(err)
	}
	for _, skill := range res.Skills {
		if skill.Name == ".system" {
			t.Fatalf("expected hidden .system directory to be ignored, got %#v", skill)
		}
	}
	if len(res.Skills) != 1 || res.Skills[0].Name != "Review" {
		t.Fatalf("expected only normal skill, got %#v", res.Skills)
	}
}

func TestLocksCorrelateBySkillName(t *testing.T) {
	home := withHome(t)
	cwd := t.TempDir()
	writeSkill(t, filepath.Join(cwd, ".agents", "skills", "pretty-name"), "Pretty Name", "Pretty desc")
	writeSkill(t, filepath.Join(home, ".agents", "skills", "pretty-name"), "Pretty Name", "Pretty desc")
	localLock := `{"version":1,"skills":{"Pretty Name":{"source":"owner/repo","sourceType":"github","computedHash":"abc"}}}`
	if err := os.WriteFile(filepath.Join(cwd, "skills-lock.json"), []byte(localLock), 0o644); err != nil {
		t.Fatal(err)
	}
	globalLockPath := filepath.Join(home, ".local", "state", "skills", ".skill-lock.json")
	if err := os.MkdirAll(filepath.Dir(globalLockPath), 0o755); err != nil {
		t.Fatal(err)
	}
	globalLock := `{"version":3,"skills":{"pretty-name":{"source":"owner/repo","sourceType":"github","sourceUrl":"https://github.com/owner/repo","skillFolderHash":"def","installedAt":"now","updatedAt":"now"}}}`
	if err := os.WriteFile(globalLockPath, []byte(globalLock), 0o644); err != nil {
		t.Fatal(err)
	}
	res, err := Run(cwd)
	if err != nil {
		t.Fatal(err)
	}
	var sawLocal, sawGlobal bool
	for _, sk := range res.Skills {
		if sk.Scope == "project" && sk.LocalLock != nil {
			sawLocal = true
		}
		if sk.Scope == "global" && sk.GlobalLock != nil {
			sawGlobal = true
		}
	}
	if !sawLocal || !sawGlobal {
		t.Fatalf("expected scoped locks correlated, local=%v global=%v skills=%#v", sawLocal, sawGlobal, res.Skills)
	}
}

func TestGlobalCanonicalVisibleToUniversalAgents(t *testing.T) {
	home := withHome(t)
	cwd := t.TempDir()
	writeSkill(t, filepath.Join(home, ".agents", "skills", "global-build"), "Global Build", "Global build desc")

	res, err := Run(cwd)
	if err != nil {
		t.Fatal(err)
	}
	if len(res.Skills) != 1 {
		t.Fatalf("expected 1 skill, got %d", len(res.Skills))
	}
	seen := map[string]bool{}
	for _, p := range res.Skills[0].ObservedPaths {
		if p.Scope == "global" && p.Status == "canonical" {
			seen[p.Agent] = true
		}
	}
	for _, agent := range []string{"universal", "opencode", "cursor", "codex"} {
		if !seen[agent] {
			t.Fatalf("expected %s to observe global canonical .agents/skills", agent)
		}
	}
}

func TestProjectAndGlobalSameNameAreSeparateWithShadowing(t *testing.T) {
	home := withHome(t)
	cwd := t.TempDir()
	writeSkill(t, filepath.Join(cwd, ".agents", "skills", "same"), "Same", "Project desc")
	writeSkill(t, filepath.Join(home, ".agents", "skills", "same"), "Same", "Global desc")

	res, err := Run(cwd)
	if err != nil {
		t.Fatal(err)
	}
	if len(res.Skills) != 2 {
		t.Fatalf("expected separate project/global skills, got %d", len(res.Skills))
	}
	for _, sk := range res.Skills {
		var shadow bool
		for _, issue := range sk.HealthIssues {
			if issue.Type == "project_global_shadowing" {
				shadow = true
			}
		}
		if !shadow {
			t.Fatalf("expected shadowing issue on %#v", sk)
		}
	}
}

func TestScopeSpecificMissingLocks(t *testing.T) {
	home := withHome(t)
	cwd := t.TempDir()
	writeSkill(t, filepath.Join(cwd, ".agents", "skills", "locked-global-only"), "Locked Global Only", "Project desc")
	globalLockPath := filepath.Join(home, ".local", "state", "skills", ".skill-lock.json")
	if err := os.MkdirAll(filepath.Dir(globalLockPath), 0o755); err != nil {
		t.Fatal(err)
	}
	globalLock := `{"version":3,"skills":{"Locked Global Only":{"source":"owner/repo","sourceType":"github","sourceUrl":"https://github.com/owner/repo","skillFolderHash":"def","installedAt":"now","updatedAt":"now"}}}`
	if err := os.WriteFile(globalLockPath, []byte(globalLock), 0o644); err != nil {
		t.Fatal(err)
	}

	res, err := Run(cwd)
	if err != nil {
		t.Fatal(err)
	}
	if len(res.Skills) != 2 {
		t.Fatalf("expected project skill plus global lock-without-files, got %d", len(res.Skills))
	}
	var missingProject, lockWithoutFiles bool
	for _, sk := range res.Skills {
		for _, issue := range sk.HealthIssues {
			missingProject = missingProject || issue.Type == "missing_project_lock"
			lockWithoutFiles = lockWithoutFiles || issue.Type == "lock_without_files"
		}
	}
	if !missingProject || !lockWithoutFiles {
		t.Fatalf("expected missing project lock and global lock_without_files, got %#v", res.Skills)
	}
}

func TestMissingSkillMDAndMetadataSanitization(t *testing.T) {
	withHome(t)
	cwd := t.TempDir()
	root := filepath.Join(cwd, ".agents", "skills")
	if err := os.MkdirAll(filepath.Join(root, "missing-md"), 0o755); err != nil {
		t.Fatal(err)
	}
	esc := filepath.Join(root, "escape")
	if err := os.MkdirAll(esc, 0o755); err != nil {
		t.Fatal(err)
	}
	content := "---\nname: \"Bad\\u001b[31m Name\"\ndescription: \"Line one\\nLine two\"\n---\nbody"
	if err := os.WriteFile(filepath.Join(esc, "SKILL.md"), []byte(content), 0o644); err != nil {
		t.Fatal(err)
	}

	res, err := Run(cwd)
	if err != nil {
		t.Fatal(err)
	}
	var missing, sanitized bool
	for _, sk := range res.Skills {
		for _, issue := range sk.HealthIssues {
			missing = missing || issue.Type == "missing_skill_md"
		}
		if sk.Name == "Bad Name" && sk.Description == "Line one Line two" {
			sanitized = true
		}
	}
	if !missing || !sanitized {
		t.Fatalf("expected missing_skill_md and sanitized metadata, missing=%v sanitized=%v skills=%#v", missing, sanitized, res.Skills)
	}
}

func TestBrokenSymlinkAndInvalidFrontmatter(t *testing.T) {
	withHome(t)
	cwd := t.TempDir()
	root := filepath.Join(cwd, ".agents", "skills")
	if err := os.MkdirAll(root, 0o755); err != nil {
		t.Fatal(err)
	}
	if err := os.Symlink(filepath.Join(cwd, "missing"), filepath.Join(root, "broken")); err != nil {
		t.Fatal(err)
	}
	bad := filepath.Join(root, "bad")
	if err := os.MkdirAll(bad, 0o755); err != nil {
		t.Fatal(err)
	}
	if err := os.WriteFile(filepath.Join(bad, "SKILL.md"), []byte("---\nname: 123\n---\nbody"), 0o644); err != nil {
		t.Fatal(err)
	}
	res, err := Run(cwd)
	if err != nil {
		t.Fatal(err)
	}
	var broken, invalid bool
	for _, sk := range res.Skills {
		for _, issue := range sk.HealthIssues {
			if issue.Type == "broken_symlink" {
				broken = true
			}
			if issue.Type == "invalid_frontmatter" {
				invalid = true
			}
		}
	}
	if !broken || !invalid {
		t.Fatalf("expected broken and invalid issues, broken=%v invalid=%v", broken, invalid)
	}
}

func TestSharedUniversalHealthIssuesAreDeduplicated(t *testing.T) {
	withHome(t)
	cwd := t.TempDir()
	root := filepath.Join(cwd, ".agents", "skills")
	if err := os.MkdirAll(filepath.Join(root, "missing-md"), 0o755); err != nil {
		t.Fatal(err)
	}

	res, err := Run(cwd)
	if err != nil {
		t.Fatal(err)
	}
	if len(res.Skills) != 1 {
		t.Fatalf("expected 1 skill, got %d", len(res.Skills))
	}
	var missingCount int
	for _, issue := range res.Skills[0].HealthIssues {
		if issue.Type == "missing_skill_md" {
			missingCount++
		}
	}
	if missingCount != 1 {
		t.Fatalf("expected exactly one missing_skill_md issue, got %d issues=%#v", missingCount, res.Skills[0].HealthIssues)
	}
	seen := map[string]bool{}
	for _, observed := range res.Skills[0].ObservedPaths {
		seen[observed.Agent] = true
	}
	for _, agent := range []string{"universal", "opencode", "cursor", "codex"} {
		if !seen[agent] {
			t.Fatalf("expected observed path for %s, got %#v", agent, res.Skills[0].ObservedPaths)
		}
	}
}

func TestSnapshotBoundary(t *testing.T) {
	withHome(t)
	cwd := t.TempDir()
	writeSkill(t, filepath.Join(cwd, ".agents", "skills", "build"), "Build", "Build desc")

	res, err := Snapshot(cwd)
	if err != nil {
		t.Fatal(err)
	}
	if len(res.Skills) != 1 || res.Skills[0].Name != "Build" {
		t.Fatalf("unexpected snapshot result: %#v", res.Skills)
	}
}

func TestScanIncludesAgentStatesAndVisibilityReasons(t *testing.T) {
	home := withHome(t)
	cwd := t.TempDir()
	writeSkill(t, filepath.Join(cwd, ".agents", "skills", "build"), "Build", "Build desc")
	if err := os.MkdirAll(filepath.Join(home, ".claude"), 0o755); err != nil {
		t.Fatal(err)
	}

	res, err := Run(cwd)
	if err != nil {
		t.Fatal(err)
	}
	if len(res.Agents) < 70 {
		t.Fatalf("expected upstream-compatible agent states, got %d", len(res.Agents))
	}
	if !agentState(res.Agents, "claude-code").Detected {
		t.Fatalf("expected claude-code detected from cwd .claude")
	}
	if agentState(res.Agents, "promptscript").SupportsGlobal {
		t.Fatalf("expected promptscript global unsupported")
	}

	sk := res.Skills[0]
	if got := visibilityReason(sk.Visibility, "opencode"); got != "visible_via_universal_canonical" {
		t.Fatalf("expected opencode universal visibility, got %q", got)
	}
	if got := visibilityReason(sk.Visibility, "claude-code"); got != "missing_agent_link" {
		t.Fatalf("expected claude-code missing link visibility, got %q", got)
	}
	if got := visibilityReason(sk.Visibility, "augment"); got != "agent_not_detected" {
		t.Fatalf("expected augment not detected visibility, got %q", got)
	}
}

func TestUnsupportedGlobalAgentDoesNotGetGlobalCanonicalVisibility(t *testing.T) {
	home := withHome(t)
	cwd := t.TempDir()
	writeSkill(t, filepath.Join(home, ".agents", "skills", "global-build"), "Global Build", "desc")

	res, err := Run(cwd)
	if err != nil {
		t.Fatal(err)
	}
	sk := res.Skills[0]
	if got := visibilityReason(sk.Visibility, "promptscript"); got != "unsupported_global" {
		t.Fatalf("expected promptscript unsupported global, got %q visibility=%#v", got, sk.Visibility)
	}
	for _, observed := range sk.ObservedPaths {
		if observed.Agent == "promptscript" && observed.Scope == model.ScopeGlobal {
			t.Fatalf("promptscript should not observe global canonical skills: %#v", sk.ObservedPaths)
		}
	}
}

func TestGhostAgentDirectoryWithoutCanonicalDiagnostic(t *testing.T) {
	withHome(t)
	cwd := t.TempDir()
	writeSkill(t, filepath.Join(cwd, ".claude", "skills", "ghost"), "Ghost", "desc")

	res, err := Run(cwd)
	if err != nil {
		t.Fatal(err)
	}
	sk := res.Skills[0]
	if !hasIssue(sk.HealthIssues, "ghost_agent_skill") {
		t.Fatalf("expected ghost_agent_skill issue, got %#v", sk.HealthIssues)
	}
}

func agentState(states []model.AgentState, name string) model.AgentState {
	for _, state := range states {
		if state.Name == name {
			return state
		}
	}
	return model.AgentState{}
}

func visibilityReason(items []model.SkillVisibility, agent string) string {
	for _, item := range items {
		if item.Agent == agent {
			return item.Reason
		}
	}
	return ""
}

func hasIssue(issues []model.HealthIssue, issueType string) bool {
	for _, issue := range issues {
		if issue.Type == issueType {
			return true
		}
	}
	return false
}

func TestSnapshotLoadsPreviewOnce(t *testing.T) {
	withHome(t)
	cwd := t.TempDir()
	writeSkill(t, filepath.Join(cwd, ".agents", "skills", "preview"), "Preview", "Preview desc")
	res, err := Run(cwd)
	if err != nil {
		t.Fatal(err)
	}
	if len(res.Skills) != 1 || res.Skills[0].Preview == "" {
		t.Fatalf("expected scanner snapshot to load preview, got %#v", res.Skills)
	}
}

func TestCorruptLockProducesTopLevelWarningAndJSONEncodes(t *testing.T) {
	withHome(t)
	cwd := t.TempDir()
	if err := os.WriteFile(filepath.Join(cwd, "skills-lock.json"), []byte("{not json"), 0o644); err != nil {
		t.Fatal(err)
	}
	res, err := Run(cwd)
	if err != nil {
		t.Fatal(err)
	}
	if len(res.HealthIssues) == 0 || res.HealthIssues[0].Type != "corrupt_project_lock" {
		t.Fatalf("expected corrupt_project_lock issue, got %#v", res.HealthIssues)
	}
	if _, err := json.Marshal(res); err != nil {
		t.Fatalf("result should JSON encode: %v", err)
	}
}

func TestPreflightChecking(t *testing.T) {
	oldLookPath := LookPath
	defer func() { LookPath = oldLookPath }()

	LookPath = func(name string) (string, error) {
		if name == "skills" {
			return "/usr/local/bin/skills", nil
		}
		if name == "node" {
			return "/usr/local/bin/node", nil
		}
		return "", os.ErrNotExist
	}

	withHome(t)
	cwd := t.TempDir()
	res, err := Run(cwd)
	if err != nil {
		t.Fatal(err)
	}

	if res.Preflight == nil {
		t.Fatal("expected Preflight to not be nil")
	}

	if !res.Preflight.CanRunSkills {
		t.Error("expected CanRunSkills to be true when skills exists")
	}

	if !res.Preflight.Tools["skills"].Exists || res.Preflight.Tools["skills"].Path != "/usr/local/bin/skills" {
		t.Errorf("expected skills tool status to reflect exists/path, got %+v", res.Preflight.Tools["skills"])
	}

	if res.Preflight.Tools["npx"].Exists {
		t.Error("expected npx to not exist")
	}

	// Now check when skills does not exist, but npx/node/npm exist
	LookPath = func(name string) (string, error) {
		if name == "npx" {
			return "/usr/local/bin/npx", nil
		}
		if name == "node" {
			return "/usr/local/bin/node", nil
		}
		if name == "npm" {
			return "/usr/local/bin/npm", nil
		}
		return "", os.ErrNotExist
	}

	res, err = Run(cwd)
	if err != nil {
		t.Fatal(err)
	}
	if !res.Preflight.CanRunSkills {
		t.Error("expected CanRunSkills to be true when npx, node, npm exist but skills does not")
	}

	// Now check when skills does not exist and npx exists, but node is missing
	LookPath = func(name string) (string, error) {
		if name == "npx" {
			return "/usr/local/bin/npx", nil
		}
		if name == "npm" {
			return "/usr/local/bin/npm", nil
		}
		return "", os.ErrNotExist
	}
	res, err = Run(cwd)
	if err != nil {
		t.Fatal(err)
	}
	if res.Preflight.CanRunSkills {
		t.Error("expected CanRunSkills to be false when npx exists but node is missing")
	}
}

func TestScanDisabledSkills(t *testing.T) {
	withHome(t)
	cwd := t.TempDir()

	// 1. Create a disabled skill inside project-local aider-desk agent root
	disabledDir := filepath.Join(cwd, ".aider-desk", "skills", ".lazyskills-disabled", "review")
	writeSkill(t, disabledDir, "Review", "Review code")

	// Run scan
	res, err := Run(cwd)
	if err != nil {
		t.Fatal(err)
	}

	// Verify we found the disabled skill
	found := false
	for _, sk := range res.Skills {
		if sk.Name == "Review" {
			found = true
			if !sk.Disabled {
				t.Errorf("expected skill 'Review' to be disabled, got %#v", sk)
			}
			if len(sk.ObservedPaths) != 1 {
				t.Fatalf("expected 1 observed path, got %d", len(sk.ObservedPaths))
			}
			obs := sk.ObservedPaths[0]
			if obs.Status != model.StatusDisabled {
				t.Errorf("expected status 'disabled', got %s", obs.Status)
			}
			expectedTarget := filepath.Join(cwd, ".aider-desk", "skills", "review")
			if obs.TargetPath != expectedTarget {
				t.Errorf("expected target path %q, got %q", expectedTarget, obs.TargetPath)
			}
		}
	}
	if !found {
		t.Fatal("expected to find disabled skill 'Review'")
	}
}

func TestScanDisabledRelativeSymlinkDoesNotReportBroken(t *testing.T) {
	home := withHome(t)
	cwd := t.TempDir()

	targetDir := filepath.Join(home, "source-skills", "cloudflare")
	writeSkill(t, targetDir, "Cloudflare", "Cloudflare skills")

	disabledRoot := filepath.Join(home, ".claude", "skills", ".lazyskills-disabled")
	if err := os.MkdirAll(disabledRoot, 0o755); err != nil {
		t.Fatal(err)
	}
	// This is the kind of relative symlink that works from ~/.claude/skills,
	// but becomes broken according to the OS after it is moved into the disabled
	// shelf. Disabled scans should resolve it relative to the active root and not
	// report a false broken_symlink health issue.
	if err := os.Symlink(filepath.Join("..", "..", "source-skills", "cloudflare"), filepath.Join(disabledRoot, "cloudflare")); err != nil {
		t.Fatal(err)
	}

	res, err := Run(cwd)
	if err != nil {
		t.Fatal(err)
	}

	var found *model.Skill
	for _, sk := range res.Skills {
		if sk.Name == "Cloudflare" {
			found = sk
			break
		}
	}
	if found == nil {
		t.Fatal("expected to find disabled symlink skill 'Cloudflare'")
	}
	if !found.Disabled {
		t.Fatalf("expected Cloudflare skill to be disabled, got %#v", found)
	}
	for _, issue := range found.HealthIssues {
		if issue.Type == "broken_symlink" {
			t.Fatalf("disabled symlink should not report broken_symlink: %#v", issue)
		}
	}
	if found.Preview == "" {
		t.Fatal("expected disabled symlink target preview to be parsed")
	}
}

func TestNestedSkillsUnderSymlinkedRootKeepLogicalPaths(t *testing.T) {
	withHome(t)
	cwd := t.TempDir()
	target := filepath.Join(t.TempDir(), "skills")
	writeSkill(t, filepath.Join(target, "direct"), "Direct", "immediate")
	writeSkill(t, filepath.Join(target, "bundle", "one"), "One", "first")
	writeSkill(t, filepath.Join(target, "bundle", "two"), "Two", "second")
	root := filepath.Join(cwd, ".agents", "skills")
	if err := os.MkdirAll(filepath.Dir(root), 0o755); err != nil {
		t.Fatal(err)
	}
	if err := os.Symlink(target, root); err != nil {
		t.Fatal(err)
	}

	records := scanLocationRecords(agents.Location{
		Root:      root,
		Scope:     model.ScopeProject,
		AgentName: "universal",
		Canonical: true,
	})
	if len(records) != 3 {
		t.Fatalf("expected immediate and nested skills, got %#v", records)
	}
	expectedPaths := map[string]string{
		"Direct": filepath.Join(root, "direct"),
		"One":    filepath.Join(root, "bundle", "one"),
		"Two":    filepath.Join(root, "bundle", "two"),
	}
	seen := map[string]bool{}
	for _, record := range records {
		expectedPath, ok := expectedPaths[record.name]
		if !ok {
			t.Fatalf("unexpected skill record: %#v", record)
		}
		if seen[record.name] {
			t.Fatalf("expected %s exactly once, got %#v", record.name, records)
		}
		seen[record.name] = true
		if record.observed.Path != expectedPath {
			t.Fatalf("expected logical observed path %q, got %#v", expectedPath, record.observed)
		}
		if strings.Contains(record.skillPath, target) || strings.Contains(record.canonicalPath, target) {
			t.Fatalf("resolved target leaked into skill paths: %#v", record)
		}
		if strings.Contains(record.observed.Path, target) {
			t.Fatalf("resolved target leaked into observed path: %#v", record.observed)
		}
	}
	if len(seen) != len(expectedPaths) {
		t.Fatalf("expected all logical skills exactly once, got %#v", records)
	}
}

func TestNestedSymlinkedSkillIsRecordedWithoutTraversal(t *testing.T) {
	withHome(t)
	cwd := t.TempDir()
	target := filepath.Join(t.TempDir(), "linked-skill")
	writeSkill(t, target, "Linked", "linked skill")
	root := filepath.Join(cwd, ".agents", "skills")
	link := filepath.Join(root, "bundle", "linked")
	if err := os.MkdirAll(filepath.Dir(link), 0o755); err != nil {
		t.Fatal(err)
	}
	if err := os.Symlink(target, link); err != nil {
		t.Fatal(err)
	}

	res, err := Run(cwd)
	if err != nil {
		t.Fatal(err)
	}
	if len(res.Skills) != 1 || res.Skills[0].Name != "Linked" {
		t.Fatalf("expected only linked nested skill, got %#v", res.Skills)
	}
	sk := res.Skills[0]
	if sk.SkillPath != filepath.Join(link, "SKILL.md") || sk.CanonicalPath != link {
		t.Fatalf("expected logical canonical paths, got %#v", sk)
	}
	if hasIssue(sk.HealthIssues, "ghost_agent_skill") {
		t.Fatalf("canonical nested symlink should not be a ghost skill: %#v", sk.HealthIssues)
	}
	for _, observed := range sk.ObservedPaths {
		if observed.Path != link || observed.Status != model.StatusSymlink || observed.TargetPath != target {
			t.Fatalf("unexpected nested symlink observation: %#v", observed)
		}
	}
}

func TestNestedSymlinkedSkillFileIsDiscoveredNormally(t *testing.T) {
	withHome(t)
	cwd := t.TempDir()
	source := filepath.Join(t.TempDir(), "source")
	writeSkill(t, source, "Linked File", "linked file skill")
	child := filepath.Join(cwd, ".agents", "skills", "bundle", "child")
	if err := os.MkdirAll(child, 0o755); err != nil {
		t.Fatal(err)
	}
	if err := os.Symlink(filepath.Join(source, "SKILL.md"), filepath.Join(child, "SKILL.md")); err != nil {
		t.Fatal(err)
	}

	res, err := Run(cwd)
	if err != nil {
		t.Fatal(err)
	}
	if len(res.Skills) != 1 || res.Skills[0].Name != "Linked File" {
		t.Fatalf("expected symlinked SKILL.md to be discovered, got %#v", res.Skills)
	}
	if res.Skills[0].ObservedPaths[0].Status != model.StatusCanonical {
		t.Fatalf("symlinked SKILL.md should use ordinary canonical handling: %#v", res.Skills[0].ObservedPaths)
	}
}

func TestMalformedNestedSkillReportsDiagnosticAndSuppressesBundle(t *testing.T) {
	withHome(t)
	cwd := t.TempDir()
	root := filepath.Join(cwd, ".agents", "skills")
	bundle := filepath.Join(root, "bundle")
	writeSkill(t, bundle, "Bundle", "container")
	bad := filepath.Join(bundle, "bad")
	if err := os.MkdirAll(bad, 0o755); err != nil {
		t.Fatal(err)
	}
	if err := os.WriteFile(filepath.Join(bad, "SKILL.md"), []byte("---\nname: 123\n---\n"), 0o644); err != nil {
		t.Fatal(err)
	}

	res, err := Run(cwd)
	if err != nil {
		t.Fatal(err)
	}
	if len(res.Skills) != 1 || res.Skills[0].Name != "bad" {
		t.Fatalf("expected malformed nested skill and no bundle record, got %#v", res.Skills)
	}
	if !hasIssue(res.Skills[0].HealthIssues, "invalid_frontmatter") {
		t.Fatalf("expected invalid_frontmatter issue, got %#v", res.Skills[0].HealthIssues)
	}
}

func TestNestedBundleDiscoversChildrenAndSuppressesContainer(t *testing.T) {
	withHome(t)
	cwd := t.TempDir()
	bundle := filepath.Join(cwd, ".agents", "skills", "bundle")
	writeSkill(t, bundle, "Bundle", "container")
	writeSkill(t, filepath.Join(bundle, "child"), "Child", "nested skill")

	res, err := Run(cwd)
	if err != nil {
		t.Fatal(err)
	}
	if len(res.Skills) != 1 || res.Skills[0].Name != "Child" {
		t.Fatalf("expected only nested child skill, got %#v", res.Skills)
	}
}

// TestSharedScopeRootSymlinkFlagsOnlyTheSymlinkedAgent covers the
// alvinunreal/lazyskills#34 scenario: an agent's scope root directory is
// itself a symlink into another agent's canonical skills tree (e.g.
// ~/.codex/skills -> ~/.claude/skills). The skill must remain visible to
// both agents, but only the observation reached through the symlink is
// flagged as shared/non-owned.
func TestSharedScopeRootSymlinkFlagsOnlyTheSymlinkedAgent(t *testing.T) {
	home := withHome(t)
	cwd := t.TempDir()

	claudeSkills := filepath.Join(home, ".claude", "skills")
	writeSkill(t, filepath.Join(claudeSkills, "shared"), "Shared", "shared skill")

	codexHome := filepath.Join(home, ".codex")
	if err := os.MkdirAll(codexHome, 0o755); err != nil {
		t.Fatal(err)
	}
	if err := os.Symlink(claudeSkills, filepath.Join(codexHome, "skills")); err != nil {
		t.Fatal(err)
	}

	res, err := Run(cwd)
	if err != nil {
		t.Fatal(err)
	}

	var sk *model.Skill
	for _, s := range res.Skills {
		if s.Name == "Shared" {
			sk = s
		}
	}
	if sk == nil {
		t.Fatalf("expected Shared skill in scan results, got %#v", res.Skills)
	}

	var codexObserved, claudeObserved *model.ObservedPath
	for i := range sk.ObservedPaths {
		obs := &sk.ObservedPaths[i]
		switch obs.Agent {
		case "codex":
			codexObserved = obs
		case "claude-code":
			claudeObserved = obs
		}
	}
	if codexObserved == nil || claudeObserved == nil {
		t.Fatalf("expected both codex and claude-code observed paths, got %#v", sk.ObservedPaths)
	}
	if !codexObserved.SharedRoot {
		t.Fatalf("expected codex observed path to be flagged shared root: %#v", codexObserved)
	}
	if claudeObserved.SharedRoot {
		t.Fatalf("expected claude-code observed path to not be flagged shared root: %#v", claudeObserved)
	}

	if !hasIssue(sk.HealthIssues, "shared_scope_root") {
		t.Fatalf("expected shared_scope_root health issue, got %#v", sk.HealthIssues)
	}
	for _, issue := range sk.HealthIssues {
		if issue.Type == "shared_scope_root" && issue.Severity != "warning" {
			t.Fatalf("expected shared_scope_root severity warning, got %#v", issue)
		}
	}
}

// TestSharedScopeRootDisabledObservationGetsHealthWarning ensures disabled
// shelf observations under a shared scope root receive the same
// shared_scope_root health warning as active observations.
func TestSharedScopeRootDisabledObservationGetsHealthWarning(t *testing.T) {
	home := withHome(t)
	cwd := t.TempDir()

	claudeSkills := filepath.Join(home, ".claude", "skills")
	if err := os.MkdirAll(claudeSkills, 0o755); err != nil {
		t.Fatal(err)
	}
	// Put the skill only on the disabled shelf under the shared codex root.
	codexHome := filepath.Join(home, ".codex")
	if err := os.MkdirAll(codexHome, 0o755); err != nil {
		t.Fatal(err)
	}
	if err := os.Symlink(claudeSkills, filepath.Join(codexHome, "skills")); err != nil {
		t.Fatal(err)
	}
	disabled := filepath.Join(claudeSkills, ".lazyskills-disabled", "parked")
	writeSkill(t, disabled, "Parked", "disabled shared skill")

	res, err := Run(cwd)
	if err != nil {
		t.Fatal(err)
	}
	var sk *model.Skill
	for _, s := range res.Skills {
		if s.Name == "Parked" {
			sk = s
			break
		}
	}
	if sk == nil {
		t.Fatalf("expected Parked skill, got %#v", res.Skills)
	}
	var codexDisabled *model.ObservedPath
	for i := range sk.ObservedPaths {
		op := &sk.ObservedPaths[i]
		if op.Agent == "codex" && op.Status == model.StatusDisabled {
			codexDisabled = op
		}
	}
	if codexDisabled == nil {
		t.Fatalf("expected codex disabled observation, got %#v", sk.ObservedPaths)
	}
	if !codexDisabled.SharedRoot {
		t.Fatalf("expected disabled codex observation SharedRoot, got %#v", codexDisabled)
	}
	found := false
	for _, issue := range sk.HealthIssues {
		if issue.Type == "shared_scope_root" && issue.Path == codexDisabled.Path {
			found = true
			if issue.Severity != "warning" {
				t.Fatalf("expected warning severity, got %#v", issue)
			}
		}
	}
	if !found {
		t.Fatalf("expected shared_scope_root health issue on disabled path, got %#v", sk.HealthIssues)
	}
}