Files
skillshare/internal/server/handler_hub_refs_test.go
T
Willie 13a93242f8 feat(ui): redesign the Hubs page around one list with in-place editing
Previewing a hub from the builder landed on an empty "pick a hub" state:
the builder and the browser kept separate copies of the drafts and the
browser's copy went stale. Merging both into one page removes the split,
so your own hub is shown exactly as others will see it and edited in place.

Hub entries can now be added from any repository URL, not only from
installed skills, with a branch or tag picked from the remote. The version
is written into the source URL, the only place the hub format keeps it,
and a new /api/hub/refs endpoint lists refs and rewrites sources so the UI
never builds tree/<ref> URLs itself. Skills added from installed ones keep
the version they were installed from.

Draft reads and saves read refs from the URLs only, so opening a hub never
waits on a remote. Added hubs get unique labels, since the default hub is
stored by label.
2026-09-28 23:39:19 +08:00

88 lines
2.8 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)
}
}
// 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)
}
}