Files
Willie e51134af24 feat(git-root): surface scope mismatch in UI and document git_root field
Add Config.GitRootMismatch() as the single source of truth for detecting
when the git repo lives at a different scope than git_root configures, and
reuse it across the CLI guidance, the server status response (scopeMismatch
/mismatchScope/mismatchDir), and a new warning banner on the Git Sync page.

Document git_root in the config structure-view field docs (fieldDocs) and the
GitStatus client type, and add the gitSync.scope.* / gitSync.mismatch.* i18n
keys across all locales to keep dictionary parity.

Cover the behavior with config, server handler, and CLI integration tests:
push/pull honor the root scope (agents files reach the remote, config.yaml
stays untracked), POST /git/root inits+persists the scope and rejects invalid
scopes and project mode, and git status reports scope mismatches.
2026-05-30 19:23:12 +08:00

80 lines
2.1 KiB
Go

package config
import (
"os"
"path/filepath"
"testing"
)
func TestEffectiveGitRoot(t *testing.T) {
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
base := BaseDir()
cases := []struct {
gitRoot string
want string
}{
{"", filepath.Join(base, "skills")},
{"skills", filepath.Join(base, "skills")},
{"agents", filepath.Join(base, "agents")},
{"extras", filepath.Join(base, "extras")},
{"root", base},
}
for _, c := range cases {
cfg := &Config{GitRoot: c.gitRoot}
if got := cfg.EffectiveGitRoot(); got != c.want {
t.Errorf("GitRoot=%q: got %q, want %q", c.gitRoot, got, c.want)
}
}
}
func TestValidGitRoot(t *testing.T) {
for _, s := range []string{"", "skills", "agents", "extras", "root"} {
if !ValidGitRoot(s) {
t.Errorf("ValidGitRoot(%q) = false, want true", s)
}
}
for _, s := range []string{"foo", "Root", "all", "global"} {
if ValidGitRoot(s) {
t.Errorf("ValidGitRoot(%q) = true, want false", s)
}
}
}
func TestGitRootMismatch(t *testing.T) {
base := t.TempDir()
skills := filepath.Join(base, "skills")
agents := filepath.Join(base, "agents")
for _, d := range []string{skills, agents} {
if err := os.MkdirAll(d, 0o755); err != nil {
t.Fatal(err)
}
}
cfg := &Config{
GitRoot: "agents", // configured to operate on agents/
Sources: GlobalSources{Skills: skills, Agents: agents},
}
// No repo anywhere → no mismatch.
if _, _, m := cfg.GitRootMismatch(); m {
t.Errorf("no repo present: expected no mismatch")
}
// Repo at skills/ but git_root=agents → mismatch pointing at skills.
if err := os.MkdirAll(filepath.Join(skills, ".git"), 0o755); err != nil {
t.Fatal(err)
}
scope, dir, m := cfg.GitRootMismatch()
if !m || scope != "skills" || dir != skills {
t.Errorf("expected mismatch at skills, got scope=%q dir=%q mismatch=%v", scope, dir, m)
}
// Repo also at agents/ (the configured root) → no mismatch (configured wins).
if err := os.MkdirAll(filepath.Join(agents, ".git"), 0o755); err != nil {
t.Fatal(err)
}
if _, _, m := cfg.GitRootMismatch(); m {
t.Errorf("configured root has a repo: expected no mismatch")
}
}