package domain import "testing" func TestValidResourceID(t *testing.T) { tests := []struct { name string id string want bool }{ {name: "ordinary", id: "draft", want: true}, {name: "dotted", id: "group.prompt.v2", want: true}, {name: "preserved surrounding whitespace", id: " draft ", want: true}, {name: "qualified", id: "group/draft", want: true}, {name: "qualified components preserve spelling", id: " Group / draft ", want: true}, {name: "empty", id: "", want: false}, {name: "blank", id: " \t", want: false}, {name: "missing namespace", id: "/draft", want: false}, {name: "missing local id", id: "group/", want: false}, {name: "multiple separators", id: "group/subgroup/draft", want: false}, } for _, test := range tests { t.Run(test.name, func(t *testing.T) { if got := validResourceID(test.id); got != test.want { t.Fatalf("validResourceID(%q) = %t, want %t", test.id, got, test.want) } }) } } func TestValidResourceNamespace(t *testing.T) { tests := []struct { name string namespace string want bool }{ {name: "ordinary", namespace: "group", want: true}, {name: "dotted", namespace: "group.v2", want: true}, {name: "preserved whitespace", namespace: " group ", want: true}, {name: "empty", namespace: "", want: false}, {name: "blank", namespace: " \t", want: false}, {name: "slash", namespace: "group/subgroup", want: false}, } for _, test := range tests { t.Run(test.name, func(t *testing.T) { if got := validResourceNamespace(test.namespace); got != test.want { t.Fatalf("validResourceNamespace(%q) = %t, want %t", test.namespace, got, test.want) } }) } } func TestValidLocalResourceKey(t *testing.T) { if !validLocalResourceKey("draft.v2") || !validLocalResourceKey(" draft ") { t.Fatal("expected nonblank slash-free keys to be valid") } for _, key := range []string{"", " \t", "group/draft"} { if validLocalResourceKey(key) { t.Errorf("validLocalResourceKey(%q) = true, want false", key) } } } func TestQualifyResourceID(t *testing.T) { tests := []struct { name string id string namespace string want string wantOK bool }{ {name: "without scope", id: "draft", want: "draft", wantOK: true}, {name: "with scope", id: "draft", namespace: "group", want: "group/draft", wantOK: true}, {name: "qualified bypass", id: "other/draft", namespace: "group", want: "other/draft", wantOK: true}, {name: "preserve spelling", id: " draft ", namespace: " Group ", want: " Group / draft ", wantOK: true}, {name: "omission remains caller policy", id: "", wantOK: false}, {name: "malformed ID", id: "group/draft/extra", namespace: "group", wantOK: false}, {name: "malformed scope", id: "draft", namespace: "group/subgroup", wantOK: false}, } for _, test := range tests { t.Run(test.name, func(t *testing.T) { got, ok := qualifyResourceID(test.id, test.namespace) if got != test.want || ok != test.wantOK { t.Fatalf("qualifyResourceID(%q, %q) = (%q, %t), want (%q, %t)", test.id, test.namespace, got, ok, test.want, test.wantOK) } }) } } func TestResourceNamespaceFromID(t *testing.T) { tests := []struct { name string id string namespace string wantOK bool }{ {name: "qualified", id: "team/item", namespace: "team", wantOK: true}, {name: "same-prefix namespace stays distinct", id: "team-extra/item", namespace: "team-extra", wantOK: true}, {name: "unqualified", id: "item", wantOK: false}, {name: "malformed", id: "team/sub/item", wantOK: false}, } for _, test := range tests { t.Run(test.name, func(t *testing.T) { got, ok := resourceNamespaceFromID(test.id) if got != test.namespace || ok != test.wantOK { t.Fatalf("resourceNamespaceFromID(%q) = (%q, %t), want (%q, %t)", test.id, got, ok, test.namespace, test.wantOK) } }) } }