Files
skillshare/internal/server/handler_update_branch_test.go
T
Willieandsim 5e90cf4b71 feat(ui): show a skill's branch and switch it from the detail page (#784)
* feat(ui): show a skill's branch and switch it from the detail page

A skill installed with --branch looked the same in the dashboard as one
from the default branch, and moving it to another branch meant a manual
reinstall from the CLI (#783).

The list, tiles, tree pane and Updates tab now tag the branch a skill was
installed from. The detail page lists the source's branches; picking one
confirms, then reinstalls through POST /api/update with a new `branch`
field, so the audit gate and the local-changes check still apply and a
branch without the skill leaves the install in place.

In project mode, reconciling config.yaml only added missing skills, so a
reinstall from another branch (the switch, or install --branch --force)
left config.yaml naming the old branch while the lock pinned the new
commit. Reconcile now moves the declared branch with the record.

Closes #783

* fix(update): make branch switches install the requested branch safely

Codex review of #784 found four ways a switch could misreport or lose work:

- A root skill installed as a full clone took the pull fast path on later
  switches, pulling its current branch instead of the requested one. The fast
  path now applies only when the checkout is already on that branch.
- A branch whose skill folder lacks SKILL.md replaced the skill with a plain
  folder. A staged update now refuses a candidate without SKILL.md when the
  installed copy has one, and keeps the install.
- Reconciling config.yaml from local metadata could rewrite a teammate's
  newer branch declaration back to a stale one on an unrelated install. The
  generic reconcile change is reverted; the switch updates only the switched
  skill's declaration.
- The config editor cache kept the old branch after a switch, so a later save
  could undo it. A successful switch now invalidates the config query.

---------

Co-authored-by: sim <sim@example.invalid>
2026-11-05 22:44:03 +08:00

130 lines
3.9 KiB
Go

package server
import (
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"
"skillshare/internal/config"
"skillshare/internal/install"
)
// installFromDev installs skills/foo into src from the dev branch of a remote
// whose main branch has its own skills/foo, and returns the recorded source.
func installFromDev(t *testing.T, s *Server, src string) string {
t.Helper()
remote := t.TempDir()
initGitRepo(t, remote)
runGit(t, remote, "branch", "-M", "main")
writeSkill := func(body string) {
dir := filepath.Join(remote, "skills", "foo")
if err := os.MkdirAll(dir, 0755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(dir, "SKILL.md"), []byte("---\nname: foo\n---\n"+body+"\n"), 0644); err != nil {
t.Fatal(err)
}
runGit(t, remote, "add", ".")
runGit(t, remote, "commit", "-m", body)
}
writeSkill("main body")
runGit(t, remote, "checkout", "-b", "dev")
writeSkill("dev body")
runGit(t, remote, "checkout", "main")
local := filepath.Join(src, "foo")
if err := os.MkdirAll(local, 0755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(local, "SKILL.md"), []byte("---\nname: foo\n---\ndev body\n"), 0644); err != nil {
t.Fatal(err)
}
source := "file://" + remote + "//skills/foo"
s.skillsStore = install.NewMetadataStore()
s.skillsStore.Set("foo", &install.MetadataEntry{
Source: source,
RepoURL: "file://" + remote,
Subdir: "skills/foo",
Branch: "dev",
})
if err := s.skillsStore.Save(src); err != nil {
t.Fatal(err)
}
return source
}
func postUpdate(s *Server, body string) *httptest.ResponseRecorder {
rr := httptest.NewRecorder()
s.handleUpdate(rr, httptest.NewRequest(http.MethodPost, "/api/update", strings.NewReader(body)))
return rr
}
func TestHandleUpdate_BranchReinstallsFromThatBranch(t *testing.T) {
s, src := newTestServer(t)
installFromDev(t, s, src)
results := updateResults(t, postUpdate(s, `{"name":"foo","branch":"main"}`), false)
if len(results) != 1 || results[0].Action != "updated" {
t.Fatalf("results = %+v, want one updated", results)
}
got, _ := os.ReadFile(filepath.Join(src, "foo", "SKILL.md"))
stored, err := install.LoadMetadata(src)
if err != nil {
t.Fatal(err)
}
if !strings.Contains(string(got), "main body") || stored.GetByPath("foo").Branch != "main" {
t.Fatalf("SKILL.md = %q, recorded branch = %q; want main's content and branch", got, stored.GetByPath("foo").Branch)
}
}
// Only the switched skill's declaration moves; another skill whose local record
// lags the shared config keeps what config.yaml says.
func TestHandleUpdate_BranchMovesOnlyThatProjectDeclaration(t *testing.T) {
s, root := newTestProjectServerWithExtras(t, nil)
src := s.cfg.EffectiveSkillsSource()
source := installFromDev(t, s, src)
s.skillsStore.Set("bar", &install.MetadataEntry{Source: "github.com/o/r/bar", Branch: "old"})
if err := s.skillsStore.Save(src); err != nil {
t.Fatal(err)
}
if err := os.MkdirAll(filepath.Join(src, "bar"), 0755); err != nil {
t.Fatal(err)
}
s.projectCfg.Skills = []config.SkillEntry{
{Name: "foo", Source: source, Branch: "dev"},
{Name: "bar", Source: "github.com/o/r/bar", Branch: "new"},
}
if err := s.projectCfg.Save(root); err != nil {
t.Fatal(err)
}
updateResults(t, postUpdate(s, `{"name":"foo","branch":"main"}`), false)
loaded, err := config.LoadProject(root)
if err != nil {
t.Fatal(err)
}
got := map[string]string{}
for _, skill := range loaded.Skills {
got[skill.Name] = skill.Branch
}
if got["foo"] != "main" || got["bar"] != "new" {
t.Fatalf("declared branches = %v, want foo on main and bar left on new", got)
}
}
func TestHandleUpdate_BranchNeedsOneSkill(t *testing.T) {
s, _ := newTestServer(t)
for _, body := range []string{
`{"all":true,"branch":"main"}`,
`{"name":"foo","kind":"agent","branch":"main"}`,
} {
if rr := postUpdate(s, body); rr.Code != http.StatusBadRequest {
t.Errorf("%s: status = %d, want 400", body, rr.Code)
}
}
}