Skip to content

mission control for agent skills

164 KB
Raw Blame
package tui

import (
	"errors"
	"fmt"
	"os"
	"path/filepath"
	"strings"
	"sync"
	"testing"
	"time"

	"charm.land/glamour/v2"
	"github.com/charmbracelet/bubbles/viewport"
	tea "github.com/charmbracelet/bubbletea"
	"github.com/charmbracelet/lipgloss"

	"github.com/alvinunreal/lazyskills/internal/actions"
	"github.com/alvinunreal/lazyskills/internal/compat"
	"github.com/alvinunreal/lazyskills/internal/display"
	"github.com/alvinunreal/lazyskills/internal/model"
	"github.com/alvinunreal/lazyskills/internal/runner"
)

func TestFilteredSkillsUsesSanitizedSearch(t *testing.T) {
	m := appModel{
		search: "bad name",
		result: model.ScanResult{Skills: []*model.Skill{{
			Name:        "Bad\x1b[31m Name",
			Description: "desc",
			Scope:       model.ScopeProject,
		}}},
	}
	items := m.filteredSkills()
	if len(items) != 1 {
		t.Fatalf("expected sanitized search to match, got %d", len(items))
	}
}

func TestErrorViewIsSanitized(t *testing.T) {
	m := appModel{err: errors.New("bad\x1b[31m path")}
	out := m.View()
	if strings.Contains(out, "\x1b[31m") || strings.Contains(out, "\x1b") {
		t.Fatalf("expected sanitized error view, got %q", out)
	}
	if !strings.Contains(out, "bad path") {
		t.Fatalf("expected sanitized error text, got %q", out)
	}
}

func TestViewRendersWithoutFooter(t *testing.T) {
	m := appModel{width: 100, height: 30, result: model.ScanResult{Skills: []*model.Skill{{Name: "Build", Description: "Build desc", Scope: model.ScopeProject}}}}
	out := m.View()
	if strings.Contains(out, "LazySkills v1") || strings.Contains(out, "actions are guarded") || !strings.Contains(out, "Build") {
		t.Fatalf("unexpected view: %s", out)
	}
}

func TestAgentFilterLimitsVisibleSkills(t *testing.T) {
	m := appModel{agent: "opencode", result: model.ScanResult{Skills: []*model.Skill{
		{Name: "OpenCode Skill", Description: "desc", Scope: model.ScopeProject, ObservedPaths: []model.ObservedPath{{Agent: "opencode"}}},
		{Name: "Claude Skill", Description: "desc", Scope: model.ScopeProject, ObservedPaths: []model.ObservedPath{{Agent: "claude-code"}}},
	}}}
	items := m.filteredSkills()
	if len(items) != 1 || items[0].Name != "OpenCode Skill" {
		t.Fatalf("unexpected filtered skills: %#v", items)
	}
}

func TestListTitleReflectsAgentFilter(t *testing.T) {
	m := appModel{width: 100, height: 20, result: model.ScanResult{
		Agents: []model.AgentState{{Name: "opencode", Display: "OpenCode"}},
		Skills: []*model.Skill{{Name: "Build", Scope: model.ScopeProject}},
	}}
	if title := m.listTitle(); !strings.Contains(title, "1 Inventory") {
		t.Fatalf("expected all-skills title, got %q", title)
	}
	out := m.View()
	if !strings.Contains(out, "1 Inventory") {
		t.Fatalf("expected view to contain all-skills title in border, got %q", out)
	}
	m.agent = "opencode"
	if title := m.listTitle(); !strings.Contains(title, "1 Inventory (OpenCode)") {
		t.Fatalf("expected agent-specific title, got %q", title)
	}
	out = m.View()
	if !strings.Contains(out, "1 Inventory (OpenCode)") {
		t.Fatalf("expected view to contain agent-specific title in border, got %q", out)
	}
}

func TestNextAgentFilterCyclesThroughDetectedAgents(t *testing.T) {
	m := appModel{result: model.ScanResult{Agents: []model.AgentState{
		{Name: "opencode", Detected: true},
		{Name: "cursor", Detected: true},
	}}}
	first := m.nextAgentFilter()
	if first != "cursor" {
		t.Fatalf("expected first detected agent cursor, got %q", first)
	}
	m.agent = first
	second := m.nextAgentFilter()
	if second != "opencode" {
		t.Fatalf("expected second detected agent opencode, got %q", second)
	}
	m.agent = second
	if got := m.nextAgentFilter(); got != "" {
		t.Fatalf("expected cycle back to all, got %q", got)
	}
}

func TestAgentFilterCyclesDetectedAgentsNoSupportedFallback(t *testing.T) {
	m := appModel{result: model.ScanResult{Agents: []model.AgentState{
		{Name: "opencode", Display: "OpenCode", Detected: true},
		{Name: "cursor", Display: "Cursor"},
		{Name: "claude-code", Display: "Claude Code", Detected: true},
	}}}
	if got := m.agentFilters(); strings.Join(got, ",") != "claude-code,opencode" {
		t.Fatalf("expected only detected agents in rotation, got %#v", got)
	}
	m = appModel{result: model.ScanResult{Agents: []model.AgentState{{Name: "opencode", Display: "OpenCode"}}}}
	if got := m.agentFilters(); len(got) != 0 {
		t.Fatalf("expected no fallback to supported agents when none detected, got %#v", got)
	}
}

func TestAgentClearedOnRefreshIfNoLongerDetected(t *testing.T) {
	m := appModel{
		agent: "opencode",
		result: model.ScanResult{
			Agents: []model.AgentState{
				{Name: "opencode", Detected: true},
			},
		},
	}
	// Simulate a snapshotMsg where opencode is no longer detected (or not present in Agents)
	updated, _ := m.Update(snapshotMsg{
		result: model.ScanResult{
			Agents: []model.AgentState{
				{Name: "opencode", Detected: false},
			},
		},
	})
	next := updated.(appModel)
	if next.agent != "" {
		t.Fatalf("expected agent to be cleared when no longer detected after refresh, got %q", next.agent)
	}
}

func TestAgentFilterCanSelectSupportedAgentWithNoSkills(t *testing.T) {
	m := appModel{width: 100, height: 30, agent: "claude-code", result: model.ScanResult{
		Agents: []model.AgentState{{Name: "claude-code", Display: "Claude Code"}},
		Skills: []*model.Skill{
			{Name: "OpenCode Skill", Description: "desc", Scope: model.ScopeProject, ObservedPaths: []model.ObservedPath{{Agent: "opencode"}}},
		},
	}}
	items := m.filteredSkills()
	if len(items) != 0 {
		t.Fatalf("expected no skills for claude-code, got %#v", items)
	}
	out := m.View()
	if !strings.Contains(out, "Claude Code") || !strings.Contains(out, "No skills matched") {
		t.Fatalf("expected zero-skill agent empty state, got %q", out)
	}
}

func TestAgentFilterCanResetDirectlyToAll(t *testing.T) {
	m := appModel{width: 100, height: 30, agent: "claude-code", result: model.ScanResult{Skills: []*model.Skill{
		{Name: "OpenCode Skill", Description: "desc", Scope: model.ScopeProject, ObservedPaths: []model.ObservedPath{{Agent: "opencode"}}},
	}}}
	updated, _ := m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'A'}})
	next := updated.(appModel)
	if next.agent != "" {
		t.Fatalf("expected A to reset agent filter, got %q", next.agent)
	}
	updated, _ = appModel{agent: "claude-code", result: m.result}.Update(tea.KeyMsg{Type: tea.KeyEsc})
	next = updated.(appModel)
	if next.agent != "" {
		t.Fatalf("expected esc to reset agent filter when no mode is active, got %q", next.agent)
	}
}

func TestAgentFilterPreservesSelectedSkill(t *testing.T) {
	m := appModel{width: 100, height: 30, selected: 2, result: model.ScanResult{
		Agents: []model.AgentState{{Name: "claude-code", Detected: true}, {Name: "opencode", Detected: true}},
		Skills: []*model.Skill{
			{Name: "OpenOnly", Scope: model.ScopeProject, ObservedPaths: []model.ObservedPath{{Agent: "opencode"}}},
			{Name: "Shared", Scope: model.ScopeProject, ObservedPaths: []model.ObservedPath{{Agent: "opencode"}, {Agent: "claude-code"}}},
			{Name: "ClaudeOnly", Scope: model.ScopeProject, ObservedPaths: []model.ObservedPath{{Agent: "claude-code"}}},
		},
	}}
	updated, _ := m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'a'}})
	next := updated.(appModel)
	items := next.filteredSkills()
	if next.agent != "claude-code" || len(items) <= next.selected || items[next.selected].Name != "ClaudeOnly" {
		t.Fatalf("expected agent switch to preserve ClaudeOnly, agent=%q selected=%d items=%#v", next.agent, next.selected, items)
	}
}

func TestClearingAgentFilterPreservesSelectedSkill(t *testing.T) {
	m := appModel{width: 100, height: 30, agent: "claude-code", selected: 1, result: model.ScanResult{
		Agents: []model.AgentState{{Name: "claude-code", Detected: true}},
		Skills: []*model.Skill{
			{Name: "OpenOnly", Scope: model.ScopeProject, ObservedPaths: []model.ObservedPath{{Agent: "opencode"}}},
			{Name: "Shared", Scope: model.ScopeProject, ObservedPaths: []model.ObservedPath{{Agent: "opencode"}, {Agent: "claude-code"}}},
			{Name: "ClaudeOnly", Scope: model.ScopeProject, ObservedPaths: []model.ObservedPath{{Agent: "claude-code"}}},
		},
	}}
	updated, _ := m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'A'}})
	next := updated.(appModel)
	items := next.filteredSkills()
	if next.agent != "" || len(items) <= next.selected || items[next.selected].Name != "ClaudeOnly" {
		t.Fatalf("expected clearing agent to preserve ClaudeOnly, agent=%q selected=%d items=%#v", next.agent, next.selected, items)
	}
}

func TestTopLevelScanHealthIsRenderedSanitized(t *testing.T) {
	m := appModel{width: 100, height: 30, result: model.ScanResult{HealthIssues: []model.HealthIssue{{Type: "corrupt_lock", Message: "bad\x1b[31m lock"}}}}
	out := m.View()
	if !strings.Contains(out, "Scan health") || !strings.Contains(out, "bad") || !strings.Contains(out, "lock") {
		t.Fatalf("expected scan health in output: %q", out)
	}
	if strings.Contains(out, "\x1b[31m") || strings.ContainsRune(out, '\x1b') {
		t.Fatalf("expected sanitized scan health: %q", out)
	}
}

func TestCommandPreviewModeRendersWithoutExecuting(t *testing.T) {
	m := appModel{width: 120, height: 40, selected: 1, commands: true, result: model.ScanResult{Skills: []*model.Skill{{
		Name:      "Deploy Skill",
		Scope:     model.ScopeProject,
		LocalLock: &model.LocalLockEntry{Source: "owner/repo"},
	}}}}
	out := m.View()
	if !strings.Contains(out, "Actions") || !strings.Contains(out, "Reinstall/update selected skill") || !strings.Contains(out, "skills add") || !strings.Contains(out, "--yes") {
		t.Fatalf("expected actions in output: %q", out)
	}
	if strings.Contains(out, "Refresh LazySkills") {
		t.Fatalf("refresh belongs on r key, not action list: %q", out)
	}
}

func TestActiveAgentVisibilityReasonIsRendered(t *testing.T) {
	m := appModel{width: 120, height: 32, selected: 1, agent: "claude-code", result: model.ScanResult{Skills: []*model.Skill{{
		Name:          "Build",
		Description:   "desc",
		Scope:         model.ScopeProject,
		CanonicalPath: "/tmp/build",
		Visibility:    []model.SkillVisibility{{Agent: "claude-code", Display: "Claude Code", Visible: false, Reason: "missing_agent_link"}},
	}}}}
	out := m.View()
	if !strings.Contains(out, "Build") || !strings.Contains(out, "Claude Code: not linked") {
		t.Fatalf("expected active agent visibility reason, got %q", out)
	}
}

func TestAgentFilterListMarksNonVisibleSkills(t *testing.T) {
	m := appModel{width: 120, height: 32, agent: "claude-code", result: model.ScanResult{Skills: []*model.Skill{
		{
			Name:          "Visible",
			Scope:         model.ScopeProject,
			CanonicalPath: "/tmp/visible",
			Visibility:    []model.SkillVisibility{{Agent: "claude-code", Display: "Claude Code", Visible: true, Reason: "visible_via_symlink"}},
		},
		{
			Name:          "Missing",
			Scope:         model.ScopeProject,
			CanonicalPath: "/tmp/missing",
			Visibility:    []model.SkillVisibility{{Agent: "claude-code", Display: "Claude Code", Visible: false, Reason: "missing_agent_link"}},
		},
	}}}
	out := compat.StripTerminalEscapes(m.listPane(20, 80, m.visibleRows()))
	if !strings.Contains(out, "Visible [P] ✓") || !strings.Contains(out, "Missing [P] ×") {
		t.Fatalf("expected list-level visibility badges, got %q", out)
	}
	if strings.Contains(out, "not linked") || strings.Contains(out, "available") {
		t.Fatalf("expected compact list badges without explanatory text, got %q", out)
	}
}

func TestListRendersIssueRowsWithSeverityBadges(t *testing.T) {
	m := appModel{width: 120, height: 32, selected: 1, result: model.ScanResult{Skills: []*model.Skill{
		{Name: "Healthy", Scope: model.ScopeProject},
		{Name: "Warning", Scope: model.ScopeProject, HealthIssues: []model.HealthIssue{{Type: "missing_global_lock", Severity: "warning", Message: "not tracked"}}},
		{Name: "Error", Scope: model.ScopeProject, HealthIssues: []model.HealthIssue{{Type: "missing_file", Severity: "error", Message: "missing SKILL.md"}}},
	}}}
	out := m.listPane(20, 80, m.visibleRows())
	if !strings.Contains(out, "Warning [P] ▲1") {
		t.Fatalf("expected warning issue badge, got %q", out)
	}
	if !strings.Contains(out, "Error [P] !1") {
		t.Fatalf("expected error issue badge, got %q", out)
	}
	if strings.Contains(out, "BROKEN") {
		t.Fatalf("issue badge should stay subtle, got %q", out)
	}
}

func TestDetailRendersWarningOnlyIssuesAsWarnings(t *testing.T) {
	m := appModel{selected: 1, result: model.ScanResult{Skills: []*model.Skill{{Name: "Custom", Scope: model.ScopeGlobal, HealthIssues: []model.HealthIssue{{Type: "ghost_agent_skill", Severity: "warning", Message: "agent-specific skill"}}}}}}
	out := m.detailText(80)
	if !strings.Contains(out, "Warnings") || strings.Contains(out, "Health Issues") {
		t.Fatalf("expected warning-only detail section, got %q", out)
	}
}

func TestCommandPreviewToggle(t *testing.T) {
	m := appModel{}
	updated, _ := m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'c'}})
	next := updated.(appModel)
	if !next.commands {
		t.Fatalf("expected command preview mode enabled")
	}
}

func TestActionConfirmationCancelDoesNotExecute(t *testing.T) {
	called := false
	old := runExec
	runExec = func(spec runner.ExecSpec) runner.Result { called = true; return runner.Result{ExitCode: 0} }
	t.Cleanup(func() { runExec = old })

	m := actionTestModel(t.TempDir())
	m.commands = true
	m.action = actionIndex(t, m, "Reinstall/update selected skill")
	updated, cmd := m.Update(tea.KeyMsg{Type: tea.KeyEnter})
	next := updated.(appModel)
	if cmd != nil || !next.confirming {
		t.Fatalf("expected confirmation without command, confirming=%v cmd=%v", next.confirming, cmd)
	}
	updated, cmd = next.Update(tea.KeyMsg{Type: tea.KeyEsc})
	next = updated.(appModel)
	if cmd != nil || next.confirming || called {
		t.Fatalf("expected cancel without execution, confirming=%v called=%v", next.confirming, called)
	}
}

func TestConfirmationAcceptsDefaultYAndYes(t *testing.T) {
	cases := []struct {
		name  string
		input string
	}{
		{"y", "y"},
		{"yes", "yes"},
	}
	for _, tc := range cases {
		t.Run(tc.name, func(t *testing.T) {
			called := false
			old := runExec
			runExec = func(spec runner.ExecSpec) runner.Result { called = true; return runner.Result{ExitCode: 0} }
			t.Cleanup(func() { runExec = old })

			m := actionTestModel(t.TempDir())
			m.commands = true
			m.action = actionIndex(t, m, "Reinstall/update selected skill")
			updated, _ := m.Update(tea.KeyMsg{Type: tea.KeyEnter})
			m = updated.(appModel)
			for _, r := range []rune(tc.input) {
				updated, _ = m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{r}})
				m = updated.(appModel)
			}
			updated, cmd := m.Update(tea.KeyMsg{Type: tea.KeyEnter})
			m = updated.(appModel)
			if cmd == nil || !m.running {
				t.Fatalf("expected accepted confirmation to run, input=%q running=%v cmd=%v", tc.input, m.running, cmd)
			}
			cmd()
			if !called {
				t.Fatalf("expected command execution")
			}
		})
	}
}

func TestSafeConfirmationAcceptsEmptyInput(t *testing.T) {
	called := false
	old := runExec
	runExec = func(spec runner.ExecSpec) runner.Result { called = true; return runner.Result{ExitCode: 0} }
	t.Cleanup(func() { runExec = old })

	m := actionTestModel(t.TempDir())
	m.commands = true
	m.action = actionIndex(t, m, "Reinstall/update selected skill")
	updated, _ := m.Update(tea.KeyMsg{Type: tea.KeyEnter})
	m = updated.(appModel)

	// Safe actions accept empty Enter as the default confirmation.
	updated, cmd := m.Update(tea.KeyMsg{Type: tea.KeyEnter})
	m = updated.(appModel)
	if cmd == nil || !m.running || m.confirming {
		t.Fatalf("expected empty input to run safe confirmation, running=%v confirming=%v cmd=%v", m.running, m.confirming, cmd)
	}
	cmd()
	if !called {
		t.Fatalf("expected command execution")
	}
}

func TestConfirmationRendersCenteredModal(t *testing.T) {
	m := actionTestModel(t.TempDir())
	m.commands = true
	m.action = actionIndex(t, m, "Reinstall/update selected skill")
	updated, _ := m.Update(tea.KeyMsg{Type: tea.KeyEnter})
	m = updated.(appModel)
	out := m.View()
	if !strings.Contains(out, "Reinstall/update selected skill") || !strings.Contains(out, "Press Enter to continue") || strings.Contains(out, "Bulk actions") {
		t.Fatalf("expected standalone confirmation modal, got %q", out)
	}
}

func TestRunningActionRendersProgressModal(t *testing.T) {
	m := actionTestModel(t.TempDir())
	m.running = true
	m.runningTitle = "Reinstall/update selected skill"
	out := m.View()
	if !strings.Contains(out, "Running") || !strings.Contains(out, "Reinstall/update selected skill") || !strings.Contains(out, "Working") {
		t.Fatalf("expected running progress modal, got %q", out)
	}
}

func TestActionExecUsesProjectCwdAndPreventsDuplicateWhileRunning(t *testing.T) {
	cwd := t.TempDir()
	old := runExec
	runExec = func(spec runner.ExecSpec) runner.Result {
		if spec.Cwd != cwd {
			t.Fatalf("expected cwd %q, got %q", cwd, spec.Cwd)
		}
		return runner.Result{Program: spec.Program, Args: spec.Args, Cwd: spec.Cwd, ExitCode: 0, Stdout: "ok"}
	}
	t.Cleanup(func() { runExec = old })

	m := actionTestModel(cwd)
	m.commands = true
	m.action = actionIndex(t, m, "Reinstall/update selected skill")
	updated, _ := m.Update(tea.KeyMsg{Type: tea.KeyEnter})
	m = updated.(appModel)
	for _, r := range []rune("yes") {
		updated, _ = m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{r}})
		m = updated.(appModel)
	}
	updated, cmd := m.Update(tea.KeyMsg{Type: tea.KeyEnter})
	m = updated.(appModel)
	if cmd == nil || !m.running {
		t.Fatalf("expected running command")
	}
	updated, dup := m.Update(tea.KeyMsg{Type: tea.KeyEnter})
	if dup != nil || !updated.(appModel).running {
		t.Fatalf("expected duplicate enter ignored while running")
	}
	msg := cmd()
	updated, rescan := m.Update(msg)
	m = updated.(appModel)
	if m.running || m.actionResult == nil || m.actionResult.Stdout != "ok" || rescan == nil {
		t.Fatalf("expected result and rescan, model=%#v rescan=%v", m.actionResult, rescan)
	}
}

func TestSpaceMarksSkillsAndEscClearsSelection(t *testing.T) {
	m := bulkActionTestModel(t.TempDir())
	updated, _ := m.Update(tea.KeyMsg{Type: tea.KeySpace})
	m = updated.(appModel)
	if m.selectedCount() != 1 || !strings.Contains(m.View(), "● One") {
		t.Fatalf("expected one marked skill, count=%d view=%q", m.selectedCount(), m.View())
	}
	updated, _ = m.Update(tea.KeyMsg{Type: tea.KeyEsc})
	m = updated.(appModel)
	if m.selectedCount() != 0 {
		t.Fatalf("expected esc to clear selection, got %d", m.selectedCount())
	}
}

func TestEscClosesActionPickerOverlay(t *testing.T) {
	m := bulkActionTestModel(t.TempDir())
	m.commands = true
	m.agent = "opencode"
	m.selectedKeys = map[string]bool{skillKey(m.result.Skills[0]): true}
	updated, _ := m.Update(tea.KeyMsg{Type: tea.KeyEsc})
	next := updated.(appModel)
	if next.commands {
		t.Fatalf("expected esc to close action mode overlay, commands is still true")
	}
	if next.selectedCount() != 1 {
		t.Fatalf("expected selection to be preserved when action mode is closed, got %d", next.selectedCount())
	}
	if next.agent != "opencode" {
		t.Fatalf("expected agent to be preserved when action mode is closed, got %q", next.agent)
	}
}

func TestBulkActionsRenderAndExecuteSequentially(t *testing.T) {
	cwd := t.TempDir()
	calls := 0
	old := runExec
	runExec = func(spec runner.ExecSpec) runner.Result {
		calls++
		return runner.Result{Program: spec.Program, Args: spec.Args, Cwd: spec.Cwd, ExitCode: 0}
	}
	t.Cleanup(func() { runExec = old })

	m := bulkActionTestModel(cwd)
	m.selectedKeys = map[string]bool{}
	for _, skill := range m.result.Skills {
		m.selectedKeys[skillKey(skill)] = true
	}
	m.commands = true
	out := m.View()
	if !strings.Contains(out, "Bulk actions") || !strings.Contains(out, "Reinstall/update 2 selected skills") || strings.Contains(out, "Open selected skill") {
		t.Fatalf("expected constrained bulk actions, got %q", out)
	}
	updated, _ := m.Update(tea.KeyMsg{Type: tea.KeyEnter})
	m = updated.(appModel)
	for _, r := range []rune("update 2 skills") {
		updated, _ = m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{r}})
		m = updated.(appModel)
	}
	updated, cmd := m.Update(tea.KeyMsg{Type: tea.KeyEnter})
	m = updated.(appModel)
	if cmd == nil || !m.running {
		t.Fatalf("expected bulk command to run")
	}
	updated, rescan := m.Update(cmd())
	m = updated.(appModel)
	if calls != 2 || m.selectedCount() != 0 || rescan == nil {
		t.Fatalf("expected two commands, cleared selection, and rescan; calls=%d selected=%d rescan=%v", calls, m.selectedCount(), rescan)
	}
}

