feat(qaops): 增加版本化部门区域归属配置与预览
This commit is contained in:
@@ -0,0 +1,146 @@
|
||||
package qaopsorg_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net/url"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
postgresstore "knowledge-governance/internal/platform/postgres"
|
||||
"knowledge-governance/internal/qaopsorg"
|
||||
)
|
||||
|
||||
func TestOrganizationStoreRoundTripAndOptimisticConflict(t *testing.T) {
|
||||
dsn := os.Getenv("KG_QAOPS_ORG_TEST_DSN")
|
||||
if dsn == "" {
|
||||
t.Skip("explicit isolated organization database is not configured")
|
||||
}
|
||||
u, err := url.Parse(dsn)
|
||||
if err != nil || u.Hostname() != "127.0.0.1" || !strings.HasPrefix(u.Path, "/kg_qaops_org_test_") || os.Getenv("KG_QAOPS_ORG_TEST_ISOLATED") != "1" {
|
||||
t.Fatal("refusing non-isolated organization database")
|
||||
}
|
||||
ctx := context.Background()
|
||||
pool, err := pgxpool.New(ctx, dsn)
|
||||
if err != nil {
|
||||
t.Fatal("test database connection failed")
|
||||
}
|
||||
defer pool.Close()
|
||||
if err = postgresstore.ApplyMigrations(ctx, pool); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
store := qaopsorg.NewStore(pool, "fixture-tenant")
|
||||
empty, err := store.GetView(ctx)
|
||||
if err != nil || empty.State != "empty" {
|
||||
t.Fatalf("empty state must be explicit: %+v %v", empty, err)
|
||||
}
|
||||
input := qaopsorg.Import{Source: "weekly_report_snapshot", SyncedAt: time.Date(2026, 9, 27, 14, 3, 44, 0, time.UTC), Complete: true, Directory: qaopsorg.Directory{
|
||||
Departments: []qaopsorg.Department{{ID: "root", Name: "示例公司"}, {ID: "a", ParentID: "root", Name: "国内组"}, {ID: "b", ParentID: "a", Name: "海外组"}},
|
||||
People: []qaopsorg.Person{{ID: "u1", DepartmentIDs: []string{"a"}}, {ID: "u2", DepartmentIDs: []string{"b"}}},
|
||||
}}
|
||||
imported, err := store.ReplaceDirectory(ctx, input)
|
||||
if err != nil || imported.DirectoryID == "" || imported.View.TotalPeople != 2 {
|
||||
t.Fatalf("import failed: %+v %v", imported, err)
|
||||
}
|
||||
request := qaopsorg.SaveRequest{DirectoryID: imported.DirectoryID, Rules: []qaopsorg.Rule{{DepartmentID: "a", Region: "国内工业"}, {DepartmentID: "b", Region: "APAC"}}}
|
||||
preview, err := store.PreviewRules(ctx, request)
|
||||
if err != nil || preview.View.Regions["APAC"] != 1 {
|
||||
t.Fatalf("preview failed: %+v %v", preview, err)
|
||||
}
|
||||
before, _ := store.GetView(ctx)
|
||||
if before.RevisionID != "" {
|
||||
t.Fatal("preview must not persist changes")
|
||||
}
|
||||
saved, err := store.SaveRules(ctx, request, "fixture-admin")
|
||||
if err != nil || saved.RevisionID == "" || saved.UpdatedBy != "fixture-admin" {
|
||||
t.Fatalf("save failed: %+v %v", saved, err)
|
||||
}
|
||||
read, err := store.GetView(ctx)
|
||||
if err != nil || read.RevisionID != saved.RevisionID || read.View.Regions["国内工业"] != 1 {
|
||||
t.Fatal("saved rules not consumed by read path")
|
||||
}
|
||||
if _, err = store.SaveRules(ctx, request, "stale-admin"); !errors.Is(err, qaopsorg.ErrConflict) {
|
||||
t.Fatalf("stale update overwrote newer rules: %v", err)
|
||||
}
|
||||
request.BaseRevision = saved.RevisionID
|
||||
again, err := store.SaveRules(ctx, request, "fixture-admin")
|
||||
if err != nil || again.RevisionID != saved.RevisionID {
|
||||
t.Fatal("unchanged save must not append duplicate revisions")
|
||||
}
|
||||
broken := input
|
||||
broken.Complete = false
|
||||
if _, err = store.ReplaceDirectory(ctx, broken); !errors.Is(err, qaopsorg.ErrInvalid) {
|
||||
t.Fatal("partial sync activated")
|
||||
}
|
||||
request.Rules = []qaopsorg.Rule{{DepartmentID: "absent", Region: "EMEA"}}
|
||||
if _, err = store.SaveRules(ctx, request, "fixture-admin"); !errors.Is(err, qaopsorg.ErrInvalid) {
|
||||
t.Fatal("invalid rule persisted")
|
||||
}
|
||||
after, _ := store.GetView(ctx)
|
||||
if after.RevisionID != saved.RevisionID {
|
||||
t.Fatal("failed writes changed active revision")
|
||||
}
|
||||
other, _ := qaopsorg.NewStore(pool, "another-tenant").GetView(ctx)
|
||||
if other.State != "empty" {
|
||||
t.Fatal("directory leaked across tenants")
|
||||
}
|
||||
input.Directory.Departments = input.Directory.Departments[:2]
|
||||
input.Directory.People = input.Directory.People[:1]
|
||||
input.SyncedAt = input.SyncedAt.Add(time.Hour)
|
||||
newer, err := store.ReplaceDirectory(ctx, input)
|
||||
if err != nil || newer.RevisionID != saved.RevisionID || len(newer.View.MissingDepartments) != 1 {
|
||||
t.Fatalf("directory refresh deleted rules or hid missing department: %+v %v", newer, err)
|
||||
}
|
||||
if _, err = store.PreviewRules(ctx, request); !errors.Is(err, qaopsorg.ErrConflict) {
|
||||
t.Fatal("preview accepted an obsolete directory snapshot")
|
||||
}
|
||||
older := input
|
||||
start := make(chan struct{})
|
||||
results := make(chan error, 2)
|
||||
for _, region := range []string{"APAC", "EMEA"} {
|
||||
go func(region string) {
|
||||
<-start
|
||||
_, failure := store.SaveRules(ctx, qaopsorg.SaveRequest{DirectoryID: newer.DirectoryID, BaseRevision: newer.RevisionID, Rules: []qaopsorg.Rule{{DepartmentID: "a", Region: region}}}, "concurrent-admin")
|
||||
results <- failure
|
||||
}(region)
|
||||
}
|
||||
close(start)
|
||||
successes, conflicts := 0, 0
|
||||
for range 2 {
|
||||
failure := <-results
|
||||
if failure == nil {
|
||||
successes++
|
||||
} else if errors.Is(failure, qaopsorg.ErrConflict) {
|
||||
conflicts++
|
||||
} else {
|
||||
t.Fatal(failure)
|
||||
}
|
||||
}
|
||||
if successes != 1 || conflicts != 1 {
|
||||
t.Fatalf("concurrent stale writes were not serialized: success=%d conflict=%d", successes, conflicts)
|
||||
}
|
||||
older.SyncedAt = older.SyncedAt.Add(-2 * time.Hour)
|
||||
if _, err = store.ReplaceDirectory(ctx, older); !errors.Is(err, qaopsorg.ErrConflict) {
|
||||
t.Fatalf("older directory rolled back the active roster: %v", err)
|
||||
}
|
||||
var revisions int
|
||||
if err = pool.QueryRow(ctx, "SELECT count(*) FROM qa_ops_region_revisions WHERE tenant_key=$1", "fixture-tenant").Scan(&revisions); err != nil || revisions != 2 {
|
||||
t.Fatal("revision history changed unexpectedly")
|
||||
}
|
||||
if err = postgresstore.RollbackLatestMigration(ctx, pool); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var removed bool
|
||||
if err = pool.QueryRow(ctx, "SELECT to_regclass('qa_ops_organization_state') IS NULL AND to_regclass('qa_ops_region_revisions') IS NULL AND to_regclass('qa_ops_directory_snapshots') IS NULL").Scan(&removed); err != nil || !removed {
|
||||
t.Fatal("organization down migration left residue")
|
||||
}
|
||||
if err = postgresstore.ApplyMigrations(ctx, pool); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if state, failure := store.GetView(ctx); failure != nil || state.State != "empty" {
|
||||
t.Fatal("organization migration could not be reapplied")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user