Files
knowledge-governance/internal/qaopsmirror/department_statistics_integration_test.go
T
姚章浩 99d6109bef
build / build (push) Successful in 1m12s
feat(qaops): 区域统计展示直属人员与多部门兼属
按稳定身份展示零使用人员、个人次数与最近提问时间,直接部门去重计算兼属数量;保留原分支统计、配置和源问答。

验证:396项前端测试及类型检查通过;隔离PG全mirror/org竞态回归通过,个人/别名/同名/零使用/未关联与原部门汇总回归通过。
正式同镜像及上线验证尚未执行。
2026-10-21 17:17:08 +08:00

116 lines
4.9 KiB
Go

package qaopsmirror
import (
"context"
"encoding/json"
"knowledge-governance/internal/qaopsorg"
"testing"
"time"
)
func TestDepartmentStatisticsReadsMasterAndInvalidatesCurrentDirectory(t *testing.T) {
p := mirrorFixture(t)
s := NewStore(p, "fixture-department-statistics")
reader, ok := any(s).(interface {
ReadDepartmentStatistics(context.Context, DepartmentStatisticsQuery, time.Time) (DepartmentStatisticsResult, error)
})
if !ok {
t.Fatal("master-backed asynchronous department statistics reader is missing")
}
ctx := context.Background()
org := qaopsorg.NewStore(p, s.tenant)
directory := qaopsorg.Directory{Departments: []qaopsorg.Department{{ID: "root", Name: "Fixture Root"}, {ID: "child", ParentID: "root", Name: "Fixture Child"}, {ID: "other", ParentID: "root", Name: "Fixture Other"}, {ID: "empty", ParentID: "root", Name: "Fixture Empty"}}, People: []qaopsorg.Person{{ID: "person", UnionID: "union-person", DepartmentIDs: []string{"root", "child"}}, {ID: "second", DepartmentIDs: []string{"other"}}}}
state, e := org.ReplaceDirectory(ctx, qaopsorg.Import{Source: "weekly_report_snapshot", SyncedAt: time.Now(), Complete: true, Directory: directory})
if e != nil {
t.Fatal(e)
}
for i, identity := range []map[string]string{{"staff_id": "person"}, {"union_id": "union-person"}, {"staff_id": "second"}} {
id := []string{"one", "two", "three"}[i]
at := "2026-10-05T01:00:00+08:00"
wire := map[string]any{"id": id, "asked_at": at, "question": "Fixture question " + id, "answer": "Fixture nonbusiness answer", "status": "succeeded", "asker": "Fixture display", "trigger_person": "", "department": "Former department must not decide current attribution", "references": []any{}, "media": []any{}}
raw, _ := json.Marshal(wire)
actor, _ := json.Marshal(identity)
_, e = p.Exec(ctx, `INSERT INTO qa_ops_master_records(tenant_key,id,event_key,origin,asked_at,question,status,identity,quality_hash,payload)VALUES($1,$2,repeat('a',64),'dobotai_2_0',$3,'fixture','succeeded',$4,repeat('b',64),$5)`, s.tenant, id, at, string(actor), string(raw))
if e != nil {
t.Fatal(e)
}
}
now := time.Now()
query := DepartmentStatisticsQuery{Period: "all"}
wait := func() DepartmentStatisticsResult {
t.Helper()
deadline := time.Now().Add(3 * time.Second)
for {
r, e := reader.ReadDepartmentStatistics(ctx, query, now)
if e != nil {
t.Fatal(e)
}
if r.State == "ready" {
return r
}
if time.Now().After(deadline) {
t.Fatal("statistics did not finish in the background")
}
time.Sleep(5 * time.Millisecond)
}
}
cctx, cancel := context.WithCancel(ctx)
first, e := reader.ReadDepartmentStatistics(cctx, query, now)
cancel()
if e != nil || first.State != "pending" {
t.Fatal("first request must enqueue background aggregation", e)
}
ready := wait()
if ready.Snapshot.TotalVisits != 3 || ready.Snapshot.TotalPeople != 2 || ready.Snapshot.Organization.DirectoryID != state.DirectoryID {
t.Fatal("statistics used mirror-only records or wrong roster")
}
if len(ready.Snapshot.People) != 2 {
t.Fatal("personal roster missing from real persisted statistics")
}
personalAliasFound := false
for _, person := range ready.Snapshot.People {
if len(person.DepartmentIDs) == 2 {
personalAliasFound = true
if person.Visits != 2 || person.LastAskedAt == nil {
t.Fatal("staff/union aliases were not aggregated per person")
}
}
}
if !personalAliasFound {
t.Fatal("direct multiple memberships lost in cached snapshot")
}
rows := map[string]qaopsorg.DepartmentStatistic{}
for _, r := range ready.Snapshot.Rows {
rows[r.ID] = r
}
if rows["root"].Visitors != 2 || rows["root"].Visits != 3 || rows["child"].Visitors != 1 || rows["child"].Visits != 2 || rows["empty"].Ratio != nil {
t.Fatal("subtree/alias/empty-denominator contract lost")
}
again, e := reader.ReadDepartmentStatistics(ctx, query, now.Add(time.Second))
if e != nil || !again.CacheHit || again.Snapshot.GeneratedAt != ready.Snapshot.GeneratedAt {
t.Fatal("same condition was recomputed within 60 seconds")
}
directory.People[0].DepartmentIDs = []string{"other"}
state, e = org.ReplaceDirectory(ctx, qaopsorg.Import{Source: "weekly_report_snapshot", SyncedAt: time.Now(), Complete: true, Directory: directory})
if e != nil {
t.Fatal(e)
}
moved := wait()
if moved.CacheKey == ready.CacheKey || moved.Snapshot.Organization.DirectoryID != state.DirectoryID {
t.Fatal("directory changes did not invalidate cache")
}
for _, r := range moved.Snapshot.Rows {
if r.ID == "child" && (r.TotalPeople != 0 || r.Visitors != 0 || r.Ratio != nil) {
t.Fatal("transferred person remained in former department")
}
}
_, e = org.SaveRules(ctx, qaopsorg.SaveRequest{DirectoryID: state.DirectoryID, BaseRevision: state.RevisionID, Rules: []qaopsorg.Rule{{DepartmentID: "other", Region: "APAC"}}}, "fixture-admin")
if e != nil {
t.Fatal(e)
}
updated := wait()
if updated.CacheKey == moved.CacheKey {
t.Fatal("saved regional configuration was hidden behind stale cached results")
}
}