func TestBulkActionStopsOnFirstFailure(t *testing.T) {
	cwd := t.TempDir()
	calls := 0
	old := runExec
	runExec = func(spec runner.ExecSpec) runner.Result {
		calls++
		if calls == 1 {
			return runner.Result{Program: spec.Program, Args: spec.Args, Cwd: spec.Cwd, ExitCode: 2, Stderr: "nope"}
		}
		return runner.Result{Program: spec.Program, Args: spec.Args, Cwd: spec.Cwd, ExitCode: 0}
	}
	t.Cleanup(func() { runExec = old })
	m := bulkActionTestModel(cwd)
	m.selectedKeys = map[string]bool{}
	for _, skill := range m.result.Skills {
		m.selectedKeys[skillKey(skill)] = true
	}
	result, partialSuccess := m.runBatch(m.currentActions()[0].ID, m.currentActions()[0].Exec.Batch)
	if calls != 1 || result.ExitCode != 2 {
		t.Fatalf("expected stop on first failure, calls=%d result=%#v", calls, result)
	}
	if partialSuccess {
		t.Fatalf("first-command failure should not count as partial success")
	}
}

func TestBulkPartialFailureRescansAndKeepsSelection(t *testing.T) {
	cwd := t.TempDir()
	calls := 0
	old := runExec
	runExec = func(spec runner.ExecSpec) runner.Result {
		calls++
		if calls == 2 {
			return runner.Result{Program: spec.Program, Args: spec.Args, Cwd: spec.Cwd, ExitCode: 2, Stderr: "nope"}
		}
		return runner.Result{Program: spec.Program, Args: spec.Args, Cwd: spec.Cwd, ExitCode: 0}
	}
	t.Cleanup(func() { runExec = old })
	m := bulkActionTestModel(cwd)
	m.selectedKeys = map[string]bool{}
	for _, skill := range m.result.Skills {
		m.selectedKeys[skillKey(skill)] = true
	}
	m.commands = true
	updated, _ := m.Update(tea.KeyMsg{Type: tea.KeyEnter})
	m = updated.(appModel)
	for _, r := range []rune("update 2 skills") {
		updated, _ = m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{r}})
		m = updated.(appModel)
	}
	updated, cmd := m.Update(tea.KeyMsg{Type: tea.KeyEnter})
	m = updated.(appModel)
	if cmd == nil {
		t.Fatalf("expected bulk command")
	}
	updated, rescan := m.Update(cmd())
	m = updated.(appModel)
	if calls != 2 || rescan == nil || m.selectedCount() != 2 || m.actionResult == nil || m.actionResult.ExitCode != 2 {
		t.Fatalf("expected partial failure to rescan and keep selection; calls=%d rescan=%v selected=%d result=%#v", calls, rescan, m.selectedCount(), m.actionResult)
	}
}

func TestDeleteBrokenSymlinkPartialFailureRescans(t *testing.T) {
	if os.Getuid() == 0 {
		t.Skip("permission-denied symlink removal path is not meaningful as root")
	}
	cwd := t.TempDir()
	skillsRoot := filepath.Join(cwd, ".agents", "skills")
	if err := os.MkdirAll(skillsRoot, 0o755); err != nil {
		t.Fatal(err)
	}
	goodLink := filepath.Join(skillsRoot, "good-broken")
	if err := os.Symlink(filepath.Join(cwd, "missing-good"), goodLink); err != nil {
		t.Fatal(err)
	}
	lockedDir := filepath.Join(skillsRoot, "locked")
	if err := os.Mkdir(lockedDir, 0o755); err != nil {
		t.Fatal(err)
	}
	badLink := filepath.Join(lockedDir, "bad-broken")
	if err := os.Symlink(filepath.Join(cwd, "missing-bad"), badLink); err != nil {
		t.Fatal(err)
	}
	if err := os.Chmod(lockedDir, 0o555); err != nil {
		t.Fatal(err)
	}
	t.Cleanup(func() { _ = os.Chmod(lockedDir, 0o755) })

	m := appModel{cwd: cwd, width: 120, height: 32, selected: 1, result: model.ScanResult{Skills: []*model.Skill{{
		Name:  "Broken",
		Scope: model.ScopeProject,
		ObservedPaths: []model.ObservedPath{
			{Path: goodLink, Status: model.StatusBrokenSymlink},
			{Path: badLink, Status: model.StatusBrokenSymlink},
		},
	}}}}
	action := actions.CommandPreview{ID: "delete_broken_symlink", ConfirmValue: "Broken", Exec: actions.ExecSpec{Internal: "delete_broken_symlink", Args: []string{string(model.ScopeProject), "Broken"}}}

	updated, cmd := m.executeAction(action)
	next := updated.(appModel)
	if cmd == nil {
		t.Fatal("expected partial delete success to trigger a rescan")
	}
	if next.actionResult == nil || next.actionResult.ExitCode != -1 || !strings.Contains(next.actionResult.Err, "removed 1 broken symlink") {
		t.Fatalf("expected partial failure action result, got %#v", next.actionResult)
	}
	updated, _ = next.Update(cmd())
	next = updated.(appModel)
	if next.actionResult == nil || next.actionResult.ExitCode != -1 || !strings.Contains(next.actionResult.Err, "removed 1 broken symlink") {
		t.Fatalf("expected partial failure result to survive rescan, got %#v", next.actionResult)
	}
	if _, err := os.Lstat(goodLink); !os.IsNotExist(err) {
		t.Fatalf("expected removable broken symlink to be deleted, lstat err=%v", err)
	}
	if _, err := os.Lstat(badLink); err != nil {
		t.Fatalf("expected failed broken symlink to remain, lstat err=%v", err)
	}
}

func TestDeleteBrokenSymlinkFailureRescans(t *testing.T) {
	if os.Getuid() == 0 {
		t.Skip("permission-denied symlink removal path is not meaningful as root")
	}
	cwd := t.TempDir()
	skillsRoot := filepath.Join(cwd, ".agents", "skills")
	if err := os.MkdirAll(skillsRoot, 0o755); err != nil {
		t.Fatal(err)
	}
	lockedDir := filepath.Join(skillsRoot, "locked")
	if err := os.Mkdir(lockedDir, 0o755); err != nil {
		t.Fatal(err)
	}
	badLink := filepath.Join(lockedDir, "bad-broken")
	if err := os.Symlink(filepath.Join(cwd, "missing-bad"), badLink); err != nil {
		t.Fatal(err)
	}
	if err := os.Chmod(lockedDir, 0o555); err != nil {
		t.Fatal(err)
	}
	t.Cleanup(func() { _ = os.Chmod(lockedDir, 0o755) })

	m := appModel{cwd: cwd, width: 120, height: 32, selected: 1, result: model.ScanResult{Skills: []*model.Skill{{
		Name:          "Broken",
		Scope:         model.ScopeProject,
		ObservedPaths: []model.ObservedPath{{Path: badLink, Status: model.StatusBrokenSymlink}},
	}}}}
	action := actions.CommandPreview{ID: "delete_broken_symlink", ConfirmValue: "Broken", Exec: actions.ExecSpec{Internal: "delete_broken_symlink", Args: []string{string(model.ScopeProject), "Broken"}}}

	updated, cmd := m.executeAction(action)
	next := updated.(appModel)
	if cmd == nil {
		t.Fatal("expected failed delete to trigger a rescan")
	}
	if next.actionResult == nil || next.actionResult.ExitCode != -1 || !strings.Contains(next.actionResult.Err, "removed 0 broken symlink") {
		t.Fatalf("expected failed delete action result, got %#v", next.actionResult)
	}
	updated, _ = next.Update(cmd())
	next = updated.(appModel)
	if next.actionResult == nil || next.actionResult.ExitCode != -1 || !strings.Contains(next.actionResult.Err, "removed 0 broken symlink") {
		t.Fatalf("expected failed delete result to survive rescan, got %#v", next.actionResult)
	}
	if _, err := os.Lstat(badLink); err != nil {
		t.Fatalf("expected failed broken symlink to remain after rescan, lstat err=%v", err)
	}
}

func TestDeleteBrokenSymlinkRequiresScopedIdentity(t *testing.T) {
	cwd := t.TempDir()
	projectRoot := filepath.Join(cwd, ".agents", "skills")
	if err := os.MkdirAll(projectRoot, 0o755); err != nil {
		t.Fatal(err)
	}
	projectLink := filepath.Join(projectRoot, "project-broken")
	globalLink := filepath.Join(projectRoot, "global-broken")
	if err := os.Symlink(filepath.Join(cwd, "missing-project"), projectLink); err != nil {
		t.Fatal(err)
	}
	if err := os.Symlink(filepath.Join(cwd, "missing-global"), globalLink); err != nil {
		t.Fatal(err)
	}
	m := appModel{cwd: cwd, width: 120, height: 32, selected: 1, result: model.ScanResult{Skills: []*model.Skill{
		{Name: "Duplicate", Scope: model.ScopeGlobal, ObservedPaths: []model.ObservedPath{{Path: globalLink, Status: model.StatusBrokenSymlink}}},
		{Name: "Duplicate", Scope: model.ScopeProject, ObservedPaths: []model.ObservedPath{{Path: projectLink, Status: model.StatusBrokenSymlink}}},
	}}}
	action := actions.CommandPreview{ID: "delete_broken_symlink", ConfirmValue: "Duplicate", Exec: actions.ExecSpec{Internal: "delete_broken_symlink"}}

	updated, cmd := m.executeAction(action)
	next := updated.(appModel)
	if cmd != nil {
		t.Fatalf("expected malformed delete action not to run, got cmd=%v", cmd)
	}
	if next.actionResult == nil || next.actionResult.ExitCode != -1 || !strings.Contains(next.actionResult.Err, "missing scoped skill identity") {
		t.Fatalf("expected missing scoped identity error, got %#v", next.actionResult)
	}
	if _, err := os.Lstat(projectLink); err != nil {
		t.Fatalf("expected project link to remain, err=%v", err)
	}
	if _, err := os.Lstat(globalLink); err != nil {
		t.Fatalf("expected global link to remain, err=%v", err)
	}

	action.Exec.Args = []string{"", "Duplicate"}
	updated, cmd = m.executeAction(action)
	next = updated.(appModel)
	if cmd != nil {
		t.Fatalf("expected empty-scope delete action not to run, got cmd=%v", cmd)
	}
	if next.actionResult == nil || next.actionResult.ExitCode != -1 || !strings.Contains(next.actionResult.Err, "missing scoped skill identity") {
		t.Fatalf("expected empty-scope identity error, got %#v", next.actionResult)
	}
	if _, err := os.Lstat(projectLink); err != nil {
		t.Fatalf("expected project link to remain after empty-scope action, err=%v", err)
	}
	if _, err := os.Lstat(globalLink); err != nil {
		t.Fatalf("expected global link to remain after empty-scope action, err=%v", err)
	}
}

func TestDeleteBrokenSymlinkTargetsMatchingScope(t *testing.T) {
	cwd := t.TempDir()
	projectRoot := filepath.Join(cwd, ".agents", "skills")
	if err := os.MkdirAll(projectRoot, 0o755); err != nil {
		t.Fatal(err)
	}
	projectLink := filepath.Join(projectRoot, "project-broken")
	globalLink := filepath.Join(projectRoot, "global-broken")
	if err := os.Symlink(filepath.Join(cwd, "missing-project"), projectLink); err != nil {
		t.Fatal(err)
	}
	if err := os.Symlink(filepath.Join(cwd, "missing-global"), globalLink); err != nil {
		t.Fatal(err)
	}

	m := appModel{cwd: cwd, width: 120, height: 32, selected: 1, result: model.ScanResult{Skills: []*model.Skill{
		{Name: "Duplicate", Scope: model.ScopeGlobal, ObservedPaths: []model.ObservedPath{{Path: globalLink, Status: model.StatusBrokenSymlink}}},
		{Name: "Duplicate", Scope: model.ScopeProject, ObservedPaths: []model.ObservedPath{{Path: projectLink, Status: model.StatusBrokenSymlink}}},
	}}}
	action := actions.CommandPreview{ID: "delete_broken_symlink", ConfirmValue: "Duplicate", Exec: actions.ExecSpec{Internal: "delete_broken_symlink", Args: []string{string(model.ScopeProject), "Duplicate"}}}

	updated, cmd := m.executeAction(action)
	next := updated.(appModel)
	if cmd == nil || next.actionResult == nil || !strings.Contains(next.actionResult.Stdout, "1 broken symlink") {
		t.Fatalf("expected scoped delete success with rescan, result=%#v cmd=%v", next.actionResult, cmd)
	}
	updated, _ = next.Update(cmd())
	next = updated.(appModel)
	if next.actionResult == nil || !strings.Contains(next.actionResult.Stdout, "1 broken symlink") {
		t.Fatalf("expected scoped delete success result to survive rescan, got %#v", next.actionResult)
	}
	if _, err := os.Lstat(projectLink); !os.IsNotExist(err) {
		t.Fatalf("expected project broken symlink to be deleted, lstat err=%v", err)
	}
	if _, err := os.Lstat(globalLink); err != nil {
		t.Fatalf("expected global broken symlink to remain, lstat err=%v", err)
	}
}

func TestDeleteBrokenSymlinkRechecksPathBeforeRemove(t *testing.T) {
	cwd := t.TempDir()
	skillsRoot := filepath.Join(cwd, ".agents", "skills")
	if err := os.MkdirAll(skillsRoot, 0o755); err != nil {
		t.Fatal(err)
	}
	restoredFile := filepath.Join(skillsRoot, "restored-file")
	if err := os.WriteFile(restoredFile, []byte("do not delete"), 0o644); err != nil {
		t.Fatal(err)
	}
	restoredTarget := filepath.Join(cwd, "restored-target")
	if err := os.WriteFile(restoredTarget, []byte("target"), 0o644); err != nil {
		t.Fatal(err)
	}
	restoredLink := filepath.Join(skillsRoot, "restored-link")
	if err := os.Symlink(restoredTarget, restoredLink); err != nil {
		t.Fatal(err)
	}
	brokenLink := filepath.Join(skillsRoot, "still-broken")
	if err := os.Symlink(filepath.Join(cwd, "missing"), brokenLink); err != nil {
		t.Fatal(err)
	}

	m := appModel{cwd: cwd, width: 120, height: 32, selected: 1, result: model.ScanResult{Skills: []*model.Skill{{
		Name:  "Broken",
		Scope: model.ScopeProject,
		ObservedPaths: []model.ObservedPath{
			{Path: restoredFile, Status: model.StatusBrokenSymlink},
			{Path: restoredLink, Status: model.StatusBrokenSymlink},
			{Path: brokenLink, Status: model.StatusBrokenSymlink},
		},
	}}}}
	action := actions.CommandPreview{ID: "delete_broken_symlink", ConfirmValue: "Broken", Exec: actions.ExecSpec{Internal: "delete_broken_symlink", Args: []string{string(model.ScopeProject), "Broken"}}}

	updated, cmd := m.executeAction(action)
	next := updated.(appModel)
	if cmd == nil || next.actionResult == nil || !strings.Contains(next.actionResult.Stdout, "1 broken symlink") {
		t.Fatalf("expected cleanup success with rescan, result=%#v cmd=%v", next.actionResult, cmd)
	}
	updated, _ = next.Update(cmd())
	next = updated.(appModel)
	if next.actionResult == nil || !strings.Contains(next.actionResult.Stdout, "1 broken symlink") {
		t.Fatalf("expected successful cleanup result to survive rescan, got %#v", next.actionResult)
	}
	if _, err := os.Lstat(brokenLink); !os.IsNotExist(err) {
		t.Fatalf("expected still-broken symlink to be deleted, lstat err=%v", err)
	}
	if data, err := os.ReadFile(restoredFile); err != nil || string(data) != "do not delete" {
		t.Fatalf("expected restored regular file to remain, data=%q err=%v", data, err)
	}
	if _, err := os.Lstat(restoredLink); err != nil {
		t.Fatalf("expected restored working symlink to remain, lstat err=%v", err)
	}
}

func TestDeleteBrokenSymlinkNoopReportsAndRescans(t *testing.T) {
	cwd := t.TempDir()
	missingLink := filepath.Join(cwd, ".agents", "skills", "already-gone")
	if err := os.MkdirAll(filepath.Dir(missingLink), 0o755); err != nil {
		t.Fatal(err)
	}
	m := appModel{cwd: cwd, width: 120, height: 32, selected: 1, result: model.ScanResult{Skills: []*model.Skill{{
		Name:          "Broken",
		Scope:         model.ScopeProject,
		ObservedPaths: []model.ObservedPath{{Path: missingLink, Status: model.StatusBrokenSymlink}},
	}}}}
	action := actions.CommandPreview{ID: "delete_broken_symlink", ConfirmValue: "Broken", Exec: actions.ExecSpec{Internal: "delete_broken_symlink", Args: []string{string(model.ScopeProject), "Broken"}}}

	updated, cmd := m.executeAction(action)
	next := updated.(appModel)
	if cmd == nil {
		t.Fatal("expected no-op delete to trigger a rescan")
	}
	if next.actionResult == nil || !strings.Contains(next.actionResult.Stdout, "0 broken symlink") {
		t.Fatalf("expected no-op informational action result, got %#v", next.actionResult)
	}
	updated, _ = next.Update(cmd())
	next = updated.(appModel)
	if next.actionResult == nil || !strings.Contains(next.actionResult.Stdout, "0 broken symlink") {
		t.Fatalf("expected no-op informational result to survive rescan, got %#v", next.actionResult)
	}
}

func TestDirectUpdateHotkeyStartsCurrentOrBulkAction(t *testing.T) {
	m := actionTestModel(t.TempDir())
	updated, cmd := m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'u'}})
	next := updated.(appModel)
	if cmd != nil || next.commands || !next.confirming || next.currentActions()[next.action].ID != "reinstall_update" {
		t.Fatalf("expected u to open current-skill update confirmation without actions, action=%#v confirming=%v commands=%v cmd=%v", next.currentActions()[next.action], next.confirming, next.commands, cmd)
	}
	m = bulkActionTestModel(t.TempDir())
	m.selectedKeys = map[string]bool{skillKey(m.result.Skills[0]): true, skillKey(m.result.Skills[1]): true}
	updated, cmd = m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'u'}})
	next = updated.(appModel)
	if cmd != nil || next.commands || !next.confirming || next.currentActions()[next.action].ID != "bulk_reinstall_update" {
		t.Fatalf("expected u to open bulk update confirmation without actions, action=%#v confirming=%v commands=%v cmd=%v", next.currentActions()[next.action], next.confirming, next.commands, cmd)
	}
}

func TestDirectRemoveHotkeyStartsStrictConfirmation(t *testing.T) {
	m := actionTestModel(t.TempDir())
	updated, cmd := m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'x'}})
	next := updated.(appModel)
	if cmd != nil || next.commands || !next.confirming || next.currentActions()[next.action].ID != "remove" || next.currentActions()[next.action].ConfirmValue != "deploy-skill" {
		t.Fatalf("expected x to open single remove confirmation without actions, action=%#v confirming=%v commands=%v cmd=%v", next.currentActions()[next.action], next.confirming, next.commands, cmd)
	}
}

func TestDirectOpenHotkeyUsesCurrentSkillEvenWithBulkSelection(t *testing.T) {
	t.Setenv("EDITOR", "vim")
	m := bulkActionTestModel(t.TempDir())
	m.result.Skills[0].SkillPath = "/tmp/one/SKILL.md"
	m.selectedKeys = map[string]bool{skillKey(m.result.Skills[1]): true}
	updated, cmd := m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'o'}})
	next := updated.(appModel)
	if cmd == nil || !next.running || next.commands {
		t.Fatalf("expected o to start interactive open without opening actions, running=%v commands=%v cmd=%v", next.running, next.commands, cmd)
	}
}

func TestDirectOpenHotkeyClosesExistingActionMode(t *testing.T) {
	t.Setenv("EDITOR", "vim")
	m := actionTestModel(t.TempDir())
	m.result.Skills[0].SkillPath = "/tmp/deploy-skill/SKILL.md"
	m.commands = true
	updated, cmd := m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'o'}})
	next := updated.(appModel)
	if cmd == nil || !next.running || next.commands {
		t.Fatalf("expected o to close action mode before opening editor, running=%v commands=%v cmd=%v", next.running, next.commands, cmd)
	}
}

func TestSourceMarkSelectsCurrentSource(t *testing.T) {
	m := appModel{width: 120, height: 32, selected: 1, result: model.ScanResult{Skills: []*model.Skill{
		{Name: "One", Scope: model.ScopeProject, CanonicalPath: "/tmp/one", LocalLock: &model.LocalLockEntry{Source: "owner/repo", SkillPath: "skills/web/SKILL.md"}},
		{Name: "Two", Scope: model.ScopeProject, CanonicalPath: "/tmp/two", LocalLock: &model.LocalLockEntry{Source: "owner/repo", SkillPath: "skills/web/SKILL.md"}},
		{Name: "Other", Scope: model.ScopeProject, CanonicalPath: "/tmp/other", LocalLock: &model.LocalLockEntry{Source: "owner/repo", SkillPath: "skills/data/SKILL.md"}},
		{Name: "Global", Scope: model.ScopeGlobal, CanonicalPath: "/tmp/global", GlobalLock: &model.GlobalLockEntry{Source: "owner/repo"}},
		{Name: "Different", Scope: model.ScopeProject, CanonicalPath: "/tmp/different", LocalLock: &model.LocalLockEntry{Source: "other/repo", SkillPath: "skills/web/SKILL.md"}},
	}}}
	updated, _ := m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'s'}})
	next := updated.(appModel)
	if next.selectedCount() != 4 || !next.isSelected(next.result.Skills[0]) || !next.isSelected(next.result.Skills[1]) || !next.isSelected(next.result.Skills[2]) || !next.isSelected(next.result.Skills[3]) || next.isSelected(next.result.Skills[4]) {
		t.Fatalf("expected source mark to select all skills from owner/repo only, selected=%#v", next.selectedKeys)
	}
	updated, _ = next.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'s'}})
	next = updated.(appModel)
	if next.selectedCount() != 0 {
		t.Fatalf("expected second source mark to unselect marked source skills, selected=%#v", next.selectedKeys)
	}
}

func TestSourceMarkOnlySelectsFilteredSkills(t *testing.T) {
	m := appModel{width: 120, height: 32, search: "one", result: model.ScanResult{Skills: []*model.Skill{
		{Name: "One", Scope: model.ScopeProject, CanonicalPath: "/tmp/one", LocalLock: &model.LocalLockEntry{Source: "owner/repo"}},
		{Name: "Two", Scope: model.ScopeProject, CanonicalPath: "/tmp/two", LocalLock: &model.LocalLockEntry{Source: "owner/repo"}},
	}}}
	updated, _ := m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'s'}})
	next := updated.(appModel)
	if next.selectedCount() != 1 || !next.isSelected(next.result.Skills[0]) || next.isSelected(next.result.Skills[1]) {
		t.Fatalf("expected source mark to select only visible filtered skills, selected=%#v", next.selectedKeys)
	}
}

func TestSourceAndFolderDetailsRender(t *testing.T) {
	m := appModel{width: 120, height: 32, selected: 1, result: model.ScanResult{Skills: []*model.Skill{{
		Name: "One", Scope: model.ScopeProject, LocalLock: &model.LocalLockEntry{Source: "owner/repo", SkillPath: "skills/web/SKILL.md", Ref: "main"},
	}}}}
	out := m.View()
	if !strings.Contains(out, "Source:") || !strings.Contains(out, "owner/repo") ||
		!strings.Contains(out, "Folder:") || !strings.Contains(out, "skills/web") ||
		!strings.Contains(out, "Ref:") || !strings.Contains(out, "main") {
		t.Fatalf("expected source/folder/ref details, got %q", out)
	}
}

