Files
skillshare/internal/server/handler_update_test.go
T
Willie 2edf7649fd refactor(update): update tracked repos through one shared operation
Updating a tracked repo is a fail-closed sequence: check for uncommitted
changes, pull, audit what was pulled, reset to the pre-pull commit when the
audit blocks. The CLI had two copies of the pull flow, the dashboard a third,
and the audit gate existed four times (CLI, dashboard, and two in install).
The copies had drifted: the dashboard ignored a failed git status and never
refreshed root-skill metadata, and each copy worded a failed rollback on its
own.

update.TrackedRepo now owns the sequence and returns a structured result;
install.AuditGate is the only gate. internal/update is a new package because
internal/git already imports internal/install. The CLI keeps its spinner,
rows and the "Apply anyway?" prompt, passed in as one Confirm callback; the
dashboard turns the result into its JSON item.

Behaviour changes, all towards the stricter copy:
- the dashboard reports a failed git status as an error instead of pulling;
- the dashboard refreshes root-skill metadata after an update, like the CLI;
- a prompt that fails rolls the update back instead of leaving it applied;
- the rollback-failed and rollback-unavailable texts come from one place.

Accepted findings stay as they were (CLI honours and records them, the
dashboard does neither), now as the explicit AcceptedFindings input. The
"blocked and rolled back" texts are unchanged per caller because the
dashboard matches on them.

Refs #443
2026-10-07 00:10:54 +08:00

247 lines
8.3 KiB
Go

