mirror of
https://github.com/runkids/skillshare.git
synced 2026-11-18 18:04:17 +00:00
After #884, an install without --force keeps existing skills and agents and names them in the --json skipped array, but the operation log recorded only installed and failed names, so `ss log` could not show what an install kept. Record the kept names as skipped_skills for CLI installs and for the dashboard's install-from-config action, and list them in `ss log`. The config install starts with an empty list, so the dashboard log shows no "null" when nothing was kept.
154 lines
5.9 KiB
Go
154 lines
5.9 KiB
Go
package server
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"skillshare/internal/config"
|
|
"skillshare/internal/install"
|
|
"skillshare/internal/oplog"
|
|
"skillshare/internal/testutil"
|
|
)
|
|
|
|
func TestHandleInstallFromConfig_FollowsAdvancedProjectLock(t *testing.T) {
|
|
s, project := newTestProjectServerWithExtras(t, nil)
|
|
base := t.TempDir()
|
|
remote := testutil.SetupBareRemoteRepo(t, base)
|
|
testutil.SeedRemoteBranch(t, base, remote, "main", map[string]string{"demo/SKILL.md": "# Original\n"})
|
|
source := "file://" + remote + "//demo"
|
|
s.projectCfg.Skills = []config.SkillEntry{{Name: "demo", Source: source}}
|
|
if err := s.projectCfg.Save(project); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
rr := httptest.NewRecorder()
|
|
s.handleInstallFromConfig(rr, httptest.NewRequest(http.MethodPost, "/api/install/from-config", nil))
|
|
installed := filepath.Join(s.skillsSource(), "demo", "SKILL.md")
|
|
if body, err := os.ReadFile(installed); err != nil || string(body) != "# Original\n" {
|
|
t.Fatalf("initial install: %q, %v; response: %s", body, err, rr.Body.String())
|
|
}
|
|
|
|
seed := filepath.Join(base, "seed-main")
|
|
if err := os.WriteFile(filepath.Join(seed, "demo", "SKILL.md"), []byte("# New pin\n"), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
testutil.RunGit(t, seed, "commit", "-am", "advance skill")
|
|
testutil.RunGit(t, seed, "push", "origin", "HEAD:main")
|
|
lockDir := filepath.Join(project, ".skillshare")
|
|
lock, err := install.LoadLock(lockDir)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
pin := lock.Skills["demo"]
|
|
pin.Commit = strings.TrimSpace(testutil.RunGit(t, seed, "rev-parse", "HEAD"))
|
|
lock.Skills["demo"] = pin
|
|
if err := lock.Save(lockDir); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
rr = httptest.NewRecorder()
|
|
s.handleInstallFromConfig(rr, httptest.NewRequest(http.MethodPost, "/api/install/from-config", nil))
|
|
if body, err := os.ReadFile(installed); err != nil || string(body) != "# New pin\n" {
|
|
t.Fatalf("dashboard did not follow advanced lock: %q, %v; response: %s", body, err, rr.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestHandleMissingConfigEntries_ListsAbsentEntries(t *testing.T) {
|
|
s, src := newTestServer(t)
|
|
store := install.LoadMetadataOrNew(src)
|
|
store.Set("_team-skills", &install.MetadataEntry{Source: "https://github.com/team/skills.git", Tracked: true, Branch: "main"})
|
|
if err := store.Save(src); err != nil {
|
|
t.Fatalf("save metadata: %v", err)
|
|
}
|
|
|
|
rr := httptest.NewRecorder()
|
|
s.handleMissingConfigEntries(rr, httptest.NewRequest(http.MethodGet, "/api/install/missing", nil))
|
|
var resp struct {
|
|
Entries []configEntryInfo `json:"entries"`
|
|
}
|
|
if err := json.Unmarshal(rr.Body.Bytes(), &resp); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
want := configEntryInfo{Name: "_team-skills", Source: "https://github.com/team/skills.git", Tracked: true, Branch: "main"}
|
|
if len(resp.Entries) != 1 || resp.Entries[0] != want {
|
|
t.Fatalf("entries = %+v, want [%+v]", resp.Entries, want)
|
|
}
|
|
}
|
|
|
|
func TestHandleInstallFromConfig_NothingMissing(t *testing.T) {
|
|
s, _ := newTestServer(t)
|
|
rr := httptest.NewRecorder()
|
|
s.handleInstallFromConfig(rr, httptest.NewRequest(http.MethodPost, "/api/install/from-config", nil))
|
|
if rr.Code != http.StatusOK {
|
|
t.Fatalf("expected 200, got %d: %s", rr.Code, rr.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestHandleInstallFromConfig_AllFailedLogsError(t *testing.T) {
|
|
s, src := newTestServer(t)
|
|
store := install.LoadMetadataOrNew(src)
|
|
store.Set("broken", &install.MetadataEntry{Source: ""})
|
|
if err := store.Save(src); err != nil {
|
|
t.Fatalf("save metadata: %v", err)
|
|
}
|
|
|
|
rr := httptest.NewRecorder()
|
|
s.handleInstallFromConfig(rr, httptest.NewRequest(http.MethodPost, "/api/install/from-config", nil))
|
|
entries, _ := oplog.Read(s.configPath(), oplog.OpsFile, 10)
|
|
if len(entries) != 1 || entries[0].Status != "error" || !strings.Contains(entries[0].Message, "broken: ") {
|
|
t.Errorf("oplog = %+v, want one error entry naming the skill", entries)
|
|
}
|
|
// Nothing kept is an empty list, not null, which the dashboard log would print.
|
|
if len(entries) == 1 && entries[0].Args["skipped_skills"] == nil {
|
|
t.Errorf("skipped_skills = nil, want an empty list: %+v", entries[0].Args)
|
|
}
|
|
}
|
|
|
|
// Skills already on disk are kept; the log names them, as the CLI's does.
|
|
func TestHandleInstallFromConfig_LogsSkippedSkills(t *testing.T) {
|
|
s, src := newTestServer(t)
|
|
store := install.LoadMetadataOrNew(src)
|
|
store.Set("kept", &install.MetadataEntry{Source: "https://github.com/team/kept.git"})
|
|
if err := store.Save(src); err != nil {
|
|
t.Fatalf("save metadata: %v", err)
|
|
}
|
|
if err := os.MkdirAll(filepath.Join(src, "kept"), 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(filepath.Join(src, "kept", "SKILL.md"), []byte("# Kept\n"), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
rr := httptest.NewRecorder()
|
|
s.handleInstallFromConfig(rr, httptest.NewRequest(http.MethodPost, "/api/install/from-config", nil))
|
|
entries, _ := oplog.Read(s.configPath(), oplog.OpsFile, 10)
|
|
if len(entries) != 1 || fmt.Sprint(entries[0].Args["skipped_skills"]) != "[kept]" {
|
|
t.Errorf("oplog = %+v, want skipped_skills [kept]; response: %s", entries, rr.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestHandleMissingConfigEntries_ProjectModeChecksProjectSource(t *testing.T) {
|
|
s, root := newTestProjectServerWithExtras(t, nil)
|
|
s.projectCfg.Skills = []config.SkillEntry{{Name: "present", Source: "github.com/team/skills/present"}, {Name: "absent", Source: "github.com/team/skills/absent"}}
|
|
// Only the project source has "present"; the global source has neither.
|
|
if err := os.MkdirAll(filepath.Join(s.projectCfg.EffectiveSkillsSource(root), "present"), 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
rr := httptest.NewRecorder()
|
|
s.handleMissingConfigEntries(rr, httptest.NewRequest(http.MethodGet, "/api/install/missing", nil))
|
|
var resp struct {
|
|
Entries []configEntryInfo `json:"entries"`
|
|
}
|
|
if err := json.Unmarshal(rr.Body.Bytes(), &resp); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(resp.Entries) != 1 || resp.Entries[0].Name != "absent" {
|
|
t.Fatalf("entries = %+v, want only absent", resp.Entries)
|
|
}
|
|
}
|