func TestSkillListShowsSourceGroups(t *testing.T) {
	m := appModel{width: 120, height: 32, selected: 1, result: model.ScanResult{Skills: []*model.Skill{
		{Name: "One", Scope: model.ScopeProject, LocalLock: &model.LocalLockEntry{Source: "owner/repo", SkillPath: "skills/web/SKILL.md"}},
		{Name: "Two", Scope: model.ScopeProject, LocalLock: &model.LocalLockEntry{Source: "owner/repo", SkillPath: "skills/web/SKILL.md"}},
		{Name: "Other", Scope: model.ScopeProject, LocalLock: &model.LocalLockEntry{Source: "owner/repo", SkillPath: "skills/data/SKILL.md"}},
	}}}
	out := m.View()
	if !strings.Contains(out, "owner/repo") {
		t.Fatalf("expected source group header, got %q", out)
	}
	if strings.Count(out, "owner/repo") != 2 || strings.Contains(out, "owner/repo / skills/web") || strings.Contains(out, "owner/repo / skills/data") {
		t.Fatalf("expected repo-level group header (one in list, one in Source detail), got %q", out)
	}
}

func TestSkillListSeparatesNoSourceMetadata(t *testing.T) {
	m := appModel{width: 120, height: 32, selected: 1, result: model.ScanResult{Skills: []*model.Skill{
		{Name: "Tracked", Scope: model.ScopeProject, LocalLock: &model.LocalLockEntry{Source: "owner/repo"}},
		{Name: "Manual", Scope: model.ScopeProject},
	}}}
	out := m.View()
	if !strings.Contains(out, "owner/repo") || !strings.Contains(out, "Custom / untracked") {
		t.Fatalf("expected explicit source and no-source groups, got %q", out)
	}
}

func TestBracketJumpSourceGroupsWithoutChangingScope(t *testing.T) {
	m := appModel{width: 120, height: 32, result: model.ScanResult{Skills: []*model.Skill{
		{Name: "One", Scope: model.ScopeProject, LocalLock: &model.LocalLockEntry{Source: "owner/one"}},
		{Name: "Two", Scope: model.ScopeProject, LocalLock: &model.LocalLockEntry{Source: "owner/one"}},
		{Name: "Three", Scope: model.ScopeProject, LocalLock: &model.LocalLockEntry{Source: "owner/two"}},
		{Name: "Manual", Scope: model.ScopeProject},
	}}}

	updated, _ := m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{']'}})
	next := updated.(appModel)
	if next.filter != scopeAll || next.selected != 3 {
		t.Fatalf("expected ] to jump to next source group without changing scope, filter=%d selected=%d", next.filter, next.selected)
	}

	updated, _ = next.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'['}})
	next = updated.(appModel)
	if next.filter != scopeAll || next.selected != 0 {
		t.Fatalf("expected [ to jump to previous source group without changing scope, filter=%d selected=%d", next.filter, next.selected)
	}
}

func TestPaneFocusAndScroll(t *testing.T) {
	m := appModel{width: 120, height: 32, result: model.ScanResult{Skills: []*model.Skill{
		{Name: "One", Scope: model.ScopeProject},
		{Name: "Two", Scope: model.ScopeProject},
	}}}

	if m.detailsFocused {
		t.Fatal("expected detailsFocused to start false")
	}

	// Press 2 to focus metadata/details area.
	updated, _ := m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'2'}})
	next := updated.(appModel)
	if !next.detailsFocused {
		t.Fatal("expected detailsFocused to be true after pressing 2")
	}

	// Press 1 to return to skills list.
	updated, _ = next.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'1'}})
	next = updated.(appModel)
	if next.detailsFocused {
		t.Fatal("expected detailsFocused to be false after pressing 1")
	}
}

func TestScopeFilterKeys(t *testing.T) {
	m := appModel{width: 120, height: 32, result: model.ScanResult{Skills: []*model.Skill{
		{Name: "Project", Scope: model.ScopeProject},
		{Name: "Global", Scope: model.ScopeGlobal},
	}}}

	// Test 'f' cycles scope All -> Project -> Global -> All
	updated, _ := m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'f'}})
	next := updated.(appModel)
	if next.filter != scopeProject {
		t.Fatalf("expected f to switch to project scope, got %d", next.filter)
	}

	updated, _ = next.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'f'}})
	next = updated.(appModel)
	if next.filter != scopeGlobal {
		t.Fatalf("expected f to switch to global scope, got %d", next.filter)
	}

	// Test 'F' resets scope to All
	updated, _ = next.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'F'}})
	next = updated.(appModel)
	if next.filter != scopeAll {
		t.Fatalf("expected F to reset scope to All, got %d", next.filter)
	}
}

func TestMain(m *testing.M) {
	// Redirect the discovery clone cache to a throwaway dir so tests never
	// touch the real user cache directory.
	dir, err := os.MkdirTemp("", "lazyskills-test-clones-*")
	if err != nil {
		panic(err)
	}
	discoveryCacheRoot = func() (string, error) { return dir, nil }
	code := m.Run()
	os.RemoveAll(dir)
	os.Exit(code)
}

func TestCachedSourceCloneReuse(t *testing.T) {
	oldRoot := discoveryCacheRoot
	oldClone := gitClone
	defer func() { discoveryCacheRoot = oldRoot; gitClone = oldClone }()

	tmp := t.TempDir()
	discoveryCacheRoot = func() (string, error) { return tmp, nil }

	clones := 0
	gitClone = func(url, ref, dir string) error {
		clones++
		return os.MkdirAll(filepath.Join(dir, ".git"), 0o755) // simulate a real clone
	}

	if _, _, err := cachedSourceClone("https://github.com/owner/repo", "main", false); err != nil {
		t.Fatalf("first clone failed: %v", err)
	}
	if clones != 1 {
		t.Fatalf("expected 1 clone on cache miss, got %d", clones)
	}
	// Cache hit: a cached repo is reused without cloning.
	if _, _, err := cachedSourceClone("https://github.com/owner/repo", "main", false); err != nil {
		t.Fatalf("reuse failed: %v", err)
	}
	if clones != 1 {
		t.Fatalf("expected cache reuse (no extra clone), got %d clones", clones)
	}
	// Force re-clones.
	if _, _, err := cachedSourceClone("https://github.com/owner/repo", "main", true); err != nil {
		t.Fatalf("forced clone failed: %v", err)
	}
	if clones != 2 {
		t.Fatalf("expected force to re-clone, got %d clones", clones)
	}
}

func TestExecutePruneLockRemovesEntry(t *testing.T) {
	dir := t.TempDir()
	lockPath := filepath.Join(dir, "skills-lock.json")
	if err := os.WriteFile(lockPath, []byte(`{"version":1,"skills":{"ghost":{"source":"o/r"},"keep":{"source":"o/r"}}}`), 0o644); err != nil {
		t.Fatal(err)
	}

	m := appModel{cwd: dir, width: 120, height: 32}
	action := actions.CommandPreview{
		ID:           "prune_lock",
		ConfirmValue: "ghost",
		Exec:         actions.ExecSpec{Internal: "prune_project_lock"},
	}
	_, cmd := m.executeAction(action)
	if cmd == nil {
		t.Fatal("expected a rescan command after pruning")
	}

	data, err := os.ReadFile(lockPath)
	if err != nil {
		t.Fatal(err)
	}
	if strings.Contains(string(data), "ghost") {
		t.Fatalf("expected 'ghost' entry pruned, got %s", data)
	}
	if !strings.Contains(string(data), "keep") {
		t.Fatalf("expected 'keep' entry preserved, got %s", data)
	}
}

func TestExecuteRemovePrunesProjectLockEntry(t *testing.T) {
	dir := t.TempDir()
	lockPath := filepath.Join(dir, "skills-lock.json")
	if err := os.WriteFile(lockPath, []byte(`{"version":1,"skills":{"ghost":{"source":"o/r"},"keep":{"source":"o/r"}}}`), 0o644); err != nil {
		t.Fatal(err)
	}
	ghostPath := filepath.Join(dir, ".agents", "skills", "ghost")
	if err := os.MkdirAll(ghostPath, 0o755); err != nil {
		t.Fatal(err)
	}

	originalRunExec := runExec
	runExec = func(spec runner.ExecSpec) runner.Result {
		return runner.Result{Program: spec.Program, Args: spec.Args, Cwd: spec.Cwd, ExitCode: 0}
	}
	t.Cleanup(func() { runExec = originalRunExec })

	m := appModel{
		cwd: dir, width: 120, height: 32,
		result: model.ScanResult{Skills: []*model.Skill{{
			Name:  "ghost",
			Scope: model.ScopeProject,
			ObservedPaths: []model.ObservedPath{{
				Path: ghostPath, Scope: model.ScopeProject, Agent: "opencode", Status: model.StatusCopy,
			}},
		}}},
	}
	action := actions.CommandPreview{
		ID:           "remove",
		ConfirmValue: "ghost",
		Mutates:      true,
		Exec:         actions.ExecSpec{Program: "skills", Args: []string{"remove", "ghost", "--yes"}},
	}
	_, cmd := m.executeAction(action)
	if cmd == nil {
		t.Fatal("expected command")
	}
	msg, ok := cmd().(actionResultMsg)
	if !ok {
		t.Fatalf("expected actionResultMsg")
	}
	if msg.result.ExitCode != 0 || msg.result.Err != "" {
		t.Fatalf("expected successful remove result, got %#v", msg.result)
	}

	data, err := os.ReadFile(lockPath)
	if err != nil {
		t.Fatal(err)
	}
	if strings.Contains(string(data), "ghost") {
		t.Fatalf("expected 'ghost' entry pruned after remove, got %s", data)
	}
	if !strings.Contains(string(data), "keep") {
		t.Fatalf("expected 'keep' entry preserved, got %s", data)
	}
}

func TestHumanizeSince(t *testing.T) {
	now := time.Now()
	cases := []struct {
		at   time.Time
		want string
	}{
		{now.Add(-2 * time.Second), "just now"},
		{now.Add(-30 * time.Second), "30s ago"},
		{now.Add(-5 * time.Minute), "5m ago"},
		{now.Add(-3 * time.Hour), "3h ago"},
		{now.Add(-50 * time.Hour), "2d ago"},
	}
	for _, c := range cases {
		if got := humanizeSince(c.at); got != c.want {
			t.Errorf("humanizeSince(%v) = %q, want %q", c.at, got, c.want)
		}
	}
}

func TestModalShowsScanFreshness(t *testing.T) {
	m := appModel{width: 120, height: 32, modalSource: "owner/repo", detailModal: true, result: model.ScanResult{Skills: []*model.Skill{
		{Name: "Existing", Scope: model.ScopeProject, LocalLock: &model.LocalLockEntry{Source: "owner/repo"}},
	}}}
	m.discovery = map[string]SourceDiscovery{
		"owner/repo": {Status: DiscoveryReady, ScannedAt: time.Now().Add(-5 * time.Minute)},
	}
	out := strings.Join(m.sourceModalDetailLines(80), "\n")
	if !strings.Contains(out, "scanned 5m ago") {
		t.Fatalf("expected scan freshness in modal, got %q", out)
	}
}

func TestTopBottomJumpKeys(t *testing.T) {
	m := appModel{width: 120, height: 32, result: model.ScanResult{Skills: []*model.Skill{
		{Name: "One", Scope: model.ScopeProject},
		{Name: "Two", Scope: model.ScopeProject},
		{Name: "Three", Scope: model.ScopeProject},
	}}}
	last := len(m.visibleRows()) - 1

	// 'G' jumps to the bottom.
	updated, _ := m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'G'}})
	next := updated.(appModel)
	if next.selected != last {
		t.Fatalf("expected G to select last row %d, got %d", last, next.selected)
	}

	// A lone 'g' arms but does not move selection.
	updated, _ = next.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'g'}})
	armed := updated.(appModel)
	if armed.selected != last || !armed.pendingG {
		t.Fatalf("expected lone g to arm without moving, got selected=%d pendingG=%v", armed.selected, armed.pendingG)
	}

	// The second 'g' jumps to the top and disarms.
	updated, _ = armed.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'g'}})
	top := updated.(appModel)
	if top.selected != 0 || top.pendingG {
		t.Fatalf("expected gg to select first row and disarm, got selected=%d pendingG=%v", top.selected, top.pendingG)
	}

	// A non-g key disarms a pending g.
	updated, _ = next.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'g'}})
	updated, _ = updated.(appModel).Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'k'}})
	if updated.(appModel).pendingG {
		t.Fatal("expected a non-g key to disarm pendingG")
	}
}

func TestFocusControls(t *testing.T) {
	m := appModel{width: 120, height: 32}

	if m.detailsFocused {
		t.Fatal("expected detailsFocused to start false")
	}

	// '2' focuses details
	updated, _ := m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'2'}})
	m = updated.(appModel)
	if !m.detailsFocused {
		t.Fatal("expected 2 to focus details")
	}

	// '1' focuses list
	updated, _ = m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'1'}})
	m = updated.(appModel)
	if m.detailsFocused {
		t.Fatal("expected 1 to focus list")
	}

	// 'tab' cycles focus
	updated, _ = m.Update(tea.KeyMsg{Type: tea.KeyTab})
	m = updated.(appModel)
	if !m.detailsFocused {
		t.Fatal("expected tab to cycle focus")
	}

	// 'shift+tab' cycles focus back
	updated, _ = m.Update(tea.KeyMsg{Type: tea.KeyShiftTab})
	m = updated.(appModel)
	if m.detailsFocused {
		t.Fatal("expected shift+tab to cycle focus")
	}
}

func TestEnterSkillModal(t *testing.T) {
	m := appModel{width: 120, height: 32, selected: 1, result: model.ScanResult{Skills: []*model.Skill{
		{Name: "One", Scope: model.ScopeProject},
	}}}

	// Press Enter to open modal
	updated, _ := m.Update(tea.KeyMsg{Type: tea.KeyEnter})
	m = updated.(appModel)
	if !m.detailModal {
		t.Fatal("expected enter to open skill detail modal")
	}

	// Render modal view
	out := m.View()
	if !strings.Contains(out, "Skill Detail View") {
		t.Fatalf("expected modal title in View, got %s", out)
	}

	// Scroll down in modal
	updated, _ = m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'j'}})
	m = updated.(appModel)

	// Press Esc to close modal
	updated, _ = m.Update(tea.KeyMsg{Type: tea.KeyEsc})
	m = updated.(appModel)
	if m.detailModal {
		t.Fatal("expected esc to close modal")
	}
}

func TestGroupedListKeepsSelectedRowVisible(t *testing.T) {
	skills := []*model.Skill{}
	for i := 0; i < 12; i++ {
		skills = append(skills, &model.Skill{Name: fmt.Sprintf("Skill %02d", i), Scope: model.ScopeProject, LocalLock: &model.LocalLockEntry{Source: fmt.Sprintf("owner/repo-%02d", i)}})
	}
	m := appModel{width: 120, height: 10, selected: 23, result: model.ScanResult{Skills: skills}}
	out := m.View()
	if !strings.Contains(out, "Skill 11 [P]") {
		t.Fatalf("expected selected row visible with many group headers, got %q", out)
	}
}

func TestFailedMutationDoesNotTriggerRescan(t *testing.T) {
	cwd := t.TempDir()
	old := runExec
	runExec = func(spec runner.ExecSpec) runner.Result {
		return runner.Result{Program: spec.Program, Args: spec.Args, Cwd: spec.Cwd, ExitCode: 2, Stderr: "nope"}
	}
	t.Cleanup(func() { runExec = old })

	m := actionTestModel(cwd)
	m.commands = true
	m.action = actionIndex(t, m, "Reinstall/update selected skill")
	updated, _ := m.Update(tea.KeyMsg{Type: tea.KeyEnter})
	m = updated.(appModel)
	for _, r := range []rune("yes") {
		updated, _ = m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{r}})
		m = updated.(appModel)
	}
	updated, cmd := m.Update(tea.KeyMsg{Type: tea.KeyEnter})
	m = updated.(appModel)
	if cmd == nil {
		t.Fatalf("expected command")
	}
	updated, rescan := m.Update(cmd())
	m = updated.(appModel)
	if rescan != nil || m.actionResult == nil || m.actionResult.ExitCode != 2 {
		t.Fatalf("expected failed result without rescan, result=%#v rescan=%v", m.actionResult, rescan)
	}
}

func TestSingleRemoveRequiresYesConfirmation(t *testing.T) {
	m := actionTestModel(t.TempDir())
	m.result.Skills[0].CanonicalPath = "/tmp/deploy-skill"
	m.commands = true
	m.action = actionIndex(t, m, "Remove selected skill")
	updated, _ := m.Update(tea.KeyMsg{Type: tea.KeyEnter})
	m = updated.(appModel)
	for _, r := range []rune("wrong") {
		updated, _ = m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{r}})
		m = updated.(appModel)
	}
	updated, cmd := m.Update(tea.KeyMsg{Type: tea.KeyEnter})
	m = updated.(appModel)
	if cmd != nil || !m.confirming || !strings.Contains(m.confirmError, "Type y or yes") || m.actionResult != nil {
		t.Fatalf("expected inline confirmation error without command, confirming=%v err=%q result=%#v cmd=%v", m.confirming, m.confirmError, m.actionResult, cmd)
	}
}

func TestSingleRemoveRejectsSkillNameConfirmation(t *testing.T) {
	m := actionTestModel(t.TempDir())
	m.commands = true
	m.action = actionIndex(t, m, "Remove selected skill")
	updated, _ := m.Update(tea.KeyMsg{Type: tea.KeyEnter})
	m = updated.(appModel)
	for _, r := range []rune("deploy-skill") {
		updated, _ = m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{r}})
		m = updated.(appModel)
	}
	updated, cmd := m.Update(tea.KeyMsg{Type: tea.KeyEnter})
	m = updated.(appModel)
	if cmd != nil || !m.confirming || !strings.Contains(m.confirmError, "Type y or yes") {
		t.Fatalf("expected skill name not to confirm deletion, confirming=%v err=%q cmd=%v", m.confirming, m.confirmError, cmd)
	}
}

func TestSingleRemoveAcceptsYOrYesConfirmation(t *testing.T) {
	for _, input := range []string{"y", "yes"} {
		t.Run(input, func(t *testing.T) {
			old := runExec
			runExec = func(spec runner.ExecSpec) runner.Result {
				return runner.Result{Program: spec.Program, Args: spec.Args, Cwd: spec.Cwd, ExitCode: 0}
			}
			t.Cleanup(func() { runExec = old })

			m := actionTestModel(t.TempDir())
			m.commands = true
			m.action = actionIndex(t, m, "Remove selected skill")
			updated, _ := m.Update(tea.KeyMsg{Type: tea.KeyEnter})
			m = updated.(appModel)
			for _, r := range []rune(input) {
				updated, _ = m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{r}})
				m = updated.(appModel)
			}
			updated, cmd := m.Update(tea.KeyMsg{Type: tea.KeyEnter})
			m = updated.(appModel)
			if cmd == nil || !m.running {
				t.Fatalf("expected %q to run single remove, running=%v cmd=%v", input, m.running, cmd)
			}
		})
	}
}

func TestBulkRemoveAcceptsYesConfirmation(t *testing.T) {
	old := runExec
	runExec = func(spec runner.ExecSpec) runner.Result {
		return runner.Result{Program: spec.Program, Args: spec.Args, Cwd: spec.Cwd, ExitCode: 0}
	}
	t.Cleanup(func() { runExec = old })

	m := bulkActionTestModel(t.TempDir())
	m.selectedKeys = map[string]bool{skillKey(m.result.Skills[0]): true, skillKey(m.result.Skills[1]): true}
	m.commands = true
	m.action = actionIndex(t, m, "Remove 2 selected skills")
	updated, _ := m.Update(tea.KeyMsg{Type: tea.KeyEnter})
	m = updated.(appModel)
	for _, r := range []rune("yes") {
		updated, _ = m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{r}})
		m = updated.(appModel)
	}
	updated, cmd := m.Update(tea.KeyMsg{Type: tea.KeyEnter})
	m = updated.(appModel)
	if cmd == nil || !m.running || m.commands {
		t.Fatalf("expected bulk remove to accept yes and close actions, running=%v commands=%v cmd=%v", m.running, m.commands, cmd)
	}
}

func TestConfirmationAllowsSkillNamesStartingWithN(t *testing.T) {
	m := actionTestModel(t.TempDir())
	m.result.Skills[0].CanonicalPath = "/tmp/next-auth"
	updated, cmd := m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'x'}})
	m = updated.(appModel)
	if cmd != nil || !m.confirming || m.currentActions()[m.action].ConfirmValue != "next-auth" {
		t.Fatalf("expected remove confirmation for next-auth, confirming=%v action=%#v cmd=%v", m.confirming, m.currentActions()[m.action], cmd)
	}
	updated, cmd = m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'n'}})
	m = updated.(appModel)
	if cmd != nil || !m.confirming || m.confirmInput != "n" {
		t.Fatalf("expected first n to be typed, not cancel confirmation; confirming=%v input=%q cmd=%v", m.confirming, m.confirmInput, cmd)
	}
}

func TestDirectSafeConfirmationAcceptsEmptyEnter(t *testing.T) {
	old := runExec
	runExec = func(spec runner.ExecSpec) runner.Result {
		return runner.Result{Program: spec.Program, Args: spec.Args, Cwd: spec.Cwd, ExitCode: 0}
	}
	t.Cleanup(func() { runExec = old })

	m := actionTestModel(t.TempDir())
	m.commands = true
	m.action = actionIndex(t, m, "Reinstall/update selected skill")
	updated, _ := m.Update(tea.KeyMsg{Type: tea.KeyEnter})
	m = updated.(appModel)
	updated, cmd := m.Update(tea.KeyMsg{Type: tea.KeyEnter})
	m = updated.(appModel)
	if cmd == nil || !m.running {
		t.Fatalf("expected empty Enter to run safe action, running=%v cmd=%v", m.running, cmd)
	}
}

func TestConfirmationOverlayCopyMatchesRisk(t *testing.T) {
	m := actionTestModel(t.TempDir())
	m.commands = true
	m.action = actionIndex(t, m, "Reinstall/update selected skill")
	m.confirming = true
	safeOut := m.confirmationOverlay(appLayout{Width: 120, Height: 32})
	if !strings.Contains(safeOut, "Press Enter to continue, or Esc to cancel.") || strings.Contains(safeOut, "Type ") {
		t.Fatalf("expected safe confirmation copy, got %q", safeOut)
	}

	m.action = actionIndex(t, m, "Remove selected skill")
	dangerOut := m.confirmationOverlay(appLayout{Width: 120, Height: 32})
	if !strings.Contains(dangerOut, "Type y or yes to confirm") || strings.Contains(dangerOut, `Type "deploy-skill"`) {
		t.Fatalf("expected single-remove y/yes copy, got %q", dangerOut)
	}

	m = bulkActionTestModel(t.TempDir())
	m.selectedKeys = map[string]bool{skillKey(m.result.Skills[0]): true, skillKey(m.result.Skills[1]): true}
	m.commands = true
	m.action = actionIndex(t, m, "Remove 2 selected skills")
	m.confirming = true
	bulkOut := m.confirmationOverlay(appLayout{Width: 120, Height: 32})
	if !strings.Contains(bulkOut, `Remove 2 selected skills`) || !strings.Contains(bulkOut, `Type y or yes to confirm`) {
		t.Fatalf("expected bulk-remove y/yes copy, got %q", bulkOut)
	}
}

func actionTestModel(cwd string) appModel {
	skillPath := filepath.Join(cwd, ".agents", "skills", "deploy-skill")
	return appModel{cwd: cwd, width: 120, height: 32, selected: 1, result: model.ScanResult{Skills: []*model.Skill{{
		Name:          "Deploy Skill",
		Description:   "desc",
		Scope:         model.ScopeProject,
		CanonicalPath: skillPath,
		LocalLock:     &model.LocalLockEntry{Source: "owner/repo"},
		ObservedPaths: []model.ObservedPath{{
			Path:   skillPath,
			Scope:  model.ScopeProject,
			Agent:  "opencode",
			Status: model.StatusCopy,
		}},
	}}}}
}

