87 lines
3.5 KiB
Go
87 lines
3.5 KiB
Go
package qaopsorg
|
|
|
|
import (
|
|
"errors"
|
|
"testing"
|
|
)
|
|
|
|
func exampleDirectory() Directory {
|
|
return Directory{Departments: []Department{
|
|
{ID: "root", Name: "示例公司"},
|
|
{ID: "sales", ParentID: "root", Name: "销售"},
|
|
{ID: "apac", ParentID: "sales", Name: "业务组"},
|
|
{ID: "leaf", ParentID: "apac", Name: "支持"},
|
|
{ID: "other", ParentID: "root", Name: "业务组"},
|
|
}, People: []Person{
|
|
{ID: "p1", DepartmentIDs: []string{"sales"}},
|
|
{ID: "p2", DepartmentIDs: []string{"leaf"}},
|
|
{ID: "p3", DepartmentIDs: []string{"sales", "leaf"}},
|
|
{ID: "p4", DepartmentIDs: []string{"other"}},
|
|
}}
|
|
}
|
|
|
|
func TestRegionPreviewUsesNearestExplicitAncestorAndCountsPeopleOnce(t *testing.T) {
|
|
view, err := Preview(exampleDirectory(), []Rule{{DepartmentID: "sales", Region: "国内工业"}, {DepartmentID: "apac", Region: "APAC"}})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
byID := map[string]DepartmentView{}
|
|
for _, d := range view.Departments {
|
|
byID[d.ID] = d
|
|
}
|
|
if got := byID["leaf"]; got.Region != "APAC" || got.InheritedFrom != "apac" || got.Path != "示例公司/销售/业务组/支持" {
|
|
t.Fatalf("wrong child inheritance: %+v", got)
|
|
}
|
|
if !byID["apac"].Explicit || byID["other"].Region != "" {
|
|
t.Fatal("explicit choice or unmapped department lost")
|
|
}
|
|
if view.Regions["国内工业"] != 1 || view.Regions["APAC"] != 1 || view.UnassignedPeople != 1 || view.ConflictingPeople != 1 || view.TotalPeople != 4 {
|
|
t.Fatalf("person duplicated or conflict silently resolved: %+v", view)
|
|
}
|
|
}
|
|
|
|
func TestRegionPreviewRejectsBrokenDirectoryAndDuplicateAssignments(t *testing.T) {
|
|
cases := []struct {
|
|
name string
|
|
mutate func(*Directory)
|
|
rules []Rule
|
|
}{
|
|
{name: "cycle", mutate: func(d *Directory) { d.Departments[0].ParentID = "leaf" }},
|
|
{name: "missing parent", mutate: func(d *Directory) { d.Departments[1].ParentID = "absent" }},
|
|
{name: "duplicate department", mutate: func(d *Directory) { d.Departments = append(d.Departments, d.Departments[0]) }},
|
|
{name: "duplicate person", mutate: func(d *Directory) { d.People = append(d.People, d.People[0]) }},
|
|
{name: "missing membership", mutate: func(d *Directory) { d.People[0].DepartmentIDs = []string{"absent"} }},
|
|
{name: "conflicting direct rules", rules: []Rule{{DepartmentID: "sales", Region: "国内工业"}, {DepartmentID: "sales", Region: "APAC"}}},
|
|
{name: "unknown region", rules: []Rule{{DepartmentID: "sales", Region: "automatic"}}},
|
|
}
|
|
for _, tc := range cases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
d := exampleDirectory()
|
|
if tc.mutate != nil {
|
|
tc.mutate(&d)
|
|
}
|
|
_, err := Preview(d, tc.rules)
|
|
if !errors.Is(err, ErrInvalid) {
|
|
t.Fatalf("must fail closed, got %v", err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestRegionPreviewKeepsMissingRulesAndDoesNotInventPrimaryDepartment(t *testing.T) {
|
|
d := exampleDirectory()
|
|
d.People = append(d.People, Person{ID: "p5", DepartmentIDs: []string{"apac", "leaf"}})
|
|
view, err := Preview(d, []Rule{{DepartmentID: "sales", Region: "国内工业"}, {DepartmentID: "apac", Region: "APAC"}, {DepartmentID: "removed", Region: "EMEA"}})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(view.MissingDepartments) != 1 || view.MissingDepartments[0] != "removed" || view.ConflictingPeople != 1 || view.Regions["APAC"] != 2 {
|
|
t.Fatalf("lost orphan rule or duplicated same-region membership: %+v", view)
|
|
}
|
|
d.Departments[2].Name = "改名后的部门"
|
|
renamed, err := Preview(d, []Rule{{DepartmentID: "sales", Region: "国内工业"}, {DepartmentID: "apac", Region: "APAC"}})
|
|
if err != nil || renamed.Regions["APAC"] != 2 {
|
|
t.Fatal("stable department ID did not survive rename")
|
|
}
|
|
}
|