- 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>
78 lines
2.1 KiB
Go
78 lines
2.1 KiB
Go
package labels
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestParseAndSelect(t *testing.T) {
|
|
f, err := Parse([]byte(`{"version":1,"labels":{"sales":["crm","quote"],"finance":["model","crm"]}}`))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
cases := []struct {
|
|
item string
|
|
client []string
|
|
want bool
|
|
}{
|
|
{"unlabeled", nil, true},
|
|
{"unlabeled", []string{}, true},
|
|
{"crm", nil, false},
|
|
{"crm", []string{"sales"}, true},
|
|
{"crm", []string{"finance"}, true},
|
|
{"quote", []string{"finance"}, false},
|
|
{"quote", []string{"finance", "sales"}, true},
|
|
{"model", []string{"other"}, false},
|
|
}
|
|
for _, c := range cases {
|
|
if got := f.Selected(c.item, c.client); got != c.want {
|
|
t.Errorf("Selected(%q, %v) = %v, want %v", c.item, c.client, got, c.want)
|
|
}
|
|
}
|
|
if got := strings.Join(f.Names(), ","); got != "crm,model,quote" {
|
|
t.Errorf("Names() = %s", got)
|
|
}
|
|
if got := strings.Join(f.LabelsOf("crm"), ","); got != "finance,sales" {
|
|
t.Errorf("LabelsOf(crm) = %s", got)
|
|
}
|
|
}
|
|
|
|
func TestNilFileRestrictsNothing(t *testing.T) {
|
|
var f *File
|
|
if !f.Selected("anything", nil) {
|
|
t.Error("a nil file must select every item")
|
|
}
|
|
}
|
|
|
|
func TestEmptyLabelsMapIsValid(t *testing.T) {
|
|
f, err := Parse([]byte(`{"version":1,"labels":{}}`))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !f.Selected("x", nil) {
|
|
t.Error("no labels declared means no restrictions")
|
|
}
|
|
}
|
|
|
|
func TestParseRejects(t *testing.T) {
|
|
cases := map[string]string{
|
|
"not json": `{`,
|
|
"unknown key": `{"version":1,"labels":{},"extra":true}`,
|
|
"wrong version": `{"version":2,"labels":{}}`,
|
|
"missing version": `{"labels":{}}`,
|
|
"missing labels": `{"version":1}`,
|
|
"upper label": `{"version":1,"labels":{"Sales":["a"]}}`,
|
|
"space label": `{"version":1,"labels":{"sales team":["a"]}}`,
|
|
"leading dash": `{"version":1,"labels":{"-sales":["a"]}}`,
|
|
"empty list": `{"version":1,"labels":{"sales":[]}}`,
|
|
"empty name": `{"version":1,"labels":{"sales":[""]}}`,
|
|
"trailing data": `{"version":1,"labels":{}} {}`,
|
|
"trailing brace": `{"version":1,"labels":{}}}`,
|
|
}
|
|
for name, data := range cases {
|
|
if _, err := Parse([]byte(data)); err == nil {
|
|
t.Errorf("%s: expected an error", name)
|
|
}
|
|
}
|
|
}
|