func bulkActionTestModel(cwd string) appModel {
	one := filepath.Join(cwd, ".agents", "skills", "one")
	two := filepath.Join(cwd, ".agents", "skills", "two")
	return appModel{cwd: cwd, width: 120, height: 32, selected: 1, result: model.ScanResult{Skills: []*model.Skill{
		{
			Name: "One", Description: "desc", Scope: model.ScopeProject, CanonicalPath: one,
			LocalLock:     &model.LocalLockEntry{Source: "owner/repo"},
			ObservedPaths: []model.ObservedPath{{Path: one, Scope: model.ScopeProject, Agent: "opencode", Status: model.StatusCopy}},
		},
		{
			Name: "Two", Description: "desc", Scope: model.ScopeProject, CanonicalPath: two,
			LocalLock:     &model.LocalLockEntry{Source: "owner/repo"},
			ObservedPaths: []model.ObservedPath{{Path: two, Scope: model.ScopeProject, Agent: "opencode", Status: model.StatusCopy}},
		},
	}}}
}

func actionIndex(t *testing.T, m appModel, title string) int {
	t.Helper()
	for i, action := range m.currentActions() {
		if action.Title == title {
			return i
		}
	}
	t.Fatalf("action %q not found in %#v", title, m.currentActions())
	return 0
}

func actionByID(t *testing.T, previews []actions.CommandPreview, id string) actions.CommandPreview {
	t.Helper()
	for _, action := range previews {
		if action.ID == id {
			return action
		}
	}
	t.Fatalf("action %q not found in %#v", id, previews)
	return actions.CommandPreview{}
}

func argsContain(args []string, want string) bool {
	for _, arg := range args {
		if arg == want {
			return true
		}
	}
	return false
}

func TestActionSelectionDoesNotResetDetailScroll(t *testing.T) {
	m := actionTestModel(t.TempDir())
	for i := 0; i < 40; i++ {
		m.result.Skills[0].HealthIssues = append(m.result.Skills[0].HealthIssues, model.HealthIssue{Type: "warning", Message: fmt.Sprintf("issue %d", i)})
	}
	m.height = 12
	m.commands = true
	m.syncViewport()
	m.viewport.SetYOffset(5)
	updated, _ := m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'j'}})
	next := updated.(appModel)
	if next.viewport.YOffset == 0 {
		t.Fatalf("expected action selection not to reset detail scroll to top")
	}
}

func TestActionResultClearsOnSkillSelectionChange(t *testing.T) {
	m := appModel{width: 120, height: 32, actionResult: &runner.Result{ExitCode: 0}, result: model.ScanResult{Skills: []*model.Skill{
		{Name: "One", Scope: model.ScopeProject},
		{Name: "Two", Scope: model.ScopeProject},
	}}}
	updated, _ := m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'j'}})
	next := updated.(appModel)
	if next.actionResult != nil {
		t.Fatalf("expected action result cleared on skill change")
	}
}

func TestSearchEscapeClearsQueryAndBackspaceWorksOutsideSearch(t *testing.T) {
	m := appModel{searching: true, search: "build", result: model.ScanResult{Skills: []*model.Skill{{Name: "Build", Scope: model.ScopeProject}}}}
	updated, _ := m.Update(tea.KeyMsg{Type: tea.KeyEsc})
	next := updated.(appModel)
	if next.searching || next.search != "" {
		t.Fatalf("expected esc to clear search, searching=%v search=%q", next.searching, next.search)
	}
	m = appModel{searching: false, search: "abc", result: model.ScanResult{Skills: []*model.Skill{{Name: "Build", Scope: model.ScopeProject}}}}
	updated, _ = m.Update(tea.KeyMsg{Type: tea.KeyBackspace})
	next = updated.(appModel)
	if next.search != "ab" {
		t.Fatalf("expected backspace outside search to trim query, got %q", next.search)
	}
}

func TestDetailPaneClipsLongPreview(t *testing.T) {
	preview := strings.Repeat("- line\n", 80)
	m := appModel{width: 100, height: 20, result: model.ScanResult{Skills: []*model.Skill{{Name: "Long", Description: "desc", Scope: model.ScopeProject, Preview: preview}}}}
	m.syncViewport()
	out := m.previewViewport.View()
	lines := strings.Split(out, "\n")
	if len(lines) > m.previewViewport.Height {
		t.Fatalf("preview pane overflowed: got %d lines\n%s", len(lines), out)
	}
}

func TestMarkdownPreviewStripsFrontmatter(t *testing.T) {
	preview := "---\nname: better-icons\ndescription: icons\n---\n# Better Icons\n\nSearch icons."
	lines := renderMarkdownPreview(preview, 80)
	out := strings.Join(lines, "\n")
	if strings.Contains(out, "name: better-icons") || strings.Contains(out, "description: icons") || strings.Contains(out, "--------") {
		t.Fatalf("expected frontmatter to be hidden from markdown preview, got %q", out)
	}
	if !strings.Contains(out, "Better") || !strings.Contains(out, "Icons") || !strings.Contains(out, "Search") || !strings.Contains(out, "icons") {
		t.Fatalf("expected rendered markdown body, got %q", out)
	}
}

func TestMetadataShowsSkillDescription(t *testing.T) {
	m := actionTestModel(t.TempDir())
	lines := m.metadataLines(80)
	joined := strings.Join(lines, "\n")
	if !strings.Contains(joined, "Description:") || !strings.Contains(joined, "desc") {
		t.Fatalf("expected skill description in metadata, got %q", joined)
	}
}

func TestDetailScrollKeysMoveViewport(t *testing.T) {
	preview := strings.Repeat("- line\n", 80)
	m := appModel{width: 100, height: 20, selected: 1, result: model.ScanResult{Skills: []*model.Skill{{Name: "Long", Description: "desc", Scope: model.ScopeProject, Preview: preview}}}}
	_, rightWidth, _, _ := m.getThreePaneLayout()
	previewWidth := max(1, rightWidth-4)
	m.previewCache = map[previewCacheKey][]string{
		{markdown: preview, width: previewWidth}: renderMarkdownPreview(preview, previewWidth),
	}
	m.syncViewport()
	updated, _ := m.Update(tea.KeyMsg{Type: tea.KeyPgDown})
	next := updated.(appModel)
	if next.previewViewport.YOffset <= 0 {
		t.Fatalf("expected preview scroll to move down, got %d", next.previewViewport.YOffset)
	}
	updated, _ = next.Update(tea.KeyMsg{Type: tea.KeyHome})
	next = updated.(appModel)
	if next.previewViewport.YOffset != 0 {
		t.Fatalf("expected home to reset preview scroll, got %d", next.previewViewport.YOffset)
	}
}

func TestThreePaneLayoutFocusAndScroll(t *testing.T) {
	m := appModel{width: 120, height: 32, result: model.ScanResult{Skills: []*model.Skill{
		{Name: "One", Scope: model.ScopeProject, Preview: "preview content", LocalLock: &model.LocalLockEntry{Source: "owner/one"}},
		{Name: "Two", Scope: model.ScopeProject, LocalLock: &model.LocalLockEntry{Source: "owner/two"}},
	}}}
	m.syncViewport()

	// Initial focus is focusSkills (0)
	if m.focus != focusSkills {
		t.Fatalf("expected initial focus to be focusSkills, got %v", m.focus)
	}

	// In Skills focus, h/l or left/right jump source groups instead of changing focus.
	updated, _ := m.Update(tea.KeyMsg{Type: tea.KeyRight})
	m = updated.(appModel)
	if m.focus != focusSkills || m.selected != 2 {
		t.Fatalf("expected right in skills focus to jump source group, got focus %v selection %d", m.focus, m.selected)
	}
	updated, _ = m.Update(tea.KeyMsg{Type: tea.KeyLeft})
	m = updated.(appModel)
	if m.focus != focusSkills || m.selected != 0 {
		t.Fatalf("expected left in skills focus to jump source group back, got focus %v selection %d", m.focus, m.selected)
	}

	// Press 2 to focus Metadata
	updated, _ = m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'2'}})
	m = updated.(appModel)
	if m.focus != focusMetadata {
		t.Fatalf("expected 2 to focus metadata, got %v", m.focus)
	}

	// Press 3 to focus Preview
	updated, _ = m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'3'}})
	m = updated.(appModel)
	if m.focus != focusPreview {
		t.Fatalf("expected 3 to focus preview, got %v", m.focus)
	}

	// Press tab to cycle: Preview -> Skills
	updated, _ = m.Update(tea.KeyMsg{Type: tea.KeyTab})
	m = updated.(appModel)
	if m.focus != focusSkills {
		t.Fatalf("expected tab cycle to Skills, got %v", m.focus)
	}

	// Press shift+tab to cycle backward: Skills -> Preview
	updated, _ = m.Update(tea.KeyMsg{Type: tea.KeyShiftTab})
	m = updated.(appModel)
	if m.focus != focusPreview {
		t.Fatalf("expected shift+tab cycle to Preview, got %v", m.focus)
	}

	// Press left to cycle backward: Preview -> Metadata
	updated, _ = m.Update(tea.KeyMsg{Type: tea.KeyLeft})
	m = updated.(appModel)
	if m.focus != focusMetadata {
		t.Fatalf("expected left cycle to Metadata, got %v", m.focus)
	}

	// Press right to cycle forward: Metadata -> Preview
	updated, _ = m.Update(tea.KeyMsg{Type: tea.KeyRight})
	m = updated.(appModel)
	if m.focus != focusPreview {
		t.Fatalf("expected right cycle to Preview, got %v", m.focus)
	}

	// h/l should never change focus outside the Skills pane.
	updated, _ = m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'h'}})
	m = updated.(appModel)
	if m.focus != focusPreview {
		t.Fatalf("expected h to keep Preview focus, got %v", m.focus)
	}
	updated, _ = m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'l'}})
	m = updated.(appModel)
	if m.focus != focusPreview {
		t.Fatalf("expected l to keep Preview focus, got %v", m.focus)
	}

	// In Preview focus, scrolling j/k should change previewViewport offset, not skill selection
	m.selected = 0
	updated, _ = m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'j'}})
	m = updated.(appModel)
	if m.selected != 0 {
		t.Fatalf("expected preview scroll to not change skill selection, got selection %d", m.selected)
	}

	// Check View output has border titles
	out := m.View()
	if !strings.Contains(out, "1 Inventory") || !strings.Contains(out, "2 Metadata") || !strings.Contains(out, "3 Preview") {
		t.Fatalf("expected border titles in View, got:\n%s", out)
	}

	// Enter opens modal
	m.selected = 1
	updated, _ = m.Update(tea.KeyMsg{Type: tea.KeyEnter})
	m = updated.(appModel)
	if !m.detailModal {
		t.Fatal("expected enter to open modal")
	}
}

func TestFullViewFitsTerminalDimensionsWithLongPreview(t *testing.T) {
	preview := strings.Repeat("a very long line that should be clipped and not wrap the entire screen\n", 120)
	m := appModel{width: 100, height: 24, result: model.ScanResult{Skills: []*model.Skill{{Name: "Long", Description: strings.Repeat("description ", 30), Scope: model.ScopeProject, Preview: preview}}}}
	assertViewFits(t, m, 100, 24)
}

func TestSmallTerminalFallbackFitsTinyHeights(t *testing.T) {
	for _, height := range []int{4, 5, 6} {
		t.Run(fmt.Sprintf("height_%d", height), func(t *testing.T) {
			m := appModel{width: 80, height: height, result: model.ScanResult{Skills: []*model.Skill{{Name: "Long", Description: "desc", Scope: model.ScopeProject}}}}
			out := m.View()
			assertRenderedSize(t, out, 80, height)
			if strings.Contains(out, "╭") || strings.Contains(out, "╰") {
				t.Fatalf("tiny terminal should render fallback, not cards: %q", out)
			}
		})
	}
}

func TestResponsiveViewFitsCommonTerminalSizes(t *testing.T) {
	preview := strings.Repeat("a very long line that should reflow into the detail viewport without breaking borders\n", 100)
	sizes := []struct {
		width  int
		height int
	}{
		{40, 7},
		{60, 12},
		{80, 20},
		{100, 24},
		{120, 40},
	}
	for _, size := range sizes {
		t.Run(fmt.Sprintf("%dx%d", size.width, size.height), func(t *testing.T) {
			m := appModel{width: size.width, height: size.height, result: model.ScanResult{Skills: []*model.Skill{{Name: "Long", Description: strings.Repeat("description ", 12), Scope: model.ScopeProject, Preview: preview}}}}
			assertViewFits(t, m, size.width, size.height)
		})
	}
}

func TestNormalViewPreservesBottomBorders(t *testing.T) {
	m := appModel{width: 100, height: 24, result: model.ScanResult{Skills: []*model.Skill{{Name: "Build", Description: "desc", Scope: model.ScopeProject}}}}
	out := m.View()
	assertRenderedSize(t, out, 100, 24)
	if got := strings.Count(out, "╰"); got != 3 {
		t.Fatalf("expected three complete bottom-left borders, got %d\n%s", got, out)
	}
	if got := strings.Count(out, "╯"); got != 3 {
		t.Fatalf("expected three complete bottom-right borders, got %d\n%s", got, out)
	}
}

func assertViewFits(t *testing.T, m appModel, width, height int) {
	t.Helper()
	out := m.View()
	assertRenderedSize(t, out, width, height)
}

func assertRenderedSize(t *testing.T, out string, width, height int) {
	t.Helper()
	if got := lipgloss.Height(out); got > height {
		t.Fatalf("view height overflowed: got %d want <= %d\n%s", got, height, out)
	}
	for i, line := range strings.Split(out, "\n") {
		if got := lipgloss.Width(line); got > width {
			t.Fatalf("line %d width overflowed: got %d want <= %d\n%s", i, got, width, line)
		}
	}
}

func TestVisibilityReasonTranslation(t *testing.T) {
	sk := &model.Skill{
		Name:  "TestSkill",
		Scope: model.ScopeProject,
		Visibility: []model.SkillVisibility{
			{Agent: "claude-code", Display: "Claude Code", Visible: true, Reason: "visible_via_symlink"},
			{Agent: "opencode", Display: "OpenCode", Visible: false, Reason: "missing_agent_link"},
			{Agent: "cursor", Display: "Cursor", Visible: false, Reason: "not_in_universal_canonical_dir"},
		},
	}

	// List badges stay compact; detail pane carries the explanation.
	if badge := compat.StripTerminalEscapes(agentVisibilityBadge(sk, "claude-code")); badge != "✓" {
		t.Errorf("expected compact available badge, got %q", badge)
	}
	if badge := agentVisibilityBadge(sk, "opencode"); badge != "×" {
		t.Errorf("expected compact unavailable badge, got %q", badge)
	}
	if badge := agentVisibilityBadge(sk, "cursor"); badge != "×" {
		t.Errorf("expected compact unavailable badge, got %q", badge)
	}

	// Test detail summary translation
	mAll := appModel{} // agent = ""
	linesAll := mAll.visibilitySummary(display.Skill(sk), 80)
	if len(linesAll) != 1 || !strings.Contains(linesAll[0], "Available to 1/3 agents") {
		t.Errorf("expected all-agents summary, got %v", linesAll)
	}
	mDetected := appModel{result: model.ScanResult{Agents: []model.AgentState{
		{Name: "claude-code", Detected: true},
		{Name: "opencode", Detected: true},
		{Name: "cursor", Detected: false},
	}}}
	linesDetected := mDetected.visibilitySummary(display.Skill(sk), 80)
	if len(linesDetected) != 1 || !strings.Contains(linesDetected[0], "Available to 1/2 detected agents") {
		t.Errorf("expected detected-agent summary, got %v", linesDetected)
	}

	mFiltered := appModel{agent: "claude-code"}
	linesFiltered := mFiltered.visibilitySummary(display.Skill(sk), 80)
	if len(linesFiltered) != 1 || !strings.Contains(linesFiltered[0], "Claude Code: available (symlinked)") {
		t.Errorf("expected filtered agent translation, got %v", linesFiltered)
	}
}

func TestMetadataOmitsHashAndUpdateNotes(t *testing.T) {
	m := appModel{width: 120, height: 32, selected: 1, result: model.ScanResult{Skills: []*model.Skill{{
		Name: "One", Scope: model.ScopeProject, LocalLock: &model.LocalLockEntry{Source: "owner/repo", ComputedHash: "abcdef123456"},
	}}}}
	out := m.View()
	if !strings.Contains(out, "Source:") || !strings.Contains(out, "owner/repo") {
		t.Fatalf("expected source detail in metadata, got: %s", out)
	}
	if strings.Contains(out, "Hash:") || strings.Contains(out, "abcdef123456") {
		t.Fatalf("hash should no longer appear in metadata, got: %s", out)
	}
	if strings.Contains(out, "Live update status") || strings.Contains(out, "check for updates") {
		t.Fatalf("update-status notes should no longer appear in metadata, got: %s", out)
	}
}

func TestDetailsOnboardingEmptyState(t *testing.T) {
	mEmpty := appModel{
		result: model.ScanResult{
			Skills: []*model.Skill{},
		},
	}
	lines := mEmpty.detailLines(80)
	joined := strings.Join(lines, "\n")
	if !strings.Contains(joined, "Welcome to LazySkills!") || !strings.Contains(joined, "No skills were found") {
		t.Fatalf("expected onboarding instructions in details pane, got %q", joined)
	}

	mMissingDeps := appModel{
		result: model.ScanResult{
			Skills: []*model.Skill{},
			Preflight: &model.Preflight{
				CanRunSkills: false,
				Tools: map[string]model.ToolStatus{
					"skills": {Exists: false},
					"npx":    {Exists: false},
					"node":   {Exists: false},
					"npm":    {Exists: false},
				},
			},
		},
	}
	linesMissing := mMissingDeps.detailLines(80)
	joinedMissing := strings.Join(linesMissing, "\n")
	if !strings.Contains(joinedMissing, "Dependency Issue") || !strings.Contains(joinedMissing, "skills:") || !strings.Contains(joinedMissing, "missing") {
		t.Fatalf("expected dependency error details, got %q", joinedMissing)
	}
}

func TestSkillsFindTriggersRescan(t *testing.T) {
	m := appModel{}
	previews := m.currentActions()
	var findAct *actions.CommandPreview
	for _, p := range previews {
		if p.ID == "skills_find" {
			findAct = &p
			break
		}
	}
	if findAct == nil {
		t.Fatal("expected skills_find action in app-level actions")
	}
	if !findAct.Mutates {
		t.Error("expected skills_find to have Mutates = true to trigger TUI rescan")
	}
}

func TestContextualFooterAndHelpModal(t *testing.T) {
	oldLookPath := actions.LookPath
	actions.LookPath = func(file string) (string, error) {
		if file == "skills" {
			return "/usr/bin/skills", nil
		}
		return "", errors.New("not found")
	}
	actions.ResetActionCaches()
	t.Cleanup(func() {
		actions.LookPath = oldLookPath
		actions.ResetActionCaches()
	})

	m := appModel{
		width:    100,
		height:   30,
		selected: 1,
		result: model.ScanResult{
			Skills: []*model.Skill{
				{Name: "Build", Scope: model.ScopeProject, LocalLock: &model.LocalLockEntry{Source: "owner/repo"}, ObservedPaths: []model.ObservedPath{{Path: "/tmp/build", Scope: model.ScopeProject, Status: model.StatusCanonical}}},
			},
		},
	}
	m.syncViewport()

	// 1. Default footer (Skills focused)
	outDefault := m.View()
	if !strings.Contains(outDefault, "enter open · e enable/disable · c actions") {
		t.Fatalf("expected default footer in View, got:\n%s", outDefault)
	}
	if !strings.Contains(outDefault, "u update") || !strings.Contains(outDefault, "x remove") {
		t.Fatalf("expected tracked skill footer to include update/remove, got:\n%s", outDefault)
	}

	// 1b. Source row selected footer
	m.selected = 0
	outHeaderFooter := m.View()
	if !strings.Contains(outHeaderFooter, "enter browse · e enable/disable source") {
		t.Fatalf("expected source group footer in View, got:\n%s", outHeaderFooter)
	}
	m.selected = 1

	// 2. Metadata focus footer
	m.focus = focusMetadata
	outMeta := m.View()
	if !strings.Contains(outMeta, "scroll metadata") {
		t.Fatalf("expected metadata footer in View, got:\n%s", outMeta)
	}

	// 3. Preview focus footer
	m.focus = focusPreview
	outPreview := m.View()
	if !strings.Contains(outPreview, "scroll preview") {
		t.Fatalf("expected preview footer in View, got:\n%s", outPreview)
	}

	// 4. Search active footer
	m.searching = true
	outSearch := m.View()
	if !strings.Contains(outSearch, "type search · enter apply") {
		t.Fatalf("expected search footer in View, got:\n%s", outSearch)
	}
	m.searching = false

	// 5. Help modal open
	updated, _ := m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'?'}})
	m = updated.(appModel)
	if !m.helpOpen {
		t.Fatal("expected ? to open help modal")
	}

	outHelp := m.View()
	if !strings.Contains(outHelp, "LazySkills Keyboard Help") || !strings.Contains(outHelp, "Navigation & Focus:") {
		t.Fatalf("expected help modal content in View, got:\n%s", outHelp)
	}

	// 6. Help modal close with 'q'
	updated, _ = m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'q'}})
	m = updated.(appModel)
	if m.helpOpen {
		t.Fatal("expected q to close help modal")
	}
}

func TestFooterHidesUnavailableSkillHotkeys(t *testing.T) {
	oldLookPath := actions.LookPath
	actions.LookPath = func(file string) (string, error) {
		if file == "skills" {
			return "/usr/bin/skills", nil
		}
		return "", errors.New("not found")
	}
	actions.ResetActionCaches()
	t.Cleanup(func() {
		actions.LookPath = oldLookPath
		actions.ResetActionCaches()
	})

	m := appModel{
		width:    100,
		height:   30,
		selected: 1,
		result: model.ScanResult{Skills: []*model.Skill{{
			Name:          "Loose Skill",
			Scope:         model.ScopeGlobal,
			CanonicalPath: "/tmp/loose-skill",
		}}},
	}
	m.syncViewport()

	footer := m.footerText(100, m.visibleRows(), m.currentActions())
	if strings.Contains(footer, "u update") {
		t.Fatalf("untracked skill footer should hide unavailable update hotkey, got %q", footer)
	}
	if !strings.Contains(footer, "enter open") || !strings.Contains(footer, "c actions") || !strings.Contains(footer, "? help") {
		t.Fatalf("expected neutral actions to remain visible, got %q", footer)
	}

	updated, cmd := m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'u'}})
	next := updated.(appModel)
	if cmd != nil || next.confirming || next.running {
		t.Fatalf("unavailable update hotkey should stay idle, confirming=%v running=%v cmd=%v", next.confirming, next.running, cmd)
	}
}

func TestSourceScanHintIgnoresBulkSelection(t *testing.T) {
	m := appModel{
		width:    120,
		height:   32,
		selected: 0,
		result: model.ScanResult{Skills: []*model.Skill{
			{Name: "One", Scope: model.ScopeProject, LocalLock: &model.LocalLockEntry{Source: "owner/repo"}},
			{Name: "Two", Scope: model.ScopeProject, LocalLock: &model.LocalLockEntry{Source: "owner/repo"}},
		}},
	}
	m.selectedKeys = map[string]bool{skillKey(m.result.Skills[0]): true}
	m.syncViewport()

	footer := m.footerText(120, m.visibleRows(), m.currentActions())
	if !strings.Contains(footer, "d scan") {
		t.Fatalf("source header should show scan even with bulk selection active, got %q", footer)
	}
}

