mirror of
https://github.com/runkids/skillshare.git
synced 2026-11-18 18:04:17 +00:00
The CLI, GET /api/check and the check stream each wrote out the same sequence: group skills by URL and branch, ask the remote hash, skip the tree fetch when every skill matches, otherwise compare tree hashes and assign a status. A fix such as branch handling had to be made three times, and the sequence could only be tested against real repositories. internal/check now owns it as Plan and Resolution.Run, with the remote hash source injected and a per-remote progress callback. The CLI progress bar and the SSE counter are adapters on that callback; the JSON handler passes none. The callers disagreed in two places, and both behaviours are kept: the dashboard reports a skill removed upstream as update_available where the CLI says stale, and gives no source or version for a skill without a remote. The server adapter narrows the result accordingly. Refs #444
107 lines
3.6 KiB
Go
107 lines
3.6 KiB
Go
package server
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"skillshare/internal/config"
|
|
"skillshare/internal/install"
|
|
)
|
|
|
|
func TestHandleCheck_EmptySource(t *testing.T) {
|
|
s, _ := newTestServer(t)
|
|
req := httptest.NewRequest(http.MethodGet, "/api/check", nil)
|
|
rr := httptest.NewRecorder()
|
|
s.handler.ServeHTTP(rr, req)
|
|
|
|
if rr.Code != http.StatusOK {
|
|
t.Fatalf("expected 200, got %d: %s", rr.Code, rr.Body.String())
|
|
}
|
|
|
|
var resp struct {
|
|
TrackedRepos []any `json:"tracked_repos"`
|
|
Skills []any `json:"skills"`
|
|
}
|
|
json.Unmarshal(rr.Body.Bytes(), &resp)
|
|
if len(resp.TrackedRepos) != 0 {
|
|
t.Errorf("expected 0 tracked repos, got %d", len(resp.TrackedRepos))
|
|
}
|
|
if len(resp.Skills) != 0 {
|
|
t.Errorf("expected 0 skills, got %d", len(resp.Skills))
|
|
}
|
|
}
|
|
|
|
// addTrackedRepoWithBrokenIndex clones a tracked repo from a local origin and
|
|
// corrupts its index, so `git status` fails while `git fetch` still succeeds.
|
|
func addTrackedRepoWithBrokenIndex(t *testing.T, sourceDir, name string) string {
|
|
t.Helper()
|
|
seed := t.TempDir()
|
|
initGitRepo(t, seed)
|
|
origin := filepath.Join(t.TempDir(), "origin.git")
|
|
runGit(t, seed, "clone", "--bare", seed, origin)
|
|
runGit(t, sourceDir, "clone", origin, name)
|
|
repoDir := filepath.Join(sourceDir, name)
|
|
// fetch reads the index for submodule recursion unless it is disabled.
|
|
runGit(t, repoDir, "config", "fetch.recurseSubmodules", "false")
|
|
if err := os.WriteFile(filepath.Join(repoDir, ".git", "index"), []byte("corrupt"), 0644); err != nil {
|
|
t.Fatalf("corrupt index: %v", err)
|
|
}
|
|
return repoDir
|
|
}
|
|
|
|
func TestHandleCheck_DirtyCheckErrorReported(t *testing.T) {
|
|
s, src := newTestServer(t)
|
|
addTrackedRepoWithBrokenIndex(t, src, "_team")
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/api/check", nil)
|
|
rr := httptest.NewRecorder()
|
|
s.handler.ServeHTTP(rr, req)
|
|
|
|
if rr.Code != http.StatusOK {
|
|
t.Fatalf("expected 200, got %d: %s", rr.Code, rr.Body.String())
|
|
}
|
|
var resp struct {
|
|
TrackedRepos []repoCheckResult `json:"tracked_repos"`
|
|
}
|
|
if err := json.Unmarshal(rr.Body.Bytes(), &resp); err != nil {
|
|
t.Fatalf("failed to decode response: %v", err)
|
|
}
|
|
if len(resp.TrackedRepos) != 1 || resp.TrackedRepos[0].Status != "error" {
|
|
t.Fatalf("expected one repo with status error, got %+v", resp.TrackedRepos)
|
|
}
|
|
}
|
|
|
|
func TestHandleCheck_ProjectRelativeLocalSourceIsCompared(t *testing.T) {
|
|
s, _ := newTestServer(t)
|
|
projectRoot := t.TempDir()
|
|
skillsDir := filepath.Join(projectRoot, ".skillshare", "skills")
|
|
os.MkdirAll(skillsDir, 0755)
|
|
s.projectRoot = projectRoot
|
|
s.projectCfg = &config.ProjectConfig{}
|
|
if err := s.projectCfg.Save(projectRoot); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
vendor := filepath.Join(projectRoot, "vendor", "team-skill")
|
|
os.MkdirAll(vendor, 0755)
|
|
os.WriteFile(filepath.Join(vendor, "SKILL.md"), []byte("---\nname: team-skill\n---\n# v1"), 0644)
|
|
// Project installs record the source as typed, relative to the project root.
|
|
source := &install.Source{Type: install.SourceTypeLocalPath, Raw: "./vendor/team-skill", Path: vendor, Name: "team-skill"}
|
|
if _, err := install.Install(source, filepath.Join(skillsDir, "team-skill"), install.InstallOptions{SourceDir: skillsDir, SkipAudit: true}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
os.WriteFile(filepath.Join(vendor, "SKILL.md"), []byte("---\nname: team-skill\n---\n# v2"), 0644)
|
|
|
|
rr := httptest.NewRecorder()
|
|
s.handler.ServeHTTP(rr, httptest.NewRequest(http.MethodGet, "/api/check", nil))
|
|
|
|
if !strings.Contains(rr.Body.String(), `"status":"update_available"`) {
|
|
t.Errorf("expected update_available for the relative source, got %s", rr.Body.String())
|
|
}
|
|
}
|