mirror of
https://github.com/runkids/skillshare.git
synced 2026-11-19 02:17:09 +00:00
Validate config semantics (source path, sync modes, target paths) at save time (API + CLI) instead of failing at sync time with cryptic errors. Sync no longer auto-creates missing target directories — fails fast to catch typos like ~/.cusor/skills. Dry-run also validates paths. - ValidateConfig / ValidateProjectConfig with warnings + errors - handlePutConfig returns 400 for invalid configs, 200 + warnings - handleSync pre-checks via ValidateConfig - ensureRealTargetDir shared helper (replaces duplicated blocks) - UI: warning toast on config save, warning banners on sync/preview - Dashboard shows full absolute source path
117 lines
3.2 KiB
Go
117 lines
3.2 KiB
Go
package server
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestHandleGetConfig(t *testing.T) {
|
|
s, _ := newTestServer(t)
|
|
req := httptest.NewRequest(http.MethodGet, "/api/config", 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 map[string]any
|
|
json.Unmarshal(rr.Body.Bytes(), &resp)
|
|
if resp["raw"] == nil || resp["raw"] == "" {
|
|
t.Error("expected non-empty raw config")
|
|
}
|
|
if resp["config"] == nil {
|
|
t.Error("expected config object in response")
|
|
}
|
|
}
|
|
|
|
func TestHandleAvailableTargets(t *testing.T) {
|
|
s, _ := newTestServer(t)
|
|
req := httptest.NewRequest(http.MethodGet, "/api/config/available-targets", 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 {
|
|
Targets []map[string]any `json:"targets"`
|
|
}
|
|
json.Unmarshal(rr.Body.Bytes(), &resp)
|
|
if len(resp.Targets) == 0 {
|
|
t.Error("expected at least 1 available target")
|
|
}
|
|
}
|
|
|
|
func TestHandlePutConfig_InvalidJSON(t *testing.T) {
|
|
s, _ := newTestServer(t)
|
|
req := httptest.NewRequest(http.MethodPut, "/api/config", strings.NewReader("not json"))
|
|
rr := httptest.NewRecorder()
|
|
s.handler.ServeHTTP(rr, req)
|
|
|
|
if rr.Code != http.StatusBadRequest {
|
|
t.Errorf("expected 400, got %d: %s", rr.Code, rr.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestHandlePutConfig_InvalidYAML(t *testing.T) {
|
|
s, _ := newTestServer(t)
|
|
body := `{"raw":"invalid: [yaml: content"}`
|
|
req := httptest.NewRequest(http.MethodPut, "/api/config", strings.NewReader(body))
|
|
rr := httptest.NewRecorder()
|
|
s.handler.ServeHTTP(rr, req)
|
|
|
|
if rr.Code != http.StatusBadRequest {
|
|
t.Errorf("expected 400, got %d: %s", rr.Code, rr.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestHandlePutConfig_ValidYAML(t *testing.T) {
|
|
s, sourceDir := newTestServer(t)
|
|
body := `{"raw":"source: ` + sourceDir + `\nmode: merge\ntargets: {}\n"}`
|
|
req := httptest.NewRequest(http.MethodPut, "/api/config", strings.NewReader(body))
|
|
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 {
|
|
Success bool `json:"success"`
|
|
Warnings []string `json:"warnings"`
|
|
}
|
|
json.Unmarshal(rr.Body.Bytes(), &resp)
|
|
if !resp.Success {
|
|
t.Error("expected success true")
|
|
}
|
|
}
|
|
|
|
func TestHandlePutConfig_InvalidSource_400(t *testing.T) {
|
|
s, _ := newTestServer(t)
|
|
body := `{"raw":"source: /nonexistent/path\nmode: merge\ntargets: {}\n"}`
|
|
req := httptest.NewRequest(http.MethodPut, "/api/config", strings.NewReader(body))
|
|
rr := httptest.NewRecorder()
|
|
s.handler.ServeHTTP(rr, req)
|
|
|
|
if rr.Code != http.StatusBadRequest {
|
|
t.Errorf("expected 400, got %d: %s", rr.Code, rr.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestHandlePutConfig_InvalidMode_400(t *testing.T) {
|
|
s, sourceDir := newTestServer(t)
|
|
body := `{"raw":"source: ` + sourceDir + `\nmode: invalid\ntargets: {}\n"}`
|
|
req := httptest.NewRequest(http.MethodPut, "/api/config", strings.NewReader(body))
|
|
rr := httptest.NewRecorder()
|
|
s.handler.ServeHTTP(rr, req)
|
|
|
|
if rr.Code != http.StatusBadRequest {
|
|
t.Errorf("expected 400, got %d: %s", rr.Code, rr.Body.String())
|
|
}
|
|
}
|