func TestDetailModalHelpHidesUnavailableOpen(t *testing.T) {
	t.Setenv("EDITOR", "")
	m := appModel{
		width:       120,
		height:      32,
		selected:    1,
		detailModal: true,
		result: model.ScanResult{Skills: []*model.Skill{{
			Name:  "Loose Skill",
			Scope: model.ScopeProject,
		}}},
	}

	help := m.detailModalHelpLine()
	if strings.Contains(help, "o open") || strings.Contains(help, "o open in editor") {
		t.Fatalf("detail modal should hide unavailable open hint, got %q", help)
	}
	if !strings.Contains(help, "c command picker") || !strings.Contains(help, "↑/↓ scroll") {
		t.Fatalf("detail modal should keep neutral hints, got %q", help)
	}
}

func TestDetailModalOpenHintIgnoresBulkSelection(t *testing.T) {
	t.Setenv("EDITOR", "vim")
	m := appModel{
		width:       120,
		height:      32,
		selected:    2,
		detailModal: true,
		result: model.ScanResult{Skills: []*model.Skill{
			{Name: "Selected Elsewhere", Scope: model.ScopeProject, SkillPath: "/tmp/selected/SKILL.md"},
			{Name: "Current Detail", Scope: model.ScopeProject, SkillPath: "/tmp/current/SKILL.md"},
		}},
	}
	m.selectedKeys = map[string]bool{skillKey(m.result.Skills[0]): true}
	m.syncViewport()

	help := m.detailModalHelpLine()
	if !strings.Contains(help, "o open in editor") {
		t.Fatalf("detail modal should show current-row open hint even with bulk selection, got %q", help)
	}
}

func TestSourceModalHelpIsCapabilityAware(t *testing.T) {
	oldLookPath := actions.LookPath
	actions.LookPath = func(file string) (string, error) {
		if file == "skills" {
			return "/usr/bin/skills", nil
		}
		return "", errors.New("not found")
	}
	actions.ResetActionCaches()
	t.Cleanup(func() {
		actions.LookPath = oldLookPath
		actions.ResetActionCaches()
	})
	t.Setenv("EDITOR", "")

	m := appModel{
		width:       120,
		height:      32,
		selected:    0,
		detailModal: true,
		modalSource: "owner/repo",
		result: model.ScanResult{Skills: []*model.Skill{{
			Name:      "Installed",
			Scope:     model.ScopeProject,
			LocalLock: &model.LocalLockEntry{Source: "owner/repo"},
		}}},
	}
	m.syncViewport()

	help := m.detailModalHelpLine()
	if strings.Contains(help, "enter open") || strings.Contains(help, "o open") {
		t.Fatalf("source modal should hide unavailable installed-skill open hints, got %q", help)
	}
	if !strings.Contains(help, "d scan") || !strings.Contains(help, "c more") {
		t.Fatalf("source modal should keep available scan and actions hints, got %q", help)
	}
}

func TestSelectedSkillActionsDoNotIncludeAppActions(t *testing.T) {
	m := appModel{selected: 1, result: model.ScanResult{Skills: []*model.Skill{{Name: "Build", Scope: model.ScopeProject}}}}
	previews := m.currentActions()
	for _, preview := range previews {
		if preview.ID == "skills_init" || preview.ID == "skills_find" || preview.ID == "skills_update" {
			t.Fatalf("selected-skill actions should not include app-level action %q", preview.ID)
		}
	}
}

func TestCollapseExpandSourceGroups(t *testing.T) {
	m := appModel{
		width:           120,
		height:          32,
		focus:           focusSkills,
		collapsedGroups: make(map[string]bool),
		result: model.ScanResult{
			Skills: []*model.Skill{
				{Name: "One", Scope: model.ScopeProject, LocalLock: &model.LocalLockEntry{Source: "owner/one"}},
				{Name: "Two", Scope: model.ScopeProject, LocalLock: &model.LocalLockEntry{Source: "owner/one"}},
				{Name: "Three", Scope: model.ScopeProject, LocalLock: &model.LocalLockEntry{Source: "owner/two"}},
			},
		},
	}
	m.syncViewport()

	// Initially, both are expanded
	out1 := m.View()
	if !strings.Contains(out1, "- owner/one") || !strings.Contains(out1, "One") || !strings.Contains(out1, "Two") {
		t.Fatalf("expected expanded groups (with ASCII minus) and skills, got:\n%s", out1)
	}

	// Selection starts at 0 (header row of owner/one)
	if m.selected != 0 {
		t.Fatalf("expected selection to start at header row 0, got %d", m.selected)
	}

	// Press h to collapse "owner/one"
	updated, _ := m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'h'}})
	m = updated.(appModel)
	if !m.isCollapsed("owner/one") {
		t.Fatal("expected owner/one to be collapsed")
	}

	// Selection should remain on index 0 (collapsed header)
	if m.selected != 0 {
		t.Fatalf("expected selection to remain on collapsed header row 0, got %d", m.selected)
	}

	// View should show + and hide "One" and "Two"
	out2 := m.View()
	if !strings.Contains(out2, "+ owner/one") || strings.Contains(out2, "  One [P]") || strings.Contains(out2, "  Two [P]") {
		t.Fatalf("expected collapsed group A (with ASCII plus) to hide child skills, got:\n%s", out2)
	}

	// Press l to expand "owner/one" again
	updated, _ = m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'l'}})
	m = updated.(appModel)
	if m.isCollapsed("owner/one") {
		t.Fatal("expected owner/one to be expanded")
	}

	out3 := m.View()
	if !strings.Contains(out3, "- owner/one") || !strings.Contains(out3, "One") {
		t.Fatalf("expected expanded group A to show child skills, got:\n%s", out3)
	}

	// Test j/k navigation skips collapsed rows
	// Collapse "owner/one" again
	m.collapsedGroups["owner/one"] = true
	m.clampSelection() // selection should clamp to 0 (header) or 1 (owner/two) or 2 (Three)

	// Since owner/one is collapsed, visible rows are:
	// index 0: + owner/one
	// index 1: - owner/two
	// index 2: Three
	m.selected = 2 // on Three

	// Pressing up/k from Three (index 2) should go to owner/two (index 1)
	updated, _ = m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'k'}})
	m = updated.(appModel)
	if m.selected != 1 {
		t.Fatalf("expected selection to go to index 1 (owner/two), got %d", m.selected)
	}

	// Test h/l outside focusSkills do not collapse groups and do not change focus
	m.focus = focusMetadata
	m.collapsedGroups = make(map[string]bool)
	updated, _ = m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'h'}})
	m = updated.(appModel)
	if len(m.collapsedGroups) > 0 {
		t.Fatalf("h outside focusSkills should not collapse groups")
	}
	if m.focus != focusMetadata {
		t.Fatalf("h outside focusSkills should not change focus")
	}

	// Test group header row action details and commands
	m.focus = focusSkills
	m.selected = 0 // select header row owner/one
	m.collapsedGroups["owner/one"] = true
	outHeader := m.View()
	if !strings.Contains(outHeader, "State:") || !strings.Contains(outHeader, "collapsed") {
		t.Fatalf("expected header placeholder/metadata in Metadata pane, got:\n%s", outHeader)
	}

	// Verify that header row selection has no skill-scoped actions
	previewsHeader := m.currentActions()
	for _, p := range previewsHeader {
		if p.ID == "reinstall_update" || p.ID == "remove" {
			t.Fatalf("header row actions should not include skill-scoped action %q", p.ID)
		}
	}

	// Test enter on header row opens source detail modal without toggling collapse.
	updated, _ = m.Update(tea.KeyMsg{Type: tea.KeyEnter})
	m = updated.(appModel)
	if !m.detailModal {
		t.Fatal("expected enter on header to open source detail modal")
	}
	if !m.isCollapsed("owner/one") {
		t.Fatal("expected enter on header not to change collapsed state")
	}
}

func TestRichSourceInventoryMetadataAndActions(t *testing.T) {
	m := appModel{
		width:           120,
		height:          32,
		focus:           focusSkills,
		collapsedGroups: make(map[string]bool),
		result: model.ScanResult{
			Skills: []*model.Skill{
				{Name: "One", Scope: model.ScopeProject, CanonicalPath: "/tmp/one", LocalLock: &model.LocalLockEntry{Source: "owner/one", ComputedHash: "hash1"}},
				{Name: "Two", Scope: model.ScopeGlobal, CanonicalPath: "/tmp/two", LocalLock: &model.LocalLockEntry{Source: "owner/one"}},
			},
		},
	}
	m.syncViewport()

	// Select the header row owner/one (index 0)
	m.selected = 0

	// 1. Assert structured metadata info
	metaLines := m.metadataLines(80)
	metaJoined := strings.Join(metaLines, "\n")
	if !strings.Contains(metaJoined, "Source:      owner/one") {
		t.Errorf("expected source name in metadata, got %q", metaJoined)
	}
	if !strings.Contains(metaJoined, "State:") || !strings.Contains(metaJoined, "expanded") {
		t.Errorf("expected state in metadata, got %q", metaJoined)
	}
	if !strings.Contains(metaJoined, "Skills:      2 visible / 2 total") {
		t.Errorf("expected skills count in metadata, got %q", metaJoined)
	}
	if !strings.Contains(metaJoined, "Scope:") || (!strings.Contains(metaJoined, "mixed") && !strings.Contains(metaJoined, "Mixed")) {
		t.Errorf("expected scope mixed in metadata, got %q", metaJoined)
	}
	if strings.Contains(metaJoined, "Hash:") {
		t.Errorf("hash should no longer appear in metadata, got %q", metaJoined)
	}

	// 2. Assert source preview lists installed skills + available section
	prevLines := m.previewLines(80)
	prevJoined := strings.Join(prevLines, "\n")
	if !strings.Contains(prevJoined, "Installed (2)") {
		t.Errorf("expected installed count header in preview, got %q", prevJoined)
	}
	if !strings.Contains(prevJoined, "• One [P]") || !strings.Contains(prevJoined, "• Two [G]") {
		t.Errorf("expected installed skills listed in preview, got %q", prevJoined)
	}
	if !strings.Contains(prevJoined, "press d to scan this source") {
		t.Errorf("expected available scan hint in preview, got %q", prevJoined)
	}
	if strings.Contains(prevJoined, "enter to browse") {
		t.Errorf("source preview should not duplicate footer actions, got %q", prevJoined)
	}

	// 3. Assert currentActions for source row
	acts := m.currentActions()
	var hasUpdate, hasRemove, hasAppLevel, hasSkillLevel bool
	for _, act := range acts {
		if act.ID == "bulk_reinstall_update" && act.Title == "Update installed skills from source" {
			hasUpdate = true
		}
		if act.ID == "bulk_remove" && act.Title == "Remove installed skills from source" {
			hasRemove = true
		}
		if act.ID == "skills_init" || act.ID == "skills_find" || act.ID == "skills_update" {
			hasAppLevel = true
		}
		if act.ID == "reinstall_update" || act.ID == "remove" || act.ID == "open_skill" {
			hasSkillLevel = true
		}
	}
	if !hasUpdate || !hasRemove {
		t.Errorf("expected source update/remove actions to be present, got %+v", acts)
	}
	if hasAppLevel {
		t.Errorf("did not expect app-level actions for source row, got %+v", acts)
	}
	if hasSkillLevel {
		t.Errorf("did not expect skill-scoped actions for source row, got %+v", acts)
	}
}

func TestRemoteDiscoverySuccess(t *testing.T) {
	oldGitClone := gitClone
	defer func() { gitClone = oldGitClone }()

	var capturedURL, capturedRef string
	gitClone = func(url, ref, tempDir string) error {
		capturedURL = url
		capturedRef = ref
		if err := os.MkdirAll(filepath.Join(tempDir, ".git"), 0o755); err != nil {
			return err
		}
		skillPath := filepath.Join(tempDir, "SKILL.md")
		skillContent := "---\nname: \"Remote Skill\"\ndescription: \"Remote description\"\n---\nRemote preview content"
		if err := os.WriteFile(skillPath, []byte(skillContent), 0o644); err != nil {
			return err
		}
		return nil
	}

	m := appModel{
		width: 120, height: 32, selected: 0,
		result: model.ScanResult{
			Skills: []*model.Skill{
				{Name: "Existing", Scope: model.ScopeProject, LocalLock: &model.LocalLockEntry{Source: "owner/repo"}},
			},
		},
	}
	m.syncViewport()

	updated, cmd := m.startDiscovery("owner/repo", false)
	if cmd == nil {
		t.Fatal("expected discovery cmd")
	}
	msg := cmd()
	updated, _ = updated.Update(msg)
	next := updated.(appModel)

	if next.discovery["owner/repo"].Status != DiscoveryReady {
		t.Fatalf("expected discovery ready, got status %s, error: %s", next.discovery["owner/repo"].Status, next.discovery["owner/repo"].Error)
	}
	if capturedURL != "https://github.com/owner/repo" {
		t.Fatalf("expected URL to be https://github.com/owner/repo, got %s", capturedURL)
	}
	if capturedRef != "" {
		t.Fatalf("expected empty captured ref, got %s", capturedRef)
	}

	var foundAvailable bool
	for _, ds := range next.discovery["owner/repo"].Skills {
		if ds.Name == "Remote Skill" {
			foundAvailable = true
			break
		}
	}
	if !foundAvailable {
		t.Fatalf("expected 'Remote Skill' among discovered skills, got %#v", next.discovery["owner/repo"].Skills)
	}
	if next.availableCount("owner/repo") < 1 {
		t.Fatalf("expected at least one available (uninstalled) skill, got %d", next.availableCount("owner/repo"))
	}
}

func TestRemoteDiscoveryFailure(t *testing.T) {
	oldGitClone := gitClone
	defer func() { gitClone = oldGitClone }()

	gitClone = func(url, ref, tempDir string) error {
		return fmt.Errorf("network connection error")
	}

	m := appModel{
		width: 120, height: 32, selected: 0,
		result: model.ScanResult{
			Skills: []*model.Skill{
				{Name: "Existing", Scope: model.ScopeProject, LocalLock: &model.LocalLockEntry{Source: "owner/repo-fail"}},
			},
		},
	}
	updated, cmd := m.startDiscovery("owner/repo-fail", false)
	if cmd == nil {
		t.Fatal("expected discovery cmd")
	}
	msg := cmd()
	updated, _ = updated.Update(msg)
	next := updated.(appModel)

	disc := next.discovery["owner/repo-fail"]
	if disc.Status != DiscoveryFailed {
		t.Fatalf("expected status failed, got %s", disc.Status)
	}
	if !strings.Contains(disc.Error, "network connection error") {
		t.Fatalf("expected error to contain network connection error, got %s", disc.Error)
	}
}

func TestRemoteDiscoveryRejectsUnsafe(t *testing.T) {
	cases := []struct {
		source string
		safe   bool
	}{
		{"owner/repo", true},
		{"github:owner/repo", true},
		{"https://github.com/owner/repo", true},
		{"https://github.com/owner/repo.git", true},
		{"owner/repo#ref", true},
		{"owner/repo#ref-name_123.4", true},
		{"owner/repo#ref/with/slash", true},
		{"-owner/repo", false},
		{"owner/-repo", false},
		{"owner/repo#--ref", false},
		{"owner/repo;-somecmd", false},
		{"owner/repo/sub", false},
		{"https://gitlab.com/owner/repo", false},
	}
	for _, tc := range cases {
		_, _, ok := parseRemoteGitHubSource(tc.source)
		if ok != tc.safe {
			t.Errorf("expected %q safe=%v, got %v", tc.source, tc.safe, ok)
		}
	}
}

func TestIsSafeGitHubRef(t *testing.T) {
	cases := []struct {
		ref  string
		safe bool
	}{
		{"main", true},
		{"v1.2.3", true},
		{"feature/branch-name_1", true},
		{"", false},
		{"-branch", false},
		{"/branch", false},
		{"branch/", false},
		{"branch..name", false},
		{"branch@{1}", false},
		{"branch\\name", false},
		{"branch\x00name", false},
		{"branch\x1bname", false},
		{"branch name", false},
	}
	for _, tc := range cases {
		ok := isSafeGitHubRef(tc.ref)
		if ok != tc.safe {
			t.Errorf("expected ref %q safe=%v, got %v", tc.ref, tc.safe, ok)
		}
	}
}

func TestRemoteDiscoverySanitization(t *testing.T) {
	oldGitClone := gitClone
	defer func() { gitClone = oldGitClone }()

	gitClone = func(url, ref, tempDir string) error {
		if err := os.MkdirAll(filepath.Join(tempDir, ".git"), 0o755); err != nil {
			return err
		}
		skillPath := filepath.Join(tempDir, "SKILL.md")
		skillContent := "---\nname: \"Bad\\u001b[31m Skill\"\ndescription: \"Bad\\u001b[31m Description\"\n---\nBad\x1b[31m preview content"
		if err := os.WriteFile(skillPath, []byte(skillContent), 0o644); err != nil {
			return err
		}
		return nil
	}

	m := appModel{
		width: 120, height: 32, selected: 0,
		result: model.ScanResult{
			Skills: []*model.Skill{
				{Name: "Existing", Scope: model.ScopeProject, LocalLock: &model.LocalLockEntry{Source: "owner/repo"}},
			},
		},
	}
	m.syncViewport()

	updated, cmd := m.startDiscovery("owner/repo", false)
	if cmd == nil {
		t.Fatal("expected discovery cmd")
	}
	msg := cmd()
	updated, _ = updated.Update(msg)
	next := updated.(appModel)

	disc := next.discovery["owner/repo"]
	if disc.Status != DiscoveryReady {
		t.Fatalf("expected discovery ready, got status %s, error: %s", disc.Status, disc.Error)
	}

	if len(disc.Skills) != 1 {
		t.Fatalf("expected 1 skill, got %d", len(disc.Skills))
	}

	ds := disc.Skills[0]
	if strings.Contains(ds.Name, "\x1b") {
		t.Errorf("expected name to be sanitized, got %q", ds.Name)
	}
	if strings.Contains(ds.Description, "\x1b") {
		t.Errorf("expected description to be sanitized, got %q", ds.Description)
	}
	if strings.Contains(ds.Preview, "\x1b") {
		t.Errorf("expected preview to be sanitized, got %q", ds.Preview)
	}
}

func TestRemoteDiscoveryUnsafeRef(t *testing.T) {
	m := appModel{
		width: 120, height: 32, selected: 0,
		result: model.ScanResult{
			Skills: []*model.Skill{
				{Name: "Existing", Scope: model.ScopeProject, LocalLock: &model.LocalLockEntry{Source: "owner/repo"}},
			},
		},
	}
	// Verify that starting discovery on an unsafe ref fails immediately with a clear message
	updated, cmd := m.startDiscovery("owner/repo#--unsafe-ref", false)
	if cmd != nil {
		t.Fatal("expected discovery cmd to be nil for unsafe ref")
	}
	next := updated.(appModel)
	disc := next.discovery["owner/repo#--unsafe-ref"]
	if disc.Status != DiscoveryFailed {
		t.Fatalf("expected status failed, got %s", disc.Status)
	}
	if !strings.Contains(disc.Error, "ref contains unsafe or invalid characters") {
		t.Fatalf("expected error to mention unsafe ref, got %s", disc.Error)
	}
}

func TestRemoteDiscoveryUnsafeFallbackRef(t *testing.T) {
	oldGitClone := gitClone
	defer func() { gitClone = oldGitClone }()

	calledClone := false
	gitClone = func(url, ref, tempDir string) error {
		calledClone = true
		return nil
	}

	m := appModel{
		width: 120, height: 32, selected: 0,
		result: model.ScanResult{
			Skills: []*model.Skill{
				{
					Name:      "Existing",
					Scope:     model.ScopeProject,
					LocalLock: &model.LocalLockEntry{Source: "owner/repo", Ref: "--unsafe-ref"},
				},
			},
		},
	}
	// Trigger discovery on "owner/repo" which has an unsafe fallback ref
	updated, cmd := m.startDiscovery("owner/repo", false)
	if cmd != nil {
		t.Fatal("expected discovery cmd to be nil for unsafe fallback ref")
	}
	next := updated.(appModel)
	disc := next.discovery["owner/repo"]
	if disc.Status != DiscoveryFailed {
		t.Fatalf("expected status failed, got %s", disc.Status)
	}
	if !strings.Contains(disc.Error, "ref contains unsafe or invalid characters") {
		t.Fatalf("expected error to mention unsafe ref, got %s", disc.Error)
	}
	if calledClone {
		t.Fatal("gitClone was called but it should not have been")
	}
}

func TestSourceDiscoverActionAvailability(t *testing.T) {
	m := appModel{
		width: 120, height: 32, selected: 0,
		result: model.ScanResult{
			Skills: []*model.Skill{
				{Name: "Untracked", Scope: model.ScopeProject}, // Custom / untracked group
			},
		},
	}
	m.syncViewport()
	acts := m.sourceActions("Custom / untracked")
	var discAction *actions.CommandPreview
	for _, act := range acts {
		if act.ID == "source_discover" {
			discAction = &act
			break
		}
	}
	if discAction == nil {
		t.Fatal("expected source_discover action to be present")
	}
	if discAction.Available {
		t.Fatal("expected discovery to be unavailable for untracked/custom group")
	}
	if !strings.Contains(discAction.Reason, "requires a local checkout") {
		t.Fatalf("expected meaningful reason, got %q", discAction.Reason)
	}
	if discAction.Command != "discover Custom / untracked" {
		t.Fatalf("expected Command text 'discover Custom / untracked', got %q", discAction.Command)
	}
}

func TestRemoteDiscoveryRawFallbackRefBypass(t *testing.T) {
	oldGitClone := gitClone
	defer func() { gitClone = oldGitClone }()

	calledClone := false
	gitClone = func(url, ref, tempDir string) error {
		calledClone = true
		return nil
	}

	// Case 1: Ref contains control/escape char \x1b
	m1 := appModel{
		width: 120, height: 32, selected: 0,
		result: model.ScanResult{
			Skills: []*model.Skill{
				{
					Name:      "Existing",
					Scope:     model.ScopeProject,
					LocalLock: &model.LocalLockEntry{Source: "owner/repo", Ref: "main\x1b"},
				},
			},
		},
	}
	updated1, cmd1 := m1.startDiscovery("owner/repo", false)
	if cmd1 != nil {
		t.Fatal("expected discovery cmd to be nil for unsafe fallback ref containing escape char")
	}
	next1 := updated1.(appModel)
	disc1 := next1.discovery["owner/repo"]
	if disc1.Status != DiscoveryFailed {
		t.Fatalf("expected status failed, got %s", disc1.Status)
	}
	if !strings.Contains(disc1.Error, "ref contains unsafe or invalid characters") {
		t.Fatalf("expected error to mention unsafe ref, got %s", disc1.Error)
	}

	// Case 2: Ref contains newline char \n
	m2 := appModel{
		width: 120, height: 32, selected: 0,
		result: model.ScanResult{
			Skills: []*model.Skill{
				{
					Name:      "Existing",
					Scope:     model.ScopeProject,
					LocalLock: &model.LocalLockEntry{Source: "owner/repo", Ref: "main\nnext"},
				},
			},
		},
	}
	updated2, cmd2 := m2.startDiscovery("owner/repo", false)
	if cmd2 != nil {
		t.Fatal("expected discovery cmd to be nil for unsafe fallback ref containing newline")
	}
	next2 := updated2.(appModel)
	disc2 := next2.discovery["owner/repo"]
	if disc2.Status != DiscoveryFailed {
		t.Fatalf("expected status failed, got %s", disc2.Status)
	}
	if !strings.Contains(disc2.Error, "ref contains unsafe or invalid characters") {
		t.Fatalf("expected error to mention unsafe ref, got %s", disc2.Error)
	}

	if calledClone {
		t.Fatal("gitClone was called but it should not have been")
	}
}

