mirror of
https://github.com/runkids/skillshare.git
synced 2026-11-20 20:34:19 +00:00
The CLI, GET /api/check and the check stream each carry their own copy of the resolution sequence, and only its leaf helpers were tested. Pin what each caller reports for every outcome, against local bare remotes, so the copies can be merged without changing output. Refs #444
141 lines
4.5 KiB
Go
141 lines
4.5 KiB
Go
package server
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"reflect"
|
|
"sort"
|
|
"strings"
|
|
"testing"
|
|
|
|
"skillshare/internal/check/checktest"
|
|
)
|
|
|
|
// checkPayload is the body of GET /api/check and of the stream's done event.
|
|
type checkPayload struct {
|
|
TrackedRepos []repoCheckResult `json:"tracked_repos"`
|
|
LinkedRepos []any `json:"linked_repos"`
|
|
Skills []skillCheckResult `json:"skills"`
|
|
}
|
|
|
|
func decodeCheckPayload(t *testing.T, raw string) checkPayload {
|
|
t.Helper()
|
|
var keys map[string]json.RawMessage
|
|
if err := json.Unmarshal([]byte(raw), &keys); err != nil {
|
|
t.Fatalf("invalid payload: %v: %s", err, raw)
|
|
}
|
|
for _, key := range []string{"tracked_repos", "linked_repos", "skills"} {
|
|
if _, ok := keys[key]; !ok {
|
|
t.Errorf("payload lacks %q: %s", key, raw)
|
|
}
|
|
}
|
|
if len(keys) != 3 {
|
|
t.Errorf("payload has unexpected keys: %s", raw)
|
|
}
|
|
var payload checkPayload
|
|
if err := json.Unmarshal([]byte(raw), &payload); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return payload
|
|
}
|
|
|
|
// wantDashboardSkills is what the dashboard reports for checktest.Seed. Unlike
|
|
// the CLI it reports a skill removed upstream as update_available, and gives
|
|
// only name, status and message for a skill without a remote.
|
|
func wantDashboardSkills(versions map[string]string) []skillCheckResult {
|
|
statuses := map[string]string{
|
|
"broken": "error",
|
|
"changed": "update_available",
|
|
"current": "up_to_date",
|
|
"doomed": "update_available",
|
|
"notree": "update_available",
|
|
"same": "up_to_date",
|
|
"slash": "up_to_date",
|
|
}
|
|
want := []skillCheckResult{{Name: "plain", Status: "local"}}
|
|
for name, status := range statuses {
|
|
r := skillCheckResult{Name: name, Source: "src/" + name, Version: versions[name], Status: status}
|
|
if name == "current" {
|
|
r.InstalledAt = "2024-05-06"
|
|
}
|
|
want = append(want, r)
|
|
}
|
|
sortSkillResults(want)
|
|
return want
|
|
}
|
|
|
|
func sortSkillResults(results []skillCheckResult) {
|
|
sort.Slice(results, func(i, j int) bool { return results[i].Name < results[j].Name })
|
|
}
|
|
|
|
func TestHandleCheck_ResolvesEveryStatus(t *testing.T) {
|
|
s, src := newTestServer(t)
|
|
versions := checktest.Seed(t, t.TempDir(), src)
|
|
s.reloadSkillsStore()
|
|
|
|
rr := httptest.NewRecorder()
|
|
s.handler.ServeHTTP(rr, httptest.NewRequest(http.MethodGet, "/api/check", nil))
|
|
if rr.Code != http.StatusOK {
|
|
t.Fatalf("expected 200, got %d: %s", rr.Code, rr.Body.String())
|
|
}
|
|
|
|
payload := decodeCheckPayload(t, rr.Body.String())
|
|
if payload.Skills[0].Name != "plain" {
|
|
t.Errorf("skills without a remote must come first, got %+v", payload.Skills[0])
|
|
}
|
|
sortSkillResults(payload.Skills)
|
|
if want := wantDashboardSkills(versions); !reflect.DeepEqual(payload.Skills, want) {
|
|
t.Errorf("skills:\n got %+v\nwant %+v", payload.Skills, want)
|
|
}
|
|
if len(payload.TrackedRepos) != 0 || len(payload.LinkedRepos) != 0 {
|
|
t.Errorf("unexpected repos: %s", rr.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestHandleCheckStream_EventSequence(t *testing.T) {
|
|
s, src := newTestServer(t)
|
|
versions := checktest.Seed(t, t.TempDir(), src)
|
|
s.reloadSkillsStore()
|
|
|
|
rr := httptest.NewRecorder()
|
|
s.handleCheckStream(rr, httptest.NewRequest(http.MethodGet, "/api/check/stream", nil))
|
|
|
|
type event struct{ name, data string }
|
|
var events []event
|
|
for _, block := range strings.Split(strings.TrimSpace(rr.Body.String()), "\n\n") {
|
|
name, data, ok := strings.Cut(block, "\n")
|
|
if !ok {
|
|
t.Fatalf("malformed event %q", block)
|
|
}
|
|
events = append(events, event{strings.TrimPrefix(name, "event: "), strings.TrimPrefix(data, "data: ")})
|
|
}
|
|
if len(events) < 3 {
|
|
t.Fatalf("expected at least discovering, start and done, got %+v", events)
|
|
}
|
|
|
|
if want := (event{"discovering", `{"phase":"scanning source directory"}`}); events[0] != want {
|
|
t.Errorf("first event = %+v, want %+v", events[0], want)
|
|
}
|
|
// Three remotes: the moved one, the pinned branch, and the unreachable one.
|
|
if want := (event{"start", `{"repos":0,"sources":3,"total":3}`}); events[1] != want {
|
|
t.Errorf("second event = %+v, want %+v", events[1], want)
|
|
}
|
|
for _, e := range events[2 : len(events)-1] {
|
|
var progress map[string]int64
|
|
if e.name != "progress" || json.Unmarshal([]byte(e.data), &progress) != nil || len(progress) != 1 || progress["checked"] > 3 {
|
|
t.Errorf("unexpected event between start and done: %+v", e)
|
|
}
|
|
}
|
|
|
|
done := events[len(events)-1]
|
|
if done.name != "done" {
|
|
t.Fatalf("last event = %+v, want done", done)
|
|
}
|
|
payload := decodeCheckPayload(t, done.data)
|
|
sortSkillResults(payload.Skills)
|
|
if want := wantDashboardSkills(versions); !reflect.DeepEqual(payload.Skills, want) {
|
|
t.Errorf("skills:\n got %+v\nwant %+v", payload.Skills, want)
|
|
}
|
|
}
|