Files
skillshare/internal/server/handler_hub_refs_test.go
T
Willieandsim 2c41fa57cc fix(ui): let any git source switch branch on the skill detail page (#790)
A skill installed from a non-default branch of an SSH or other-host
remote (git@host:o/r.git, Gitea, self-hosted HTTPS) could not switch
back to main: the Branch picker was greyed out with "This source can
only use its default branch" (#787). The picker reused /api/hub/refs,
which only lists refs for GitHub, GitLab and Bitbucket web URLs because
a hub entry pins its ref in the URL. Reinstalling from a branch does
not need that; the update endpoint already accepts any git source.

/api/hub/refs now takes anyGit, which lists a non-pinnable git source's
refs while still reporting pinnable=false. The detail page asks for it
under its own query key; the hub editors keep the old request, so they
run no extra ls-remote for sources they cannot pin.

Closes #787

Co-authored-by: sim <sim@example.invalid>
2026-11-06 21:19:50 +08:00

107 lines
3.5 KiB
Go

package server
import (
"bytes"
"encoding/json"
"maps"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"testing"
)
func postHubRefs(t *testing.T, s *Server, body string) *httptest.ResponseRecorder {
t.Helper()
rr := httptest.NewRecorder()
s.handler.ServeHTTP(rr, httptest.NewRequest("POST", "/api/hub/refs", bytes.NewBufferString(body)))
return rr
}
func TestHubRefs_RejectsInvalidSource(t *testing.T) {
s, _ := newTestServer(t)
for _, body := range []string{`{}`, `{"source":""}`, `not json`} {
if rr := postHubRefs(t, s, body); rr.Code != http.StatusBadRequest {
t.Errorf("%s: status %d, want 400", body, rr.Code)
}
}
}
func TestHubRefs_NotPinnable(t *testing.T) {
s, _ := newTestServer(t)
rr := postHubRefs(t, s, `{"source":"https://git.example.com/o/r/skills/foo","ref":"v1"}`)
if rr.Code != http.StatusOK {
t.Fatalf("status %d: %s", rr.Code, rr.Body.String())
}
want := `{"pinnable":false,"current":"","defaultBranch":"","branches":[],"tags":[]}`
if got := bytes.TrimSpace(rr.Body.Bytes()); string(got) != want {
t.Errorf("body = %s, want %s", got, want)
}
}
// A skill installed from an SSH or other-host remote can still switch branch
// from its detail page (issue #787).
func TestHubRefs_AnyGitListsUnpinnableRefs(t *testing.T) {
s, _ := newTestServer(t)
remote := t.TempDir()
initGitRepo(t, remote)
runGit(t, remote, "branch", "-M", "main")
runGit(t, remote, "branch", "dev")
rr := postHubRefs(t, s, `{"source":"file://`+remote+`//skills/foo","anyGit":true}`)
if rr.Code != http.StatusOK {
t.Fatalf("status %d: %s", rr.Code, rr.Body.String())
}
want := `{"pinnable":false,"current":"","defaultBranch":"main","branches":["dev","main"],"tags":[]}`
if got := bytes.TrimSpace(rr.Body.Bytes()); string(got) != want {
t.Errorf("body = %s, want %s", got, want)
}
}
// Draft refs are read from the URLs alone, without asking the remote.
func TestHubDraftRefs(t *testing.T) {
s, _ := newTestServer(t)
rr := httptest.NewRecorder()
body := `{"schemaVersion":1,"skills":[` +
`{"name":"a","source":"github.com/acme/r/tree/v1.0/skills/a"},` +
`{"name":"b","source":"acme/r/skills/b"}]}`
s.handler.ServeHTTP(rr, httptest.NewRequest("POST", "/api/hub/drafts/import", bytes.NewBufferString(body)))
var resp struct {
Draft struct {
Entries []struct {
ID string `json:"id"`
} `json:"entries"`
} `json:"draft"`
Refs map[string]string `json:"refs"`
}
if err := json.Unmarshal(rr.Body.Bytes(), &resp); err != nil || len(resp.Draft.Entries) != 2 {
t.Fatalf("import: %d %s", rr.Code, rr.Body.String())
}
entries := resp.Draft.Entries
want := map[string]string{entries[0].ID: "v1.0"}
if !maps.Equal(resp.Refs, want) {
t.Errorf("refs = %v, want %v", resp.Refs, want)
}
}
func TestHandleSearch_BuiltinResultsCarryRef(t *testing.T) {
s, src := newTestServer(t)
os.MkdirAll(filepath.Join(src, "foo"), 0755)
os.WriteFile(filepath.Join(src, "foo", "SKILL.md"), []byte("---\nname: foo\n---\nFoo."), 0644)
os.WriteFile(filepath.Join(src, ".metadata.json"), []byte(`{"version":1,"entries":{"foo":{"source":"github.com/acme/r/tree/v2.0/skills/foo"}}}`), 0644)
rr := httptest.NewRecorder()
s.handler.ServeHTTP(rr, httptest.NewRequest("GET", "/api/search?q=foo&hub=@builtin", nil))
var resp struct {
Results []struct {
Ref string `json:"ref"`
} `json:"results"`
}
if err := json.Unmarshal(rr.Body.Bytes(), &resp); err != nil || len(resp.Results) != 1 {
t.Fatalf("search: %d %s", rr.Code, rr.Body.String())
}
if resp.Results[0].Ref != "v2.0" {
t.Errorf("ref = %q, want v2.0", resp.Results[0].Ref)
}
}