func TestDiscoveryInProgressNoop(t *testing.T) {
	// Set up gitClone mock
	oldGitClone := gitClone
	defer func() { gitClone = oldGitClone }()
	calledCloneCount := 0
	gitClone = func(url, ref, tempDir string) error {
		calledCloneCount++
		return nil
	}

	m := appModel{
		width: 120, height: 32, selected: 0,
		result: model.ScanResult{
			Skills: []*model.Skill{
				{
					Name:      "Existing",
					Scope:     model.ScopeProject,
					LocalLock: &model.LocalLockEntry{Source: "owner/repo1", Ref: "main"},
				},
				{
					Name:      "Existing2",
					Scope:     model.ScopeProject,
					LocalLock: &model.LocalLockEntry{Source: "owner/repo2", Ref: "main"},
				},
			},
		},
	}
	m.discovery = make(map[string]SourceDiscovery)

	// Set owner/repo1 to DiscoveryLoading
	m.discovery["owner/repo1"] = SourceDiscovery{
		Status: DiscoveryLoading,
	}

	// Call startDiscovery for owner/repo1 (same group is loading)
	updated, cmd := m.startDiscovery("owner/repo1", true)
	if cmd != nil {
		t.Fatal("expected discovery cmd to be nil when the same group is already loading")
	}
	next := updated.(appModel)
	disc := next.discovery["owner/repo1"]
	if disc.Status != DiscoveryLoading {
		t.Fatalf("expected status to remain DiscoveryLoading, got %s", disc.Status)
	}
	if disc.Error != "" {
		t.Fatalf("expected empty error, got %s", disc.Error)
	}
	if calledCloneCount != 0 {
		t.Fatalf("expected git clone not to be called, but called %d times", calledCloneCount)
	}

	// Call startDiscovery for owner/repo2 (different group, not loading)
	_, cmdDifferent := m.startDiscovery("owner/repo2", true)
	if cmdDifferent == nil {
		t.Fatal("expected discovery cmd to start for a different group")
	}

	// Verify that if status of owner/repo1 is no longer loading (e.g. DiscoveryFailed), it can be rescanned when force is requested
	m.discovery["owner/repo1"] = SourceDiscovery{
		Status: DiscoveryFailed,
		Error:  "some legacy error",
	}
	updatedRescan, cmdRescan := m.startDiscovery("owner/repo1", true)
	if cmdRescan == nil {
		t.Fatal("expected discovery cmd to start for a non-loading status when force is requested")
	}
	nextRescan := updatedRescan.(appModel)
	if nextRescan.discovery["owner/repo1"].Status != DiscoveryLoading {
		t.Fatalf("expected status to transition back to DiscoveryLoading, got %s", nextRescan.discovery["owner/repo1"].Status)
	}
	if nextRescan.discovery["owner/repo1"].Error != "" {
		t.Fatalf("expected error block to be cleared/empty on rescan, got %s", nextRescan.discovery["owner/repo1"].Error)
	}
}

func TestInteractiveSourceDetailModal(t *testing.T) {
	t.Setenv("EDITOR", "nano")

	// Mock git clone
	oldGitClone := gitClone
	defer func() { gitClone = oldGitClone }()
	gitClone = func(url, ref, tempDir string) error {
		return nil
	}

	// 1. Set up model with a header/source row and one installed skill
	m := appModel{
		width:           120,
		height:          32,
		focus:           focusSkills,
		collapsedGroups: make(map[string]bool),
		result: model.ScanResult{
			Skills: []*model.Skill{
				{
					Name:          "InstalledSkill",
					Scope:         model.ScopeProject,
					CanonicalPath: "/path/to/InstalledSkill",
					Description:   "This is installed.",
					LocalLock:     &model.LocalLockEntry{Source: "owner/repo", Ref: "main"},
				},
			},
		},
	}
	m.discovery = make(map[string]SourceDiscovery)

	// In visibleRows, index 0 is the header "owner/repo", and index 1 is "InstalledSkill"
	rows := m.visibleRows()
	if len(rows) < 2 || !rows[0].isHeader {
		t.Fatalf("expected first row to be header, got visibleRows: %+v", rows)
	}

	// Select the header row
	m.selected = 0

	// 2. Open source modal (triggers discovery command when not checked)
	updated, cmd := m.Update(tea.KeyMsg{Type: tea.KeyEnter})
	m = updated.(appModel)
	if !m.detailModal {
		t.Fatal("expected enter on header to open detail modal")
	}
	if m.modalSource != "owner/repo" {
		t.Fatalf("expected modalSource to be owner/repo, got %q", m.modalSource)
	}
	if cmd == nil {
		t.Fatal("expected opening unchecked source modal to trigger discovery command")
	}

	// 3. Re-opening (from the sidebar) an already-loading source doesn't re-trigger
	//    discovery. Use a modal-closed copy so enter targets the header, leaving the
	//    open modal in m intact for the steps below.
	mLoading := m
	mLoading.detailModal = false
	mLoading.modalSource = ""
	mLoading.discovery = map[string]SourceDiscovery{"owner/repo": {Status: DiscoveryLoading}}
	if _, cmdLoading := mLoading.Update(tea.KeyMsg{Type: tea.KeyEnter}); cmdLoading != nil {
		t.Fatal("expected re-opening a loading source not to trigger discovery again")
	}

	// Let's set it to ready with some available skills
	m.discovery["owner/repo"] = SourceDiscovery{
		Status: DiscoveryReady,
		Skills: []DiscoveredSkill{
			{Name: "InstalledSkill", Description: "This is installed.", Source: "owner/repo"},
			{Name: "AvailableSkill", Description: "This is available.", Source: "owner/repo"},
		},
	}

	// 4. Test j/k inside source modal changes modal child selection
	childRows := m.modalChildRows("owner/repo")
	if len(childRows) != 2 {
		t.Fatalf("expected 2 child rows (1 installed, 1 available), got %d", len(childRows))
	}
	if childRows[0].isAvailable || childRows[0].skill.Name != "InstalledSkill" {
		t.Fatalf("expected first child row to be InstalledSkill, got: %+v", childRows[0])
	}
	if !childRows[1].isAvailable || childRows[1].discoveredSkill.Name != "AvailableSkill" {
		t.Fatalf("expected second child row to be AvailableSkill, got: %+v", childRows[1])
	}

	if m.modalSelected != 0 {
		t.Fatalf("expected initial modalSelected to be 0, got %d", m.modalSelected)
	}

	// Press 'j' to go to next child row
	updatedJ, _ := m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'j'}})
	m = updatedJ.(appModel)
	if m.modalSelected != 1 {
		t.Fatalf("expected modalSelected to be 1 after pressing j, got %d", m.modalSelected)
	}

	// Press 'k' to go back to first child row
	updatedK, _ := m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'k'}})
	m = updatedK.(appModel)
	if m.modalSelected != 0 {
		t.Fatalf("expected modalSelected to be 0 after pressing k, got %d", m.modalSelected)
	}

	// 5. Test 'c' on installed child exposes installed skill actions
	m.modalSelected = 0 // installed child
	updatedC, _ := m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'c'}})
	mC := updatedC.(appModel)
	if mC.detailModal {
		t.Fatal("expected 'c' to close detailModal")
	}
	if !mC.commands {
		t.Fatal("expected 'c' to open commands picker")
	}
	acts := mC.currentActions()
	hasOpen := false
	hasRemove := false
	for _, act := range acts {
		if act.ID == "open_skill" {
			hasOpen = true
		}
		if act.ID == "remove" {
			hasRemove = true
		}
	}
	if !hasOpen || !hasRemove {
		t.Fatalf("expected installed child actions to include open and remove, got: %+v", acts)
	}

	// 6. Test 'c' on available child exposes install action
	m.modalSelected = 1  // available child
	m.detailModal = true // reopen modal for test
	m.commands = false
	updatedCAvail, _ := m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'c'}})
	mCAvail := updatedCAvail.(appModel)
	if mCAvail.detailModal {
		t.Fatal("expected 'c' to close detailModal")
	}
	if !mCAvail.commands {
		t.Fatal("expected 'c' to open commands picker")
	}
	actsAvail := mCAvail.currentActions()
	hasInstall := false
	for _, act := range actsAvail {
		if act.ID == "install_skill" {
			hasInstall = true
		}
	}
	if !hasInstall {
		t.Fatalf("expected available child actions to include install_skill, got: %+v", actsAvail)
	}

	// 7. Verify Source modal shows installed and available sections after discovery
	m.detailModal = true
	m.modalSelected = 0
	viewOut := m.View()
	if !strings.Contains(viewOut, "Installed Skills:") {
		t.Fatal("expected view to contain Installed Skills: section header")
	}
	if !strings.Contains(viewOut, "InstalledSkill [P]") {
		t.Fatal("expected view to contain InstalledSkill [P]")
	}
	if !strings.Contains(viewOut, "Available Skills:") {
		t.Fatal("expected view to contain Available Skills: section header")
	}
	if !strings.Contains(viewOut, "AvailableSkill [available]") {
		t.Fatal("expected view to contain AvailableSkill [available]")
	}
}

func TestSourceModalAvailableSkillInstallUsesGlobalScopeForGlobalSource(t *testing.T) {
	m := appModel{
		modalSource:   "owner/repo",
		modalSelected: 1,
		result: model.ScanResult{
			Skills: []*model.Skill{
				{
					Name:       "InstalledSkill",
					Scope:      model.ScopeGlobal,
					GlobalLock: &model.GlobalLockEntry{Source: "owner/repo", Ref: "main"},
				},
			},
		},
		discovery: map[string]SourceDiscovery{
			"owner/repo": {
				Status: DiscoveryReady,
				Skills: []DiscoveredSkill{{Name: "AvailableSkill", Source: "owner/repo"}},
			},
		},
	}

	install := actionByID(t, m.currentActions(), "install_skill")
	if !argsContain(install.Exec.Args, "--global") {
		t.Fatalf("expected global source install to include --global, got %+v", install.Exec.Args)
	}
}

func TestSourceModalAvailableSkillInstallKeepsProjectScopeForMixedSource(t *testing.T) {
	m := appModel{
		modalSource:   "owner/repo",
		modalSelected: 2,
		result: model.ScanResult{
			Skills: []*model.Skill{
				{
					Name:       "GlobalSkill",
					Scope:      model.ScopeGlobal,
					GlobalLock: &model.GlobalLockEntry{Source: "owner/repo", Ref: "main"},
				},
				{
					Name:      "ProjectSkill",
					Scope:     model.ScopeProject,
					LocalLock: &model.LocalLockEntry{Source: "owner/repo", Ref: "main"},
				},
			},
		},
		discovery: map[string]SourceDiscovery{
			"owner/repo": {
				Status: DiscoveryReady,
				Skills: []DiscoveredSkill{{Name: "AvailableSkill", Source: "owner/repo"}},
			},
		},
	}

	install := actionByID(t, m.currentActions(), "install_skill")
	if argsContain(install.Exec.Args, "--global") {
		t.Fatalf("expected mixed source install to stay project-scoped, got %+v", install.Exec.Args)
	}
}

func TestModalEnterInstallsAvailableSkill(t *testing.T) {
	m := appModel{width: 120, height: 32, detailModal: true, modalSource: "owner/repo", modalSelected: 1,
		result: model.ScanResult{Skills: []*model.Skill{
			{Name: "Installed", Scope: model.ScopeProject, LocalLock: &model.LocalLockEntry{Source: "owner/repo"}},
		}}}
	m.discovery = map[string]SourceDiscovery{
		"owner/repo": {Status: DiscoveryReady, Skills: []DiscoveredSkill{
			{Name: "Installed", Source: "owner/repo"},
			{Name: "Available", Source: "owner/repo"},
		}},
	}
	// modalSelected 1 = the available child.
	updated, _ := m.Update(tea.KeyMsg{Type: tea.KeyEnter})
	next := updated.(appModel)
	if next.detailModal {
		t.Fatal("expected enter on available child to close the modal")
	}
	if !next.confirming || next.pendingAction == nil {
		t.Fatalf("expected enter to arm install confirmation, confirming=%v pending=%v", next.confirming, next.pendingAction)
	}
	if next.pendingAction.ID != "install_skill" {
		t.Fatalf("expected pending install_skill action, got %q", next.pendingAction.ID)
	}
	// Cancel clears the pending action and restores the state of the detail modal.
	updated, _ = next.Update(tea.KeyMsg{Type: tea.KeyEsc})
	c := updated.(appModel)
	if c.confirming || c.pendingAction != nil {
		t.Fatalf("expected esc to clear confirm, confirming=%v pending=%v", c.confirming, c.pendingAction)
	}
	if !c.detailModal {
		t.Fatal("expected esc to restore detailModal=true")
	}
	if c.modalSource != "owner/repo" {
		t.Fatalf("expected esc to restore modalSource=owner/repo, got %q", c.modalSource)
	}
	if c.modalSelected != 1 {
		t.Fatalf("expected esc to restore modalSelected=1, got %d", c.modalSelected)
	}
}

func TestModalEnterInstallsAvailableSkillSuccessPath(t *testing.T) {
	// Mock gitClone to do nothing
	oldGit := gitClone
	gitClone = func(url, ref, dir string) error { return nil }
	t.Cleanup(func() { gitClone = oldGit })

	m := appModel{width: 120, height: 32, detailModal: true, modalSource: "owner/repo", modalSelected: 1,
		result: model.ScanResult{Skills: []*model.Skill{
			{Name: "Installed", Scope: model.ScopeProject, LocalLock: &model.LocalLockEntry{Source: "owner/repo"}},
		}}}
	m.discovery = map[string]SourceDiscovery{
		"owner/repo": {Status: DiscoveryReady, Skills: []DiscoveredSkill{
			{Name: "Installed", Source: "owner/repo"},
			{Name: "Available", Source: "owner/repo"},
		}},
	}

	// Press Enter to go from detail modal to confirmation
	updated, _ := m.Update(tea.KeyMsg{Type: tea.KeyEnter})
	next := updated.(appModel)
	if !next.confirming {
		t.Fatal("expected confirmation to be active")
	}

	// Press Enter on confirmation screen to execute and succeed
	finalModel, cmd := next.Update(tea.KeyMsg{Type: tea.KeyEnter})
	f := finalModel.(appModel)
	if f.confirming || f.pendingAction != nil {
		t.Fatal("expected confirmation and pending action to be cleared")
	}
	if f.confirmReturnDetailModal {
		t.Fatal("expected confirmReturnDetailModal to be cleared on success")
	}
	if cmd == nil {
		t.Fatal("expected interactive cmd to be returned")
	}
	if !f.running {
		t.Fatal("expected f.running to be true")
	}
}

func TestModalCommandsMenuExitAndCancels(t *testing.T) {
	m := appModel{width: 120, height: 32, detailModal: true, modalSource: "owner/repo", modalSelected: 0,
		result: model.ScanResult{Skills: []*model.Skill{
			{Name: "Installed", Scope: model.ScopeProject, LocalLock: &model.LocalLockEntry{Source: "owner/repo"}},
		}}}
	m.discovery = map[string]SourceDiscovery{
		"owner/repo": {Status: DiscoveryReady, Skills: []DiscoveredSkill{
			{Name: "Installed", Source: "owner/repo"},
		}},
	}

	// 1. Press 'c' to open commands (action picker) from detail modal
	updated, _ := m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'c'}})
	next := updated.(appModel)
	if next.detailModal {
		t.Fatal("expected detailModal = false when opening commands")
	}
	if !next.commands {
		t.Fatal("expected commands = true when opening action picker")
	}
	if next.modalSource != "owner/repo" {
		t.Fatalf("expected modalSource preserve, got %q", next.modalSource)
	}

	// 2. Cancel commands menu with Esc -> should return to detailModal=true
	canceledCommands, _ := next.Update(tea.KeyMsg{Type: tea.KeyEsc})
	cc := canceledCommands.(appModel)
	if !cc.detailModal {
		t.Fatal("expected detailModal = true after canceling commands menu")
	}
	if cc.commands {
		t.Fatal("expected commands = false after canceling commands menu")
	}
	if cc.modalSource != "owner/repo" {
		t.Fatalf("expected modalSource restore, got %q", cc.modalSource)
	}

	// 3. Open commands menu again
	updated, _ = cc.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'c'}})
	next = updated.(appModel)

	// 4. Trigger "reinstall_update" using hotkey 'u' inside commands menu (which requires confirm)
	updated, _ = next.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'u'}})
	confirmState := updated.(appModel)
	if !confirmState.confirming {
		t.Fatal("expected hotkey 'u' in commands to arm confirmation")
	}
	if confirmState.commands {
		t.Fatal("expected commands = false when confirmation is active")
	}

	// 5. Cancel confirmation with Esc -> should return to commands menu
	canceledConfirm, _ := confirmState.Update(tea.KeyMsg{Type: tea.KeyEsc})
	cc2 := canceledConfirm.(appModel)
	if cc2.confirming {
		t.Fatal("expected confirming = false after cancel")
	}
	if !cc2.commands {
		t.Fatal("expected commands = true (returned to action picker)")
	}

	// 6. Cancel commands menu with Esc -> should return to detailModal
	finalModel, _ := cc2.Update(tea.KeyMsg{Type: tea.KeyEsc})
	fm := finalModel.(appModel)
	if !fm.detailModal {
		t.Fatal("expected final model to return to detailModal = true")
	}
	if fm.commands {
		t.Fatal("expected commands = false")
	}
	if fm.modalSource != "owner/repo" {
		t.Fatalf("expected modalSource = owner/repo, got %q", fm.modalSource)
	}
}

func TestSourceDetailModalSelectionScrollsIntoView(t *testing.T) {
	discovered := make([]DiscoveredSkill, 0, 30)
	for i := 0; i < 30; i++ {
		discovered = append(discovered, DiscoveredSkill{
			Name:        fmt.Sprintf("Available%02d", i),
			Description: "available skill",
			Source:      "owner/repo",
		})
	}
	m := appModel{
		width:           120,
		height:          24,
		focus:           focusSkills,
		selected:        0,
		detailModal:     true,
		modalSource:     "owner/repo",
		modalSelected:   0,
		viewport:        viewport.New(80, 8),
		collapsedGroups: make(map[string]bool),
		discovery: map[string]SourceDiscovery{
			"owner/repo": {Status: DiscoveryReady, Skills: discovered},
		},
		result: model.ScanResult{Skills: []*model.Skill{{
			Name:      "InstalledSkill",
			Scope:     model.ScopeProject,
			LocalLock: &model.LocalLockEntry{Source: "owner/repo"},
		}}},
	}

	for i := 0; i < 12; i++ {
		updated, _ := m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'j'}})
		m = updated.(appModel)
	}
	if m.modalSelected != 12 {
		t.Fatalf("expected modalSelected to follow j navigation, got %d", m.modalSelected)
	}
	if m.viewport.YOffset == 0 {
		t.Fatal("expected source modal viewport to scroll down as selection moves")
	}
}

func TestRawSourceValidationBeforeRemoteDiscovery(t *testing.T) {
	oldGitClone := gitClone
	defer func() { gitClone = oldGitClone }()

	calledClone := false
	gitClone = func(url, ref, tempDir string) error {
		calledClone = true
		return nil
	}

	// Case 1: LocalLock.Source contains controls/escapes
	m1 := appModel{
		width: 120, height: 32, selected: 0,
		result: model.ScanResult{
			Skills: []*model.Skill{
				{
					Name:      "BadLocal",
					Scope:     model.ScopeProject,
					LocalLock: &model.LocalLockEntry{Source: "owner/repo\x1b[31m", Ref: "main"},
				},
			},
		},
	}
	m1.discovery = make(map[string]SourceDiscovery)
	// Open detail modal on header row
	updated1, cmd1 := m1.Update(tea.KeyMsg{Type: tea.KeyEnter})
	m1 = updated1.(appModel)
	if !m1.detailModal {
		t.Fatal("expected detailModal to be open")
	}
	if cmd1 != nil {
		t.Fatal("expected no discovery command to be generated due to raw local source validation failure")
	}
	disc1 := m1.discovery["owner/repo"]
	if disc1.Status != DiscoveryFailed {
		t.Fatalf("expected discovery to fail, got status: %s", disc1.Status)
	}
	if !strings.Contains(disc1.Error, "raw source contains control, newline, or escape characters") {
		t.Fatalf("expected validation error message, got: %s", disc1.Error)
	}

	// Case 2: GlobalLock.SourceURL contains newline
	m2 := appModel{
		width: 120, height: 32, selected: 0,
		result: model.ScanResult{
			Skills: []*model.Skill{
				{
					Name:       "BadGlobal",
					Scope:      model.ScopeGlobal,
					GlobalLock: &model.GlobalLockEntry{SourceURL: "owner/repo\nnext", Ref: "main"},
				},
			},
		},
	}
	m2.discovery = make(map[string]SourceDiscovery)
	updated2, cmd2 := m2.Update(tea.KeyMsg{Type: tea.KeyEnter})
	m2 = updated2.(appModel)
	if !m2.detailModal {
		t.Fatal("expected detailModal to be open")
	}
	if cmd2 != nil {
		t.Fatal("expected no discovery command to be generated due to raw global source validation failure")
	}
	disc2 := m2.discovery["owner/repo next"]
	if disc2.Status != DiscoveryFailed {
		t.Fatalf("expected discovery to fail, got status: %s", disc2.Status)
	}
	if !strings.Contains(disc2.Error, "raw source contains control, newline, or escape characters") {
		t.Fatalf("expected validation error message, got: %s", disc2.Error)
	}

	// Case 3: LocalLock.Source contains trailing space (sanitization changes it)
	m3 := appModel{
		width: 120, height: 32, selected: 0,
		result: model.ScanResult{
			Skills: []*model.Skill{
				{
					Name:      "BadLocalSpace",
					Scope:     model.ScopeProject,
					LocalLock: &model.LocalLockEntry{Source: "owner/repo ", Ref: "main"},
				},
			},
		},
	}
	m3.discovery = make(map[string]SourceDiscovery)
	updated3, cmd3 := m3.Update(tea.KeyMsg{Type: tea.KeyEnter})
	m3 = updated3.(appModel)
	if !m3.detailModal {
		t.Fatal("expected detailModal to be open")
	}
	if cmd3 != nil {
		t.Fatal("expected no discovery command to be generated due to raw local source sanitization check failure")
	}
	disc3 := m3.discovery["owner/repo"]
	if disc3.Status != DiscoveryFailed {
		t.Fatalf("expected discovery to fail, got status: %s", disc3.Status)
	}
	if !strings.Contains(disc3.Error, "raw source contains unsafe characters or is modified by sanitization") {
		t.Fatalf("expected validation error message, got: %s", disc3.Error)
	}

	if calledClone {
		t.Fatal("gitClone was unexpectedly called")
	}
}

func TestModalCommandPickerUXWithBulkSelection(t *testing.T) {
	t.Setenv("EDITOR", "nano")

	// Set up model with a header/source row, one installed skill, bulk selectedKeys, source modal open, modal selected installed child, commands open.
	m := appModel{
		width:           120,
		height:          32,
		focus:           focusSkills,
		collapsedGroups: make(map[string]bool),
		selectedKeys:    map[string]bool{"InstalledSkill": true},
		detailModal:     false,
		commands:        true,
		modalSource:     "owner/repo",
		modalSelected:   0,
		result: model.ScanResult{
			Skills: []*model.Skill{
				{
					Name:          "InstalledSkill",
					Scope:         model.ScopeProject,
					CanonicalPath: "/path/to/InstalledSkill",
					Description:   "This is installed.",
					LocalLock:     &model.LocalLockEntry{Source: "owner/repo", Ref: "main"},
				},
			},
		},
	}

	// Pressing 'u' should set reinstall_update (single-skill modal child action) instead of bulk
	updatedU, _ := m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'u'}})
	mU := updatedU.(appModel)
	if mU.confirming {
		actions := mU.currentActions()
		if len(actions) == 0 || mU.action >= len(actions) || actions[mU.action].ID != "reinstall_update" {
			t.Fatalf("expected reinstall_update action to be selected, got actions: %+v, selected action: %d", actions, mU.action)
		}
	} else {
		t.Fatal("expected update action to trigger confirmation overlay")
	}

	// Pressing 'x' should set remove instead of bulk
	m.confirming = false
	updatedX, _ := m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'x'}})
	mX := updatedX.(appModel)
	if mX.confirming {
		actions := mX.currentActions()
		if len(actions) == 0 || mX.action >= len(actions) || actions[mX.action].ID != "remove" {
			t.Fatalf("expected remove action to be selected, got actions: %+v, selected action: %d", actions, mX.action)
		}
	} else {
		t.Fatal("expected remove action to trigger confirmation overlay")
	}
}

