99d6109bef
build / build (push) Successful in 1m12s
按稳定身份展示零使用人员、个人次数与最近提问时间,直接部门去重计算兼属数量;保留原分支统计、配置和源问答。 验证:396项前端测试及类型检查通过;隔离PG全mirror/org竞态回归通过,个人/别名/同名/零使用/未关联与原部门汇总回归通过。 正式同镜像及上线验证尚未执行。
72 lines
3.1 KiB
Go
72 lines
3.1 KiB
Go
package qaopsorg
|
|
|
|
import (
|
|
"encoding/json"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestDepartmentStatisticsExposeDirectPeopleWithoutDuplicatingBranchTotals(t *testing.T) {
|
|
d := Directory{Departments: []Department{{ID: "root", Name: "Root"}, {ID: "child", ParentID: "root", Name: "Child"}, {ID: "peer", ParentID: "root", Name: "Peer"}}, People: []Person{
|
|
{ID: "private-first", UnionID: "private-union", Name: "同名样例", DepartmentIDs: []string{"root", "child", "peer", "child"}},
|
|
{ID: "private-second", Name: "同名样例", DepartmentIDs: []string{"child"}},
|
|
{ID: "private-zero", DepartmentIDs: []string{"peer"}},
|
|
{ID: "private-unassigned", Name: "未关联样例", DepartmentIDs: []string{}},
|
|
}}
|
|
p, err := BuildAttribution(State{State: "ready", DirectoryID: "fixture"}, d)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
at := time.Date(2026, 10, 1, 0, 0, 0, 0, time.UTC)
|
|
got, err := AggregateDepartmentStatistics(p, []DepartmentActivity{{StaffID: "private-first", Visits: 2, LastAskedAt: at}, {UnionID: "private-union", Visits: 3, LastAskedAt: at.Add(time.Hour)}, {StaffID: "private-second", Visits: 4, LastAskedAt: at}, {Asker: "同名样例", Visits: 7, LastAskedAt: at}, {StaffID: "private-unassigned", Visits: 1, LastAskedAt: at}})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
raw, err := json.Marshal(got)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var wire struct {
|
|
People []struct {
|
|
Key string `json:"key"`
|
|
Name string `json:"name"`
|
|
DepartmentIDs []string `json:"department_ids"`
|
|
Visits int64 `json:"visits"`
|
|
Last *time.Time `json:"last_asked_at"`
|
|
} `json:"people"`
|
|
}
|
|
if json.Unmarshal(raw, &wire) != nil || len(wire.People) != 4 {
|
|
t.Fatal("department statistics must include the full unique roster, including zero-use and unassigned people")
|
|
}
|
|
visits := map[int64]int{}
|
|
keys := map[string]bool{}
|
|
for _, person := range wire.People {
|
|
if !strings.HasPrefix(person.Key, "person-") || len(person.Key) != 71 || keys[person.Key] {
|
|
t.Fatal("missing stable private row key or same-name people merged")
|
|
}
|
|
keys[person.Key] = true
|
|
visits[person.Visits]++
|
|
if person.Visits == 5 && (len(person.DepartmentIDs) != 3 || person.Last == nil || !person.Last.Equal(at.Add(time.Hour))) {
|
|
t.Fatal("direct memberships, alias aggregation or most recent personal activity lost")
|
|
}
|
|
if person.Visits == 0 && person.Last != nil {
|
|
t.Fatal("zero-use person received invented activity")
|
|
}
|
|
}
|
|
if visits[5] != 1 || visits[4] != 1 || visits[0] != 1 || visits[1] != 1 {
|
|
t.Fatal("one person's activity was multiplied by departments or a same-name record was misattributed")
|
|
}
|
|
if strings.Contains(string(raw), "private-") || strings.Contains(string(raw), "union_id") || strings.Contains(string(raw), "staff_id") {
|
|
t.Fatal("raw identity leaked")
|
|
}
|
|
if got.TotalPeople != 4 || got.TotalVisits != 17 || got.UnmappedVisits != 8 {
|
|
t.Fatal("adding personal rows changed existing totals")
|
|
}
|
|
for _, row := range got.Rows {
|
|
if row.ID == "root" && (row.TotalPeople != 3 || row.Visits != 9 || row.Visitors != 2) {
|
|
t.Fatal("expanded personal rows changed deduplicated parent totals")
|
|
}
|
|
}
|
|
}
|