package server
import (
"net/http"
"net/http/httptest"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
"skillshare/internal/install"
)
// initGitRepo creates a minimal git repo with an initial commit.
func initGitRepo(t *testing.T, dir string) {
t.Helper()
for _, args := range [][]string{
{"init"},
{"config", "user.email", "test@test.com"},
{"config", "user.name", "Test"},
} {
cmd := exec.Command("git", args...)
cmd.Dir = dir
if out, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("git %v failed: %s %v", args, out, err)
}
}
// Create initial commit
f := filepath.Join(dir, "README.md")
os.WriteFile(f, []byte("# init"), 0644)
for _, args := range [][]string{
{"add", "."},
{"commit", "-m", "init"},
} {
cmd := exec.Command("git", args...)
cmd.Dir = dir
if out, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("git %v failed: %s %v", args, out, err)
}
}
}
// TestMissingTrackedRepos_ReportedFromMetadata verifies the server detects tracked
// repos declared in metadata whose clone dirs are absent (issue #212).
func TestMissingTrackedRepos_ReportedFromMetadata(t *testing.T) {
s, src := newTestServer(t)
store := install.LoadMetadataOrNew(src)
store.Set("_team-skills", &install.MetadataEntry{
Source: "https://github.com/example/team-skills",
Branch: "main",
Tracked: true,
})
if err := store.Save(src); err != nil {
t.Fatalf("save metadata: %v", err)
}
missing := s.missingTrackedRepos()
if len(missing) != 1 {
t.Fatalf("expected 1 missing repo, got %d (%+v)", len(missing), missing)
}
if missing[0].Name != "_team-skills" || missing[0].Branch != "main" {
t.Fatalf("unexpected missing repo: %+v", missing[0])
}
}
// TestHandleRehydrate_NoMissing_OK verifies the rehydrate endpoint succeeds with
// an empty result set when nothing is missing.
func TestHandleRehydrate_NoMissing_OK(t *testing.T) {
s, _ := newTestServer(t)
req := httptest.NewRequest(http.MethodPost, "/api/update/rehydrate", nil)
rr := httptest.NewRecorder()
s.handleRehydrateTrackedRepos(rr, req)
if rr.Code != http.StatusOK {
t.Fatalf("expected 200, got %d: %s", rr.Code, rr.Body.String())
}
if !strings.Contains(rr.Body.String(), "results") {
t.Fatalf("expected results field, got %s", rr.Body.String())
}
}
func TestUpdateSingle_NestedTrackedRepo_Resolves(t *testing.T) {
s, src := newTestServer(t)
// Create a nested tracked repo with a real git init
repoDir := filepath.Join(src, "org", "_team-skills")
os.MkdirAll(repoDir, 0755)
initGitRepo(t, repoDir)
result := s.updateSingle("org/_team-skills", false, true)
// Should resolve (may be up-to-date or updated, but NOT "error"/"not found")
if result.Action == "error" && strings.Contains(result.Message, "not found") {
t.Fatalf("updateSingle failed to resolve nested repo: %s", result.Message)
}
}
func TestUpdateSingle_BasenameFallback(t *testing.T) {
s, src := newTestServer(t)
// Create a top-level tracked repo with _ prefix
repoDir := filepath.Join(src, "_team-skills")
os.MkdirAll(repoDir, 0755)
initGitRepo(t, repoDir)
// Call with unprefixed basename — should resolve to _team-skills
result := s.updateSingle("team-skills", false, true)
if result.Action == "error" && strings.Contains(result.Message, "not found") {
t.Fatalf("updateSingle failed to resolve basename: %s", result.Message)
}
if result.Name != "_team-skills" && result.Name != "team-skills" {
// Name should reflect the resolved repo
t.Logf("resolved name: %s, action: %s", result.Name, result.Action)
}
}
func TestUpdateSingle_NotFound(t *testing.T) {
s, _ := newTestServer(t)
result := s.updateSingle("nonexistent-repo", false, true)
if result.Action != "error" {
t.Fatalf("expected action=error for not-found, got %q", result.Action)
}
if !strings.Contains(result.Message, "not found") && !strings.Contains(result.Message, "no update source") {
t.Fatalf("expected not-found message, got %q", result.Message)
}
}
func TestUpdateRegularSkill_NestedSkillRefreshesRootMetadataStore(t *testing.T) {
s, src := newTestServer(t)
remote := t.TempDir()
initGitRepo(t, remote)
remoteSkill := filepath.Join(remote, "skills", "agent-browser")
if err := os.MkdirAll(remoteSkill, 0755); err != nil {
t.Fatalf("create remote skill: %v", err)
}
if err := os.WriteFile(filepath.Join(remoteSkill, "SKILL.md"), []byte("---\nname: agent-browser\n---\n# Agent Browser\n"), 0644); err != nil {
t.Fatalf("write remote skill: %v", err)
}
runGit(t, remote, "add", ".")
runGit(t, remote, "commit", "-m", "add skill")
latestCommit := strings.TrimSpace(string(runGit(t, remote, "rev-parse", "--short", "HEAD")))
localSkill := filepath.Join(src, "tools", "agent-browser")
if err := os.MkdirAll(localSkill, 0755); err != nil {
t.Fatalf("create local skill: %v", err)
}
if err := os.WriteFile(filepath.Join(localSkill, "SKILL.md"), []byte("---\nname: agent-browser\n---\n# Old\n"), 0644); err != nil {
t.Fatalf("write local skill: %v", err)
}
store := install.NewMetadataStore()
store.Set("tools/agent-browser", &install.MetadataEntry{
Source: "file://" + remote + "//skills/agent-browser",
RepoURL: "file://" + remote,
Subdir: "skills/agent-browser",
Version: "old-version",
})
if err := store.Save(src); err != nil {
t.Fatalf("save metadata: %v", err)
}
s.skillsStore = store
result := s.updateRegularSkill("tools/agent-browser", localSkill, false, true)
if result.Action != "updated" {
t.Fatalf("expected updated action, got %q: %s", result.Action, result.Message)
}
rootStore, err := install.LoadMetadata(src)
if err != nil {
t.Fatalf("load root metadata: %v", err)
}
entry := rootStore.GetByPath("tools/agent-browser")
if entry == nil {
t.Fatal("expected root metadata entry for nested skill")
}
if entry.Version != latestCommit {
t.Fatalf("root metadata version = %q, want %q", entry.Version, latestCommit)
}
if cached := s.skillsStore.GetByPath("tools/agent-browser"); cached == nil || cached.Version != latestCommit {
t.Fatalf("server metadata cache was not refreshed, got %#v", cached)
}
if _, err := os.Stat(filepath.Join(src, "tools", install.MetadataFileName)); !os.IsNotExist(err) {
t.Fatalf("expected no nested metadata store, stat err=%v", err)
}
}
func runGit(t *testing.T, dir string, args ...string) []byte {
t.Helper()
cmd := exec.Command("git", args...)
cmd.Dir = dir
out, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("git %v failed: %s %v", args, out, err)
}
return out
}
func TestUpdateSingle_RegularSkillPriority(t *testing.T) {
s, src := newTestServer(t)
// Create both a regular skill with meta AND a tracked repo with _ prefix
skillDir := filepath.Join(src, "my-skill")
os.MkdirAll(skillDir, 0755)
os.WriteFile(filepath.Join(skillDir, "SKILL.md"), []byte("---\nname: my-skill\n---\n# Skill"), 0644)
addSkillMeta(t, src, "my-skill", "https://github.com/example/my-skill")
repoDir := filepath.Join(src, "_my-skill")
os.MkdirAll(repoDir, 0755)
initGitRepo(t, repoDir)
// updateSingle("my-skill") should try regular skill first (has meta).
// It will fail to clone (fake URL) but the point is it chose the skill path,
// not the tracked repo path. A "not found" would mean it tried the repo instead.
result := s.updateSingle("my-skill", false, true)
// The error should be about cloning (skill path), not "not found" (repo path)
if result.Action == "error" && strings.Contains(result.Message, "not found") && !strings.Contains(result.Message, "clone") {
t.Fatalf("expected regular skill to be tried first, got repo-like not-found: %s", result.Message)
}
}
func TestUpdateAgent_AzureOnPrem_UsesParseOpts(t *testing.T) {
s, _ := newTestServer(t)
s.cfg.AzureHosts = []string{"azuredevops.corp.com"}
// Verify that server.parseOpts() + ParseSourceWithOptions produces
// the correct CloneURL for an on-prem Azure agent source.
// This is the exact parse path used inside updateAgent.
source, err := install.ParseSourceWithOptions(
"https://azuredevops.corp.com/Org/Project/_git/Repo/agents/my-agent.md",
s.parseOpts(),
)
if err != nil {
t.Fatalf("ParseSourceWithOptions error: %v", err)
}
wantURL := "https://azuredevops.corp.com/Org/Project/_git/Repo"
if source.CloneURL != wantURL {
t.Errorf("CloneURL = %q, want %q", source.CloneURL, wantURL)
}
if source.Subdir != "agents/my-agent.md" {
t.Errorf("Subdir = %q, want %q", source.Subdir, "agents/my-agent.md")
}
}