func TestTuiEnableDisableActions(t *testing.T) {
	tempDir := t.TempDir()
	skillDir := filepath.Join(tempDir, ".aider-desk", "skills", "my-skill")
	if err := os.MkdirAll(skillDir, 0755); err != nil {
		t.Fatal(err)
	}
	skillFile := filepath.Join(skillDir, "SKILL.md")
	if err := os.WriteFile(skillFile, []byte("---\nname: my-skill\n---\n"), 0644); err != nil {
		t.Fatal(err)
	}

	sk := &model.Skill{
		Name:  "my-skill",
		Scope: model.ScopeProject,
		ObservedPaths: []model.ObservedPath{
			{
				Path:   skillDir,
				Scope:  model.ScopeProject,
				Agent:  "aider-desk",
				Status: model.StatusCopy,
			},
		},
	}

	m := appModel{
		cwd:   tempDir,
		agent: "aider-desk",
	}

	acts := m.appendEnableDisableActions(nil, sk)
	if len(acts) != 1 || acts[0].ID != "disable_skill" {
		t.Fatalf("expected 1 disable action, got %+v", acts)
	}
	disableAction := acts[0]

	_, cmd := m.executeAction(disableAction)
	if cmd == nil {
		t.Fatal("expected loadSnapshot cmd to be returned on success")
	}

	disabledPath := filepath.Join(tempDir, ".aider-desk", "skills", ".lazyskills-disabled", "my-skill")
	if _, err := os.Stat(disabledPath); err != nil {
		t.Fatalf("expected disabled folder to exist at %s, got error: %v", disabledPath, err)
	}
	if _, err := os.Stat(skillDir); !os.IsNotExist(err) {
		t.Fatalf("expected active folder at %s to be gone, got error: %v", skillDir, err)
	}

	sk.ObservedPaths[0].Status = model.StatusDisabled
	sk.ObservedPaths[0].Path = disabledPath
	sk.ObservedPaths[0].TargetPath = skillDir

	acts = m.appendEnableDisableActions(nil, sk)
	if len(acts) != 1 || acts[0].ID != "enable_skill" {
		t.Fatalf("expected 1 enable action, got %+v", acts)
	}
	enableAction := acts[0]

	_, cmd = m.executeAction(enableAction)
	if cmd == nil {
		t.Fatal("expected loadSnapshot cmd to be returned on success")
	}

	if _, err := os.Stat(skillDir); err != nil {
		t.Fatalf("expected active folder to exist at %s, got error: %v", skillDir, err)
	}
	if _, err := os.Stat(disabledPath); !os.IsNotExist(err) {
		t.Fatalf("expected disabled folder at %s to be gone, got error: %v", disabledPath, err)
	}
}

func TestTuiEnableDisableEdgeCases(t *testing.T) {
	tempDir := t.TempDir()
	skillDir := filepath.Join(tempDir, ".aider-desk", "skills", "my-skill")
	if err := os.MkdirAll(skillDir, 0755); err != nil {
		t.Fatal(err)
	}

	sk := &model.Skill{
		Name:  "my-skill",
		Scope: model.ScopeProject,
		ObservedPaths: []model.ObservedPath{
			{
				Path:   skillDir,
				Scope:  model.ScopeProject,
				Agent:  "aider-desk",
				Status: model.StatusCopy,
			},
			{
				Path:   skillDir,
				Scope:  model.ScopeProject,
				Agent:  "amp",
				Status: model.StatusCopy,
			},
		},
	}

	// 1. Guard check: per-agent disable blocked when path is shared by multiple agents
	m := appModel{
		cwd:   tempDir,
		agent: "aider-desk",
	}
	acts := m.appendEnableDisableActions(nil, sk)
	if len(acts) != 1 || acts[0].ID != "disable_skill" || acts[0].Available {
		t.Fatalf("expected disable action to be unavailable for shared path, got: %+v", acts)
	}
	if !strings.Contains(acts[0].Reason, "shared by multiple agents") {
		t.Fatalf("expected shared reason, got %q", acts[0].Reason)
	}

	// 2. Guard check: per-agent enable blocked when disabled path is shared by multiple agents
	disabledPath := filepath.Join(tempDir, ".aider-desk", "skills", ".lazyskills-disabled", "my-skill")
	skDisabled := &model.Skill{
		Name:  "my-skill",
		Scope: model.ScopeProject,
		ObservedPaths: []model.ObservedPath{
			{
				Path:       disabledPath,
				TargetPath: skillDir,
				Scope:      model.ScopeProject,
				Agent:      "aider-desk",
				Status:     model.StatusDisabled,
			},
			{
				Path:       disabledPath,
				TargetPath: skillDir,
				Scope:      model.ScopeProject,
				Agent:      "amp",
				Status:     model.StatusDisabled,
			},
		},
	}
	acts = m.appendEnableDisableActions(nil, skDisabled)
	if len(acts) != 1 || acts[0].ID != "enable_skill" || acts[0].Available {
		t.Fatalf("expected enable action to be unavailable for shared path, got: %+v", acts)
	}
	if !strings.Contains(acts[0].Reason, "shared by multiple agents") {
		t.Fatalf("expected shared reason, got %q", acts[0].Reason)
	}

	// 3. Deduplication check: scope-level disable has no duplicates in Args
	mNoAgent := appModel{
		cwd: tempDir,
	}
	acts = mNoAgent.appendEnableDisableActions(nil, sk)
	if len(acts) != 1 || acts[0].ID != "disable_skill" {
		t.Fatalf("expected 1 scope disable action, got %+v", acts)
	}
	if len(acts[0].Exec.Args) != 1 {
		t.Fatalf("expected deduplicated args, got %+v", acts[0].Exec.Args)
	}

	// 4. Preflight validate dest already exists
	destDir := filepath.Join(tempDir, ".aider-desk", "skills", ".lazyskills-disabled", "my-skill")
	if err := os.MkdirAll(destDir, 0755); err != nil {
		t.Fatal(err)
	}
	disableAction := acts[0]
	resModel, cmd := mNoAgent.executeAction(disableAction)
	if cmd != nil {
		t.Fatal("expected action to fail preflight (no cmd returned) when dest exists")
	}
	resAppModel := resModel.(appModel)
	if resAppModel.actionResult == nil || !strings.Contains(resAppModel.actionResult.Err, "destination already exists") {
		t.Fatalf("expected actionResult to carry destination already exists error, got: %+v", resAppModel.actionResult)
	}
}

// TestSharedRootDisablesPerAgentEnableDisable covers alvinunreal/lazyskills#34:
// a per-agent disable/enable preview must be refused (not silently offered)
// when the observation it targets was reached through a symlinked, shared
// scope root, since moving it would mutate the canonical repo other agents
// share.
func TestSharedRootDisablesPerAgentEnableDisable(t *testing.T) {
	tempDir := t.TempDir()
	skillDir := filepath.Join(tempDir, "shared-skill")
	if err := os.MkdirAll(skillDir, 0755); err != nil {
		t.Fatal(err)
	}

	sk := &model.Skill{
		Name:  "shared-skill",
		Scope: model.ScopeGlobal,
		ObservedPaths: []model.ObservedPath{
			{
				Path:       skillDir,
				Scope:      model.ScopeGlobal,
				Agent:      "codex",
				Status:     model.StatusCopy,
				SharedRoot: true,
			},
		},
	}

	m := appModel{cwd: tempDir, agent: "codex"}
	acts := m.appendEnableDisableActions(nil, sk)
	if len(acts) != 1 || acts[0].ID != "disable_skill" || acts[0].Available {
		t.Fatalf("expected disable action to be unavailable for a shared-root path, got: %+v", acts)
	}
	if !strings.Contains(acts[0].Reason, "symlinked") || !strings.Contains(acts[0].Reason, skillDir) {
		t.Fatalf("expected shared-root reason naming the path, got %q", acts[0].Reason)
	}

	disabledPath := filepath.Join(tempDir, ".lazyskills-disabled", "shared-skill")
	skDisabled := &model.Skill{
		Name:  "shared-skill",
		Scope: model.ScopeGlobal,
		ObservedPaths: []model.ObservedPath{
			{
				Path:       disabledPath,
				TargetPath: skillDir,
				Scope:      model.ScopeGlobal,
				Agent:      "codex",
				Status:     model.StatusDisabled,
				SharedRoot: true,
			},
		},
	}
	acts = m.appendEnableDisableActions(nil, skDisabled)
	if len(acts) != 1 || acts[0].ID != "enable_skill" || acts[0].Available {
		t.Fatalf("expected enable action to be unavailable for a shared-root disabled path, got: %+v", acts)
	}
	if !strings.Contains(acts[0].Reason, "symlinked") {
		t.Fatalf("expected shared-root reason, got %q", acts[0].Reason)
	}
}

// TestSharedRootGatesScopeLevelEnableDisable covers the scope-level move,
// which touches every agent root for the skill at once: if even one of
// those roots is shared, the whole action must be refused rather than
// silently moving the remaining, owned subset.
func TestSharedRootGatesScopeLevelEnableDisable(t *testing.T) {
	tempDir := t.TempDir()
	ownedDir := filepath.Join(tempDir, "owned-skill")
	sharedDir := filepath.Join(tempDir, "shared-skill-root", "shared-skill")
	if err := os.MkdirAll(ownedDir, 0755); err != nil {
		t.Fatal(err)
	}
	if err := os.MkdirAll(sharedDir, 0755); err != nil {
		t.Fatal(err)
	}

	sk := &model.Skill{
		Name:  "mixed-skill",
		Scope: model.ScopeGlobal,
		ObservedPaths: []model.ObservedPath{
			{Path: ownedDir, Scope: model.ScopeGlobal, Agent: "claude-code", Status: model.StatusCopy},
			{Path: sharedDir, Scope: model.ScopeGlobal, Agent: "codex", Status: model.StatusCopy, SharedRoot: true},
		},
	}

	m := appModel{cwd: tempDir}
	acts := m.appendEnableDisableActions(nil, sk)
	if len(acts) != 1 || acts[0].ID != "disable_skill" || acts[0].Available {
		t.Fatalf("expected the whole scope-level disable to be refused when any path is shared-root, got: %+v", acts)
	}
	if !strings.Contains(acts[0].Reason, "symlinked") {
		t.Fatalf("expected shared-root reason, got %q", acts[0].Reason)
	}
}

// TestSharedRootGatesSourceEnableDisableActions covers the source-level bulk
// disable/enable builder: any shared-root observation among the source's
// skills must gate the whole preview unavailable.
func TestSharedRootGatesSourceEnableDisableActions(t *testing.T) {
	sk := &model.Skill{
		Name:  "shared-skill",
		Scope: model.ScopeGlobal,
		ObservedPaths: []model.ObservedPath{
			{Path: "/home/user/.codex/skills/shared-skill", Scope: model.ScopeGlobal, Agent: "codex", Status: model.StatusCopy, SharedRoot: true},
		},
	}
	m := appModel{}
	previews := m.sourceEnableDisableActions([]*model.Skill{sk})
	var disable *actions.CommandPreview
	for i := range previews {
		if previews[i].ID == "disable_skill" {
			disable = &previews[i]
		}
	}
	if disable == nil || disable.Available {
		t.Fatalf("expected disable_skill preview to be unavailable for a shared-root skill, got %+v", previews)
	}
	if !strings.Contains(disable.Reason, "symlinked") {
		t.Fatalf("expected shared-root reason, got %q", disable.Reason)
	}

	skDisabled := &model.Skill{
		Name:  "shared-skill",
		Scope: model.ScopeGlobal,
		ObservedPaths: []model.ObservedPath{
			{
				Path:       "/home/user/.codex/skills/.lazyskills-disabled/shared-skill",
				TargetPath: "/home/user/.codex/skills/shared-skill",
				Scope:      model.ScopeGlobal,
				Agent:      "codex",
				Status:     model.StatusDisabled,
				SharedRoot: true,
			},
		},
	}
	previews = m.sourceEnableDisableActions([]*model.Skill{skDisabled})
	var enable *actions.CommandPreview
	for i := range previews {
		if previews[i].ID == "enable_skill" {
			enable = &previews[i]
		}
	}
	if enable == nil || enable.Available {
		t.Fatalf("expected enable_skill preview to be unavailable for a shared-root disabled skill, got %+v", previews)
	}
	if !strings.Contains(enable.Reason, "symlinked") {
		t.Fatalf("expected shared-root reason, got %q", enable.Reason)
	}
}

// TestSharedRootMoveHandlerRefusesRename is the defense-in-depth check on the
// exec handler itself: even if a stale or forged action offers a move whose
// src is currently reached through a shared scope root, the handler must
// re-check live ancestry and abort the whole plan without renaming anything.
func TestSharedRootMoveHandlerRefusesRename(t *testing.T) {
	home := t.TempDir()
	cwd := t.TempDir()
	t.Setenv("HOME", home)

	claudeSkills := filepath.Join(home, ".claude", "skills")
	if err := os.MkdirAll(claudeSkills, 0o755); err != nil {
		t.Fatal(err)
	}
	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)
	}
	skillDir := filepath.Join(codexHome, "skills", "shared-skill")
	if err := os.MkdirAll(skillDir, 0o755); err != nil {
		t.Fatal(err)
	}
	marker := filepath.Join(skillDir, "SKILL.md")
	if err := os.WriteFile(marker, []byte("canonical"), 0o644); err != nil {
		t.Fatal(err)
	}

	m := appModel{
		cwd: cwd,
		result: model.ScanResult{
			Skills: []*model.Skill{
				{
					Name:  "shared-skill",
					Scope: model.ScopeGlobal,
					// Scan-time flag deliberately false: execution must not
					// trust cached SharedRoot alone.
					ObservedPaths: []model.ObservedPath{
						{Path: skillDir, Scope: model.ScopeGlobal, Agent: "codex", Status: model.StatusCopy, SharedRoot: false},
					},
				},
			},
		},
	}

	action := actions.CommandPreview{
		ID:   "disable_skill",
		Exec: actions.ExecSpec{Internal: "disable_skill", Args: []string{skillDir}},
	}

	resModel, cmd := m.executeAction(action)
	if cmd != nil {
		t.Fatal("expected the move to fail preflight (no cmd returned) for a shared-root path")
	}
	resAppModel := resModel.(appModel)
	if resAppModel.actionResult == nil || !strings.Contains(resAppModel.actionResult.Err, "symlinked") {
		t.Fatalf("expected actionResult to carry a shared-root refusal, got: %+v", resAppModel.actionResult)
	}
	if data, err := os.ReadFile(marker); err != nil || string(data) != "canonical" {
		t.Fatalf("expected canonical content untouched, data=%q err=%v", data, err)
	}
	if _, err := os.Stat(filepath.Join(codexHome, "skills", ".lazyskills-disabled", "shared-skill")); !os.IsNotExist(err) {
		t.Fatal("expected no disabled shelf entry to be created for the shared-root path")
	}
}

// TestLiveTopologyChangeRefusesStaleSourceAndDestMove covers the P1 case where
// an owned scope root is replaced by a symlink after scan/preview but before
// execution: both source and destination parent chains must be refused without
// mutating canonical content.
func TestLiveTopologyChangeRefusesStaleSourceAndDestMove(t *testing.T) {
	home := t.TempDir()
	cwd := t.TempDir()
	t.Setenv("HOME", home)

	claudeSkills := filepath.Join(home, ".claude", "skills")
	if err := os.MkdirAll(claudeSkills, 0o755); err != nil {
		t.Fatal(err)
	}
	// Canonical content that must not be moved.
	canonicalSkill := filepath.Join(claudeSkills, "owned-skill")
	if err := os.MkdirAll(canonicalSkill, 0o755); err != nil {
		t.Fatal(err)
	}
	marker := filepath.Join(canonicalSkill, "MARKER")
	if err := os.WriteFile(marker, []byte("do-not-move"), 0o644); err != nil {
		t.Fatal(err)
	}

	// At scan time codex skills is a real owned directory with its own copy.
	codexSkills := filepath.Join(home, ".codex", "skills")
	if err := os.MkdirAll(codexSkills, 0o755); err != nil {
		t.Fatal(err)
	}
	skillDir := filepath.Join(codexSkills, "owned-skill")
	if err := os.MkdirAll(skillDir, 0o755); err != nil {
		t.Fatal(err)
	}
	if err := os.WriteFile(filepath.Join(skillDir, "MARKER"), []byte("codex-copy"), 0o644); err != nil {
		t.Fatal(err)
	}

	// After scan: replace the owned codex skills root with a symlink into
	// claude's canonical skills. The skill path string is unchanged.
	if err := os.RemoveAll(codexSkills); err != nil {
		t.Fatal(err)
	}
	if err := os.Symlink(claudeSkills, codexSkills); err != nil {
		t.Fatal(err)
	}
	// Through the new symlink, skillDir now resolves to canonical content.
	if data, err := os.ReadFile(filepath.Join(skillDir, "MARKER")); err != nil || string(data) != "do-not-move" {
		t.Fatalf("precondition: skillDir should now read canonical content, data=%q err=%v", data, err)
	}

	m := appModel{
		cwd: cwd,
		result: model.ScanResult{
			Skills: []*model.Skill{{
				Name:  "owned-skill",
				Scope: model.ScopeGlobal,
				ObservedPaths: []model.ObservedPath{{
					Path: skillDir, Scope: model.ScopeGlobal, Agent: "codex", Status: model.StatusCopy, SharedRoot: false,
				}},
			}},
		},
	}
	action := actions.CommandPreview{
		ID:   "disable_skill",
		Exec: actions.ExecSpec{Internal: "disable_skill", Args: []string{skillDir}},
	}
	resModel, cmd := m.executeAction(action)
	if cmd != nil {
		t.Fatal("expected live topology guard to refuse the move")
	}
	res := resModel.(appModel)
	if res.actionResult == nil || !strings.Contains(res.actionResult.Err, "symlinked") {
		t.Fatalf("expected symlinked-root refusal, got %#v", res.actionResult)
	}
	if data, err := os.ReadFile(marker); err != nil || string(data) != "do-not-move" {
		t.Fatalf("canonical content must remain unmoved, data=%q err=%v", data, err)
	}
	if _, err := os.Stat(filepath.Join(claudeSkills, ".lazyskills-disabled", "owned-skill")); !os.IsNotExist(err) {
		t.Fatal("expected no disabled shelf under the canonical root")
	}
}

// TestLiveTopologyAllowsNormalSkillEntrySymlink ensures the final skill entry
// may itself be a symlink under an owned root without being rejected.
func TestLiveTopologyAllowsNormalSkillEntrySymlink(t *testing.T) {
	home := t.TempDir()
	cwd := t.TempDir()
	t.Setenv("HOME", home)

	claudeSkills := filepath.Join(home, ".claude", "skills")
	if err := os.MkdirAll(claudeSkills, 0o755); err != nil {
		t.Fatal(err)
	}
	realSkill := filepath.Join(home, "real-skill-body")
	if err := os.MkdirAll(realSkill, 0o755); err != nil {
		t.Fatal(err)
	}
	if err := os.WriteFile(filepath.Join(realSkill, "SKILL.md"), []byte("body"), 0o644); err != nil {
		t.Fatal(err)
	}
	entry := filepath.Join(claudeSkills, "linked-skill")
	if err := os.Symlink(realSkill, entry); err != nil {
		t.Fatal(err)
	}

	m := appModel{
		cwd: cwd,
		result: model.ScanResult{Skills: []*model.Skill{{
			Name:  "linked-skill",
			Scope: model.ScopeGlobal,
			ObservedPaths: []model.ObservedPath{{
				Path: entry, Scope: model.ScopeGlobal, Agent: "claude-code", Status: model.StatusSymlink,
			}},
		}}},
	}
	action := actions.CommandPreview{
		ID:   "disable_skill",
		Exec: actions.ExecSpec{Internal: "disable_skill", Args: []string{entry}},
	}
	_, cmd := m.executeAction(action)
	if cmd == nil {
		t.Fatal("expected skill-entry symlink under owned root to be movable")
	}
	disabled := filepath.Join(claudeSkills, ".lazyskills-disabled", "linked-skill")
	if _, err := os.Lstat(disabled); err != nil {
		t.Fatalf("expected disabled shelf entry for skill-entry symlink, err=%v", err)
	}
	if _, err := os.Lstat(entry); !os.IsNotExist(err) {
		t.Fatalf("expected active entry to be gone after disable, err=%v", err)
	}
	// Canonical body remains reachable via the moved symlink.
	if _, err := os.Stat(filepath.Join(realSkill, "SKILL.md")); err != nil {
		t.Fatalf("expected real skill body preserved, err=%v", err)
	}
}

// TestLiveUnsafePathBlocksExternalRemoveClosure ensures remove execution does
// not invoke runExec after a live shared-root path is detected.
func TestLiveUnsafePathBlocksExternalRemoveClosure(t *testing.T) {
	home := t.TempDir()
	cwd := t.TempDir()
	t.Setenv("HOME", home)

	claudeSkills := filepath.Join(home, ".claude", "skills")
	if err := os.MkdirAll(claudeSkills, 0o755); err != nil {
		t.Fatal(err)
	}
	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)
	}
	skillDir := filepath.Join(codexHome, "skills", "shared")
	if err := os.MkdirAll(skillDir, 0o755); err != nil {
		t.Fatal(err)
	}

	called := false
	old := runExec
	runExec = func(spec runner.ExecSpec) runner.Result {
		called = true
		return runner.Result{Program: spec.Program, Args: spec.Args, Cwd: spec.Cwd, ExitCode: 0}
	}
	t.Cleanup(func() { runExec = old })

	m := appModel{
		cwd: cwd,
		result: model.ScanResult{Skills: []*model.Skill{{
			Name:  "shared",
			Scope: model.ScopeGlobal,
			ObservedPaths: []model.ObservedPath{{
				Path: skillDir, Scope: model.ScopeGlobal, Agent: "codex", Status: model.StatusCopy, SharedRoot: false,
			}},
		}}},
	}
	action := actions.CommandPreview{
		ID:           "remove",
		ConfirmValue: "shared",
		Mutates:      true,
		Exec:         actions.ExecSpec{Program: "skills", Args: []string{"remove", "shared", "--yes", "-g"}},
	}
	resModel, cmd := m.executeAction(action)
	if cmd != nil {
		// Synchronous refusal should not schedule a command.
		t.Fatal("expected external remove to be refused before scheduling runExec")
	}
	res := resModel.(appModel)
	if res.actionResult == nil || !strings.Contains(res.actionResult.Err, "symlinked") {
		t.Fatalf("expected shared-root refusal, got %#v", res.actionResult)
	}
	if called {
		t.Fatal("runExec must not run after live unsafe path detection")
	}
}

