Files
skillshare/internal/server/handler_open_in_editor_test.go
T
Willie 5fc5ee4fe5 feat(ui): add web-based skill editor with metadata, live preview, and diff review
Introduces a full in-browser skill editor reachable from the skill detail
page, replacing the previous read-only detail view for edits.

Editor
- Two-pane layout with Markdown textarea, live preview, and synced scroll
- Edit / Split / Preview mode toggle in the status bar (⌘P to cycle)
- Keyboard shortcuts: ⌘S save (with diff review), Esc cancel
- Outline drawer with jump-to-heading
- Token / word / line / file count in status bar, 5K budget warning

Frontmatter
- Grouped field editor (Identity / Invocation / Execution / Metadata)
  matching the 13 official SKILL.md fields
- Shared 1,536-char budget enforced across description + when_to_use
- Switch toggles for booleans, segmented control for enums, chip array
  inputs for list-valued fields
- Metadata group edits nested metadata.* keys directly, with stable row
  ids so typing key → clicking value no longer remounts the inputs
- Special handling for metadata.targets so values serialize as a YAML
  list (backend ParseFrontmatterList requires arrays)
- YAML mode for raw editing; Fields ↔ YAML round-trips cleanly
- Legacy root-level 'targets:' migrated to metadata.targets on load

Diff review
- Save triggers a side-by-side diff modal built on the shared DialogShell
  with focus trap and backdrop, using the Button component for actions
- YAML serializer emits plain scalars when safe instead of wrapping every
  value containing ':', '*', '#', or '"', eliminating spurious churn in
  the save diff

Misc
- DialogShell gains 4xl–7xl maxWidth presets
- Resource detail sidebar shows a Targets row when metadata.targets is set

Backend additions (server handlers):
- POST /api/resources/:name/content   save SKILL.md content
- POST /api/resources/:name/open      open in local editor
2026-04-19 09:29:18 +08:00

101 lines
2.8 KiB
Go

package server
import (
"bytes"
"encoding/json"
"net/http"
"net/http/httptest"
"path/filepath"
"testing"
)
func TestHandleOpenInEditor_UsesEchoAsEditor(t *testing.T) {
s, src := newTestServer(t)
addSkill(t, src, "my-skill")
// `true` is a universally-available no-op binary on POSIX.
body := openInEditorRequest{Editor: "true"}
raw, _ := json.Marshal(body)
req := httptest.NewRequest(http.MethodPost, "/api/resources/my-skill/open-in-editor", bytes.NewReader(raw))
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 openInEditorResponse
if err := json.Unmarshal(rr.Body.Bytes(), &resp); err != nil {
t.Fatalf("decode: %v", err)
}
if resp.PID == 0 {
t.Errorf("expected pid > 0, got %d", resp.PID)
}
if resp.Path != filepath.Join(src, "my-skill") {
t.Errorf("unexpected path: %s", resp.Path)
}
}
func TestHandleOpenInEditor_NotFound(t *testing.T) {
s, _ := newTestServer(t)
raw, _ := json.Marshal(openInEditorRequest{Editor: "true"})
req := httptest.NewRequest(http.MethodPost, "/api/resources/no-skill/open-in-editor", bytes.NewReader(raw))
rr := httptest.NewRecorder()
s.handler.ServeHTTP(rr, req)
if rr.Code != http.StatusNotFound {
t.Fatalf("expected 404, got %d: %s", rr.Code, rr.Body.String())
}
}
func TestHandleOpenInEditor_HeadlessRefuses(t *testing.T) {
t.Setenv("SKILLSHARE_HEADLESS", "1")
s, src := newTestServer(t)
addSkill(t, src, "my-skill")
raw, _ := json.Marshal(openInEditorRequest{Editor: "true"})
req := httptest.NewRequest(http.MethodPost, "/api/resources/my-skill/open-in-editor", bytes.NewReader(raw))
rr := httptest.NewRecorder()
s.handler.ServeHTTP(rr, req)
if rr.Code != http.StatusConflict {
t.Fatalf("expected 409, got %d", rr.Code)
}
}
func TestHandleOpenInEditor_UnknownBinary(t *testing.T) {
s, src := newTestServer(t)
addSkill(t, src, "my-skill")
raw, _ := json.Marshal(openInEditorRequest{Editor: "this-is-not-a-real-editor-xyz"})
req := httptest.NewRequest(http.MethodPost, "/api/resources/my-skill/open-in-editor", bytes.NewReader(raw))
rr := httptest.NewRecorder()
s.handler.ServeHTTP(rr, req)
if rr.Code != http.StatusConflict {
t.Fatalf("expected 409, got %d", rr.Code)
}
}
func TestPickEditor_AutoPrefersEditorEnv(t *testing.T) {
t.Setenv("VISUAL", "")
t.Setenv("EDITOR", "true")
cand, name, err := pickEditor("auto")
if err != nil {
t.Fatalf("pickEditor: %v", err)
}
if cand.bin != "true" || name != "true" {
t.Errorf("expected bin=true, got bin=%s name=%s", cand.bin, name)
}
}
func TestSplitCommand(t *testing.T) {
head, rest := splitCommand("code --wait --new-window")
if head != "code" {
t.Errorf("expected head=code, got %s", head)
}
if len(rest) != 2 || rest[0] != "--wait" || rest[1] != "--new-window" {
t.Errorf("unexpected rest: %v", rest)
}
}