Files
skillshare/internal/server/handler_git_hooks_test.go
Rusty Shackleford 1a2389dea2 feat(hooks): manage Git config hooks (#310)
Git 2.54 added named config hooks, but keeping their registrations and
helper paths consistent across machines still required manual config
edits. Bring them into the same preview, sync, and recovery workflow as
Agent hooks.

- Global and project scope, with linked worktrees sharing the common dir
- Portable helper files, collision detection, and ownership tracking of
  generated files and include.path entries
- Backups and restore preserve unrelated settings and conditional includes
- Management never executes hooks; Git below 2.54 is reported as inactive
  while outputs are still generated
- CLI/API inventory, raw YAML editing in the dashboard, localized docs
2026-10-02 02:56:54 +08:00

85 lines
3.4 KiB
Go

package server
import (
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"
"skillshare/internal/hooks"
)
func TestGitHooksAPIContract(t *testing.T) {
s, _ := newTestServerWithExtras(t, nil, "")
home := hooksTestHome(t)
t.Setenv("XDG_CONFIG_HOME", filepath.Join(home, ".config"))
t.Setenv("GIT_CONFIG_GLOBAL", filepath.Join(home, "explicit.gitconfig"))
t.Setenv("GIT_CONFIG_NOSYSTEM", "1")
t.Setenv("GIT_CONFIG_SYSTEM", filepath.Join(home, "system.gitconfig"))
if err := os.WriteFile(os.Getenv("GIT_CONFIG_GLOBAL"), []byte("[credential]\n helper = SECRET\n"), 0600); err != nil {
t.Fatal(err)
}
mutation := `{"name":"guard","entry":{"bindings":{"git":{"commands":{"check":{"events":["pre-commit"],"command":"echo check"}}}}}}`
w := hooksPost(s, s.handleHooksPreview, "/api/hooks/preview", `{"mutation":`+mutation+`}`)
var preview struct {
Revision string
Files []hooks.FileDiff
Changes []hooks.Change
Blocked bool
}
if w.Code != http.StatusOK || json.Unmarshal(w.Body.Bytes(), &preview) != nil || preview.Revision == "" || preview.Blocked {
t.Fatalf("Git preview: %d %s", w.Code, w.Body)
}
if strings.Contains(w.Body.String(), "SECRET") {
t.Fatal("private config in API preview")
}
w = hooksUIConfigure(s, mutation, preview.Revision, true)
if w.Code != http.StatusOK {
t.Fatalf("save and sync: %d %s", w.Code, w.Body)
}
w = httptest.NewRecorder()
s.handleHooksList(w, httptest.NewRequest(http.MethodGet, "/api/hooks", nil))
var inv hooks.Inventory
if w.Code != http.StatusOK || json.Unmarshal(w.Body.Bytes(), &inv) != nil || inv.Git == nil || inv.Git.Include.Target != os.Getenv("GIT_CONFIG_GLOBAL") || !inv.Git.Include.Present || !inv.Git.Include.Owned {
t.Fatalf("list: %d %s", w.Code, w.Body)
}
w = hooksPost(s, s.handleHooksPreview, "/api/hooks/preview", `{"mutation":{"name":"guard","remove":true,"unmanage":true}}`)
if w.Code != http.StatusBadRequest || !strings.Contains(w.Body.String(), "cannot keep files") {
t.Fatalf("keep files: %d %s", w.Code, w.Body)
}
// A symlink requires a manual include and produces inactive without blocking.
t.Setenv("GIT_CONFIG_GLOBAL", filepath.Join(home, "linked.gitconfig"))
if err := os.Symlink(filepath.Join(home, "explicit.gitconfig"), os.Getenv("GIT_CONFIG_GLOBAL")); err != nil {
t.Fatal(err)
}
// Existing manual include is present through the link, so it remains active.
w = hooksPost(s, s.handleHooksPreview, "/api/hooks/preview", `{"mutation":{}}`)
if w.Code != http.StatusOK {
t.Fatalf("symlink manual include: %d %s", w.Code, w.Body)
}
if err := os.WriteFile(filepath.Join(home, "empty.gitconfig"), []byte("[credential]\n helper = SECRET\n"), 0600); err != nil {
t.Fatal(err)
}
t.Setenv("GIT_CONFIG_GLOBAL", filepath.Join(home, "inactive.gitconfig"))
if err := os.Symlink(filepath.Join(home, "empty.gitconfig"), os.Getenv("GIT_CONFIG_GLOBAL")); err != nil {
t.Fatal(err)
}
w = hooksPost(s, s.handleHooksPreview, "/api/hooks/preview", `{"mutation":{}}`)
if w.Code != http.StatusOK || json.Unmarshal(w.Body.Bytes(), &preview) != nil || preview.Blocked {
t.Fatalf("inactive contract: %d %s", w.Code, w.Body)
}
inactive := false
for _, c := range preview.Changes {
inactive = inactive || c.InactiveReason != ""
if c.Action != "unchanged" {
t.Fatalf("inactive output counted as a write after sync: %+v", c)
}
}
if !inactive || strings.Contains(w.Body.String(), "SECRET") {
t.Fatalf("inactive/private contract: %s", w.Body)
}
}