- Resolve the Windows admin config via the ProgramData known folder instead of the user-controlled ProgramData variable, so a user cannot redirect the admin lookup and drop pinned labels - vendor rename rewrites matching agent-lib.labels.json entries, so a renamed item keeps its labels instead of going to every client - validate checks the labels file in repositories without a lockfile - A kept modified copy of a deselected item is no longer drift in status, and the toast explains it as "no longer assigned to you" - Labels parser rejects trailing data such as an extra closing brace - Tests: admin pin wiring (config chain, --repo, admin as chosen config), reselect redeploy with byte-level no-op, status labels JSON, validate without lockfile, rename keeps labels - README: document unreadable admin config, drift semantics, rename Refs: agent_lib-fh4 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
301 lines
8.9 KiB
Go
301 lines
8.9 KiB
Go
package client
|
|
|
|
import (
|
|
"encoding/json"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func writeConfigFile(t *testing.T, path, repoURL string) {
|
|
t.Helper()
|
|
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
data, err := json.Marshal(map[string]string{"repo_url": repoURL, "ref": "main"})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(path, data, 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
// runIsolated points HOME/XDG at empty temp dirs and clears the override env
|
|
// so chain tests never see the real machine's config.
|
|
func runIsolated(t *testing.T, fn func()) {
|
|
t.Helper()
|
|
base := t.TempDir()
|
|
home := filepath.Join(base, "home")
|
|
xdg := filepath.Join(base, "xdg-config")
|
|
for _, d := range []string{home, xdg} {
|
|
if err := os.MkdirAll(d, 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
t.Setenv("HOME", home)
|
|
t.Setenv("XDG_CONFIG_HOME", xdg)
|
|
t.Setenv(ConfigEnv, "")
|
|
fn()
|
|
}
|
|
|
|
func TestUserConfigPathHonorsXDG(t *testing.T) {
|
|
runIsolated(t, func() {
|
|
base := t.TempDir()
|
|
t.Setenv("XDG_CONFIG_HOME", base)
|
|
want := filepath.Join(base, "agent-lib", "config.json")
|
|
if got := UserConfigPath(); got != want {
|
|
t.Errorf("UserConfigPath() = %q, want %q", got, want)
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestLoadClientConfigChainOrder(t *testing.T) {
|
|
runIsolated(t, func() {
|
|
base := t.TempDir()
|
|
flagCfg := filepath.Join(base, "flag.json")
|
|
envCfg := filepath.Join(base, "env.json")
|
|
userCfg := filepath.Join(os.Getenv("XDG_CONFIG_HOME"), "agent-lib", "config.json")
|
|
writeConfigFile(t, flagCfg, "https://flag.example/repo")
|
|
writeConfigFile(t, envCfg, "https://env.example/repo")
|
|
writeConfigFile(t, userCfg, "https://user.example/repo")
|
|
|
|
assertRepo := func(name, got, want string) {
|
|
t.Helper()
|
|
if got != want {
|
|
t.Errorf("%s: repo_url = %q, want %q", name, got, want)
|
|
}
|
|
}
|
|
|
|
cfg, err := LoadClientConfig(flagCfg)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
assertRepo("flag beats env and user", cfg.RepoURL, "https://flag.example/repo")
|
|
|
|
t.Setenv(ConfigEnv, envCfg)
|
|
cfg, err = LoadClientConfig("")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
assertRepo("env beats user", cfg.RepoURL, "https://env.example/repo")
|
|
|
|
t.Setenv(ConfigEnv, "")
|
|
cfg, err = LoadClientConfig("")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
assertRepo("user picked up without flags", cfg.RepoURL, "https://user.example/repo")
|
|
})
|
|
}
|
|
|
|
func TestLoadClientConfigMissingIsActionable(t *testing.T) {
|
|
runIsolated(t, func() {
|
|
_, err := LoadClientConfig("")
|
|
if err == nil {
|
|
t.Fatal("empty chain must fail")
|
|
}
|
|
var missing *MissingConfigError
|
|
if e, ok := err.(*MissingConfigError); !ok {
|
|
t.Fatalf("error must be MissingConfigError, got %T: %v", err, err)
|
|
} else {
|
|
missing = e
|
|
}
|
|
msg := missing.Error()
|
|
userPath := UserConfigPath()
|
|
for _, want := range []string{
|
|
"no client config found",
|
|
userPath,
|
|
DefaultConfigPath(),
|
|
`"repo_url"`,
|
|
"--repo",
|
|
} {
|
|
if !strings.Contains(msg, want) {
|
|
t.Errorf("error message must mention %q:\n%s", want, msg)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestLoadOptionalConfigNilWhenEmpty(t *testing.T) {
|
|
runIsolated(t, func() {
|
|
cfg, err := LoadOptionalConfig("")
|
|
if err != nil {
|
|
t.Fatalf("empty chain must be (nil, nil), got %v", err)
|
|
}
|
|
if cfg != nil {
|
|
t.Errorf("cfg must be nil, got %+v", cfg)
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestLoadOptionalConfigMalformedStillFails(t *testing.T) {
|
|
runIsolated(t, func() {
|
|
userCfg := filepath.Join(os.Getenv("XDG_CONFIG_HOME"), "agent-lib", "config.json")
|
|
writeConfigFile(t, userCfg, "")
|
|
if _, err := LoadOptionalConfig(""); err == nil {
|
|
t.Error("user config with empty repo_url must fail even in optional mode")
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestLoadClientConfigExplicitFlagMustExist(t *testing.T) {
|
|
runIsolated(t, func() {
|
|
_, err := LoadClientConfig(filepath.Join(t.TempDir(), "nope.json"))
|
|
if err == nil || !strings.Contains(err.Error(), "nope.json") {
|
|
t.Errorf("explicit --config pointing nowhere must name the path, got %v", err)
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestFillTokenAuthEnvOnlyFillsEmptyToken(t *testing.T) {
|
|
t.Setenv("AGENT_LIB_GIT_TOKEN", "env-token")
|
|
t.Setenv("AGENT_LIB_GIT_USER", "env-user")
|
|
fromEnv := &ClientConfig{}
|
|
fromEnv.FillTokenAuth()
|
|
if fromEnv.Token != "env-token" || fromEnv.TokenUser != "env-user" {
|
|
t.Errorf("empty token must be filled from env: %+v", fromEnv)
|
|
}
|
|
configured := &ClientConfig{Token: "config-token", TokenUser: "config-user"}
|
|
configured.FillTokenAuth()
|
|
if configured.Token != "config-token" || configured.TokenUser != "config-user" {
|
|
t.Errorf("env must never override a config token: %+v", configured)
|
|
}
|
|
}
|
|
|
|
func writeRaw(t *testing.T, path, content string) {
|
|
t.Helper()
|
|
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(path, []byte(content), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func TestConfigLabelsAbsentEmptyAndSet(t *testing.T) {
|
|
dir := t.TempDir()
|
|
cases := map[string]struct {
|
|
body string
|
|
want *[]string
|
|
source string
|
|
}{
|
|
"absent": {`{"repo_url":"r"}`, nil, ""},
|
|
"empty": {`{"repo_url":"r","labels":[]}`, &[]string{}, "flag"},
|
|
"set": {`{"repo_url":"r","labels":["sales","lead"]}`, &[]string{"sales", "lead"}, "flag"},
|
|
}
|
|
for name, tc := range cases {
|
|
p := filepath.Join(dir, name+".json")
|
|
writeRaw(t, p, tc.body)
|
|
cfg, err := LoadClientConfig(p)
|
|
if err != nil {
|
|
t.Fatalf("%s: %v", name, err)
|
|
}
|
|
if (cfg.Labels == nil) != (tc.want == nil) {
|
|
t.Fatalf("%s: labels nil-ness mismatch: %v", name, cfg.Labels)
|
|
}
|
|
if tc.want != nil && strings.Join(*cfg.Labels, ",") != strings.Join(*tc.want, ",") {
|
|
t.Errorf("%s: labels = %v", name, *cfg.Labels)
|
|
}
|
|
if cfg.LabelSource != tc.source {
|
|
t.Errorf("%s: source = %q, want %q", name, cfg.LabelSource, tc.source)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestConfigRejectsMalformedLabel(t *testing.T) {
|
|
p := filepath.Join(t.TempDir(), "c.json")
|
|
writeRaw(t, p, `{"repo_url":"r","labels":["Sales"]}`)
|
|
if _, err := LoadClientConfig(p); err == nil {
|
|
t.Fatal("an uppercase label must be rejected")
|
|
}
|
|
}
|
|
|
|
func TestAdminLabelsWinOverUserLabels(t *testing.T) {
|
|
dir := t.TempDir()
|
|
admin := filepath.Join(dir, "admin.json")
|
|
writeRaw(t, admin, `{"repo_url":"r","labels":["finance"]}`)
|
|
user := []string{"sales", "it"}
|
|
cfg := &ClientConfig{RepoURL: "r", Labels: &user, LabelSource: "user"}
|
|
if err := ApplyAdminLabels(cfg, admin); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if cfg.LabelSource != "admin" || strings.Join(*cfg.Labels, ",") != "finance" {
|
|
t.Fatalf("admin labels must replace user labels, got %v (%s)", *cfg.Labels, cfg.LabelSource)
|
|
}
|
|
}
|
|
|
|
func TestAdminEmptyLabelsAlsoPin(t *testing.T) {
|
|
admin := filepath.Join(t.TempDir(), "admin.json")
|
|
writeRaw(t, admin, `{"repo_url":"r","labels":[]}`)
|
|
user := []string{"sales"}
|
|
cfg := &ClientConfig{Labels: &user, LabelSource: "user"}
|
|
if err := ApplyAdminLabels(cfg, admin); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if cfg.Labels == nil || len(*cfg.Labels) != 0 {
|
|
t.Fatalf("an admin [] must pin 'unlabeled only', got %v", cfg.Labels)
|
|
}
|
|
}
|
|
|
|
func TestAdminWithoutLabelsLeavesUserLabels(t *testing.T) {
|
|
dir := t.TempDir()
|
|
admin := filepath.Join(dir, "admin.json")
|
|
writeRaw(t, admin, `{"repo_url":"r"}`)
|
|
user := []string{"sales"}
|
|
cfg := &ClientConfig{Labels: &user, LabelSource: "user"}
|
|
if err := ApplyAdminLabels(cfg, admin); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if cfg.LabelSource != "user" || strings.Join(*cfg.Labels, ",") != "sales" {
|
|
t.Fatalf("without admin labels the user's labels apply, got %v (%s)", *cfg.Labels, cfg.LabelSource)
|
|
}
|
|
if err := ApplyAdminLabels(cfg, filepath.Join(dir, "missing.json")); err != nil {
|
|
t.Fatalf("a missing admin config is not an error: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestAdminConfigBrokenIsFatal(t *testing.T) {
|
|
admin := filepath.Join(t.TempDir(), "admin.json")
|
|
writeRaw(t, admin, `{`)
|
|
if err := ApplyAdminLabels(&ClientConfig{}, admin); err == nil {
|
|
t.Fatal("an unreadable admin config must abort: its label intent is unknown")
|
|
}
|
|
}
|
|
|
|
func TestResolveRunConfigAppliesAdminPin(t *testing.T) {
|
|
dir := t.TempDir()
|
|
admin := filepath.Join(dir, "admin.json")
|
|
writeRaw(t, admin, `{"repo_url":"r","labels":["finance"]}`)
|
|
orig := adminConfigPath
|
|
t.Cleanup(func() { adminConfigPath = orig })
|
|
adminConfigPath = func() string { return admin }
|
|
|
|
user := filepath.Join(dir, "user.json")
|
|
writeRaw(t, user, `{"repo_url":"r","labels":["sales","it"]}`)
|
|
cfg, err := resolveRunConfig(SyncOptions{ConfigPath: user})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if cfg.LabelSource != "admin" || strings.Join(*cfg.Labels, ",") != "finance" {
|
|
t.Errorf("config-chain run: admin pin not applied, got %v (%s)", *cfg.Labels, cfg.LabelSource)
|
|
}
|
|
|
|
cfg, err = resolveRunConfig(SyncOptions{ConfigPath: user, RepoURL: "other"})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if cfg.LabelSource != "admin" || strings.Join(*cfg.Labels, ",") != "finance" {
|
|
t.Errorf("--repo run: admin pin not applied, got %v (%s)", *cfg.Labels, cfg.LabelSource)
|
|
}
|
|
|
|
cfg, err = resolveRunConfig(SyncOptions{ConfigPath: admin})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if cfg.LabelSource != "admin" {
|
|
t.Errorf("admin config as chosen config: source = %q", cfg.LabelSource)
|
|
}
|
|
}
|