// TestLiveUnsafePathBlocksBulkExecutionClosure ensures bulk execution validates
// upfront and does not run any batch command when a live unsafe path is found.
func TestLiveUnsafePathBlocksBulkExecutionClosure(t *testing.T) {
	home := t.TempDir()
	cwd := t.TempDir()
	t.Setenv("HOME", home)

	claudeSkills := filepath.Join(home, ".claude", "skills")
	if err := os.MkdirAll(claudeSkills, 0o755); err != nil {
		t.Fatal(err)
	}
	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)
	}
	skillDir := filepath.Join(codexHome, "skills", "shared")
	if err := os.MkdirAll(skillDir, 0o755); err != nil {
		t.Fatal(err)
	}

	calls := 0
	old := runExec
	runExec = func(spec runner.ExecSpec) runner.Result {
		calls++
		return runner.Result{Program: spec.Program, Args: spec.Args, Cwd: spec.Cwd, ExitCode: 0}
	}
	t.Cleanup(func() { runExec = old })

	m := appModel{
		cwd: cwd,
		result: model.ScanResult{Skills: []*model.Skill{{
			Name:  "shared",
			Scope: model.ScopeGlobal,
			ObservedPaths: []model.ObservedPath{{
				Path: skillDir, Scope: model.ScopeGlobal, Agent: "codex", Status: model.StatusCopy,
			}},
		}}},
	}
	batch := []actions.ExecSpec{{Program: "skills", Args: []string{"remove", "shared", "--yes", "-g"}}}
	result, partial := m.runBatch("bulk_remove", batch)
	if result.ExitCode == 0 || !strings.Contains(result.Err, "symlinked") {
		t.Fatalf("expected bulk upfront refusal, got %#v", result)
	}
	if partial {
		t.Fatal("upfront refusal must not count as partial success")
	}
	if calls != 0 {
		t.Fatalf("expected no batch commands to run, calls=%d", calls)
	}
}

func TestTUIPreviewRenderInFlightSuppression(t *testing.T) {
	m := appModel{
		width:            120,
		height:           32,
		selected:         1,
		previewRendering: true,
		previewCache:     make(map[previewCacheKey][]string),
		result: model.ScanResult{
			Skills: []*model.Skill{
				{
					Name:      "TestSkill",
					Scope:     model.ScopeProject,
					Preview:   "some preview text",
					LocalLock: &model.LocalLockEntry{Source: "owner/repo"},
				},
			},
		},
	}
	m.syncViewport()

	cmd := m.dispatchPreviewRender()
	if cmd != nil {
		t.Fatalf("expected dispatchPreviewRender to return nil when previewRendering is true, got %v", cmd)
	}

	m.previewRendering = false
	cmd = m.dispatchPreviewRender()
	if cmd == nil {
		t.Fatalf("expected dispatchPreviewRender to return a cmd when previewRendering is false")
	}
}

func TestTUIPreviewRenderUsesSkillModalWidth(t *testing.T) {
	m := appModel{
		width:        120,
		height:       32,
		selected:     1,
		detailModal:  true,
		modalSource:  "",
		previewCache: make(map[previewCacheKey][]string),
		result: model.ScanResult{
			Skills: []*model.Skill{
				{
					Name:      "TestSkill",
					Scope:     model.ScopeProject,
					Preview:   "some preview text",
					LocalLock: &model.LocalLockEntry{Source: "owner/repo"},
				},
			},
		},
	}

	cmd := m.dispatchPreviewRender()
	if cmd == nil {
		t.Fatal("expected skill detail modal to dispatch preview render on cache miss")
	}

	msg, ok := cmd().(previewRenderedMsg)
	if !ok {
		t.Fatalf("expected previewRenderedMsg, got %T", msg)
	}

	modalWidth, _ := detailModalDimensions(newAppLayout(m.width, m.height))
	wantWidth := max(1, modalWidth-4)
	if msg.width != wantWidth {
		t.Fatalf("expected modal preview width %d, got %d", wantWidth, msg.width)
	}

	_, rightWidth, _, _ := m.getThreePaneLayout()
	if msg.width == max(1, rightWidth-4) {
		t.Fatalf("expected modal width, got three-pane preview width %d", msg.width)
	}
}

func TestTUIWindowResizeDispatchesPreviewRender(t *testing.T) {
	m := appModel{
		width:        100,
		height:       30,
		selected:     1,
		previewCache: make(map[previewCacheKey][]string),
		result: model.ScanResult{
			Skills: []*model.Skill{
				{
					Name:      "TestSkill",
					Scope:     model.ScopeProject,
					Preview:   "some preview text",
					LocalLock: &model.LocalLockEntry{Source: "owner/repo"},
				},
			},
		},
	}

	updated, cmd := m.Update(tea.WindowSizeMsg{Width: 120, Height: 32})
	next := updated.(appModel)
	if cmd == nil {
		t.Fatal("expected window resize to dispatch preview render on cache miss")
	}
	if !next.previewRendering {
		t.Fatal("expected window resize dispatch to mark preview rendering")
	}
	if next.previewRenderingGeneration != next.previewGeneration {
		t.Fatalf("expected rendering generation %d, got %d", next.previewGeneration, next.previewRenderingGeneration)
	}
}

func TestPreviewRendererCacheBoundedTo12(t *testing.T) {
	previewRenderersMu.Lock()
	// Back up and restore previewRenderers map
	backup := previewRenderers
	// Reset package-level map
	previewRenderers = make(map[int]*glamour.TermRenderer)
	previewRenderersMu.Unlock()

	defer func() {
		previewRenderersMu.Lock()
		previewRenderers = backup
		previewRenderersMu.Unlock()
	}()

	// Request 12 different widths to fill the cache up to 12
	for i := 10; i < 22; i++ {
		renderer := previewRenderer(i)
		if renderer == nil {
			t.Fatalf("expected renderer to be created for width %d", i)
		}
	}

	previewRenderersMu.Lock()
	lengthBefore := len(previewRenderers)
	previewRenderersMu.Unlock()

	if lengthBefore != 12 {
		t.Fatalf("expected exactly 12 renderers in cache, got %d", lengthBefore)
	}

	// Requesting an existing width (cache hit) must not evict or change cache size
	renderer := previewRenderer(15)
	if renderer == nil {
		t.Fatal("expected renderer to not be nil for existing width")
	}

	previewRenderersMu.Lock()
	lengthAfterRefetch := len(previewRenderers)
	previewRenderersMu.Unlock()

	if lengthAfterRefetch != 12 {
		t.Fatalf("expected exactly 12 renderers in cache after hit, got %d", lengthAfterRefetch)
	}

	// Now request a new width (cache miss), which must evict one entry and keep the total size bounded to 12
	renderer2 := previewRenderer(30)
	if renderer2 == nil {
		t.Fatal("expected renderer to not be nil for new width")
	}

	previewRenderersMu.Lock()
	lengthAfterMiss := len(previewRenderers)
	previewRenderersMu.Unlock()

	if lengthAfterMiss != 12 {
		t.Fatalf("expected cache size to be capped at 12 after eviction/insert, got %d", lengthAfterMiss)
	}
}

func TestPreviewRendererCacheConcurrency(t *testing.T) {
	previewRenderersMu.Lock()
	backup := previewRenderers
	previewRenderers = make(map[int]*glamour.TermRenderer)
	previewRenderersMu.Unlock()

	defer func() {
		previewRenderersMu.Lock()
		previewRenderers = backup
		previewRenderersMu.Unlock()
	}()

	var wg sync.WaitGroup
	const numGoroutines = 20
	renderers := make([]*glamour.TermRenderer, numGoroutines)
	warmed := previewRenderer(50)
	if warmed == nil {
		t.Fatal("expected warm-up renderer")
	}

	for i := 0; i < numGoroutines; i++ {
		wg.Add(1)
		go func(idx int) {
			defer wg.Done()
			// Fetch the same width concurrently
			renderers[idx] = previewRenderer(50)
		}(i)
	}
	wg.Wait()

	// Verify all returned same non-nil renderer
	first := renderers[0]
	if first == nil {
		t.Fatal("expected non-nil renderer")
	}
	for i, r := range renderers {
		if r != first {
			t.Errorf("renderer at index %d is different: %p vs %p", i, r, first)
		}
	}

	previewRenderersMu.Lock()
	cacheSize := len(previewRenderers)
	previewRenderersMu.Unlock()
	if cacheSize != 1 {
		t.Errorf("expected cache size to be 1, got %d", cacheSize)
	}
}

func TestTUIPreviewRenderStaleCompletion(t *testing.T) {
	m := appModel{
		width:                      120,
		height:                     32,
		selected:                   1,
		previewRendering:           true,
		previewRenderingGeneration: 1,
		previewGeneration:          2,
		previewCache:               make(map[previewCacheKey][]string),
		result: model.ScanResult{
			Skills: []*model.Skill{
				{
					Name:      "TestSkill",
					Scope:     model.ScopeProject,
					Preview:   "some preview text",
					LocalLock: &model.LocalLockEntry{Source: "owner/repo"},
				},
			},
		},
	}
	m.syncViewport()

	_, rightWidth, _, _ := m.getThreePaneLayout()
	expectedWidth := max(1, rightWidth-4)
	currentKey := previewCacheKey{markdown: "some preview text", width: expectedWidth}

	staleMsg := previewRenderedMsg{
		markdown:   "old preview text",
		width:      expectedWidth,
		lines:      []string{"stale lines content"},
		generation: 1,
	}

	updatedModel, cmd := m.Update(staleMsg)
	resM := updatedModel.(appModel)

	staleKey := previewCacheKey{markdown: "old preview text", width: expectedWidth}
	lines, cached := resM.previewCache[staleKey]
	if !cached {
		t.Fatalf("expected stale render result to be cached in previewCache")
	}
	if len(lines) != 1 || lines[0] != "stale lines content" {
		t.Fatalf("expected cached lines to be 'stale lines content', got %v", lines)
	}
	if _, cached := resM.previewCache[currentKey]; cached {
		t.Fatalf("did not expect current preview to be cached by stale completion")
	}

	// The stale completion corresponded to the only in-flight render, so it must
	// free the slot and dispatch a render for the current generation.
	if !resM.previewRendering {
		t.Fatalf("expected previewRendering to remain true for redispatched current render")
	}
	if resM.previewRenderingGeneration != 2 {
		t.Fatalf("expected redispatched render generation 2, got %d", resM.previewRenderingGeneration)
	}
	if cmd == nil {
		t.Fatalf("expected stale completion to dispatch current preview render")
	}

	currentMsg := previewRenderedMsg{
		markdown:   "some preview text",
		width:      expectedWidth,
		lines:      []string{"current lines content"},
		generation: 2,
	}

	updatedModel2, cmd2 := resM.Update(currentMsg)
	resM2 := updatedModel2.(appModel)

	lines2, cached2 := resM2.previewCache[currentKey]
	if !cached2 {
		t.Fatalf("expected current render result to be cached in previewCache")
	}
	if len(lines2) != 1 || lines2[0] != "current lines content" {
		t.Fatalf("expected cached lines to be 'current lines content', got %v", lines2)
	}
	if resM2.previewRendering {
		t.Fatalf("expected previewRendering to be cleared (false) for current-generation preview completion, got true")
	}
	if cmd2 != nil {
		t.Fatalf("expected cmd2 to be nil for current-generation preview completion, got %v", cmd2)
	}
}

func TestTUIPreviewRenderIgnoresCompletionWhenNewerRenderInFlight(t *testing.T) {
	m := appModel{
		width:                      120,
		height:                     32,
		selected:                   1,
		previewRendering:           true,
		previewRenderingGeneration: 2,
		previewGeneration:          2,
		previewCache:               make(map[previewCacheKey][]string),
		result:                     model.ScanResult{Skills: []*model.Skill{{Name: "TestSkill", Scope: model.ScopeProject, Preview: "current preview", LocalLock: &model.LocalLockEntry{Source: "owner/repo"}}}},
	}
	m.syncViewport()
	_, rightWidth, _, _ := m.getThreePaneLayout()
	expectedWidth := max(1, rightWidth-4)

	updatedModel, cmd := m.Update(previewRenderedMsg{markdown: "old preview", width: expectedWidth, lines: []string{"old"}, generation: 1})
	resM := updatedModel.(appModel)
	if !resM.previewRendering || resM.previewRenderingGeneration != 2 {
		t.Fatalf("expected newer in-flight render to remain active, rendering=%t generation=%d", resM.previewRendering, resM.previewRenderingGeneration)
	}
	if cmd != nil {
		t.Fatalf("expected no duplicate dispatch while newer render remains in flight, got %v", cmd)
	}
}

// TestMoveRejectsSymlinkedNestedShelfParent ensures a .lazyskills-disabled
// parent that is a symlink is refused and canonical shelf content is untouched.
func TestMoveRejectsSymlinkedNestedShelfParent(t *testing.T) {
	home := t.TempDir()
	cwd := t.TempDir()
	t.Setenv("HOME", home)

	claudeSkills := filepath.Join(home, ".claude", "skills")
	if err := os.MkdirAll(claudeSkills, 0o755); err != nil {
		t.Fatal(err)
	}
	skillDir := filepath.Join(claudeSkills, "shelved-skill")
	if err := os.MkdirAll(skillDir, 0o755); err != nil {
		t.Fatal(err)
	}
	if err := os.WriteFile(filepath.Join(skillDir, "MARKER"), []byte("src"), 0o644); err != nil {
		t.Fatal(err)
	}

	// Destination parent chain: scope root is real, but .lazyskills-disabled
	// is a symlink into a canonical shelf elsewhere.
	canonicalShelf := filepath.Join(home, "other-agent-disabled")
	if err := os.MkdirAll(canonicalShelf, 0o755); err != nil {
		t.Fatal(err)
	}
	if err := os.WriteFile(filepath.Join(canonicalShelf, "KEEP"), []byte("canonical"), 0o644); err != nil {
		t.Fatal(err)
	}
	if err := os.Symlink(canonicalShelf, filepath.Join(claudeSkills, ".lazyskills-disabled")); err != nil {
		t.Fatal(err)
	}

	m := appModel{cwd: cwd}
	action := actions.CommandPreview{
		ID:   "disable_skill",
		Exec: actions.ExecSpec{Internal: "disable_skill", Args: []string{skillDir}},
	}
	resModel, cmd := m.executeAction(action)
	if cmd != nil {
		t.Fatal("expected move to refuse symlinked nested shelf parent")
	}
	res := resModel.(appModel)
	if res.actionResult == nil || !strings.Contains(res.actionResult.Err, "symlinked") {
		t.Fatalf("expected symlinked refusal, got %#v", res.actionResult)
	}
	if data, err := os.ReadFile(filepath.Join(skillDir, "MARKER")); err != nil || string(data) != "src" {
		t.Fatalf("source must remain, data=%q err=%v", data, err)
	}
	if data, err := os.ReadFile(filepath.Join(canonicalShelf, "KEEP")); err != nil || string(data) != "canonical" {
		t.Fatalf("canonical shelf must remain untouched, data=%q err=%v", data, err)
	}
	// No skill entry should have been created under the aliased shelf.
	if _, err := os.Stat(filepath.Join(canonicalShelf, "shelved-skill")); !os.IsNotExist(err) {
		t.Fatal("expected no skill entry under canonical aliased shelf")
	}
}

// TestMoveRejectsTopologySwapBeforeMkdirAll swaps the destination scope root
// to a symlink after preflight has passed, on the first execution-time check
// (before MkdirAll). MkdirAll must not create directories under the canonical
// target, and the rename must not run.
func TestMoveRejectsTopologySwapBeforeMkdirAll(t *testing.T) {
	home := t.TempDir()
	cwd := t.TempDir()
	t.Setenv("HOME", home)

	claudeSkills := filepath.Join(home, ".claude", "skills")
	if err := os.MkdirAll(claudeSkills, 0o755); err != nil {
		t.Fatal(err)
	}
	// Canonical tree that must not receive mkdir/rename side effects.
	if err := os.WriteFile(filepath.Join(claudeSkills, "CANONICAL"), []byte("keep"), 0o644); err != nil {
		t.Fatal(err)
	}

	codexSkills := filepath.Join(home, ".codex", "skills")
	if err := os.MkdirAll(codexSkills, 0o755); err != nil {
		t.Fatal(err)
	}
	skillDir := filepath.Join(codexSkills, "move-me")
	if err := os.MkdirAll(skillDir, 0o755); err != nil {
		t.Fatal(err)
	}
	if err := os.WriteFile(filepath.Join(skillDir, "MARKER"), []byte("codex"), 0o644); err != nil {
		t.Fatal(err)
	}

	// Preflight sees a safe owned root. On the first execution-time
	// checkDestructivePath call (before MkdirAll), replace codex skills with
	// a symlink into claude's canonical skills.
	calls := 0
	old := checkDestructivePath
	checkDestructivePath = func(path, checkCwd string) error {
		calls++
		// Preflight: src + dest = 2 calls. Third call is first execution check.
		if calls == 3 {
			if err := os.RemoveAll(codexSkills); err != nil {
				t.Fatalf("swap remove: %v", err)
			}
			if err := os.Symlink(claudeSkills, codexSkills); err != nil {
				t.Fatalf("swap symlink: %v", err)
			}
		}
		return old(path, checkCwd)
	}
	t.Cleanup(func() { checkDestructivePath = old })

	m := appModel{cwd: cwd}
	action := actions.CommandPreview{
		ID:   "disable_skill",
		Exec: actions.ExecSpec{Internal: "disable_skill", Args: []string{skillDir}},
	}
	resModel, cmd := m.executeAction(action)
	if cmd != nil {
		t.Fatal("expected topology-swap guard to refuse before/without successful move")
	}
	res := resModel.(appModel)
	if res.actionResult == nil || !strings.Contains(res.actionResult.Err, "symlinked") {
		t.Fatalf("expected symlinked refusal after topology swap, got %#v", res.actionResult)
	}
	// Canonical tree must not have gained a disabled shelf from MkdirAll.
	if _, err := os.Stat(filepath.Join(claudeSkills, ".lazyskills-disabled")); !os.IsNotExist(err) {
		t.Fatal("MkdirAll must not create .lazyskills-disabled under the canonical target after topology swap")
	}
	if data, err := os.ReadFile(filepath.Join(claudeSkills, "CANONICAL")); err != nil || string(data) != "keep" {
		t.Fatalf("canonical marker must remain, data=%q err=%v", data, err)
	}
}

// TestExternalRemoveValidatesUnobservedAliasedRoot covers: only one observed
// safe root exists in scan results, but an unobserved agent root that the
// skills CLI would still target becomes an alias before execution — runExec
// must not be invoked.
func TestExternalRemoveValidatesUnobservedAliasedRoot(t *testing.T) {
	home := t.TempDir()
	cwd := t.TempDir()
	t.Setenv("HOME", home)

	claudeSkills := filepath.Join(home, ".claude", "skills")
	skillDir := filepath.Join(claudeSkills, "only-claude")
	if err := os.MkdirAll(skillDir, 0o755); err != nil {
		t.Fatal(err)
	}
	if err := os.WriteFile(filepath.Join(skillDir, "MARKER"), []byte("safe"), 0o644); err != nil {
		t.Fatal(err)
	}
	// Codex root starts as a real empty directory (unobserved — skill not there).
	codexSkills := filepath.Join(home, ".codex", "skills")
	if err := os.MkdirAll(codexSkills, 0o755); err != nil {
		t.Fatal(err)
	}

	// After "scan": alias the unobserved codex root into claude's tree.
	if err := os.RemoveAll(codexSkills); err != nil {
		t.Fatal(err)
	}
	if err := os.Symlink(claudeSkills, codexSkills); err != nil {
		t.Fatal(err)
	}

	called := false
	old := runExec
	runExec = func(spec runner.ExecSpec) runner.Result {
		called = true
		return runner.Result{Program: spec.Program, Args: spec.Args, Cwd: spec.Cwd, ExitCode: 0}
	}
	t.Cleanup(func() { runExec = old })

	m := appModel{
		cwd: cwd,
		// Stale observations: only the safe claude path is known.
		result: model.ScanResult{Skills: []*model.Skill{{
			Name:  "only-claude",
			Scope: model.ScopeGlobal,
			ObservedPaths: []model.ObservedPath{{
				Path: skillDir, Scope: model.ScopeGlobal, Agent: "claude-code", Status: model.StatusCopy, SharedRoot: false,
			}},
		}}},
	}
	action := actions.CommandPreview{
		ID:           "remove",
		ConfirmValue: "only-claude",
		Mutates:      true,
		Exec:         actions.ExecSpec{Program: "skills", Args: []string{"remove", "only-claude", "--yes", "-g"}},
	}
	resModel, cmd := m.executeAction(action)
	if cmd != nil {
		t.Fatal("expected remove to refuse due to unobserved aliased agent root")
	}
	res := resModel.(appModel)
	if res.actionResult == nil || !strings.Contains(res.actionResult.Err, "symlinked") {
		t.Fatalf("expected shared-root refusal from unobserved aliased root, got %#v", res.actionResult)
	}
	if called {
		t.Fatal("runExec must not run when an unobserved targeted root is live-unsafe")
	}
	if data, err := os.ReadFile(filepath.Join(skillDir, "MARKER")); err != nil || string(data) != "safe" {
		t.Fatalf("canonical skill must remain, data=%q err=%v", data, err)
	}
}

// TestBrokenLinkDeleteRejectsSymlinkedNestedParent ensures broken-link
// deletion refuses when a parent below the scope root is a symlink.
func TestBrokenLinkDeleteRejectsSymlinkedNestedParent(t *testing.T) {
	home := t.TempDir()
	cwd := t.TempDir()
	t.Setenv("HOME", home)

	claudeSkills := filepath.Join(home, ".claude", "skills")
	if err := os.MkdirAll(claudeSkills, 0o755); err != nil {
		t.Fatal(err)
	}
	// Nested parent becomes a symlink to a canonical directory holding a
	// broken link name that must not be deleted through the alias.
	canonicalNest := filepath.Join(home, "canonical-nest")
	if err := os.MkdirAll(canonicalNest, 0o755); err != nil {
		t.Fatal(err)
	}
	brokenInCanonical := filepath.Join(canonicalNest, "broken-link")
	if err := os.Symlink(filepath.Join(home, "missing-target"), brokenInCanonical); err != nil {
		t.Fatal(err)
	}
	nestLink := filepath.Join(claudeSkills, "nested")
	if err := os.Symlink(canonicalNest, nestLink); err != nil {
		t.Fatal(err)
	}
	// Path as observed through the owned scope root.
	observedBroken := filepath.Join(nestLink, "broken-link")

	m := appModel{
		cwd: cwd,
		result: model.ScanResult{Skills: []*model.Skill{{
			Name:  "Broken",
			Scope: model.ScopeGlobal,
			ObservedPaths: []model.ObservedPath{{
				Path: observedBroken, Scope: model.ScopeGlobal, Agent: "claude-code", Status: model.StatusBrokenSymlink, SharedRoot: false,
			}},
		}}},
	}
	action := actions.CommandPreview{
		ID:           "delete_broken_symlink",
		ConfirmValue: "Broken",
		Exec:         actions.ExecSpec{Internal: "delete_broken_symlink", Args: []string{string(model.ScopeGlobal), "Broken"}},
	}
	updated, cmd := m.executeAction(action)
	next := updated.(appModel)
	if next.actionResult == nil || next.actionResult.ExitCode != -1 {
		t.Fatalf("expected failed delete due to symlinked nested parent, got %#v", next.actionResult)
	}
	if !strings.Contains(next.actionResult.Err, "symlinked") {
		t.Fatalf("expected symlinked reason, got %#v", next.actionResult)
	}
	// Broken link under canonical nest must remain.
	if _, err := os.Lstat(brokenInCanonical); err != nil {
		t.Fatalf("expected canonical broken link to remain, err=%v", err)
	}
	// cmd may still rescan on failure paths; either way the link stays.
	if cmd != nil {
		_, _ = next.Update(cmd())
	}
	if _, err := os.Lstat(brokenInCanonical); err != nil {
		t.Fatalf("expected canonical broken link to remain after handler, err=%v", err)
	}
}